Merge branch 'develop' into feature/database-compaction
[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
8 alias Pleroma.Formatter
9 alias Pleroma.HTML
10 alias Pleroma.Object
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.CommonAPI.Utils
15 alias Pleroma.Web.MastodonAPI.StatusView
16 alias Pleroma.Web.TwitterAPI.ActivityView
17 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
18 alias Pleroma.Web.TwitterAPI.UserView
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 -> Utils.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 User.error_user(ap_id)
105 end
106 end
107
108 def render("index.json", opts) do
109 context_ids = collect_context_ids(opts.activities)
110 users = collect_users(opts.activities)
111
112 opts =
113 opts
114 |> Map.put(:context_ids, context_ids)
115 |> Map.put(:users, users)
116
117 safe_render_many(
118 opts.activities,
119 ActivityView,
120 "activity.json",
121 opts
122 )
123 end
124
125 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
126 user = get_user(activity.data["actor"], opts)
127 created_at = activity.data["published"] |> Utils.date_to_asctime()
128
129 %{
130 "id" => activity.id,
131 "uri" => activity.data["object"],
132 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
133 "attentions" => [],
134 "statusnet_html" => "deleted notice {{tag",
135 "text" => "deleted notice {{tag",
136 "is_local" => activity.local,
137 "is_post_verb" => false,
138 "created_at" => created_at,
139 "in_reply_to_status_id" => nil,
140 "external_url" => activity.data["id"],
141 "activity_type" => "delete"
142 }
143 end
144
145 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
146 user = get_user(activity.data["actor"], opts)
147 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
148 created_at = created_at |> Utils.date_to_asctime()
149
150 followed = get_user(activity.data["object"], opts)
151 text = "#{user.nickname} started following #{followed.nickname}"
152
153 %{
154 "id" => activity.id,
155 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
156 "attentions" => [],
157 "statusnet_html" => text,
158 "text" => text,
159 "is_local" => activity.local,
160 "is_post_verb" => false,
161 "created_at" => created_at,
162 "in_reply_to_status_id" => nil,
163 "external_url" => activity.data["id"],
164 "activity_type" => "follow"
165 }
166 end
167
168 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
169 user = get_user(activity.data["actor"], opts)
170 created_at = activity.data["published"] |> Utils.date_to_asctime()
171 announced_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
172
173 text = "#{user.nickname} retweeted a status."
174
175 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
176
177 %{
178 "id" => activity.id,
179 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
180 "statusnet_html" => text,
181 "text" => text,
182 "is_local" => activity.local,
183 "is_post_verb" => false,
184 "uri" => "tag:#{activity.data["id"]}:objectType=note",
185 "created_at" => created_at,
186 "retweeted_status" => retweeted_status,
187 "statusnet_conversation_id" => get_context_id(announced_activity, opts),
188 "external_url" => activity.data["id"],
189 "activity_type" => "repeat"
190 }
191 end
192
193 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
194 user = get_user(activity.data["actor"], opts)
195 liked_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
196 liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
197
198 created_at =
199 activity.data["published"]
200 |> Utils.date_to_asctime()
201
202 text = "#{user.nickname} favorited a status."
203
204 favorited_status =
205 if liked_activity,
206 do: render("activity.json", Map.merge(opts, %{activity: liked_activity})),
207 else: nil
208
209 %{
210 "id" => activity.id,
211 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
212 "statusnet_html" => text,
213 "text" => text,
214 "is_local" => activity.local,
215 "is_post_verb" => false,
216 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
217 "created_at" => created_at,
218 "favorited_status" => favorited_status,
219 "in_reply_to_status_id" => liked_activity_id,
220 "external_url" => activity.data["id"],
221 "activity_type" => "like"
222 }
223 end
224
225 def render(
226 "activity.json",
227 %{activity: %{data: %{"type" => "Create", "object" => object_id}} = activity} = opts
228 ) do
229 user = get_user(activity.data["actor"], opts)
230
231 object = Object.normalize(object_id)
232
233 created_at = object.data["published"] |> Utils.date_to_asctime()
234 like_count = object.data["like_count"] || 0
235 announcement_count = object.data["announcement_count"] || 0
236 favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
237 repeated = opts[:for] && opts[:for].ap_id in (object.data["announcements"] || [])
238 pinned = activity.id in user.info.pinned_activities
239
240 attentions =
241 []
242 |> Utils.maybe_notify_to_recipients(activity)
243 |> Utils.maybe_notify_mentioned_recipients(activity)
244 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
245 |> Enum.filter(& &1)
246 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
247
248 conversation_id = get_context_id(activity, opts)
249
250 tags = object.data["tag"] || []
251 possibly_sensitive = object.data["sensitive"] || Enum.member?(tags, "nsfw")
252
253 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
254
255 {summary, content} = render_content(object.data)
256
257 html =
258 content
259 |> HTML.get_cached_scrubbed_html_for_activity(
260 User.html_filter_policy(opts[:for]),
261 activity,
262 "twitterapi:content"
263 )
264 |> Formatter.emojify(object.data["emoji"])
265
266 text =
267 if content do
268 content
269 |> String.replace(~r/<br\s?\/?>/, "\n")
270 |> HTML.get_cached_stripped_html_for_activity(activity, "twitterapi:content")
271 else
272 ""
273 end
274
275 reply_parent = Activity.get_in_reply_to_activity(activity)
276
277 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
278
279 summary = HTML.strip_tags(summary)
280
281 card =
282 StatusView.render(
283 "card.json",
284 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
285 )
286
287 %{
288 "id" => activity.id,
289 "uri" => object.data["id"],
290 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
291 "statusnet_html" => html,
292 "text" => text,
293 "is_local" => activity.local,
294 "is_post_verb" => true,
295 "created_at" => created_at,
296 "in_reply_to_status_id" => reply_parent && reply_parent.id,
297 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
298 "in_reply_to_profileurl" => User.profile_url(reply_user),
299 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
300 "in_reply_to_user_id" => reply_user && reply_user.id,
301 "statusnet_conversation_id" => conversation_id,
302 "attachments" => (object.data["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
303 "attentions" => attentions,
304 "fave_num" => like_count,
305 "repeat_num" => announcement_count,
306 "favorited" => !!favorited,
307 "repeated" => !!repeated,
308 "pinned" => pinned,
309 "external_url" => object.data["external_url"] || object.data["id"],
310 "tags" => tags,
311 "activity_type" => "post",
312 "possibly_sensitive" => possibly_sensitive,
313 "visibility" => StatusView.get_visibility(object),
314 "summary" => summary,
315 "summary_html" => summary |> Formatter.emojify(object.data["emoji"]),
316 "card" => card,
317 "muted" => CommonAPI.thread_muted?(user, activity) || User.mutes?(opts[:for], user)
318 }
319 end
320
321 def render("activity.json", %{activity: unhandled_activity}) do
322 Logger.warn("#{__MODULE__} unhandled activity: #{inspect(unhandled_activity)}")
323 nil
324 end
325
326 def render_content(%{"type" => "Note"} = object) do
327 summary = object["summary"]
328
329 content =
330 if !!summary and summary != "" do
331 "<p>#{summary}</p>#{object["content"]}"
332 else
333 object["content"]
334 end
335
336 {summary, content}
337 end
338
339 def render_content(%{"type" => object_type} = object)
340 when object_type in ["Article", "Page", "Video"] do
341 summary = object["name"] || object["summary"]
342
343 content =
344 if !!summary and summary != "" and is_bitstring(object["url"]) do
345 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
346 else
347 object["content"]
348 end
349
350 {summary, content}
351 end
352
353 def render_content(object) do
354 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
355 content = "<p>#{summary}</p>#{object["content"]}"
356
357 {summary, content}
358 end
359 end