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