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