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