1 defmodule Pleroma.Web.MastodonAPI.StatusView do
13 MastodonAPI.AccountView,
14 MastodonAPI.StatusView,
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_activity_by_object_id_query()
31 |> Enum.reduce(%{}, fn activity, acc ->
32 Map.put(acc, activity.data["object"]["id"], activity)
36 def render("index.json", opts) do
37 replied_to_activities = get_replied_to_activities(opts.activities)
43 Map.put(opts, :replied_to_activities, replied_to_activities)
45 |> Enum.filter(fn x -> not is_nil(x) end)
50 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
52 user = User.get_cached_by_ap_id(activity.data["actor"])
53 created_at = Utils.to_masto_date(activity.data["published"])
55 reblogged = Activity.get_create_activity_by_object_ap_id(object)
56 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
60 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
62 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
65 id: to_string(activity.id),
68 account: AccountView.render("account.json", %{user: user}),
70 in_reply_to_account_id: nil,
72 content: reblogged[:content] || "",
73 created_at: created_at,
83 media_attachments: reblogged[:media_attachments] || [],
85 tags: reblogged[:tags] || [],
95 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
96 user = User.get_cached_by_ap_id(activity.data["actor"])
98 like_count = object["like_count"] || 0
99 announcement_count = object["announcement_count"] || 0
101 tags = object["tag"] || []
102 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
106 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
108 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
110 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
111 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
113 attachment_data = object["attachment"] || []
114 attachment_data = attachment_data ++ if object["type"] == "Video", do: [object], else: []
115 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
117 created_at = Utils.to_masto_date(object["published"])
119 reply_to = get_reply_to(activity, opts)
120 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
125 |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
128 id: to_string(activity.id),
130 url: object["external_url"] || object["id"],
131 account: AccountView.render("account.json", %{user: user}),
132 in_reply_to_id: reply_to && to_string(reply_to.id),
133 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
136 created_at: created_at,
137 reblogs_count: announcement_count,
139 favourites_count: like_count,
140 reblogged: present?(repeated),
141 favourited: present?(favorited),
143 sensitive: sensitive,
144 spoiler_text: object["summary"] || "",
145 visibility: get_visibility(object),
146 media_attachments: attachments |> Enum.take(4),
154 emojis: build_emojis(activity.data["object"]["emoji"])
158 def render("status.json", _) do
162 def render("attachment.json", %{attachment: attachment}) do
163 [attachment_url | _] = attachment["url"]
164 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
165 href = attachment_url["href"] |> MediaProxy.url()
169 String.contains?(media_type, "image") -> "image"
170 String.contains?(media_type, "video") -> "video"
171 String.contains?(media_type, "audio") -> "audio"
175 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
178 id: to_string(attachment["id"] || hash_id),
184 description: attachment["name"]
188 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
189 _id = activity.data["object"]["inReplyTo"]
190 replied_to_activities[activity.data["object"]["inReplyTo"]]
193 def get_reply_to(%{data: %{"object" => object}}, _) do
194 if object["inReplyTo"] && object["inReplyTo"] != "" do
195 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
201 def get_visibility(object) do
202 public = "https://www.w3.org/ns/activitystreams#Public"
203 to = object["to"] || []
204 cc = object["cc"] || []
213 # this should use the sql for the object's activity
214 Enum.any?(to, &String.contains?(&1, "/followers")) ->
222 def render_content(%{"type" => "Video"} = object) do
223 with name when not is_nil(name) and name != "" <- object["name"] do
224 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
226 _ -> object["content"] || ""
230 def render_content(%{"type" => object_type} = object)
231 when object_type in ["Article", "Page"] do
232 with summary when not is_nil(summary) and summary != "" <- object["name"],
233 url when is_bitstring(url) <- object["url"] do
234 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
236 _ -> object["content"] || ""
240 def render_content(object), do: object["content"] || ""
245 Arguments: `nil` or list tuple of name and url.
251 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
252 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
255 @spec build_emojis(nil | list(tuple())) :: list(map())
256 def build_emojis(nil), do: []
258 def build_emojis(emojis) do
260 |> Enum.map(fn {name, url} ->
261 name = HTML.strip_tags(name)
268 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
272 defp present?(nil), do: false
273 defp present?(false), do: false
274 defp present?(_), do: true