cc5c0e9b67aa9c416c2f6d3caa9c6cc75fbfba83
[akkoma] / lib / pleroma / web / mastodon_api / views / status_view.ex
1 defmodule Pleroma.Web.MastodonAPI.StatusView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.MastodonAPI.{AccountView, StatusView}
4 alias Pleroma.User
5
6 def render("index.json", opts) do
7 render_many(opts.activities, StatusView, "status.json", opts)
8 end
9
10 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
11 user = User.get_cached_by_ap_id(activity.data["actor"])
12
13 like_count = object["like_count"] || 0
14 announcement_count = object["announcement_count"] || 0
15
16 tags = object["tag"] || []
17 sensitive = Enum.member?(tags, "nsfw")
18
19 mentions = activity.data["to"]
20 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
21 |> Enum.filter(&(&1))
22 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
23
24 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
25 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
26
27 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
28
29 created_at = (object["published"] || "")
30 |> String.replace(~r/\.\d+/, "")
31
32 %{
33 id: activity.id,
34 uri: object["id"],
35 url: object["external_url"],
36 account: AccountView.render("account.json", %{user: user}),
37 in_reply_to_id: object["inReplyToStatusId"],
38 in_reply_to_account_id: nil,
39 reblog: nil,
40 content: HtmlSanitizeEx.basic_html(object["content"]),
41 created_at: created_at,
42 reblogs_count: announcement_count,
43 favourites_count: like_count,
44 reblogged: !!repeated,
45 favourited: !!favorited,
46 muted: false,
47 sensitive: sensitive,
48 spoiler_text: "",
49 visibility: "public",
50 media_attachments: attachments,
51 mentions: mentions,
52 tags: [], # fix,
53 application: nil,
54 language: nil
55 }
56 end
57
58 def render("attachment.json", %{attachment: attachment}) do
59 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
60
61 type = cond do
62 String.contains?(media_type, "image") -> "image"
63 String.contains?(media_type, "video") -> "video"
64 true -> "unknown"
65 end
66
67 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
68
69 %{
70 id: attachment["id"] || hash_id,
71 url: href,
72 remote_url: href,
73 preview_url: href,
74 type: type
75 }
76 end
77 end