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