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