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