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
7 def render("index.json", opts) do
8 render_many(opts.activities, StatusView, "status.json", opts)
11 def render("status.json", %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts) do
12 user = User.get_cached_by_ap_id(activity.data["actor"])
13 created_at = Utils.to_masto_date(activity.data["published"])
15 reblogged = Activity.get_create_activity_by_object_ap_id(object)
16 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
18 mentions = activity.data["to"]
19 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
21 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
24 id: to_string(activity.id),
26 url: nil, # TODO: This might be wrong, check with mastodon.
27 account: AccountView.render("account.json", %{user: user}),
29 in_reply_to_account_id: nil,
31 content: reblogged[:content],
32 created_at: created_at,
41 media_attachments: [],
52 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
53 user = User.get_cached_by_ap_id(activity.data["actor"])
55 like_count = object["like_count"] || 0
56 announcement_count = object["announcement_count"] || 0
58 tags = object["tag"] || []
59 sensitive = Enum.member?(tags, "nsfw")
61 mentions = activity.data["to"]
62 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
64 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
66 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
67 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
69 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
71 created_at = Utils.to_masto_date(object["published"])
73 # TODO: Add cached version.
74 reply_to = Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
75 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
77 emojis = (activity.data["object"]["emoji"] || [])
78 |> Enum.map(fn {name, url} -> %{ shortcode: name, url: url, static_url: url } end)
81 id: to_string(activity.id),
83 url: object["external_url"] || object["id"],
84 account: AccountView.render("account.json", %{user: user}),
85 in_reply_to_id: reply_to && reply_to.id,
86 in_reply_to_account_id: reply_to_user && reply_to_user.id,
88 content: HtmlSanitizeEx.basic_html(object["content"]),
89 created_at: created_at,
90 reblogs_count: announcement_count,
91 favourites_count: like_count,
92 reblogged: !!repeated,
93 favourited: !!favorited,
96 spoiler_text: object["summary"] || "",
98 media_attachments: attachments,
110 def render("attachment.json", %{attachment: attachment}) do
111 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
114 String.contains?(media_type, "image") -> "image"
115 String.contains?(media_type, "video") -> "video"
119 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
122 id: attachment["id"] || hash_id,