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 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
111 created_at = Utils.to_masto_date(object["published"])
113 reply_to = get_reply_to(activity, opts)
114 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
119 |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
122 id: to_string(activity.id),
124 url: object["external_url"] || object["id"],
125 account: AccountView.render("account.json", %{user: user}),
126 in_reply_to_id: reply_to && to_string(reply_to.id),
127 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
130 created_at: created_at,
131 reblogs_count: announcement_count,
133 favourites_count: like_count,
134 reblogged: present?(repeated),
135 favourited: present?(favorited),
137 sensitive: sensitive,
138 spoiler_text: object["summary"] || "",
139 visibility: get_visibility(object),
140 media_attachments: attachments |> Enum.take(4),
142 tags: build_tags(tags),
148 emojis: build_emojis(activity.data["object"]["emoji"])
152 def render("status.json", _) do
156 def render("attachment.json", %{attachment: attachment}) do
157 [attachment_url | _] = attachment["url"]
158 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
159 href = attachment_url["href"] |> MediaProxy.url()
163 String.contains?(media_type, "image") -> "image"
164 String.contains?(media_type, "video") -> "video"
165 String.contains?(media_type, "audio") -> "audio"
169 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
172 id: to_string(attachment["id"] || hash_id),
178 description: attachment["name"]
182 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
183 _id = activity.data["object"]["inReplyTo"]
184 replied_to_activities[activity.data["object"]["inReplyTo"]]
187 def get_reply_to(%{data: %{"object" => object}}, _) do
188 if object["inReplyTo"] && object["inReplyTo"] != "" do
189 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
195 def get_visibility(object) do
196 public = "https://www.w3.org/ns/activitystreams#Public"
197 to = object["to"] || []
198 cc = object["cc"] || []
207 # this should use the sql for the object's activity
208 Enum.any?(to, &String.contains?(&1, "/followers")) ->
216 def render_content(%{"type" => "Video"} = object) do
217 with name when not is_nil(name) and name != "" <- object["name"] do
218 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
220 _ -> object["content"] || ""
224 def render_content(%{"type" => object_type} = object)
225 when object_type in ["Article", "Page"] do
226 with summary when not is_nil(summary) and summary != "" <- object["name"],
227 url when is_bitstring(url) <- object["url"] do
228 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
230 _ -> object["content"] || ""
234 def render_content(object), do: object["content"] || ""
237 Builds a dictionary tags.
241 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
242 [{"name": "fediverse", "url": "/tag/fediverse"},
243 {"name": "nextcloud", "url": "/tag/nextcloud"}]
246 @spec build_tags(list(any())) :: list(map())
247 def build_tags(object_tags) when is_list(object_tags) do
248 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
250 Enum.reduce(object_tags, [], fn tag, tags ->
251 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
255 def build_tags(_), do: []
260 Arguments: `nil` or list tuple of name and url.
266 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
267 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
270 @spec build_emojis(nil | list(tuple())) :: list(map())
271 def build_emojis(nil), do: []
273 def build_emojis(emojis) do
275 |> Enum.map(fn {name, url} ->
276 name = HTML.strip_tags(name)
283 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
287 defp present?(nil), do: false
288 defp present?(false), do: false
289 defp present?(_), do: true