Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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
13 alias Pleroma.Web.CommonAPI.Utils
14 alias Pleroma.Web.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.StatusView
16 alias Pleroma.Web.MediaProxy
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 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 bookmarked: 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 pleroma: %{
107 local: activity.local
108 }
109 }
110 end
111
112 def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
113 user = get_user(activity.data["actor"])
114
115 like_count = object["like_count"] || 0
116 announcement_count = object["announcement_count"] || 0
117
118 tags = object["tag"] || []
119 sensitive = object["sensitive"] || Enum.member?(tags, "nsfw")
120
121 mentions =
122 activity.recipients
123 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
124 |> Enum.filter(& &1)
125 |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
126
127 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
128 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
129 bookmarked = opts[:for] && object["id"] in opts[:for].bookmarks
130
131 attachment_data = object["attachment"] || []
132 attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
133
134 created_at = Utils.to_masto_date(object["published"])
135
136 reply_to = get_reply_to(activity, opts)
137 reply_to_user = reply_to && get_user(reply_to.data["actor"])
138
139 content =
140 object
141 |> render_content()
142 |> HTML.get_cached_scrubbed_html_for_object(
143 User.html_filter_policy(opts[:for]),
144 activity,
145 __MODULE__
146 )
147
148 card = render("card.json", Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity))
149
150 url =
151 if user.local do
152 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity)
153 else
154 object["external_url"] || object["id"]
155 end
156
157 %{
158 id: to_string(activity.id),
159 uri: object["id"],
160 url: url,
161 account: AccountView.render("account.json", %{user: user}),
162 in_reply_to_id: reply_to && to_string(reply_to.id),
163 in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
164 reblog: nil,
165 card: card,
166 content: content,
167 created_at: created_at,
168 reblogs_count: announcement_count,
169 replies_count: 0,
170 favourites_count: like_count,
171 reblogged: present?(repeated),
172 favourited: present?(favorited),
173 bookmarked: present?(bookmarked),
174 muted: CommonAPI.thread_muted?(user, activity) || User.mutes?(opts[:for], user),
175 pinned: pinned?(activity, user),
176 sensitive: sensitive,
177 spoiler_text: object["summary"] || "",
178 visibility: get_visibility(object),
179 media_attachments: attachments,
180 mentions: mentions,
181 tags: build_tags(tags),
182 application: %{
183 name: "Web",
184 website: nil
185 },
186 language: nil,
187 emojis: build_emojis(activity.data["object"]["emoji"]),
188 pleroma: %{
189 local: activity.local
190 }
191 }
192 end
193
194 def render("status.json", _) do
195 nil
196 end
197
198 def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
199 page_url_data = URI.parse(page_url)
200
201 page_url_data =
202 if rich_media[:url] != nil do
203 URI.merge(page_url_data, URI.parse(rich_media[:url]))
204 else
205 page_url_data
206 end
207
208 page_url = page_url_data |> to_string
209
210 image_url =
211 if rich_media[:image] != nil do
212 URI.merge(page_url_data, URI.parse(rich_media[:image]))
213 |> to_string
214 else
215 nil
216 end
217
218 site_name = rich_media[:site_name] || page_url_data.host
219
220 %{
221 type: "link",
222 provider_name: site_name,
223 provider_url: page_url_data.scheme <> "://" <> page_url_data.host,
224 url: page_url,
225 image: image_url |> MediaProxy.url(),
226 title: rich_media[:title],
227 description: rich_media[:description],
228 pleroma: %{
229 opengraph: rich_media
230 }
231 }
232 end
233
234 def render("card.json", _) do
235 nil
236 end
237
238 def render("attachment.json", %{attachment: attachment}) do
239 [attachment_url | _] = attachment["url"]
240 media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image"
241 href = attachment_url["href"] |> MediaProxy.url()
242
243 type =
244 cond do
245 String.contains?(media_type, "image") -> "image"
246 String.contains?(media_type, "video") -> "video"
247 String.contains?(media_type, "audio") -> "audio"
248 true -> "unknown"
249 end
250
251 <<hash_id::signed-32, _rest::binary>> = :crypto.hash(:md5, href)
252
253 %{
254 id: to_string(attachment["id"] || hash_id),
255 url: href,
256 remote_url: href,
257 preview_url: href,
258 text_url: href,
259 type: type,
260 description: attachment["name"],
261 pleroma: %{mime_type: media_type}
262 }
263 end
264
265 def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
266 _id = activity.data["object"]["inReplyTo"]
267 replied_to_activities[activity.data["object"]["inReplyTo"]]
268 end
269
270 def get_reply_to(%{data: %{"object" => object}}, _) do
271 if object["inReplyTo"] && object["inReplyTo"] != "" do
272 Activity.get_create_by_object_ap_id(object["inReplyTo"])
273 else
274 nil
275 end
276 end
277
278 def get_visibility(object) do
279 public = "https://www.w3.org/ns/activitystreams#Public"
280 to = object["to"] || []
281 cc = object["cc"] || []
282
283 cond do
284 public in to ->
285 "public"
286
287 public in cc ->
288 "unlisted"
289
290 # this should use the sql for the object's activity
291 Enum.any?(to, &String.contains?(&1, "/followers")) ->
292 "private"
293
294 length(cc) > 0 ->
295 "private"
296
297 true ->
298 "direct"
299 end
300 end
301
302 def render_content(%{"type" => "Video"} = object) do
303 with name when not is_nil(name) and name != "" <- object["name"] do
304 "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
305 else
306 _ -> object["content"] || ""
307 end
308 end
309
310 def render_content(%{"type" => object_type} = object)
311 when object_type in ["Article", "Page"] do
312 with summary when not is_nil(summary) and summary != "" <- object["name"],
313 url when is_bitstring(url) <- object["url"] do
314 "<p><a href=\"#{url}\">#{summary}</a></p>#{object["content"]}"
315 else
316 _ -> object["content"] || ""
317 end
318 end
319
320 def render_content(object), do: object["content"] || ""
321
322 @doc """
323 Builds a dictionary tags.
324
325 ## Examples
326
327 iex> Pleroma.Web.MastodonAPI.StatusView.build_tags(["fediverse", "nextcloud"])
328 [{"name": "fediverse", "url": "/tag/fediverse"},
329 {"name": "nextcloud", "url": "/tag/nextcloud"}]
330
331 """
332 @spec build_tags(list(any())) :: list(map())
333 def build_tags(object_tags) when is_list(object_tags) do
334 object_tags = for tag when is_binary(tag) <- object_tags, do: tag
335
336 Enum.reduce(object_tags, [], fn tag, tags ->
337 tags ++ [%{name: tag, url: "/tag/#{tag}"}]
338 end)
339 end
340
341 def build_tags(_), do: []
342
343 @doc """
344 Builds list emojis.
345
346 Arguments: `nil` or list tuple of name and url.
347
348 Returns list emojis.
349
350 ## Examples
351
352 iex> Pleroma.Web.MastodonAPI.StatusView.build_emojis([{"2hu", "corndog.png"}])
353 [%{shortcode: "2hu", static_url: "corndog.png", url: "corndog.png", visible_in_picker: false}]
354
355 """
356 @spec build_emojis(nil | list(tuple())) :: list(map())
357 def build_emojis(nil), do: []
358
359 def build_emojis(emojis) do
360 emojis
361 |> Enum.map(fn {name, url} ->
362 name = HTML.strip_tags(name)
363
364 url =
365 url
366 |> HTML.strip_tags()
367 |> MediaProxy.url()
368
369 %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
370 end)
371 end
372
373 defp present?(nil), do: false
374 defp present?(false), do: false
375 defp present?(_), do: true
376
377 defp pinned?(%Activity{id: id}, %User{info: %{pinned_activities: pinned_activities}}),
378 do: id in pinned_activities
379 end