Return "audio" info in mastodon api.
[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 alias Pleroma.Web.MediaProxy
7
8 def render("index.json", opts) do
9 render_many(opts.activities, StatusView, "status.json", opts)
10 end
11
12 def render("status.json", %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts) do
13 user = User.get_cached_by_ap_id(activity.data["actor"])
14 created_at = Utils.to_masto_date(activity.data["published"])
15
16 reblogged = Activity.get_create_activity_by_object_ap_id(object)
17 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
18
19 mentions = activity.recipients
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 %{
25 id: to_string(activity.id),
26 uri: object,
27 url: nil, # TODO: This might be wrong, check with mastodon.
28 account: AccountView.render("account.json", %{user: user}),
29 in_reply_to_id: nil,
30 in_reply_to_account_id: nil,
31 reblog: reblogged,
32 content: reblogged[:content],
33 created_at: created_at,
34 reblogs_count: 0,
35 favourites_count: 0,
36 reblogged: false,
37 favourited: false,
38 muted: false,
39 sensitive: false,
40 spoiler_text: "",
41 visibility: "public",
42 media_attachments: [],
43 mentions: mentions,
44 tags: [],
45 application: %{
46 name: "Web",
47 website: nil
48 },
49 language: nil,
50 emojis: []
51 }
52 end
53
54 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
55 user = User.get_cached_by_ap_id(activity.data["actor"])
56
57 like_count = object["like_count"] || 0
58 announcement_count = object["announcement_count"] || 0
59
60 tags = object["tag"] || []
61 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
62
63 mentions = activity.recipients
64 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
65 |> Enum.filter(&(&1))
66 |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
67
68 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
69 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
70
71 attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
72
73 created_at = Utils.to_masto_date(object["published"])
74
75 # TODO: Add cached version.
76 reply_to = Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
77 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
78
79 emojis = (activity.data["object"]["emoji"] || [])
80 |> Enum.map(fn {name, url} ->
81 name = HtmlSanitizeEx.strip_tags(name)
82 url = HtmlSanitizeEx.strip_tags(url)
83 %{ shortcode: name, url: url, static_url: url }
84 end)
85
86 %{
87 id: to_string(activity.id),
88 uri: object["id"],
89 url: object["external_url"] || object["id"],
90 account: AccountView.render("account.json", %{user: user}),
91 in_reply_to_id: reply_to && reply_to.id,
92 in_reply_to_account_id: reply_to_user && reply_to_user.id,
93 reblog: nil,
94 content: HtmlSanitizeEx.basic_html(object["content"]),
95 created_at: created_at,
96 reblogs_count: announcement_count,
97 favourites_count: like_count,
98 reblogged: !!repeated,
99 favourited: !!favorited,
100 muted: false,
101 sensitive: sensitive,
102 spoiler_text: object["summary"] || "",
103 visibility: get_visibility(object),
104 media_attachments: attachments |> Enum.take(4),
105 mentions: mentions,
106 tags: [], # fix,
107 application: %{
108 name: "Web",
109 website: nil
110 },
111 language: nil,
112 emojis: emojis
113 }
114 end
115
116 def get_visibility(object) do
117 public = "https://www.w3.org/ns/activitystreams#Public"
118 to = object["to"] || []
119 cc = object["cc"] || []
120 cond do
121 public in to -> "public"
122 public in cc -> "unlisted"
123 Enum.any?(to, &(String.contains?(&1, "/followers"))) -> "private"
124 true -> "direct"
125 end
126 end
127
128 def render("attachment.json", %{attachment: attachment}) do
129 [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
130
131 type = cond do
132 String.contains?(media_type, "image") -> "image"
133 String.contains?(media_type, "video") -> "video"
134 String.contains?(media_type, "audio") -> "audio"
135 true -> "unknown"
136 end
137
138 << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href)
139
140 %{
141 id: to_string(attachment["id"] || hash_id),
142 url: MediaProxy.url(href),
143 remote_url: href,
144 preview_url: MediaProxy.url(href),
145 text_url: href,
146 type: type
147 }
148 end
149 end