1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.StatusView do
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Web.MediaProxy
14 alias Pleroma.Web.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.StatusView
17 # TODO: Add cached version.
18 defp get_replied_to_activities(activities) do
21 %{data: %{"type" => "Create", "object" => %{"inReplyTo" => in_reply_to}}} ->
22 in_reply_to != "" && in_reply_to
28 |> Activity.create_activity_by_object_id_query()
30 |> Enum.reduce(%{}, fn activity, acc ->
31 Map.put(acc, activity.data["object"]["id"], activity)
35 def render("index.json", opts) do
36 replied_to_activities = get_replied_to_activities(opts.activities)
42 Map.put(opts, :replied_to_activities, replied_to_activities)
44 |> Enum.filter(fn x -> not is_nil(x) end)
49 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
51 user = User.get_cached_by_ap_id(activity.data["actor"])
52 created_at = Utils.to_masto_date(activity.data["published"])
54 reblogged = Activity.get_create_activity_by_object_ap_id(object)
55 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
59 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
61 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
64 id: to_string(activity.id),
67 account: AccountView.render("account.json", %{user: user}),
69 in_reply_to_account_id: nil,
71 content: reblogged[:content] || "",
72 created_at: created_at,
82 media_attachments: reblogged[:media_attachments] || [],
84 tags: reblogged[:tags] || [],
94 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
95 user = User.get_cached_by_ap_id(activity.data["actor"])
97 like_count = object["like_count"] || 0
98 announcement_count = object["announcement_count"] || 0
100 tags = object["tag"] || []
101 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
105 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
107 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
109 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
110 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
112 attachment_data = object["attachment"] || []
113 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
115 created_at = Utils.to_masto_date(object["published"])
117 reply_to = get_reply_to(activity, opts)
118 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
123 |> HTML.get_cached_scrubbed_html_for_object(
124 User.html_filter_policy(opts[:for]),
130 id: to_string(activity.id),
132 url: object["external_url"] || object["id"],
133 account: AccountView.render("account.json", %{user: user}),
134 in_reply_to_id: reply_to && to_string(reply_to.id),
135 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
138 created_at: created_at,
139 reblogs_count: announcement_count,
141 favourites_count: like_count,
142 reblogged: present?(repeated),
143 favourited: present?(favorited),
145 sensitive: sensitive,
146 spoiler_text: object["summary"] || "",
147 visibility: get_visibility(object),
148 media_attachments: attachments |> Enum.take(4),
150 tags: build_tags(tags),
156 emojis: build_emojis(activity.data["object"]["emoji"])
160 def render("status.json", _) do
164 def render("attachment.json", %{attachment: attachment}) do
165 [attachment_url | _] = attachment["url"]
166 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
167 href = attachment_url["href"] |> MediaProxy.url()
171 String.contains?(media_type, "image") -> "image"
172 String.contains?(media_type, "video") -> "video"
173 String.contains?(media_type, "audio") -> "audio"
177 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
180 id: to_string(attachment["id"] || hash_id),
186 description: attachment["name"]
190 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
191 _id = activity.data["object"]["inReplyTo"]
192 replied_to_activities[activity.data["object"]["inReplyTo"]]
195 def get_reply_to(%{data: %{"object" => object}}, _) do
196 if object["inReplyTo"] && object["inReplyTo"] != "" do
197 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
203 def get_visibility(object) do
204 public = "https://www.w3.org/ns/activitystreams#Public"
205 to = object["to"] || []
206 cc = object["cc"] || []
215 # this should use the sql for the object's activity
216 Enum.any?(to, &String.contains?(&1, "/followers")) ->
224 def render_content(%{"type" => "Video"} = object) do
225 with name when not is_nil(name) and name != "" <- object["name"] do
226 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
228 _ -> object["content"] || ""
232 def render_content(%{"type" => object_type} = object)
233 when object_type in ["Article", "Page"] do
234 with summary when not is_nil(summary) and summary != "" <- object["name"],
235 url when is_bitstring(url) <- object["url"] do
236 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
238 _ -> object["content"] || ""
242 def render_content(object), do: object["content"] || ""
245 Builds a dictionary tags.
249 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
250 [{"name": "fediverse", "url": "/tag/fediverse"},
251 {"name": "nextcloud", "url": "/tag/nextcloud"}]
254 @spec build_tags(list(any())) :: list(map())
255 def build_tags(object_tags) when is_list(object_tags) do
256 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
258 Enum.reduce(object_tags, [], fn tag, tags ->
259 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
263 def build_tags(_), do: []
268 Arguments: `nil` or list tuple of name and url.
274 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
275 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
278 @spec build_emojis(nil | list(tuple())) :: list(map())
279 def build_emojis(nil), do: []
281 def build_emojis(emojis) do
283 |> Enum.map(fn {name, url} ->
284 name = HTML.strip_tags(name)
291 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
295 defp present?(nil), do: false
296 defp present?(false), do: false
297 defp present?(_), do: true