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 defp get_context_id(%{data: %{"context_id" => context_id}}) when not is_nil(context_id),
52 defp get_context_id(%{data: %{"context" => context}}) when is_binary(context),
53 do: Utils.context_to_conversation_id(context)
55 defp get_context_id(_), do: nil
57 def render("index.json", opts) do
58 replied_to_activities = get_replied_to_activities(opts.activities)
64 Map.put(opts, :replied_to_activities, replied_to_activities)
70 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
72 user = get_user(activity.data["actor"])
73 created_at = Utils.to_masto_date(activity.data["published"])
75 reblogged = Activity.get_create_by_object_ap_id(object)
76 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
80 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
82 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
85 id: to_string(activity.id),
88 account: AccountView.render("account.json", %{user: user}),
90 in_reply_to_account_id: nil,
92 content: reblogged[:content] || "",
93 created_at: created_at,
101 pinned: pinned?(activity, user),
104 visibility: "public",
105 media_attachments: reblogged[:media_attachments] || [],
107 tags: reblogged[:tags] || [],
115 local: activity.local
120 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
121 user = get_user(activity.data["actor"])
123 like_count = object["like_count"] || 0
124 announcement_count = object["announcement_count"] || 0
126 tags = object["tag"] || []
127 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
131 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
133 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
135 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
136 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
137 bookmarked = opts[:for] && object["id"] in opts[:for].bookmarks
139 attachment_data = object["attachment"] || []
140 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
142 created_at = Utils.to_masto_date(object["published"])
144 reply_to = get_reply_to(activity, opts)
145 reply_to_user = reply_to && get_user(reply_to.data["actor"])
150 |> HTML.get_cached_scrubbed_html_for_object(
151 User.html_filter_policy(opts[:for]),
156 card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
160 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
162 object["external_url"] || object["id"]
166 id: to_string(activity.id),
169 account: AccountView.render("account.json", %{user: user}),
170 in_reply_to_id: reply_to && to_string(reply_to.id),
171 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
175 created_at: created_at,
176 reblogs_count: announcement_count,
177 replies_count: object["repliesCount"] || 0,
178 favourites_count: like_count,
179 reblogged: present?(repeated),
180 favourited: present?(favorited),
181 bookmarked: present?(bookmarked),
182 muted: CommonAPI.thread_muted?(user, activity) || User.mutes?(opts[:for], user),
183 pinned: pinned?(activity, user),
184 sensitive: sensitive,
185 spoiler_text: object["summary"] || "",
186 visibility: get_visibility(object),
187 media_attachments: attachments,
189 tags: build_tags(tags),
195 emojis: build_emojis(activity.data["object"]["emoji"]),
197 local: activity.local,
198 conversation_id: get_context_id(activity)
203 def render("status.json", _) do
207 def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
208 page_url_data = URI.parse(page_url)
211 if rich_media[:url] != nil do
212 URI.merge(page_url_data, URI.parse(rich_media[:url]))
217 page_url = page_url_data |> to_string
220 if rich_media[:image] != nil do
221 URI.merge(page_url_data, URI.parse(rich_media[:image]))
227 site_name = rich_media[:site_name] || page_url_data.host
231 provider_name: site_name,
232 provider_url: page_url_data.scheme <> "://" <> page_url_data.host,
234 image: image_url |> MediaProxy.url(),
235 title: rich_media[:title],
236 description: rich_media[:description],
238 opengraph: rich_media
243 def render("card.json", _) do
247 def render("attachment.json", %{attachment: attachment}) do
248 [attachment_url | _] = attachment["url"]
249 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
250 href = attachment_url["href"] |> MediaProxy.url()
254 String.contains?(media_type, "image") -> "image"
255 String.contains?(media_type, "video") -> "video"
256 String.contains?(media_type, "audio") -> "audio"
260 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
263 id: to_string(attachment["id"] || hash_id),
269 description: attachment["name"],
270 pleroma: %{mime_type: media_type}
274 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
275 _id = activity.data["object"]["inReplyTo"]
276 replied_to_activities[activity.data["object"]["inReplyTo"]]
279 def get_reply_to(%{data: %{"object" => object}}, _) do
280 if object["inReplyTo"] && object["inReplyTo"] != "" do
281 Activity.get_create_by_object_ap_id(object["inReplyTo"])
287 def get_visibility(object) do
288 public = "https://www.w3.org/ns/activitystreams#Public"
289 to = object["to"] || []
290 cc = object["cc"] || []
299 # this should use the sql for the object's activity
300 Enum.any?(to, &String.contains?(&1, "/followers")) ->
311 def render_content(%{"type" => "Video"} = object) do
312 with name when not is_nil(name) and name != "" <- object["name"] do
313 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
315 _ -> object["content"] || ""
319 def render_content(%{"type" => object_type} = object)
320 when object_type in ["Article", "Page"] do
321 with summary when not is_nil(summary) and summary != "" <- object["name"],
322 url when is_bitstring(url) <- object["url"] do
323 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
325 _ -> object["content"] || ""
329 def render_content(object), do: object["content"] || ""
332 Builds a dictionary tags.
336 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
337 [{"name": "fediverse", "url": "/tag/fediverse"},
338 {"name": "nextcloud", "url": "/tag/nextcloud"}]
341 @spec build_tags(list(any())) :: list(map())
342 def build_tags(object_tags) when is_list(object_tags) do
343 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
345 Enum.reduce(object_tags, [], fn tag, tags ->
346 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
350 def build_tags(_), do: []
355 Arguments: `nil` or list tuple of name and url.
361 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
362 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
365 @spec build_emojis(nil | list(tuple())) :: list(map())
366 def build_emojis(nil), do: []
368 def build_emojis(emojis) do
370 |> Enum.map(fn {name, url} ->
371 name = HTML.strip_tags(name)
378 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
382 defp present?(nil), do: false
383 defp present?(false), do: false
384 defp present?(_), do: true
386 defp pinned?(%Activity{id: id}, %User{info: %{pinned_activities: pinned_activities}}),
387 do: id in pinned_activities