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,
74 media_attachments: [],
86 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
87 user = User.get_cached_by_ap_id(activity.data["actor"])
89 like_count = object["like_count"] || 0
90 announcement_count = object["announcement_count"] || 0
92 tags = object["tag"] || []
93 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
97 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
99 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
101 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
102 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
104 attachment_data = object["attachment"] || []
105 attachment_data = attachment_data ++ if object["type"] == "Video", do: [object], else: []
106 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
108 created_at = Utils.to_masto_date(object["published"])
110 reply_to = get_reply_to(activity, opts)
111 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
114 (activity.data["object"]["emoji"] || [])
115 |> Enum.map(fn {name, url} ->
116 name = HTML.strip_tags(name)
122 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
126 render_content(object)
127 |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
130 id: to_string(activity.id),
132 url: object["external_url"] || object["id"],
133 account: AccountView.render("account.json", %{user: user}),
134 in_reply_to_id: reply_to && to_string(reply_to.id),
135 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
138 created_at: created_at,
139 reblogs_count: announcement_count,
141 favourites_count: like_count,
142 reblogged: !!repeated,
143 favourited: !!favorited,
145 sensitive: sensitive,
146 spoiler_text: object["summary"] || "",
147 visibility: get_visibility(object),
148 media_attachments: attachments |> Enum.take(4),
161 def render("attachment.json", %{attachment: attachment}) do
162 [attachment_url | _] = attachment["url"]
163 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
164 href = attachment_url["href"]
168 String.contains?(media_type, "image") -> "image"
169 String.contains?(media_type, "video") -> "video"
170 String.contains?(media_type, "audio") -> "audio"
174 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
177 id: to_string(attachment["id"] || hash_id),
178 url: MediaProxy.url(href),
180 preview_url: MediaProxy.url(href),
183 description: attachment["name"]
187 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
188 _id = activity.data["object"]["inReplyTo"]
189 replied_to_activities[activity.data["object"]["inReplyTo"]]
192 def get_reply_to(%{data: %{"object" => object}}, _) do
193 if object["inReplyTo"] && object["inReplyTo"] != "" do
194 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
200 def get_visibility(object) do
201 public = "https://www.w3.org/ns/activitystreams#Public"
202 to = object["to"] || []
203 cc = object["cc"] || []
212 # this should use the sql for the object's activity
213 Enum.any?(to, &String.contains?(&1, "/followers")) ->
221 def render_content(%{"type" => "Video"} = object) do
222 name = object["name"]
225 if !!name and name != "" do
226 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
234 def render_content(%{"type" => "Article"} = object) do
235 summary = object["name"]
238 if !!summary and summary != "" do
239 "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
247 def render_content(object), do: object["content"]