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
9 # TODO: Add cached version.
10 defp get_replied_to_activities(activities) do
13 %{data: %{"type" => "Create", "object" => %{"inReplyTo" => inReplyTo}}} ->
14 inReplyTo != "" && inReplyTo
20 |> Activity.create_activity_by_object_id_query()
22 |> Enum.reduce(%{}, fn activity, acc ->
23 Map.put(acc, activity.data["object"]["id"], activity)
27 def render("index.json", opts) do
28 replied_to_activities = get_replied_to_activities(opts.activities)
34 Map.put(opts, :replied_to_activities, replied_to_activities)
40 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
42 user = User.get_cached_by_ap_id(activity.data["actor"])
43 created_at = Utils.to_masto_date(activity.data["published"])
45 reblogged = Activity.get_create_activity_by_object_ap_id(object)
46 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
50 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
52 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
55 id: to_string(activity.id),
57 # TODO: This might be wrong, check with mastodon.
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 get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
86 id = activity.data["object"]["inReplyTo"]
87 replied_to_activities[activity.data["object"]["inReplyTo"]]
90 def get_reply_to(%{data: %{"object" => object}}, _) do
91 if object["inReplyTo"] && object["inReplyTo"] != "" do
92 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
98 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
99 user = User.get_cached_by_ap_id(activity.data["actor"])
101 like_count = object["like_count"] || 0
102 announcement_count = object["announcement_count"] || 0
104 tags = object["tag"] || []
105 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
109 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
111 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
113 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
114 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
117 render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
119 created_at = Utils.to_masto_date(object["published"])
121 reply_to = get_reply_to(activity, opts)
122 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
125 (activity.data["object"]["emoji"] || [])
126 |> Enum.map(fn {name, url} ->
127 name = HtmlSanitizeEx.strip_tags(name)
128 url = HtmlSanitizeEx.strip_tags(url)
129 %{shortcode: name, url: url, static_url: url}
133 id: to_string(activity.id),
135 url: object["external_url"] || object["id"],
136 account: AccountView.render("account.json", %{user: user}),
137 in_reply_to_id: reply_to && reply_to.id,
138 in_reply_to_account_id: reply_to_user && reply_to_user.id,
140 content: HtmlSanitizeEx.basic_html(object["content"]),
141 created_at: created_at,
142 reblogs_count: announcement_count,
143 favourites_count: like_count,
144 reblogged: !!repeated,
145 favourited: !!favorited,
147 sensitive: sensitive,
148 spoiler_text: object["summary"] || "",
149 visibility: get_visibility(object),
150 media_attachments: attachments |> Enum.take(4),
163 def get_visibility(object) do
164 public = "https://www.w3.org/ns/activitystreams#Public"
165 to = object["to"] || []
166 cc = object["cc"] || []
169 public in to -> "public"
170 public in cc -> "unlisted"
171 Enum.any?(to, &String.contains?(&1, "/followers")) -> "private"
176 def render("attachment.json", %{attachment: attachment}) do
177 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
181 String.contains?(media_type, "image") -> "image"
182 String.contains?(media_type, "video") -> "video"
183 String.contains?(media_type, "audio") -> "audio"
187 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
190 id: to_string(attachment["id"] || hash_id),
191 url: MediaProxy.url(href),
193 preview_url: MediaProxy.url(href),