d06da812cd13d21c56341923b1cd46ac0082d0f5
[akkoma] / lib / pleroma / web / mastodon_api / views / status_view.ex
1 defmodule Pleroma.Web.MastodonAPI.StatusView do
2 use Pleroma.Web, :view
3
4 alias Pleroma.Activity
5 alias Pleroma.HTML
6 alias Pleroma.Repo
7 alias Pleroma.User
8 alias Pleroma.Web.CommonAPI.Utils
9 alias Pleroma.Web.MediaProxy
10 alias Pleroma.Web.MastodonAPI.AccountView
11 alias Pleroma.Web.MastodonAPI.StatusView
12
13 # TODO: Add cached version.
14 defp get_replied_to_activities(activities) do
15 activities
16 |> Enum.map(fn
17 %{data: %{"type" => "Create", "object" => %{"inReplyTo" => in_reply_to}}} ->
18 in_reply_to != "" && in_reply_to
19
20 _ ->
21 nil
22 end)
23 |> Enum.filter(& &1)
24 |> Activity.create_activity_by_object_id_query()
25 |> Repo.all()
26 |> Enum.reduce(%{}, fn activity, acc ->
27 Map.put(acc, activity.data["object"]["id"], activity)
28 end)
29 end
30
31 def render("index.json", opts) do
32 replied_to_activities = get_replied_to_activities(opts.activities)
33
34 opts.activities
35 |> render_many(
36 StatusView,
37 "status.json",
38 Map.put(opts, :replied_to_activities, replied_to_activities)
39 )
40 |> Enum.filter(fn x -> not is_nil(x) end)
41 end
42
43 def render(
44 "status.json",
45 %{activity: %{data: %{"type" => "Announce", "object" => object}} = activity} = opts
46 ) do
47 user = User.get_cached_by_ap_id(activity.data["actor"])
48 created_at = Utils.to_masto_date(activity.data["published"])
49
50 reblogged = Activity.get_create_activity_by_object_ap_id(object)
51 reblogged = render("status.json", Map.put(opts, :activity, reblogged))
52
53 mentions =
54 activity.recipients
55 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
56 |> Enum.filter(& &1)
57 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
58
59 %{
60 id: to_string(activity.id),
61 uri: object,
62 url: object,
63 account: AccountView.render("account.json", %{user: user}),
64 in_reply_to_id: nil,
65 in_reply_to_account_id: nil,
66 reblog: reblogged,
67 content: reblogged[:content] || "",
68 created_at: created_at,
69 reblogs_count: 0,
70 replies_count: 0,
71 favourites_count: 0,
72 reblogged: false,
73 favourited: false,
74 muted: false,
75 sensitive: false,
76 spoiler_text: "",
77 visibility: "public",
78 media_attachments: reblogged[:media_attachments] || [],
79 mentions: mentions,
80 tags: reblogged[:tags] || [],
81 application: %{
82 name: "Web",
83 website: nil
84 },
85 language: nil,
86 emojis: []
87 }
88 end
89
90 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
91 user = User.get_cached_by_ap_id(activity.data["actor"])
92
93 like_count = object["like_count"] || 0
94 announcement_count = object["announcement_count"] || 0
95
96 tags = object["tag"] || []
97 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
98
99 mentions =
100 activity.recipients
101 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
102 |> Enum.filter(& &1)
103 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
104
105 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
106 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
107
108 attachment_data = object["attachment"] || []
109 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
110
111 created_at = Utils.to_masto_date(object["published"])
112
113 reply_to = get_reply_to(activity, opts)
114 reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"])
115
116 content =
117 object
118 |> render_content()
119 |> HTML.filter_tags(User.html_filter_policy(opts[:for]))
120
121 %{
122 id: to_string(activity.id),
123 uri: object["id"],
124 url: object["external_url"] || object["id"],
125 account: AccountView.render("account.json", %{user: user}),
126 in_reply_to_id: reply_to && to_string(reply_to.id),
127 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
128 reblog: nil,
129 content: content,
130 created_at: created_at,
131 reblogs_count: announcement_count,
132 replies_count: 0,
133 favourites_count: like_count,
134 reblogged: present?(repeated),
135 favourited: present?(favorited),
136 muted: false,
137 sensitive: sensitive,
138 spoiler_text: object["summary"] || "",
139 visibility: get_visibility(object),
140 media_attachments: attachments |> Enum.take(4),
141 mentions: mentions,
142 tags: build_tags(tags),
143 application: %{
144 name: "Web",
145 website: nil
146 },
147 language: nil,
148 emojis: build_emojis(activity.data["object"]["emoji"])
149 }
150 end
151
152 def render("status.json", _) do
153 nil
154 end
155
156 def render("attachment.json", %{attachment: attachment}) do
157 [attachment_url | _] = attachment["url"]
158 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
159 href = attachment_url["href"] |> MediaProxy.url()
160
161 type =
162 cond do
163 String.contains?(media_type, "image") -> "image"
164 String.contains?(media_type, "video") -> "video"
165 String.contains?(media_type, "audio") -> "audio"
166 true -> "unknown"
167 end
168
169 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
170
171 %{
172 id: to_string(attachment["id"] || hash_id),
173 url: href,
174 remote_url: href,
175 preview_url: href,
176 text_url: href,
177 type: type,
178 description: attachment["name"]
179 }
180 end
181
182 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
183 _id = activity.data["object"]["inReplyTo"]
184 replied_to_activities[activity.data["object"]["inReplyTo"]]
185 end
186
187 def get_reply_to(%{data: %{"object" => object}}, _) do
188 if object["inReplyTo"] && object["inReplyTo"] != "" do
189 Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
190 else
191 nil
192 end
193 end
194
195 def get_visibility(object) do
196 public = "https://www.w3.org/ns/activitystreams#Public"
197 to = object["to"] || []
198 cc = object["cc"] || []
199
200 cond do
201 public in to ->
202 "public"
203
204 public in cc ->
205 "unlisted"
206
207 # this should use the sql for the object's activity
208 Enum.any?(to, &String.contains?(&1, "/followers")) ->
209 "private"
210
211 true ->
212 "direct"
213 end
214 end
215
216 def render_content(%{"type" => "Video"} = object) do
217 with name when not is_nil(name) and name != "" <- object["name"] do
218 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
219 else
220 _ -> object["content"] || ""
221 end
222 end
223
224 def render_content(%{"type" => object_type} = object)
225 when object_type in ["Article", "Page"] do
226 with summary when not is_nil(summary) and summary != "" <- object["name"],
227 url when is_bitstring(url) <- object["url"] do
228 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
229 else
230 _ -> object["content"] || ""
231 end
232 end
233
234 def render_content(object), do: object["content"] || ""
235
236 @doc """
237 Builds a dictionary tags.
238
239 ## Examples
240
241 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
242 [{"name": "fediverse", "url": "/tag/fediverse"},
243 {"name": "nextcloud", "url": "/tag/nextcloud"}]
244
245 """
246 @spec build_tags(list(any())) :: list(map())
247 def build_tags(object_tags) when is_list(object_tags) do
248 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
249
250 Enum.reduce(object_tags, [], fn tag, tags ->
251 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
252 end)
253 end
254
255 def build_tags(_), do: []
256
257 @doc """
258 Builds list emojis.
259
260 Arguments: `nil` or list tuple of name and url.
261
262 Returns list emojis.
263
264 ## Examples
265
266 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
267 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
268
269 """
270 @spec build_emojis(nil | list(tuple())) :: list(map())
271 def build_emojis(nil), do: []
272
273 def build_emojis(emojis) do
274 emojis
275 |> Enum.map(fn {name, url} ->
276 name = HTML.strip_tags(name)
277
278 url =
279 url
280 |> HTML.strip_tags()
281 |> MediaProxy.url()
282
283 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
284 end)
285 end
286
287 defp present?(nil), do: false
288 defp present?(false), do: false
289 defp present?(_), do: true
290 end