Merge branch 'feature/788-separate-email-addresses' 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.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}} = activity} = opts
228 ) do
229 user = get_user(activity.data["actor"], opts)
230
231 created_at = object["published"] |> Utils.date_to_asctime()
232 like_count = object["like_count"] || 0
233 announcement_count = object["announcement_count"] || 0
234 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
235 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
236 pinned = activity.id in user.info.pinned_activities
237
238 attentions =
239 []
240 |> Utils.maybe_notify_to_recipients(activity)
241 |> Utils.maybe_notify_mentioned_recipients(activity)
242 |> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
243 |> Enum.filter(& &1)
244 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
245
246 conversation_id = get_context_id(activity, opts)
247
248 tags = activity.data["object"]["tag"] || []
249 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
250
251 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
252
253 {summary, content} = render_content(object)
254
255 html =
256 content
257 |> HTML.get_cached_scrubbed_html_for_activity(
258 User.html_filter_policy(opts[:for]),
259 activity,
260 "twitterapi:content"
261 )
262 |> Formatter.emojify(object["emoji"])
263
264 text =
265 if content do
266 content
267 |> String.replace(~r/<br\s?\/?>/, "\n")
268 |> HTML.get_cached_stripped_html_for_activity(activity, "twitterapi:content")
269 else
270 ""
271 end
272
273 reply_parent = Activity.get_in_reply_to_activity(activity)
274
275 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
276
277 summary = HTML.strip_tags(summary)
278
279 card =
280 StatusView.render(
281 "card.json",
282 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
283 )
284
285 %{
286 "id" => activity.id,
287 "uri" => activity.data["object"]["id"],
288 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
289 "statusnet_html" => html,
290 "text" => text,
291 "is_local" => activity.local,
292 "is_post_verb" => true,
293 "created_at" => created_at,
294 "in_reply_to_status_id" => reply_parent && reply_parent.id,
295 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
296 "in_reply_to_profileurl" => User.profile_url(reply_user),
297 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
298 "in_reply_to_user_id" => reply_user && reply_user.id,
299 "statusnet_conversation_id" => conversation_id,
300 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
301 "attentions" => attentions,
302 "fave_num" => like_count,
303 "repeat_num" => announcement_count,
304 "favorited" => !!favorited,
305 "repeated" => !!repeated,
306 "pinned" => pinned,
307 "external_url" => object["external_url"] || object["id"],
308 "tags" => tags,
309 "activity_type" => "post",
310 "possibly_sensitive" => possibly_sensitive,
311 "visibility" => StatusView.get_visibility(object),
312 "summary" => summary,
313 "summary_html" => summary |> Formatter.emojify(object["emoji"]),
314 "card" => card,
315 "muted" => CommonAPI.thread_muted?(user, activity) || User.mutes?(opts[:for], user)
316 }
317 end
318
319 def render("activity.json", %{activity: unhandled_activity}) do
320 Logger.warn("#{__MODULE__} unhandled activity: #{inspect(unhandled_activity)}")
321 nil
322 end
323
324 def render_content(%{"type" => "Note"} = object) do
325 summary = object["summary"]
326
327 content =
328 if !!summary and summary != "" do
329 "<p>#{summary}</p>#{object["content"]}"
330 else
331 object["content"]
332 end
333
334 {summary, content}
335 end
336
337 def render_content(%{"type" => object_type} = object)
338 when object_type in ["Article", "Page", "Video"] do
339 summary = object["name"] || object["summary"]
340
341 content =
342 if !!summary and summary != "" and is_bitstring(object["url"]) do
343 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
344 else
345 object["content"]
346 end
347
348 {summary, content}
349 end
350
351 def render_content(object) do
352 summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
353 content = "<p>#{summary}</p>#{object["content"]}"
354
355 {summary, content}
356 end
357 end