1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.StatusView do
8 require Pleroma.Constants
10 alias Pleroma.Activity
15 alias Pleroma.UserRelationship
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.CommonAPI.Utils
18 alias Pleroma.Web.MastodonAPI.AccountView
19 alias Pleroma.Web.MastodonAPI.PollView
20 alias Pleroma.Web.MastodonAPI.StatusView
21 alias Pleroma.Web.MediaProxy
23 import Pleroma.Web.ActivityPub.Visibility, only: [get_visibility: 1, visible_for_user?: 2]
25 # This is a naive way to do this, just spawning a process per activity
26 # to fetch the preview. However it should be fine considering
27 # pagination is restricted to 40 activities at a time
28 defp fetch_rich_media_for_activities(activities) do
29 Enum.each(activities, fn activity ->
31 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
36 # TODO: Add cached version.
37 defp get_replied_to_activities([]), do: %{}
39 defp get_replied_to_activities(activities) do
42 %{data: %{"type" => "Create"}} = activity ->
43 object = Object.normalize(activity)
44 object && object.data["inReplyTo"] != "" && object.data["inReplyTo"]
50 |> Activity.create_by_object_ap_id_with_object()
52 |> Enum.reduce(%{}, fn activity, acc ->
53 object = Object.normalize(activity)
54 if object, do: Map.put(acc, object.data["id"], activity), else: acc
58 def get_user(ap_id, fake_record_fallback \\ true) do
60 user = User.get_cached_by_ap_id(ap_id) ->
63 user = User.get_by_guessed_nickname(ap_id) ->
66 fake_record_fallback ->
67 # TODO: refactor (fake records is never a good idea)
68 User.error_user(ap_id)
75 defp get_context_id(%{data: %{"context_id" => context_id}}) when not is_nil(context_id),
78 defp get_context_id(%{data: %{"context" => context}}) when is_binary(context),
79 do: Utils.context_to_conversation_id(context)
81 defp get_context_id(_), do: nil
83 defp reblogged?(activity, user) do
84 object = Object.normalize(activity) || %{}
85 present?(user && user.ap_id in (object.data["announcements"] || []))
88 def render("index.json", opts) do
89 reading_user = opts[:for]
91 # To do: check AdminAPIControllerTest on the reasons behind nil activities in the list
92 activities = Enum.filter(opts.activities, & &1)
94 # Start fetching rich media before doing anything else, so that later calls to get the cards
95 # only block for timeout in the worst case, as opposed to
96 # length(activities_with_links) * timeout
97 fetch_rich_media_for_activities(activities)
98 replied_to_activities = get_replied_to_activities(activities)
102 |> Enum.filter(&(&1.data["type"] == "Announce" && &1.data["object"]))
103 |> Enum.map(&Object.normalize(&1).data["id"])
104 |> Activity.create_by_object_ap_id()
105 |> Activity.with_preloaded_object(:left)
106 |> Activity.with_preloaded_bookmark(reading_user)
107 |> Activity.with_set_thread_muted_field(reading_user)
112 Map.has_key?(opts, :relationships) ->
115 is_nil(reading_user) ->
116 UserRelationship.view_relationships_option(nil, [])
119 # Note: unresolved users are filtered out
121 (activities ++ parent_activities)
122 |> Enum.map(&get_user(&1.data["actor"], false))
125 UserRelationship.view_relationships_option(reading_user, actors, subset: :source_mutes)
130 |> Map.put(:replied_to_activities, replied_to_activities)
131 |> Map.put(:parent_activities, parent_activities)
132 |> Map.put(:relationships, relationships_opt)
134 safe_render_many(activities, StatusView, "show.json", opts)
139 %{activity: %{data: %{"type" => "Announce", "object" => _object}} = activity} = opts
141 user = get_user(activity.data["actor"])
142 created_at = Utils.to_masto_date(activity.data["published"])
143 activity_object = Object.normalize(activity)
145 reblogged_parent_activity =
146 if opts[:parent_activities] do
147 Activity.Queries.find_by_object_ap_id(
148 opts[:parent_activities],
149 activity_object.data["id"]
152 Activity.create_by_object_ap_id(activity_object.data["id"])
153 |> Activity.with_preloaded_bookmark(opts[:for])
154 |> Activity.with_set_thread_muted_field(opts[:for])
158 reblog_rendering_opts = Map.put(opts, :activity, reblogged_parent_activity)
159 reblogged = render("show.json", reblog_rendering_opts)
161 favorited = opts[:for] && opts[:for].ap_id in (activity_object.data["likes"] || [])
163 bookmarked = Activity.get_bookmark(reblogged_parent_activity, opts[:for]) != nil
167 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
169 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
172 id: to_string(activity.id),
173 uri: activity_object.data["id"],
174 url: activity_object.data["id"],
176 AccountView.render("show.json", %{
181 in_reply_to_account_id: nil,
183 content: reblogged[:content] || "",
184 created_at: created_at,
188 reblogged: reblogged?(reblogged_parent_activity, opts[:for]),
189 favourited: present?(favorited),
190 bookmarked: present?(bookmarked),
192 pinned: pinned?(activity, user),
195 visibility: get_visibility(activity),
196 media_attachments: reblogged[:media_attachments] || [],
198 tags: reblogged[:tags] || [],
206 local: activity.local
211 def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
212 object = Object.normalize(activity)
214 user = get_user(activity.data["actor"])
215 user_follower_address = user.follower_address
217 like_count = object.data["like_count"] || 0
218 announcement_count = object.data["announcement_count"] || 0
220 tags = object.data["tag"] || []
221 sensitive = object.data["sensitive"] || Enum.member?(tags, "nsfw")
225 |> Enum.filter(fn tag -> is_map(tag) and tag["type"] == "Mention" end)
226 |> Enum.map(fn tag -> tag["href"] end)
229 (object.data["to"] ++ tag_mentions)
232 Pleroma.Constants.as_public() -> nil
233 ^user_follower_address -> nil
234 ap_id -> User.get_cached_by_ap_id(ap_id)
237 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
239 favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
241 bookmarked = Activity.get_bookmark(activity, opts[:for]) != nil
243 client_posted_this_activity = opts[:for] && user.id == opts[:for].id
246 with true <- client_posted_this_activity,
247 %Oban.Job{scheduled_at: scheduled_at} <-
248 Pleroma.Workers.PurgeExpiredActivity.get_expiration(activity.id) do
256 is_nil(opts[:for]) -> false
257 is_boolean(activity.thread_muted?) -> activity.thread_muted?
258 true -> CommonAPI.thread_muted?(opts[:for], activity)
261 attachment_data = object.data["attachment"] || []
262 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
264 created_at = Utils.to_masto_date(object.data["published"])
266 reply_to = get_reply_to(activity, opts)
268 reply_to_user = reply_to && get_user(reply_to.data["actor"])
276 |> HTML.get_cached_scrubbed_html_for_activity(
277 User.html_filter_policy(opts[:for]),
284 |> HTML.get_cached_stripped_html_for_activity(
289 summary = object.data["summary"] || ""
291 card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
295 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
297 object.data["url"] || object.data["external_url"] || object.data["id"]
300 direct_conversation_id =
301 with {_, nil} <- {:direct_conversation_id, opts[:direct_conversation_id]},
302 {_, true} <- {:include_id, opts[:with_direct_conversation_id]},
303 {_, %User{} = for_user} <- {:for_user, opts[:for]} do
304 Activity.direct_conversation_id(activity, for_user)
306 {:direct_conversation_id, participation_id} when is_integer(participation_id) ->
314 with %{data: %{"reactions" => emoji_reactions}} <- object do
315 Enum.map(emoji_reactions, fn
316 [emoji, users] when is_list(users) ->
317 build_emoji_map(emoji, users, opts[:for])
319 {emoji, users} when is_list(users) ->
320 build_emoji_map(emoji, users, opts[:for])
325 |> Enum.reject(&is_nil/1)
330 # Status muted state (would do 1 request per status unless user mutes are preloaded)
333 UserRelationship.exists?(
334 get_in(opts, [:relationships, :user_relationships]),
338 fn for_user, user -> User.mutes?(for_user, user) end
342 id: to_string(activity.id),
343 uri: object.data["id"],
346 AccountView.render("show.json", %{
350 in_reply_to_id: reply_to && to_string(reply_to.id),
351 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
354 content: content_html,
355 text: opts[:with_source] && object.data["source"],
356 created_at: created_at,
357 reblogs_count: announcement_count,
358 replies_count: object.data["repliesCount"] || 0,
359 favourites_count: like_count,
360 reblogged: reblogged?(activity, opts[:for]),
361 favourited: present?(favorited),
362 bookmarked: present?(bookmarked),
364 pinned: pinned?(activity, user),
365 sensitive: sensitive,
366 spoiler_text: summary,
367 visibility: get_visibility(object),
368 media_attachments: attachments,
369 poll: render(PollView, "show.json", object: object, for: opts[:for]),
371 tags: build_tags(tags),
377 emojis: build_emojis(object.data["emoji"]),
379 local: activity.local,
380 conversation_id: get_context_id(activity),
381 in_reply_to_account_acct: reply_to_user && reply_to_user.nickname,
382 content: %{"text/plain" => content_plaintext},
383 spoiler_text: %{"text/plain" => summary},
384 expires_at: expires_at,
385 direct_conversation_id: direct_conversation_id,
386 thread_muted: thread_muted?,
387 emoji_reactions: emoji_reactions,
388 parent_visible: visible_for_user?(reply_to, opts[:for])
393 def render("show.json", _) do
397 def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
398 page_url_data = URI.parse(page_url)
401 if is_binary(rich_media["url"]) do
402 URI.merge(page_url_data, URI.parse(rich_media["url"]))
407 page_url = page_url_data |> to_string
410 if is_binary(rich_media["image"]) do
411 URI.merge(page_url_data, URI.parse(rich_media["image"]))
417 provider_name: page_url_data.host,
418 provider_url: page_url_data.scheme <> "://" <> page_url_data.host,
420 image: image_url |> MediaProxy.url(),
421 title: rich_media["title"] || "",
422 description: rich_media["description"] || "",
424 opengraph: rich_media
429 def render("card.json", _), do: nil
431 def render("attachment.json", %{attachment: attachment}) do
432 [attachment_url | _] = attachment["url"]
433 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
434 href = attachment_url["href"] |> MediaProxy.url()
438 String.contains?(media_type, "image") -> "image"
439 String.contains?(media_type, "video") -> "video"
440 String.contains?(media_type, "audio") -> "audio"
444 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
447 id: to_string(attachment["id"] || hash_id),
453 description: attachment["name"],
454 pleroma: %{mime_type: media_type}
458 def render("context.json", %{activity: activity, activities: activities, user: user}) do
459 %{ancestors: ancestors, descendants: descendants} =
462 |> Enum.group_by(fn %{id: id} -> if id < activity.id, do: :ancestors, else: :descendants end)
463 |> Map.put_new(:ancestors, [])
464 |> Map.put_new(:descendants, [])
467 ancestors: render("index.json", for: user, activities: ancestors, as: :activity),
468 descendants: render("index.json", for: user, activities: descendants, as: :activity)
472 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
473 object = Object.normalize(activity)
475 with nil <- replied_to_activities[object.data["inReplyTo"]] do
476 # If user didn't participate in the thread
477 Activity.get_in_reply_to_activity(activity)
481 def get_reply_to(%{data: %{"object" => _object}} = activity, _) do
482 object = Object.normalize(activity)
484 if object.data["inReplyTo"] && object.data["inReplyTo"] != "" do
485 Activity.get_create_by_object_ap_id(object.data["inReplyTo"])
491 def render_content(%{data: %{"name" => name}} = object) when not is_nil(name) and name != "" do
492 url = object.data["url"] || object.data["id"]
494 "<p><a href=\"#{url}\">#{name}</a></p>#{object.data["content"]}"
497 def render_content(object), do: object.data["content"] || ""
500 Builds a dictionary tags.
504 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
505 [{"name": "fediverse", "url": "/tag/fediverse"},
506 {"name": "nextcloud", "url": "/tag/nextcloud"}]
509 @spec build_tags(list(any())) :: list(map())
510 def build_tags(object_tags) when is_list(object_tags) do
512 |> Enum.filter(&is_binary/1)
513 |> Enum.map(&%{name: &1, url: "/tag/#{URI.encode(&1)}"})
516 def build_tags(_), do: []
521 Arguments: `nil` or list tuple of name and url.
527 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
528 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
531 @spec build_emojis(nil | list(tuple())) :: list(map())
532 def build_emojis(nil), do: []
534 def build_emojis(emojis) do
536 |> Enum.map(fn {name, url} ->
537 name = HTML.strip_tags(name)
544 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
548 defp present?(nil), do: false
549 defp present?(false), do: false
550 defp present?(_), do: true
552 defp pinned?(%Activity{id: id}, %User{pinned_activities: pinned_activities}),
553 do: id in pinned_activities
555 defp build_emoji_map(emoji, users, current_user) do
558 count: length(users),
559 me: !!(current_user && current_user.ap_id in users)