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,
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"] || [])
125 bookmarked = opts[:for] && object["id"] in opts[:for].bookmarks
127 attachment_data = object["attachment"] || []
128 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
130 created_at = Utils.to_masto_date(object["published"])
132 reply_to = get_reply_to(activity, opts)
133 reply_to_user = reply_to && get_user(reply_to.data["actor"])
138 |> HTML.get_cached_scrubbed_html_for_object(
139 User.html_filter_policy(opts[:for]),
144 card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
147 id: to_string(activity.id),
149 url: object["external_url"] || object["id"],
150 account: AccountView.render("account.json", %{user: user}),
151 in_reply_to_id: reply_to && to_string(reply_to.id),
152 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
156 created_at: created_at,
157 reblogs_count: announcement_count,
159 favourites_count: like_count,
160 reblogged: present?(repeated),
161 favourited: present?(favorited),
162 bookmarked: present?(bookmarked),
164 pinned: pinned?(activity, user),
165 sensitive: sensitive,
166 spoiler_text: object["summary"] || "",
167 visibility: get_visibility(object),
168 media_attachments: attachments |> Enum.take(4),
170 tags: build_tags(tags),
176 emojis: build_emojis(activity.data["object"]["emoji"])
180 def render("status.json", _) do
184 def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
185 page_url_data = URI.parse(page_url)
188 if rich_media[:url] != nil do
189 URI.merge(page_url_data, URI.parse(rich_media[:url]))
194 page_url = page_url_data |> to_string
197 if rich_media[:image] != nil do
198 URI.merge(page_url_data, URI.parse(rich_media[:image]))
204 site_name = rich_media[:site_name] || page_url_data.host
208 provider_name: site_name,
209 provider_url: page_url_data.scheme <> "://" <> page_url_data.host,
211 image: image_url |> MediaProxy.url(),
212 title: rich_media[:title],
213 description: rich_media[:description],
215 opengraph: rich_media
220 def render("card.json", _) do
224 def render("attachment.json", %{attachment: attachment}) do
225 [attachment_url | _] = attachment["url"]
226 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
227 href = attachment_url["href"] |> MediaProxy.url()
231 String.contains?(media_type, "image") -> "image"
232 String.contains?(media_type, "video") -> "video"
233 String.contains?(media_type, "audio") -> "audio"
237 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
240 id: to_string(attachment["id"] || hash_id),
246 description: attachment["name"]
250 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
251 _id = activity.data["object"]["inReplyTo"]
252 replied_to_activities[activity.data["object"]["inReplyTo"]]
255 def get_reply_to(%{data: %{"object" => object}}, _) do
256 if object["inReplyTo"] && object["inReplyTo"] != "" do
257 Activity.get_create_by_object_ap_id(object["inReplyTo"])
263 def get_visibility(object) do
264 public = "https://www.w3.org/ns/activitystreams#Public"
265 to = object["to"] || []
266 cc = object["cc"] || []
275 # this should use the sql for the object's activity
276 Enum.any?(to, &String.contains?(&1, "/followers")) ->
287 def render_content(%{"type" => "Video"} = object) do
288 with name when not is_nil(name) and name != "" <- object["name"] do
289 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
291 _ -> object["content"] || ""
295 def render_content(%{"type" => object_type} = object)
296 when object_type in ["Article", "Page"] do
297 with summary when not is_nil(summary) and summary != "" <- object["name"],
298 url when is_bitstring(url) <- object["url"] do
299 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
301 _ -> object["content"] || ""
305 def render_content(object), do: object["content"] || ""
308 Builds a dictionary tags.
312 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
313 [{"name": "fediverse", "url": "/tag/fediverse"},
314 {"name": "nextcloud", "url": "/tag/nextcloud"}]
317 @spec build_tags(list(any())) :: list(map())
318 def build_tags(object_tags) when is_list(object_tags) do
319 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
321 Enum.reduce(object_tags, [], fn tag, tags ->
322 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
326 def build_tags(_), do: []
331 Arguments: `nil` or list tuple of name and url.
337 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
338 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
341 @spec build_emojis(nil | list(tuple())) :: list(map())
342 def build_emojis(nil), do: []
344 def build_emojis(emojis) do
346 |> Enum.map(fn {name, url} ->
347 name = HTML.strip_tags(name)
354 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
358 defp present?(nil), do: false
359 defp present?(false), do: false
360 defp present?(_), do: true
362 defp pinned?(%Activity{id: id}, %User{info: %{pinned_activities: pinned_activities}}),
363 do: id in pinned_activities