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