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_by_object_ap_id()
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)
61 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
63 user = get_user(activity.data["actor"])
64 created_at = Utils.to_masto_date(activity.data["published"])
66 reblogged = Activity.get_create_by_object_ap_id(object)
67 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
71 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
73 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
76 id: to_string(activity.id),
79 account: AccountView.render("account.json", %{user: user}),
81 in_reply_to_account_id: nil,
83 content: reblogged[:content] || "",
84 created_at: created_at,
91 pinned: pinned?(activity, user),
95 media_attachments: reblogged[:media_attachments] || [],
97 tags: reblogged[:tags] || [],
107 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
108 user = get_user(activity.data["actor"])
110 like_count = object["like_count"] || 0
111 announcement_count = object["announcement_count"] || 0
113 tags = object["tag"] || []
114 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
118 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
120 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
122 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
123 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
125 attachment_data = object["attachment"] || []
126 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
128 created_at = Utils.to_masto_date(object["published"])
130 reply_to = get_reply_to(activity, opts)
131 reply_to_user = reply_to && get_user(reply_to.data["actor"])
136 |> HTML.get_cached_scrubbed_html_for_object(
137 User.html_filter_policy(opts[:for]),
142 card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
145 id: to_string(activity.id),
147 url: object["external_url"] || object["id"],
148 account: AccountView.render("account.json", %{user: user}),
149 in_reply_to_id: reply_to && to_string(reply_to.id),
150 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
154 created_at: created_at,
155 reblogs_count: announcement_count,
157 favourites_count: like_count,
158 reblogged: present?(repeated),
159 favourited: present?(favorited),
161 pinned: pinned?(activity, user),
162 sensitive: sensitive,
163 spoiler_text: object["summary"] || "",
164 visibility: get_visibility(object),
165 media_attachments: attachments |> Enum.take(4),
167 tags: build_tags(tags),
173 emojis: build_emojis(activity.data["object"]["emoji"])
177 def render("status.json", _) do
181 def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
182 page_url = rich_media[:url] || page_url
183 page_url_data = URI.parse(page_url)
184 site_name = rich_media[:site_name] || page_url_data.host
188 provider_name: site_name,
189 provider_url: page_url_data.scheme <> "://" <> page_url_data.host,
191 image: rich_media[:image] |> MediaProxy.url(),
192 title: rich_media[:title],
193 description: rich_media[:description],
195 opengraph: rich_media
200 def render("card.json", _) do
204 def render("attachment.json", %{attachment: attachment}) do
205 [attachment_url | _] = attachment["url"]
206 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
207 href = attachment_url["href"] |> MediaProxy.url()
211 String.contains?(media_type, "image") -> "image"
212 String.contains?(media_type, "video") -> "video"
213 String.contains?(media_type, "audio") -> "audio"
217 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
220 id: to_string(attachment["id"] || hash_id),
226 description: attachment["name"]
230 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
231 _id = activity.data["object"]["inReplyTo"]
232 replied_to_activities[activity.data["object"]["inReplyTo"]]
235 def get_reply_to(%{data: %{"object" => object}}, _) do
236 if object["inReplyTo"] && object["inReplyTo"] != "" do
237 Activity.get_create_by_object_ap_id(object["inReplyTo"])
243 def get_visibility(object) do
244 public = "https://www.w3.org/ns/activitystreams#Public"
245 to = object["to"] || []
246 cc = object["cc"] || []
255 # this should use the sql for the object's activity
256 Enum.any?(to, &String.contains?(&1, "/followers")) ->
267 def render_content(%{"type" => "Video"} = object) do
268 with name when not is_nil(name) and name != "" <- object["name"] do
269 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
271 _ -> object["content"] || ""
275 def render_content(%{"type" => object_type} = object)
276 when object_type in ["Article", "Page"] do
277 with summary when not is_nil(summary) and summary != "" <- object["name"],
278 url when is_bitstring(url) <- object["url"] do
279 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
281 _ -> object["content"] || ""
285 def render_content(object), do: object["content"] || ""
288 Builds a dictionary tags.
292 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
293 [{"name": "fediverse", "url": "/tag/fediverse"},
294 {"name": "nextcloud", "url": "/tag/nextcloud"}]
297 @spec build_tags(list(any())) :: list(map())
298 def build_tags(object_tags) when is_list(object_tags) do
299 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
301 Enum.reduce(object_tags, [], fn tag, tags ->
302 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
306 def build_tags(_), do: []
311 Arguments: `nil` or list tuple of name and url.
317 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
318 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
321 @spec build_emojis(nil | list(tuple())) :: list(map())
322 def build_emojis(nil), do: []
324 def build_emojis(emojis) do
326 |> Enum.map(fn {name, url} ->
327 name = HTML.strip_tags(name)
334 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
338 defp present?(nil), do: false
339 defp present?(false), do: false
340 defp present?(_), do: true
342 defp pinned?(%Activity{id: id}, %User{info: %{pinned_activities: pinned_activities}}),
343 do: id in pinned_activities