Assign reblogged in the Mastodon reblog status view
[akkoma] / lib / pleroma / web / mastodon_api / views / status_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.MastodonAPI.StatusView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.Activity
9 alias Pleroma.HTML
10 alias Pleroma.Repo
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.CommonAPI.Utils
14 alias Pleroma.Web.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.StatusView
16 alias Pleroma.Web.MediaProxy
17
18 # TODO: Add cached version.
19 defp get_replied_to_activities(activities) do
20 activities
21 |> Enum.map(fn
22 %{data: %{"type" => "Create", "object" => %{"inReplyTo" => in_reply_to}}} ->
23 in_reply_to != "" && in_reply_to
24
25 _ ->
26 nil
27 end)
28 |> Enum.filter(& &1)
29 |> Activity.create_by_object_ap_id()
30 |> Repo.all()
31 |> Enum.reduce(%{}, fn activity, acc ->
32 Map.put(acc, activity.data["object"]["id"], activity)
33 end)
34 end
35
36 defp get_user(ap_id) do
37 cond do
38 user = User.get_cached_by_ap_id(ap_id) ->
39 user
40
41 user = User.get_by_guessed_nickname(ap_id) ->
42 user
43
44 true ->
45 User.error_user(ap_id)
46 end
47 end
48
49 defp get_context_id(%{data: %{"context_id" => context_id}}) when not is_nil(context_id),
50 do: context_id
51
52 defp get_context_id(%{data: %{"context" => context}}) when is_binary(context),
53 do: Utils.context_to_conversation_id(context)
54
55 defp get_context_id(_), do: nil
56
57 defp reblogged?(activity, user) do
58 object = activity.data["object"] || %{}
59 present?(user && user.ap_id in (object["announcements"] || []))
60 end
61
62 def render("index.json", opts) do
63 replied_to_activities = get_replied_to_activities(opts.activities)
64
65 opts.activities
66 |> safe_render_many(
67 StatusView,
68 "status.json",
69 Map.put(opts, :replied_to_activities, replied_to_activities)
70 )
71 end
72
73 def render(
74 "status.json",
75 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
76 ) do
77 user = get_user(activity.data["actor"])
78 created_at = Utils.to_masto_date(activity.data["published"])
79
80 reblogged_activity = Activity.get_create_by_object_ap_id(object)
81 reblogged = render("status.json", Map.put(opts, :activity, reblogged_activity))
82
83 mentions =
84 activity.recipients
85 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
86 |> Enum.filter(& &1)
87 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
88
89 %{
90 id: to_string(activity.id),
91 uri: object,
92 url: object,
93 account: AccountView.render("account.json", %{user: user}),
94 in_reply_to_id: nil,
95 in_reply_to_account_id: nil,
96 reblog: reblogged,
97 content: reblogged[:content] || "",
98 created_at: created_at,
99 reblogs_count: 0,
100 replies_count: 0,
101 favourites_count: 0,
102 reblogged: reblogged?(reblogged_activity, opts[:for]),
103 favourited: false,
104 bookmarked: false,
105 muted: false,
106 pinned: pinned?(activity, user),
107 sensitive: false,
108 spoiler_text: "",
109 visibility: "public",
110 media_attachments: reblogged[:media_attachments] || [],
111 mentions: mentions,
112 tags: reblogged[:tags] || [],
113 application: %{
114 name: "Web",
115 website: nil
116 },
117 language: nil,
118 emojis: [],
119 pleroma: %{
120 local: activity.local
121 }
122 }
123 end
124
125 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
126 user = get_user(activity.data["actor"])
127
128 like_count = object["like_count"] || 0
129 announcement_count = object["announcement_count"] || 0
130
131 tags = object["tag"] || []
132 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
133
134 mentions =
135 activity.recipients
136 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
137 |> Enum.filter(& &1)
138 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
139
140 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
141 bookmarked = opts[:for] && object["id"] in opts[:for].bookmarks
142
143 attachment_data = object["attachment"] || []
144 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
145
146 created_at = Utils.to_masto_date(object["published"])
147
148 reply_to = get_reply_to(activity, opts)
149 reply_to_user = reply_to && get_user(reply_to.data["actor"])
150
151 content =
152 object
153 |> render_content()
154
155 content_html =
156 content
157 |> HTML.get_cached_scrubbed_html_for_activity(
158 User.html_filter_policy(opts[:for]),
159 activity,
160 "mastoapi:content"
161 )
162
163 content_plaintext =
164 content
165 |> HTML.get_cached_stripped_html_for_activity(
166 activity,
167 "mastoapi:content"
168 )
169
170 summary = object["summary"] || ""
171
172 summary_html =
173 summary
174 |> HTML.get_cached_scrubbed_html_for_activity(
175 User.html_filter_policy(opts[:for]),
176 activity,
177 "mastoapi:summary"
178 )
179
180 summary_plaintext =
181 summary
182 |> HTML.get_cached_stripped_html_for_activity(
183 activity,
184 "mastoapi:summary"
185 )
186
187 card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
188
189 url =
190 if user.local do
191 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
192 else
193 object["external_url"] || object["id"]
194 end
195
196 %{
197 id: to_string(activity.id),
198 uri: object["id"],
199 url: url,
200 account: AccountView.render("account.json", %{user: user}),
201 in_reply_to_id: reply_to && to_string(reply_to.id),
202 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
203 reblog: nil,
204 card: card,
205 content: content_html,
206 created_at: created_at,
207 reblogs_count: announcement_count,
208 replies_count: object["repliesCount"] || 0,
209 favourites_count: like_count,
210 reblogged: reblogged?(activity, opts[:for]),
211 favourited: present?(favorited),
212 bookmarked: present?(bookmarked),
213 muted: CommonAPI.thread_muted?(user, activity) || User.mutes?(opts[:for], user),
214 pinned: pinned?(activity, user),
215 sensitive: sensitive,
216 spoiler_text: summary_html,
217 visibility: get_visibility(object),
218 media_attachments: attachments,
219 mentions: mentions,
220 tags: build_tags(tags),
221 application: %{
222 name: "Web",
223 website: nil
224 },
225 language: nil,
226 emojis: build_emojis(activity.data["object"]["emoji"]),
227 pleroma: %{
228 local: activity.local,
229 conversation_id: get_context_id(activity),
230 content: %{"text/plain" => content_plaintext},
231 spoiler_text: %{"text/plain" => summary_plaintext}
232 }
233 }
234 end
235
236 def render("status.json", _) do
237 nil
238 end
239
240 def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
241 page_url_data = URI.parse(page_url)
242
243 page_url_data =
244 if rich_media[:url] != nil do
245 URI.merge(page_url_data, URI.parse(rich_media[:url]))
246 else
247 page_url_data
248 end
249
250 page_url = page_url_data |> to_string
251
252 image_url =
253 if rich_media[:image] != nil do
254 URI.merge(page_url_data, URI.parse(rich_media[:image]))
255 |> to_string
256 else
257 nil
258 end
259
260 site_name = rich_media[:site_name] || page_url_data.host
261
262 %{
263 type: "link",
264 provider_name: site_name,
265 provider_url: page_url_data.scheme <> "://" <> page_url_data.host,
266 url: page_url,
267 image: image_url |> MediaProxy.url(),
268 title: rich_media[:title],
269 description: rich_media[:description],
270 pleroma: %{
271 opengraph: rich_media
272 }
273 }
274 end
275
276 def render("card.json", _) do
277 nil
278 end
279
280 def render("attachment.json", %{attachment: attachment}) do
281 [attachment_url | _] = attachment["url"]
282 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
283 href = attachment_url["href"] |> MediaProxy.url()
284
285 type =
286 cond do
287 String.contains?(media_type, "image") -> "image"
288 String.contains?(media_type, "video") -> "video"
289 String.contains?(media_type, "audio") -> "audio"
290 true -> "unknown"
291 end
292
293 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
294
295 %{
296 id: to_string(attachment["id"] || hash_id),
297 url: href,
298 remote_url: href,
299 preview_url: href,
300 text_url: href,
301 type: type,
302 description: attachment["name"],
303 pleroma: %{mime_type: media_type}
304 }
305 end
306
307 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
308 with nil <- replied_to_activities[activity.data["object"]["inReplyTo"]] do
309 # If user didn't participate in the thread
310 Activity.get_in_reply_to_activity(activity)
311 end
312 end
313
314 def get_reply_to(%{data: %{"object" => object}}, _) do
315 if object["inReplyTo"] && object["inReplyTo"] != "" do
316 Activity.get_create_by_object_ap_id(object["inReplyTo"])
317 else
318 nil
319 end
320 end
321
322 def get_visibility(object) do
323 public = "https://www.w3.org/ns/activitystreams#Public"
324 to = object["to"] || []
325 cc = object["cc"] || []
326
327 cond do
328 public in to ->
329 "public"
330
331 public in cc ->
332 "unlisted"
333
334 # this should use the sql for the object's activity
335 Enum.any?(to, &String.contains?(&1, "/followers")) ->
336 "private"
337
338 length(cc) > 0 ->
339 "private"
340
341 true ->
342 "direct"
343 end
344 end
345
346 def render_content(%{"type" => "Video"} = object) do
347 with name when not is_nil(name) and name != "" <- object["name"] do
348 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
349 else
350 _ -> object["content"] || ""
351 end
352 end
353
354 def render_content(%{"type" => object_type} = object)
355 when object_type in ["Article", "Page"] do
356 with summary when not is_nil(summary) and summary != "" <- object["name"],
357 url when is_bitstring(url) <- object["url"] do
358 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
359 else
360 _ -> object["content"] || ""
361 end
362 end
363
364 def render_content(object), do: object["content"] || ""
365
366 @doc """
367 Builds a dictionary tags.
368
369 ## Examples
370
371 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
372 [{"name": "fediverse", "url": "/tag/fediverse"},
373 {"name": "nextcloud", "url": "/tag/nextcloud"}]
374
375 """
376 @spec build_tags(list(any())) :: list(map())
377 def build_tags(object_tags) when is_list(object_tags) do
378 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
379
380 Enum.reduce(object_tags, [], fn tag, tags ->
381 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
382 end)
383 end
384
385 def build_tags(_), do: []
386
387 @doc """
388 Builds list emojis.
389
390 Arguments: `nil` or list tuple of name and url.
391
392 Returns list emojis.
393
394 ## Examples
395
396 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
397 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
398
399 """
400 @spec build_emojis(nil | list(tuple())) :: list(map())
401 def build_emojis(nil), do: []
402
403 def build_emojis(emojis) do
404 emojis
405 |> Enum.map(fn {name, url} ->
406 name = HTML.strip_tags(name)
407
408 url =
409 url
410 |> HTML.strip_tags()
411 |> MediaProxy.url()
412
413 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
414 end)
415 end
416
417 defp present?(nil), do: false
418 defp present?(false), do: false
419 defp present?(_), do: true
420
421 defp pinned?(%Activity{id: id}, %User{info: %{pinned_activities: pinned_activities}}),
422 do: id in pinned_activities
423 end