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
13 alias Pleroma.Web.CommonAPI.Utils
14 alias Pleroma.Web.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.StatusView
16 alias Pleroma.Web.MediaProxy
18 # TODO: Add cached version.
19 defp get_replied_to_activities(activities) do
22 %{data: %{"type" => "Create", "object" => %{"inReplyTo" => in_reply_to}}} ->
23 in_reply_to != "" && in_reply_to
29 |> Activity.create_by_object_ap_id()
31 |> Enum.reduce(%{}, fn activity, acc ->
32 Map.put(acc, activity.data["object"]["id"], activity)
36 defp get_user(ap_id) do
38 user = User.get_cached_by_ap_id(ap_id) ->
41 user = User.get_by_guessed_nickname(ap_id) ->
45 User.error_user(ap_id)
49 def render("index.json", opts) do
50 replied_to_activities = get_replied_to_activities(opts.activities)
56 Map.put(opts, :replied_to_activities, replied_to_activities)
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_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,
93 pinned: pinned?(activity, user),
97 media_attachments: reblogged[:media_attachments] || [],
99 tags: reblogged[:tags] || [],
109 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
110 user = get_user(activity.data["actor"])
112 like_count = object["like_count"] || 0
113 announcement_count = object["announcement_count"] || 0
115 tags = object["tag"] || []
116 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
120 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
122 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
124 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
125 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
126 bookmarked = opts[:for] && object["id"] in opts[:for].bookmarks
128 attachment_data = object["attachment"] || []
129 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
131 created_at = Utils.to_masto_date(object["published"])
133 reply_to = get_reply_to(activity, opts)
134 reply_to_user = reply_to && get_user(reply_to.data["actor"])
139 |> HTML.get_cached_scrubbed_html_for_object(
140 User.html_filter_policy(opts[:for]),
145 card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
148 id: to_string(activity.id),
150 url: object["external_url"] || object["id"],
151 account: AccountView.render("account.json", %{user: user}),
152 in_reply_to_id: reply_to && to_string(reply_to.id),
153 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
157 created_at: created_at,
158 reblogs_count: announcement_count,
160 favourites_count: like_count,
161 reblogged: present?(repeated),
162 favourited: present?(favorited),
163 bookmarked: present?(bookmarked),
164 muted: CommonAPI.thread_muted?(user, activity),
165 pinned: pinned?(activity, user),
166 sensitive: sensitive,
167 spoiler_text: object["summary"] || "",
168 visibility: get_visibility(object),
169 media_attachments: attachments,
171 tags: build_tags(tags),
177 emojis: build_emojis(activity.data["object"]["emoji"])
181 def render("status.json", _) do
185 def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
186 page_url_data = URI.parse(page_url)
189 if rich_media[:url] != nil do
190 URI.merge(page_url_data, URI.parse(rich_media[:url]))
195 page_url = page_url_data |> to_string
198 if rich_media[:image] != nil do
199 URI.merge(page_url_data, URI.parse(rich_media[:image]))
205 site_name = rich_media[:site_name] || page_url_data.host
209 provider_name: site_name,
210 provider_url: page_url_data.scheme <> "://" <> page_url_data.host,
212 image: image_url |> MediaProxy.url(),
213 title: rich_media[:title],
214 description: rich_media[:description],
216 opengraph: rich_media
221 def render("card.json", _) do
225 def render("attachment.json", %{attachment: attachment}) do
226 [attachment_url | _] = attachment["url"]
227 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
228 href = attachment_url["href"] |> MediaProxy.url()
232 String.contains?(media_type, "image") -> "image"
233 String.contains?(media_type, "video") -> "video"
234 String.contains?(media_type, "audio") -> "audio"
238 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
241 id: to_string(attachment["id"] || hash_id),
247 description: attachment["name"]
251 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
252 _id = activity.data["object"]["inReplyTo"]
253 replied_to_activities[activity.data["object"]["inReplyTo"]]
256 def get_reply_to(%{data: %{"object" => object}}, _) do
257 if object["inReplyTo"] && object["inReplyTo"] != "" do
258 Activity.get_create_by_object_ap_id(object["inReplyTo"])
264 def get_visibility(object) do
265 public = "https://www.w3.org/ns/activitystreams#Public"
266 to = object["to"] || []
267 cc = object["cc"] || []
276 # this should use the sql for the object's activity
277 Enum.any?(to, &String.contains?(&1, "/followers")) ->
288 def render_content(%{"type" => "Video"} = object) do
289 with name when not is_nil(name) and name != "" <- object["name"] do
290 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
292 _ -> object["content"] || ""
296 def render_content(%{"type" => object_type} = object)
297 when object_type in ["Article", "Page"] do
298 with summary when not is_nil(summary) and summary != "" <- object["name"],
299 url when is_bitstring(url) <- object["url"] do
300 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
302 _ -> object["content"] || ""
306 def render_content(object), do: object["content"] || ""
309 Builds a dictionary tags.
313 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
314 [{"name": "fediverse", "url": "/tag/fediverse"},
315 {"name": "nextcloud", "url": "/tag/nextcloud"}]
318 @spec build_tags(list(any())) :: list(map())
319 def build_tags(object_tags) when is_list(object_tags) do
320 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
322 Enum.reduce(object_tags, [], fn tag, tags ->
323 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
327 def build_tags(_), do: []
332 Arguments: `nil` or list tuple of name and url.
338 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
339 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
342 @spec build_emojis(nil | list(tuple())) :: list(map())
343 def build_emojis(nil), do: []
345 def build_emojis(emojis) do
347 |> Enum.map(fn {name, url} ->
348 name = HTML.strip_tags(name)
355 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
359 defp present?(nil), do: false
360 defp present?(false), do: false
361 defp present?(_), do: true
363 defp pinned?(%Activity{id: id}, %User{info: %{pinned_activities: pinned_activities}}),
364 do: id in pinned_activities