1 defmodule Pleroma.Web.MastodonAPI.StatusView do
3 alias Pleroma.Web.MastodonAPI.{AccountView, StatusView}
4 alias Pleroma.{User, Activity, Object}
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" => object}} ->
15 object = Object.normalize(object)
16 object.data["inReplyTo"] != "" && object.data["inReplyTo"]
22 |> Activity.create_activity_by_object_id_query()
24 |> Enum.reduce(%{}, fn activity, acc ->
25 object = Object.normalize(activity.data["object"])
26 Map.put(acc, object.data["id"], activity)
30 def render("index.json", opts) do
31 replied_to_activities = get_replied_to_activities(opts.activities)
37 Map.put(opts, :replied_to_activities, replied_to_activities)
39 |> Enum.filter(fn x -> not is_nil(x) end)
44 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
46 user = User.get_cached_by_ap_id(activity.data["actor"])
47 created_at = Utils.to_masto_date(activity.data["published"])
49 reblogged = Activity.get_create_activity_by_object_ap_id(object)
50 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
54 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
56 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
59 id: to_string(activity.id),
62 account: AccountView.render("account.json", %{user: user}),
64 in_reply_to_account_id: nil,
66 content: reblogged[:content] || "",
67 created_at: created_at,
77 media_attachments: [],
89 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
90 object = Object.normalize(object)
92 user = User.get_cached_by_ap_id(activity.data["actor"])
94 like_count = object.data["like_count"] || 0
95 announcement_count = object.data["announcement_count"] || 0
97 tags = object.data["tag"] || []
98 sensitive = object.data["sensitive"] || Enum.member?(tags, "nsfw")
102 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
104 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
106 repeated = opts[:for] && opts[:for].ap_id in (object.data["announcements"] || [])
107 favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
109 attachment_data = object.data["attachment"] || []
110 attachment_data = attachment_data ++ if object.data["type"] == "Video", do: [object], else: []
111 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
113 created_at = Utils.to_masto_date(object.data["published"])
115 reply_to = get_reply_to(activity, opts)
116 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
119 (object.data["emoji"] || [])
120 |> Enum.map(fn {name, url} ->
121 name = HTML.strip_tags(name)
127 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
131 render_content(object.data)
132 |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
135 id: to_string(activity.id),
136 uri: object.data["id"],
137 url: object.data["external_url"] || object.data["id"],
138 account: AccountView.render("account.json", %{user: user}),
139 in_reply_to_id: reply_to && to_string(reply_to.id),
140 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
143 created_at: created_at,
144 reblogs_count: announcement_count,
146 favourites_count: like_count,
147 reblogged: !!repeated,
148 favourited: !!favorited,
150 sensitive: sensitive,
151 spoiler_text: object.data["summary"] || "",
152 visibility: get_visibility(object.data),
153 media_attachments: attachments |> Enum.take(4),
166 def render("status.json", _) do
170 def render("attachment.json", %{attachment: attachment}) do
171 [attachment_url | _] = attachment["url"]
172 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
173 href = attachment_url["href"] |> MediaProxy.url()
177 String.contains?(media_type, "image") -> "image"
178 String.contains?(media_type, "video") -> "video"
179 String.contains?(media_type, "audio") -> "audio"
183 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
186 id: to_string(attachment["id"] || hash_id),
192 description: attachment["name"]
196 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
197 object = Object.normalize(activity.data["object"])
198 replied_to_activities[object.data["inReplyTo"]]
201 def get_reply_to(%{data: %{"object" => object}}, _) do
202 object = Object.normalize(object)
204 if object.data["inReplyTo"] && object.data["inReplyTo"] != "" do
205 Activity.get_create_activity_by_object_ap_id(object.data["inReplyTo"])
211 def get_visibility(object) do
212 public = "https://www.w3.org/ns/activitystreams#Public"
213 to = object["to"] || []
214 cc = object["cc"] || []
223 # this should use the sql for the object's activity
224 Enum.any?(to, &String.contains?(&1, "/followers")) ->
232 def render_content(%{"type" => "Video"} = object) do
233 name = object["name"]
236 if !!name and name != "" do
237 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
239 object["content"] || ""
245 def render_content(%{"type" => object_type} = object) when object_type in ["Article", "Page"] do
246 summary = object["name"]
249 if !!summary and summary != "" and is_bitstring(object["url"]) do
250 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
252 object["content"] || ""
258 def render_content(object), do: object["content"] || ""