MastoAPI: Return id as string instead of integer.
[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: %{"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"])
14
15 reblogged = Activity.get_create_activity_by_object_ap_id(object)
16 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
17
18 mentions = activity.data["to"]
19 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
20 |> Enum.filter(&(&1))
21 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
22
23 %{
24 id: to_string(activity.id),
25 uri: object,
26 url: nil, # TODO: This might be wrong, check with mastodon.
27 account: AccountView.render("account.json", %{user: user}),
28 in_reply_to_id: nil,
29 in_reply_to_account_id: nil,
30 reblog: reblogged,
31 content: reblogged[:content],
32 created_at: created_at,
33 reblogs_count: 0,
34 favourites_count: 0,
35 reblogged: false,
36 favourited: false,
37 muted: false,
38 sensitive: false,
39 spoiler_text: "",
40 visibility: "public",
41 media_attachments: [],
42 mentions: mentions,
43 tags: [],
44 application: %{
45 name: "Web",
46 website: nil
47 },
48 language: nil
49 }
50 end
51
52 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
53 user = User.get_cached_by_ap_id(activity.data["actor"])
54
55 like_count = object["like_count"] || 0
56 announcement_count = object["announcement_count"] || 0
57
58 tags = object["tag"] || []
59 sensitive = Enum.member?(tags, "nsfw")
60
61 mentions = activity.data["to"]
62 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
63 |> Enum.filter(&(&1))
64 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
65
66 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
67 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
68
69 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
70
71 created_at = Utils.to_masto_date(object["published"])
72
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"])
76
77 emojis = (activity.data["object"]["emoji"] || [])
78 |> Enum.map(fn {name, url} -> %{ shortcode: name, url: url, static_url: url } end)
79
80 %{
81 id: to_string(activity.id),
82 uri: object["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,
87 reblog: nil,
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,
94 muted: false,
95 sensitive: sensitive,
96 spoiler_text: "",
97 visibility: "public",
98 media_attachments: attachments,
99 mentions: mentions,
100 tags: [], # fix,
101 application: %{
102 name: "Web",
103 website: nil
104 },
105 language: nil,
106 emojis: emojis
107 }
108 end
109
110 def render("attachment.json", %{attachment: attachment}) do
111 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
112
113 type = cond do
114 String.contains?(media_type, "image") -> "image"
115 String.contains?(media_type, "video") -> "video"
116 true -> "unknown"
117 end
118
119 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
120
121 %{
122 id: attachment["id"] || hash_id,
123 url: href,
124 remote_url: href,
125 preview_url: href,
126 text_url: href,
127 type: type
128 }
129 end
130 end