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)
41 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
43 user = User.get_cached_by_ap_id(activity.data["actor"])
44 created_at = Utils.to_masto_date(activity.data["published"])
46 reblogged = Activity.get_create_activity_by_object_ap_id(object)
47 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
51 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
53 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
56 id: to_string(activity.id),
59 account: AccountView.render("account.json", %{user: user}),
61 in_reply_to_account_id: nil,
63 content: reblogged[:content],
64 created_at: created_at,
73 media_attachments: [],
85 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
86 user = User.get_cached_by_ap_id(activity.data["actor"])
88 like_count = object["like_count"] || 0
89 announcement_count = object["announcement_count"] || 0
91 tags = object["tag"] || []
92 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
96 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
98 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
100 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
101 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
103 attachment_data = object["attachment"] || []
104 attachment_data = attachment_data ++ if object["type"] == "Video", do: [object], else: []
105 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
107 created_at = Utils.to_masto_date(object["published"])
109 reply_to = get_reply_to(activity, opts)
110 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
113 (activity.data["object"]["emoji"] || [])
114 |> Enum.map(fn {name, url} ->
115 name = HTML.strip_tags(name)
121 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
125 id: to_string(activity.id),
127 url: object["external_url"] || object["id"],
128 account: AccountView.render("account.json", %{user: user}),
129 in_reply_to_id: reply_to && to_string(reply_to.id),
130 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
132 content: render_content(object),
133 created_at: created_at,
134 reblogs_count: announcement_count,
135 favourites_count: like_count,
136 reblogged: !!repeated,
137 favourited: !!favorited,
139 sensitive: sensitive,
140 spoiler_text: object["summary"] || "",
141 visibility: get_visibility(object),
142 media_attachments: attachments |> Enum.take(4),
155 def render("attachment.json", %{attachment: attachment}) do
156 [attachment_url | _] = attachment["url"]
157 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
158 href = attachment_url["href"]
162 String.contains?(media_type, "image") -> "image"
163 String.contains?(media_type, "video") -> "video"
164 String.contains?(media_type, "audio") -> "audio"
168 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
171 id: to_string(attachment["id"] || hash_id),
172 url: MediaProxy.url(href),
174 preview_url: MediaProxy.url(href),
177 description: attachment["name"]
181 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
182 _id = activity.data["object"]["inReplyTo"]
183 replied_to_activities[activity.data["object"]["inReplyTo"]]
186 def get_reply_to(%{data: %{"object" => object}}, _) do
187 if object["inReplyTo"] && object["inReplyTo"] != "" do
188 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
194 def get_visibility(object) do
195 public = "https://www.w3.org/ns/activitystreams#Public"
196 to = object["to"] || []
197 cc = object["cc"] || []
206 # this should use the sql for the object's activity
207 Enum.any?(to, &String.contains?(&1, "/followers")) ->
215 def render_content(%{"type" => "Video"} = object) do
216 name = object["name"]
219 if !!name and name != "" do
220 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
225 HTML.filter_tags(content)
228 def render_content(%{"type" => "Article"} = object) do
229 summary = object["name"]
232 if !!summary and summary != "" do
233 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
238 HTML.filter_tags(content)
241 def render_content(object) do
242 HTML.filter_tags(object["content"])