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