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
8 def render("index.json", opts) do
9 render_many(opts.activities, StatusView, "status.json", opts)
12 def render("status.json", %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts) do
13 user = User.get_cached_by_ap_id(activity.data["actor"])
14 created_at = Utils.to_masto_date(activity.data["published"])
16 reblogged = Activity.get_create_activity_by_object_ap_id(object)
17 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
19 mentions = activity.recipients
20 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
22 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
25 id: to_string(activity.id),
27 url: nil, # TODO: This might be wrong, check with mastodon.
28 account: AccountView.render("account.json", %{user: user}),
30 in_reply_to_account_id: nil,
32 content: reblogged[:content],
33 created_at: created_at,
42 media_attachments: [],
54 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
55 user = User.get_cached_by_ap_id(activity.data["actor"])
57 like_count = object["like_count"] || 0
58 announcement_count = object["announcement_count"] || 0
60 tags = object["tag"] || []
61 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
63 mentions = activity.recipients
64 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
66 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
68 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
69 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
71 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
73 created_at = Utils.to_masto_date(object["published"])
75 # TODO: Add cached version.
76 reply_to = if object["inReplyTo"] && object["inReplyTo"] != "" do
77 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
81 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
83 emojis = (activity.data["object"]["emoji"] || [])
84 |> Enum.map(fn {name, url} ->
85 name = HtmlSanitizeEx.strip_tags(name)
86 url = HtmlSanitizeEx.strip_tags(url)
87 %{ shortcode: name, url: url, static_url: url }
91 id: to_string(activity.id),
93 url: object["external_url"] || object["id"],
94 account: AccountView.render("account.json", %{user: user}),
95 in_reply_to_id: reply_to && reply_to.id,
96 in_reply_to_account_id: reply_to_user && reply_to_user.id,
98 content: HtmlSanitizeEx.basic_html(object["content"]),
99 created_at: created_at,
100 reblogs_count: announcement_count,
101 favourites_count: like_count,
102 reblogged: !!repeated,
103 favourited: !!favorited,
105 sensitive: sensitive,
106 spoiler_text: object["summary"] || "",
107 visibility: get_visibility(object),
108 media_attachments: attachments |> Enum.take(4),
120 def get_visibility(object) do
121 public = "https://www.w3.org/ns/activitystreams#Public"
122 to = object["to"] || []
123 cc = object["cc"] || []
125 public in to -> "public"
126 public in cc -> "unlisted"
127 Enum.any?(to, &(String.contains?(&1, "/followers"))) -> "private"
132 def render("attachment.json", %{attachment: attachment}) do
133 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
136 String.contains?(media_type, "image") -> "image"
137 String.contains?(media_type, "video") -> "video"
138 String.contains?(media_type, "audio") -> "audio"
142 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
145 id: to_string(attachment["id"] || hash_id),
146 url: MediaProxy.url(href),
148 preview_url: MediaProxy.url(href),