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