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