Merge branch 'hotfix_broken_likes' into 'develop'
[akkoma] / lib / pleroma / web / twitter_api / views / activity_view.ex
1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.CommonAPI.Utils
4 alias Pleroma.User
5 alias Pleroma.Web.TwitterAPI.UserView
6 alias Pleroma.Web.TwitterAPI.ActivityView
7 alias Pleroma.Web.TwitterAPI.TwitterAPI
8 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
9 alias Pleroma.Activity
10 alias Pleroma.Object
11 alias Pleroma.User
12 alias Pleroma.Repo
13 alias Pleroma.Formatter
14
15 import Ecto.Query
16
17 defp query_context_ids([]), do: []
18
19 defp query_context_ids(contexts) do
20 query = from(o in Object, where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts))
21
22 Repo.all(query)
23 end
24
25 defp query_users([]), do: []
26
27 defp query_users(user_ids) do
28 query = from(user in User, where: user.ap_id in ^user_ids)
29
30 Repo.all(query)
31 end
32
33 defp collect_context_ids(activities) do
34 _contexts =
35 activities
36 |> Enum.reject(& &1.data["context_id"])
37 |> Enum.map(fn %{data: data} ->
38 data["context"]
39 end)
40 |> Enum.filter(& &1)
41 |> query_context_ids()
42 |> Enum.reduce(%{}, fn %{data: %{"id" => ap_id}, id: id}, acc ->
43 Map.put(acc, ap_id, id)
44 end)
45 end
46
47 defp collect_users(activities) do
48 activities
49 |> Enum.map(fn activity ->
50 case activity.data do
51 data = %{"type" => "Follow"} ->
52 [data["actor"], data["object"]]
53
54 data ->
55 [data["actor"]]
56 end ++ activity.recipients
57 end)
58 |> List.flatten()
59 |> Enum.uniq()
60 |> query_users()
61 |> Enum.reduce(%{}, fn user, acc ->
62 Map.put(acc, user.ap_id, user)
63 end)
64 end
65
66 defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id),
67 do: context_id
68
69 defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
70
71 defp get_context_id(%{data: %{"context" => context}}, options) do
72 cond do
73 id = options[:context_ids][context] -> id
74 true -> TwitterAPI.context_to_conversation_id(context)
75 end
76 end
77
78 defp get_context_id(_, _), do: nil
79
80 defp get_user(ap_id, opts) do
81 cond do
82 user = opts[:users][ap_id] ->
83 user
84
85 String.ends_with?(ap_id, "/followers") ->
86 nil
87
88 ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
89 nil
90
91 true ->
92 User.get_cached_by_ap_id(ap_id)
93 end
94 end
95
96 def render("index.json", opts) do
97 context_ids = collect_context_ids(opts.activities)
98 users = collect_users(opts.activities)
99
100 opts =
101 opts
102 |> Map.put(:context_ids, context_ids)
103 |> Map.put(:users, users)
104
105 render_many(
106 opts.activities,
107 ActivityView,
108 "activity.json",
109 opts
110 )
111 end
112
113 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
114 user = get_user(activity.data["actor"], opts)
115 created_at = activity.data["published"] |> Utils.date_to_asctime()
116
117 %{
118 "id" => activity.id,
119 "uri" => activity.data["object"],
120 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
121 "attentions" => [],
122 "statusnet_html" => "deleted notice {{tag",
123 "text" => "deleted notice {{tag",
124 "is_local" => activity.local,
125 "is_post_verb" => false,
126 "created_at" => created_at,
127 "in_reply_to_status_id" => nil,
128 "external_url" => activity.data["id"],
129 "activity_type" => "delete"
130 }
131 end
132
133 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
134 user = get_user(activity.data["actor"], opts)
135 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
136 created_at = created_at |> Utils.date_to_asctime()
137
138 followed = get_user(activity.data["object"], opts)
139 text = "#{user.nickname} started following #{followed.nickname}"
140
141 %{
142 "id" => activity.id,
143 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
144 "attentions" => [],
145 "statusnet_html" => text,
146 "text" => text,
147 "is_local" => activity.local,
148 "is_post_verb" => false,
149 "created_at" => created_at,
150 "in_reply_to_status_id" => nil,
151 "external_url" => activity.data["id"],
152 "activity_type" => "follow"
153 }
154 end
155
156 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
157 user = get_user(activity.data["actor"], opts)
158 created_at = activity.data["published"] |> Utils.date_to_asctime()
159 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
160
161 text = "#{user.nickname} retweeted a status."
162
163 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
164
165 %{
166 "id" => activity.id,
167 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
168 "statusnet_html" => text,
169 "text" => text,
170 "is_local" => activity.local,
171 "is_post_verb" => false,
172 "uri" => "tag:#{activity.data["id"]}:objectType=note",
173 "created_at" => created_at,
174 "retweeted_status" => retweeted_status,
175 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
176 "external_url" => activity.data["id"],
177 "activity_type" => "repeat"
178 }
179 end
180
181 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
182 user = get_user(activity.data["actor"], opts)
183 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
184 liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
185
186 created_at =
187 activity.data["published"]
188 |> Utils.date_to_asctime()
189
190 text = "#{user.nickname} favorited a status."
191
192 %{
193 "id" => activity.id,
194 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
195 "statusnet_html" => text,
196 "text" => text,
197 "is_local" => activity.local,
198 "is_post_verb" => false,
199 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
200 "created_at" => created_at,
201 "in_reply_to_status_id" => liked_activity_id,
202 "external_url" => activity.data["id"],
203 "activity_type" => "like"
204 }
205 end
206
207 def render(
208 "activity.json",
209 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
210 ) do
211 user = get_user(activity.data["actor"], opts)
212
213 created_at = object["published"] |> Utils.date_to_asctime()
214 like_count = object["like_count"] || 0
215 announcement_count = object["announcement_count"] || 0
216 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
217 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
218
219 attentions =
220 activity.recipients
221 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
222 |> Enum.filter(& &1)
223 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
224
225 conversation_id = get_context_id(activity, opts)
226
227 tags = activity.data["object"]["tag"] || []
228 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
229
230 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
231
232 {summary, content} = render_content(object)
233
234 html =
235 HtmlSanitizeEx.basic_html(content)
236 |> Formatter.emojify(object["emoji"])
237
238 %{
239 "id" => activity.id,
240 "uri" => activity.data["object"]["id"],
241 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
242 "statusnet_html" => html,
243 "text" => HtmlSanitizeEx.strip_tags(content),
244 "is_local" => activity.local,
245 "is_post_verb" => true,
246 "created_at" => created_at,
247 "in_reply_to_status_id" => object["inReplyToStatusId"],
248 "statusnet_conversation_id" => conversation_id,
249 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
250 "attentions" => attentions,
251 "fave_num" => like_count,
252 "repeat_num" => announcement_count,
253 "favorited" => !!favorited,
254 "repeated" => !!repeated,
255 "external_url" => object["external_url"] || object["id"],
256 "tags" => tags,
257 "activity_type" => "post",
258 "possibly_sensitive" => possibly_sensitive,
259 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
260 "summary" => summary
261 }
262 end
263
264 def render_content(%{"type" => "Note"} = object) do
265 summary = object["summary"]
266
267 content =
268 if !!summary and summary != "" do
269 "<p>#{summary}</p>#{object["content"]}"
270 else
271 object["content"]
272 end
273
274 {summary, content}
275 end
276
277 def render_content(%{"type" => "Article"} = object) do
278 summary = object["name"] || object["summary"]
279
280 content =
281 if !!summary and summary != "" do
282 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
283 else
284 object["content"]
285 end
286
287 {summary, content}
288 end
289
290 def render_content(object) do
291 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
292 content = "<p>#{summary}</p>#{object["content"]}"
293
294 {summary, content}
295 end
296 end