1 defmodule Pleroma.Web.MastodonAPI.StatusView do
3 alias Pleroma.Web.MastodonAPI.{AccountView, StatusView}
4 alias Pleroma.{User, Activity}
5 alias Pleroma.Web.CommonAPI.Utils
6 alias Pleroma.Web.MediaProxy
10 # TODO: Add cached version.
11 defp get_replied_to_activities(activities) do
14 %{data: %{"type" => "Create", "object" => %{"inReplyTo" => inReplyTo}}} ->
15 inReplyTo != "" && inReplyTo
21 |> Activity.create_activity_by_object_id_query()
23 |> Enum.reduce(%{}, fn activity, acc ->
24 Map.put(acc, activity.data["object"]["id"], activity)
28 def render("index.json", opts) do
29 replied_to_activities = get_replied_to_activities(opts.activities)
35 Map.put(opts, :replied_to_activities, replied_to_activities)
37 |> Enum.filter(fn x -> not is_nil(x) end)
42 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
44 user = User.get_cached_by_ap_id(activity.data["actor"])
45 created_at = Utils.to_masto_date(activity.data["published"])
47 reblogged = Activity.get_create_activity_by_object_ap_id(object)
48 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
52 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
54 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
57 id: to_string(activity.id),
60 account: AccountView.render("account.json", %{user: user}),
62 in_reply_to_account_id: nil,
64 content: reblogged[:content] || "",
65 created_at: created_at,
75 media_attachments: [],
87 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
88 user = User.get_cached_by_ap_id(activity.data["actor"])
90 like_count = object["like_count"] || 0
91 announcement_count = object["announcement_count"] || 0
93 tags = object["tag"] || []
94 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
98 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
100 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
102 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
103 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
105 attachment_data = object["attachment"] || []
106 attachment_data = attachment_data ++ if object["type"] == "Video", do: [object], else: []
107 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
109 created_at = Utils.to_masto_date(object["published"])
111 reply_to = get_reply_to(activity, opts)
112 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
115 (activity.data["object"]["emoji"] || [])
116 |> Enum.map(fn {name, url} ->
117 name = HTML.strip_tags(name)
123 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
127 render_content(object)
128 |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
131 id: to_string(activity.id),
133 url: object["external_url"] || object["id"],
134 account: AccountView.render("account.json", %{user: user}),
135 in_reply_to_id: reply_to && to_string(reply_to.id),
136 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
139 created_at: created_at,
140 reblogs_count: announcement_count,
142 favourites_count: like_count,
143 reblogged: !!repeated,
144 favourited: !!favorited,
146 sensitive: sensitive,
147 spoiler_text: object["summary"] || "",
148 visibility: get_visibility(object),
149 media_attachments: attachments |> Enum.take(4),
162 def render("status.json", _) do
166 def render("attachment.json", %{attachment: attachment}) do
167 [attachment_url | _] = attachment["url"]
168 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
169 href = attachment_url["href"] |> MediaProxy.url()
173 String.contains?(media_type, "image") -> "image"
174 String.contains?(media_type, "video") -> "video"
175 String.contains?(media_type, "audio") -> "audio"
179 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
182 id: to_string(attachment["id"] || hash_id),
188 description: attachment["name"]
192 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
193 _id = activity.data["object"]["inReplyTo"]
194 replied_to_activities[activity.data["object"]["inReplyTo"]]
197 def get_reply_to(%{data: %{"object" => object}}, _) do
198 if object["inReplyTo"] && object["inReplyTo"] != "" do
199 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
205 def get_visibility(object) do
206 public = "https://www.w3.org/ns/activitystreams#Public"
207 to = object["to"] || []
208 cc = object["cc"] || []
217 # this should use the sql for the object's activity
218 Enum.any?(to, &String.contains?(&1, "/followers")) ->
226 def render_content(%{"type" => "Video"} = object) do
227 name = object["name"]
230 if !!name and name != "" do
231 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
233 object["content"] || ""
239 def render_content(%{"type" => object_type} = object) when object_type in ["Article", "Page"] do
240 summary = object["name"]
243 if !!summary and summary != "" and is_bitstring(object["url"]) do
244 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
246 object["content"] || ""
252 def render_content(object), do: object["content"] || ""