1 defmodule Pleroma.Web.MastodonAPI.StatusView do
8 alias Pleroma.Web.CommonAPI.Utils
9 alias Pleroma.Web.MediaProxy
10 alias Pleroma.Web.MastodonAPI.AccountView
11 alias Pleroma.Web.MastodonAPI.StatusView
13 # TODO: Add cached version.
14 defp get_replied_to_activities(activities) do
17 %{data: %{"type" => "Create", "object" => %{"inReplyTo" => in_reply_to}}} ->
18 in_reply_to != "" && in_reply_to
24 |> Activity.create_activity_by_object_id_query()
26 |> Enum.reduce(%{}, fn activity, acc ->
27 Map.put(acc, activity.data["object"]["id"], activity)
31 def render("index.json", opts) do
32 replied_to_activities = get_replied_to_activities(opts.activities)
38 Map.put(opts, :replied_to_activities, replied_to_activities)
40 |> Enum.filter(fn x -> not is_nil(x) end)
45 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
47 user = User.get_cached_by_ap_id(activity.data["actor"])
48 created_at = Utils.to_masto_date(activity.data["published"])
50 reblogged = Activity.get_create_activity_by_object_ap_id(object)
51 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
55 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
57 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
60 id: to_string(activity.id),
63 account: AccountView.render("account.json", %{user: user}),
65 in_reply_to_account_id: nil,
67 content: reblogged[:content] || "",
68 created_at: created_at,
78 media_attachments: reblogged[:media_attachments] || [],
80 tags: reblogged[:tags] || [],
90 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
91 user = User.get_cached_by_ap_id(activity.data["actor"])
93 like_count = object["like_count"] || 0
94 announcement_count = object["announcement_count"] || 0
96 tags = object["tag"] || []
97 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
101 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
103 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
105 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
106 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
108 attachment_data = object["attachment"] || []
109 attachment_data = attachment_data ++ if object["type"] == "Video", do: [object], else: []
110 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
112 created_at = Utils.to_masto_date(object["published"])
114 reply_to = get_reply_to(activity, opts)
115 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
120 |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
123 id: to_string(activity.id),
125 url: object["external_url"] || object["id"],
126 account: AccountView.render("account.json", %{user: user}),
127 in_reply_to_id: reply_to && to_string(reply_to.id),
128 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
131 created_at: created_at,
132 reblogs_count: announcement_count,
134 favourites_count: like_count,
135 reblogged: present?(repeated),
136 favourited: present?(favorited),
138 sensitive: sensitive,
139 spoiler_text: object["summary"] || "",
140 visibility: get_visibility(object),
141 media_attachments: attachments |> Enum.take(4),
143 tags: build_tags(tags),
149 emojis: build_emojis(activity.data["object"]["emoji"])
153 def render("status.json", _) do
157 def render("attachment.json", %{attachment: attachment}) do
158 [attachment_url | _] = attachment["url"]
159 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
160 href = attachment_url["href"] |> MediaProxy.url()
164 String.contains?(media_type, "image") -> "image"
165 String.contains?(media_type, "video") -> "video"
166 String.contains?(media_type, "audio") -> "audio"
170 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
173 id: to_string(attachment["id"] || hash_id),
179 description: attachment["name"]
183 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
184 _id = activity.data["object"]["inReplyTo"]
185 replied_to_activities[activity.data["object"]["inReplyTo"]]
188 def get_reply_to(%{data: %{"object" => object}}, _) do
189 if object["inReplyTo"] && object["inReplyTo"] != "" do
190 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
196 def get_visibility(object) do
197 public = "https://www.w3.org/ns/activitystreams#Public"
198 to = object["to"] || []
199 cc = object["cc"] || []
208 # this should use the sql for the object's activity
209 Enum.any?(to, &String.contains?(&1, "/followers")) ->
217 def render_content(%{"type" => "Video"} = object) do
218 with name when not is_nil(name) and name != "" <- object["name"] do
219 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
221 _ -> object["content"] || ""
225 def render_content(%{"type" => object_type} = object)
226 when object_type in ["Article", "Page"] do
227 with summary when not is_nil(summary) and summary != "" <- object["name"],
228 url when is_bitstring(url) <- object["url"] do
229 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
231 _ -> object["content"] || ""
235 def render_content(object), do: object["content"] || ""
238 Builds a dictionary tags.
242 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
243 [{"name": "fediverse", "url": "/tag/fediverse"},
244 {"name": "nextcloud", "url": "/tag/nextcloud"}]
247 @spec build_tags(list(any())) :: list(map())
248 def build_tags(object_tags) when is_list(object_tags) do
249 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
251 Enum.reduce(object_tags, [], fn tag, tags ->
252 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
256 def build_tags(_), do: []
261 Arguments: `nil` or list tuple of name and url.
267 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
268 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
271 @spec build_emojis(nil | list(tuple())) :: list(map())
272 def build_emojis(nil), do: []
274 def build_emojis(emojis) do
276 |> Enum.map(fn {name, url} ->
277 name = HTML.strip_tags(name)
284 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
288 defp present?(nil), do: false
289 defp present?(false), do: false
290 defp present?(_), do: true