550ecb74f6f11e82dec90450504cda45539061eb
[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, Activity}
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 |> NaiveDateTime.from_iso8601!
31 |> NaiveDateTime.to_iso8601
32 |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false)
33
34 # TODO: Add cached version.
35 reply_to = Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
36 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
37
38 %{
39 id: activity.id,
40 uri: object["id"],
41 url: object["external_url"],
42 account: AccountView.render("account.json", %{user: user}),
43 in_reply_to_id: reply_to && reply_to.id,
44 in_reply_to_account_id: reply_to_user && reply_to_user.id,
45 reblog: nil,
46 content: HtmlSanitizeEx.basic_html(object["content"]),
47 created_at: created_at,
48 reblogs_count: announcement_count,
49 favourites_count: like_count,
50 reblogged: !!repeated,
51 favourited: !!favorited,
52 muted: false,
53 sensitive: sensitive,
54 spoiler_text: "",
55 visibility: "public",
56 media_attachments: attachments,
57 mentions: mentions,
58 tags: [], # fix,
59 application: %{
60 name: "Web",
61 website: nil
62 },
63 language: nil
64 }
65 end
66
67 def render("attachment.json", %{attachment: attachment}) do
68 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
69
70 type = cond do
71 String.contains?(media_type, "image") -> "image"
72 String.contains?(media_type, "video") -> "video"
73 true -> "unknown"
74 end
75
76 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
77
78 %{
79 id: attachment["id"] || hash_id,
80 url: href,
81 remote_url: href,
82 preview_url: href,
83 text_url: href,
84 type: type
85 }
86 end
87 end