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 = Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
77 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
79 emojis = (activity.data["object"]["emoji"] || [])
80 |> Enum.map(fn {name, url} ->
81 name = HtmlSanitizeEx.strip_tags(name)
82 url = HtmlSanitizeEx.strip_tags(url)
83 %{ shortcode: name, url: url, static_url: url }
87 id: to_string(activity.id),
89 url: object["external_url"] || object["id"],
90 account: AccountView.render("account.json", %{user: user}),
91 in_reply_to_id: reply_to && reply_to.id,
92 in_reply_to_account_id: reply_to_user && reply_to_user.id,
94 content: HtmlSanitizeEx.basic_html(object["content"]),
95 created_at: created_at,
96 reblogs_count: announcement_count,
97 favourites_count: like_count,
98 reblogged: !!repeated,
99 favourited: !!favorited,
101 sensitive: sensitive,
102 spoiler_text: object["summary"] || "",
103 visibility: get_visibility(object),
104 media_attachments: attachments |> Enum.take(4),
116 def get_visibility(object) do
117 public = "https://www.w3.org/ns/activitystreams#Public"
118 to = object["to"] || []
119 cc = object["cc"] || []
121 public in to -> "public"
122 public in cc -> "unlisted"
123 Enum.any?(to, &(String.contains?(&1, "/followers"))) -> "private"
128 def render("attachment.json", %{attachment: attachment}) do
129 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
132 String.contains?(media_type, "image") -> "image"
133 String.contains?(media_type, "video") -> "video"
134 String.contains?(media_type, "audio") -> "audio"
138 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
141 id: to_string(attachment["id"] || hash_id),
142 url: MediaProxy.url(href),
144 preview_url: MediaProxy.url(href),