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