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