364aa7af3906025e18f4c4d09c043b8c1500f696
[akkoma] / lib / pleroma / web / twitter_api / representers / activity_representer.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 # THIS MODULE IS DEPRECATED! DON'T USE IT!
6 # USE THE Pleroma.Web.TwitterAPI.Views.ActivityView MODULE!
7 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
8 use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
9 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
10 alias Pleroma.{Activity, User}
11 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Formatter
14 alias Pleroma.HTML
15 alias Pleroma.Web.MastodonAPI
16 alias Pleroma.Web.MastodonAPI.StatusView
17
18 defp user_by_ap_id(user_list, ap_id) do
19 Enum.find(user_list, fn %{ap_id: user_id} -> ap_id == user_id end)
20 end
21
22 def to_map(
23 %Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} =
24 activity,
25 %{users: users, announced_activity: announced_activity} = opts
26 ) do
27 user = user_by_ap_id(users, actor)
28 created_at = created_at |> Utils.date_to_asctime()
29
30 text = "#{user.nickname} retweeted a status."
31
32 announced_user = user_by_ap_id(users, announced_activity.data["actor"])
33 retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
34
35 %{
36 "id" => activity.id,
37 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
38 "statusnet_html" => text,
39 "text" => text,
40 "is_local" => activity.local,
41 "is_post_verb" => false,
42 "uri" => "tag:#{activity.data["id"]}:objectType=note",
43 "created_at" => created_at,
44 "retweeted_status" => retweeted_status,
45 "statusnet_conversation_id" => conversation_id(announced_activity),
46 "external_url" => activity.data["id"],
47 "activity_type" => "repeat"
48 }
49 end
50
51 def to_map(
52 %Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
53 %{user: user, liked_activity: liked_activity} = opts
54 ) do
55 created_at = created_at |> Utils.date_to_asctime()
56
57 text = "#{user.nickname} favorited a status."
58
59 %{
60 "id" => activity.id,
61 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
62 "statusnet_html" => text,
63 "text" => text,
64 "is_local" => activity.local,
65 "is_post_verb" => false,
66 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
67 "created_at" => created_at,
68 "in_reply_to_status_id" => liked_activity.id,
69 "external_url" => activity.data["id"],
70 "activity_type" => "like"
71 }
72 end
73
74 def to_map(
75 %Activity{data: %{"type" => "Follow", "object" => followed_id}} = activity,
76 %{user: user} = opts
77 ) do
78 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
79 created_at = created_at |> Utils.date_to_asctime()
80
81 followed = User.get_cached_by_ap_id(followed_id)
82 text = "#{user.nickname} started following #{followed.nickname}"
83
84 %{
85 "id" => activity.id,
86 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
87 "attentions" => [],
88 "statusnet_html" => text,
89 "text" => text,
90 "is_local" => activity.local,
91 "is_post_verb" => false,
92 "created_at" => created_at,
93 "in_reply_to_status_id" => nil,
94 "external_url" => activity.data["id"],
95 "activity_type" => "follow"
96 }
97 end
98
99 # TODO:
100 # Make this more proper. Just a placeholder to not break the frontend.
101 def to_map(
102 %Activity{
103 data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity}
104 } = activity,
105 %{user: user} = opts
106 ) do
107 created_at = created_at |> Utils.date_to_asctime()
108
109 text = "#{user.nickname} undid the action at #{undid_activity["id"]}"
110
111 %{
112 "id" => activity.id,
113 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
114 "attentions" => [],
115 "statusnet_html" => text,
116 "text" => text,
117 "is_local" => activity.local,
118 "is_post_verb" => false,
119 "created_at" => created_at,
120 "in_reply_to_status_id" => nil,
121 "external_url" => activity.data["id"],
122 "activity_type" => "undo"
123 }
124 end
125
126 def to_map(
127 %Activity{data: %{"type" => "Delete", "published" => created_at, "object" => _}} =
128 activity,
129 %{user: user} = opts
130 ) do
131 created_at = created_at |> Utils.date_to_asctime()
132
133 %{
134 "id" => activity.id,
135 "uri" => activity.data["object"],
136 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
137 "attentions" => [],
138 "statusnet_html" => "deleted notice {{tag",
139 "text" => "deleted notice {{tag",
140 "is_local" => activity.local,
141 "is_post_verb" => false,
142 "created_at" => created_at,
143 "in_reply_to_status_id" => nil,
144 "external_url" => activity.data["id"],
145 "activity_type" => "delete"
146 }
147 end
148
149 def to_map(
150 %Activity{data: %{"object" => %{"content" => _content} = object}} = activity,
151 %{user: user} = opts
152 ) do
153 created_at = object["published"] |> Utils.date_to_asctime()
154 like_count = object["like_count"] || 0
155 announcement_count = object["announcement_count"] || 0
156 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
157 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
158 pinned = activity.id in user.info.pinned_activities
159
160 mentions = opts[:mentioned] || []
161
162 attentions =
163 []
164 |> Utils.maybe_notify_to_recipients(activity)
165 |> Utils.maybe_notify_mentioned_recipients(activity)
166 |> Enum.map(fn ap_id -> Enum.find(mentions, fn user -> ap_id == user.ap_id end) end)
167 |> Enum.filter(& &1)
168 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
169
170 conversation_id = conversation_id(activity)
171
172 tags = activity.data["object"]["tag"] || []
173 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
174
175 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
176
177 {_summary, content} = ActivityView.render_content(object)
178
179 html =
180 HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
181 |> Formatter.emojify(object["emoji"])
182
183 attachments = object["attachment"] || []
184
185 reply_parent = Activity.get_in_reply_to_activity(activity)
186
187 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
188
189 summary = HTML.strip_tags(object["summary"])
190
191 card = StatusView.render("card.json", MastodonAPI.get_status_card(activity.id))
192
193 %{
194 "id" => activity.id,
195 "uri" => activity.data["object"]["id"],
196 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
197 "statusnet_html" => html,
198 "text" => HTML.strip_tags(content),
199 "is_local" => activity.local,
200 "is_post_verb" => true,
201 "created_at" => created_at,
202 "in_reply_to_status_id" => object["inReplyToStatusId"],
203 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
204 "in_reply_to_profileurl" => User.profile_url(reply_user),
205 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
206 "in_reply_to_user_id" => reply_user && reply_user.id,
207 "statusnet_conversation_id" => conversation_id,
208 "attachments" => attachments |> ObjectRepresenter.enum_to_list(opts),
209 "attentions" => attentions,
210 "fave_num" => like_count,
211 "repeat_num" => announcement_count,
212 "favorited" => to_boolean(favorited),
213 "repeated" => to_boolean(repeated),
214 "pinned" => pinned,
215 "external_url" => object["external_url"] || object["id"],
216 "tags" => tags,
217 "activity_type" => "post",
218 "possibly_sensitive" => possibly_sensitive,
219 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
220 "summary" => summary,
221 "summary_html" => summary |> Formatter.emojify(object["emoji"]),
222 "card" => card
223 }
224 end
225
226 def conversation_id(activity) do
227 with context when not is_nil(context) <- activity.data["context"] do
228 TwitterAPI.context_to_conversation_id(context)
229 else
230 _e -> nil
231 end
232 end
233
234 defp to_boolean(false) do
235 false
236 end
237
238 defp to_boolean(nil) do
239 false
240 end
241
242 defp to_boolean(_) do
243 true
244 end
245 end