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