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 defp get_user(ap_id) do
37 user = User.get_cached_by_ap_id(ap_id) ->
40 user = User.get_by_guessed_nickname(ap_id) ->
44 User.error_user(ap_id)
48 def render("index.json", opts) do
49 replied_to_activities = get_replied_to_activities(opts.activities)
55 Map.put(opts, :replied_to_activities, replied_to_activities)
57 |> Enum.filter(fn x -> not is_nil(x) end)
62 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
64 user = get_user(activity.data["actor"])
65 created_at = Utils.to_masto_date(activity.data["published"])
67 reblogged = Activity.get_create_activity_by_object_ap_id(object)
68 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
72 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
74 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
77 id: to_string(activity.id),
80 account: AccountView.render("account.json", %{user: user}),
82 in_reply_to_account_id: nil,
84 content: reblogged[:content] || "",
85 created_at: created_at,
92 pinned: pinned?(activity, user),
96 media_attachments: reblogged[:media_attachments] || [],
98 tags: reblogged[:tags] || [],
108 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
109 user = get_user(activity.data["actor"])
111 like_count = object["like_count"] || 0
112 announcement_count = object["announcement_count"] || 0
114 tags = object["tag"] || []
115 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
119 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
121 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
123 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
124 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
126 attachment_data = object["attachment"] || []
127 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
129 created_at = Utils.to_masto_date(object["published"])
131 reply_to = get_reply_to(activity, opts)
132 reply_to_user = reply_to && get_user(reply_to.data["actor"])
137 |> HTML.get_cached_scrubbed_html_for_object(
138 User.html_filter_policy(opts[:for]),
144 id: to_string(activity.id),
146 url: object["external_url"] || object["id"],
147 account: AccountView.render("account.json", %{user: user}),
148 in_reply_to_id: reply_to && to_string(reply_to.id),
149 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
152 created_at: created_at,
153 reblogs_count: announcement_count,
155 favourites_count: like_count,
156 reblogged: present?(repeated),
157 favourited: present?(favorited),
159 pinned: pinned?(activity, user),
160 sensitive: sensitive,
161 spoiler_text: object["summary"] || "",
162 visibility: get_visibility(object),
163 media_attachments: attachments |> Enum.take(4),
165 tags: build_tags(tags),
171 emojis: build_emojis(activity.data["object"]["emoji"])
175 def render("status.json", _) do
179 def render("attachment.json", %{attachment: attachment}) do
180 [attachment_url | _] = attachment["url"]
181 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
182 href = attachment_url["href"] |> MediaProxy.url()
186 String.contains?(media_type, "image") -> "image"
187 String.contains?(media_type, "video") -> "video"
188 String.contains?(media_type, "audio") -> "audio"
192 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
195 id: to_string(attachment["id"] || hash_id),
201 description: attachment["name"]
205 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
206 _id = activity.data["object"]["inReplyTo"]
207 replied_to_activities[activity.data["object"]["inReplyTo"]]
210 def get_reply_to(%{data: %{"object" => object}}, _) do
211 if object["inReplyTo"] && object["inReplyTo"] != "" do
212 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
218 def get_visibility(object) do
219 public = "https://www.w3.org/ns/activitystreams#Public"
220 to = object["to"] || []
221 cc = object["cc"] || []
230 # this should use the sql for the object's activity
231 Enum.any?(to, &String.contains?(&1, "/followers")) ->
239 def render_content(%{"type" => "Video"} = object) do
240 with name when not is_nil(name) and name != "" <- object["name"] do
241 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
243 _ -> object["content"] || ""
247 def render_content(%{"type" => object_type} = object)
248 when object_type in ["Article", "Page"] do
249 with summary when not is_nil(summary) and summary != "" <- object["name"],
250 url when is_bitstring(url) <- object["url"] do
251 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
253 _ -> object["content"] || ""
257 def render_content(object), do: object["content"] || ""
260 Builds a dictionary tags.
264 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
265 [{"name": "fediverse", "url": "/tag/fediverse"},
266 {"name": "nextcloud", "url": "/tag/nextcloud"}]
269 @spec build_tags(list(any())) :: list(map())
270 def build_tags(object_tags) when is_list(object_tags) do
271 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
273 Enum.reduce(object_tags, [], fn tag, tags ->
274 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
278 def build_tags(_), do: []
283 Arguments: `nil` or list tuple of name and url.
289 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
290 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
293 @spec build_emojis(nil | list(tuple())) :: list(map())
294 def build_emojis(nil), do: []
296 def build_emojis(emojis) do
298 |> Enum.map(fn {name, url} ->
299 name = HTML.strip_tags(name)
306 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
310 defp present?(nil), do: false
311 defp present?(false), do: false
312 defp present?(_), do: true
314 defp pinned?(%Activity{id: id}, %User{info: %{pinned_activities: pinned_activities}}),
315 do: id in pinned_activities