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: [],
53 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
54 user = User.get_cached_by_ap_id(activity.data["actor"])
56 like_count = object["like_count"] || 0
57 announcement_count = object["announcement_count"] || 0
59 tags = object["tag"] || []
60 sensitive = Enum.member?(tags, "nsfw")
62 mentions = activity.data["to"]
63 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
65 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
67 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
68 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
70 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
72 created_at = Utils.to_masto_date(object["published"])
74 # TODO: Add cached version.
75 reply_to = Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
76 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
78 emojis = (activity.data["object"]["emoji"] || [])
79 |> Enum.map(fn {name, url} -> %{ shortcode: name, url: url, static_url: url } end)
82 id: to_string(activity.id),
84 url: object["external_url"] || object["id"],
85 account: AccountView.render("account.json", %{user: user}),
86 in_reply_to_id: reply_to && reply_to.id,
87 in_reply_to_account_id: reply_to_user && reply_to_user.id,
89 content: HtmlSanitizeEx.basic_html(object["content"]),
90 created_at: created_at,
91 reblogs_count: announcement_count,
92 favourites_count: like_count,
93 reblogged: !!repeated,
94 favourited: !!favorited,
97 spoiler_text: object["summary"] || "",
99 media_attachments: attachments,
111 def render("attachment.json", %{attachment: attachment}) do
112 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
115 String.contains?(media_type, "image") -> "image"
116 String.contains?(media_type, "video") -> "video"
120 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
123 id: attachment["id"] || hash_id,