MastoAPI: Fix date in account view.
[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 alias Pleroma.Web.CommonAPI.Utils
6
7 def render("index.json", opts) do
8 render_many(opts.activities, StatusView, "status.json", opts)
9 end
10
11 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
12 user = User.get_cached_by_ap_id(activity.data["actor"])
13
14 like_count = object["like_count"] || 0
15 announcement_count = object["announcement_count"] || 0
16
17 tags = object["tag"] || []
18 sensitive = Enum.member?(tags, "nsfw")
19
20 mentions = activity.data["to"]
21 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
22 |> Enum.filter(&(&1))
23 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
24
25 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
26 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
27
28 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
29
30 created_at = Utils.to_masto_date(object["published"])
31
32 # TODO: Add cached version.
33 reply_to = Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
34 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
35
36 %{
37 id: activity.id,
38 uri: object["id"],
39 url: object["external_url"],
40 account: AccountView.render("account.json", %{user: user}),
41 in_reply_to_id: reply_to && reply_to.id,
42 in_reply_to_account_id: reply_to_user && reply_to_user.id,
43 reblog: nil,
44 content: HtmlSanitizeEx.basic_html(object["content"]),
45 created_at: created_at,
46 reblogs_count: announcement_count,
47 favourites_count: like_count,
48 reblogged: !!repeated,
49 favourited: !!favorited,
50 muted: false,
51 sensitive: sensitive,
52 spoiler_text: "",
53 visibility: "public",
54 media_attachments: attachments,
55 mentions: mentions,
56 tags: [], # fix,
57 application: %{
58 name: "Web",
59 website: nil
60 },
61 language: nil
62 }
63 end
64
65 def render("attachment.json", %{attachment: attachment}) do
66 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
67
68 type = cond do
69 String.contains?(media_type, "image") -> "image"
70 String.contains?(media_type, "video") -> "video"
71 true -> "unknown"
72 end
73
74 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
75
76 %{
77 id: attachment["id"] || hash_id,
78 url: href,
79 remote_url: href,
80 preview_url: href,
81 text_url: href,
82 type: type
83 }
84 end
85 end