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