5f86f67cdf71bf3f69c10c12d697f962b895ea11
[akkoma] / lib / pleroma / web / activity_pub / transmogrifier.ex
1 defmodule Pleroma.Web.ActivityPub.Transmogrifier do
2 @moduledoc """
3 A module to handle coding from internal to wire ActivityPub and back.
4 """
5 alias Pleroma.User
6 alias Pleroma.Object
7 alias Pleroma.Activity
8 alias Pleroma.Repo
9 alias Pleroma.Web.ActivityPub.ActivityPub
10
11 import Ecto.Query
12
13 require Logger
14
15 @doc """
16 Modifies an incoming AP object (mastodon format) to our internal format.
17 """
18 def fix_object(object) do
19 object
20 |> Map.put("actor", object["attributedTo"])
21 |> fix_attachments
22 |> fix_context
23 |> fix_in_reply_to
24 |> fix_emoji
25 |> fix_tag
26 end
27
28 def fix_in_reply_to(%{"inReplyTo" => in_reply_to_id} = object)
29 when not is_nil(in_reply_to_id) do
30 case ActivityPub.fetch_object_from_id(in_reply_to_id) do
31 {:ok, replied_object} ->
32 activity = Activity.get_create_activity_by_object_ap_id(replied_object.data["id"])
33
34 object
35 |> Map.put("inReplyTo", replied_object.data["id"])
36 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
37 |> Map.put("inReplyToStatusId", activity.id)
38 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
39 |> Map.put("context", replied_object.data["context"] || object["conversation"])
40
41 e ->
42 Logger.error("Couldn't fetch #{object["inReplyTo"]} #{inspect(e)}")
43 object
44 end
45 end
46
47 def fix_in_reply_to(object), do: object
48
49 def fix_context(object) do
50 object
51 |> Map.put("context", object["conversation"])
52 end
53
54 def fix_attachments(object) do
55 attachments =
56 (object["attachment"] || [])
57 |> Enum.map(fn data ->
58 url = [%{"type" => "Link", "mediaType" => data["mediaType"], "href" => data["url"]}]
59 Map.put(data, "url", url)
60 end)
61
62 object
63 |> Map.put("attachment", attachments)
64 end
65
66 def fix_emoji(object) do
67 tags = object["tag"] || []
68 emoji = tags |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
69
70 emoji =
71 emoji
72 |> Enum.reduce(%{}, fn data, mapping ->
73 name = data["name"]
74
75 name =
76 if String.starts_with?(name, ":") do
77 name |> String.slice(1..-2)
78 else
79 name
80 end
81
82 mapping |> Map.put(name, data["icon"]["url"])
83 end)
84
85 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
86 emoji = Map.merge(object["emoji"] || %{}, emoji)
87
88 object
89 |> Map.put("emoji", emoji)
90 end
91
92 def fix_tag(object) do
93 tags =
94 (object["tag"] || [])
95 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
96 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
97
98 combined = (object["tag"] || []) ++ tags
99
100 object
101 |> Map.put("tag", combined)
102 end
103
104 # TODO: validate those with a Ecto scheme
105 # - tags
106 # - emoji
107 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
108 with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
109 %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
110 object = fix_object(data["object"])
111
112 params = %{
113 to: data["to"],
114 object: object,
115 actor: user,
116 context: object["conversation"],
117 local: false,
118 published: data["published"],
119 additional:
120 Map.take(data, [
121 "cc",
122 "id"
123 ])
124 }
125
126 ActivityPub.create(params)
127 else
128 %Activity{} = activity -> {:ok, activity}
129 _e -> :error
130 end
131 end
132
133 def handle_incoming(
134 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data
135 ) do
136 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
137 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
138 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
139 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
140
141 User.follow(follower, followed)
142 {:ok, activity}
143 else
144 _e -> :error
145 end
146 end
147
148 def handle_incoming(
149 %{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = _data
150 ) do
151 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
152 {:ok, object} <-
153 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
154 {:ok, activity, _object} <- ActivityPub.like(actor, object, id, false) do
155 {:ok, activity}
156 else
157 _e -> :error
158 end
159 end
160
161 def handle_incoming(
162 %{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = _data
163 ) do
164 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
165 {:ok, object} <-
166 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
167 {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false) do
168 {:ok, activity}
169 else
170 _e -> :error
171 end
172 end
173
174 def handle_incoming(
175 %{"type" => "Update", "object" => %{"type" => "Person"} = object, "actor" => actor_id} =
176 data
177 ) do
178 with %User{ap_id: ^actor_id} = actor <- User.get_by_ap_id(object["id"]) do
179 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
180
181 banner = new_user_data[:info]["banner"]
182
183 update_data =
184 new_user_data
185 |> Map.take([:name, :bio, :avatar])
186 |> Map.put(:info, Map.merge(actor.info, %{"banner" => banner}))
187
188 actor
189 |> User.upgrade_changeset(update_data)
190 |> User.update_and_set_cache()
191
192 ActivityPub.update(%{
193 local: false,
194 to: data["to"] || [],
195 cc: data["cc"] || [],
196 object: object,
197 actor: actor_id
198 })
199 else
200 e ->
201 Logger.error(e)
202 :error
203 end
204 end
205
206 # TODO: Make secure.
207 def handle_incoming(
208 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => _id} = _data
209 ) do
210 object_id =
211 case object_id do
212 %{"id" => id} -> id
213 id -> id
214 end
215
216 with %User{} = _actor <- User.get_or_fetch_by_ap_id(actor),
217 {:ok, object} <-
218 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
219 {:ok, activity} <- ActivityPub.delete(object, false) do
220 {:ok, activity}
221 else
222 _e -> :error
223 end
224 end
225
226 # TODO
227 # Accept
228 # Undo
229
230 def handle_incoming(_), do: :error
231
232 def get_obj_helper(id) do
233 if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
234 end
235
236 def set_reply_to_uri(%{"inReplyTo" => inReplyTo} = object) do
237 with false <- String.starts_with?(inReplyTo, "http"),
238 {:ok, %{data: replied_to_object}} <- get_obj_helper(inReplyTo) do
239 Map.put(object, "inReplyTo", replied_to_object["external_url"] || inReplyTo)
240 else
241 _e -> object
242 end
243 end
244
245 def set_reply_to_uri(obj), do: obj
246
247 # Prepares the object of an outgoing create activity.
248 def prepare_object(object) do
249 object
250 |> set_sensitive
251 |> add_hashtags
252 |> add_mention_tags
253 |> add_emoji_tags
254 |> add_attributed_to
255 |> prepare_attachments
256 |> set_conversation
257 |> set_reply_to_uri
258 end
259
260 # @doc
261 # """
262 # internal -> Mastodon
263 # """
264
265 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
266 object =
267 object
268 |> prepare_object
269
270 data =
271 data
272 |> Map.put("object", object)
273 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
274
275 {:ok, data}
276 end
277
278 def prepare_outgoing(%{"type" => _type} = data) do
279 data =
280 data
281 |> maybe_fix_object_url
282 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
283
284 {:ok, data}
285 end
286
287 def maybe_fix_object_url(data) do
288 if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
289 case ActivityPub.fetch_object_from_id(data["object"]) do
290 {:ok, relative_object} ->
291 if relative_object.data["external_url"] do
292 _data =
293 data
294 |> Map.put("object", relative_object.data["external_url"])
295 else
296 data
297 end
298
299 e ->
300 Logger.error("Couldn't fetch #{data["object"]} #{inspect(e)}")
301 data
302 end
303 else
304 data
305 end
306 end
307
308 def add_hashtags(object) do
309 tags =
310 (object["tag"] || [])
311 |> Enum.map(fn tag ->
312 %{
313 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
314 "name" => "##{tag}",
315 "type" => "Hashtag"
316 }
317 end)
318
319 object
320 |> Map.put("tag", tags)
321 end
322
323 def add_mention_tags(object) do
324 recipients = object["to"] ++ (object["cc"] || [])
325
326 mentions =
327 recipients
328 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
329 |> Enum.filter(& &1)
330 |> Enum.map(fn user ->
331 %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"}
332 end)
333
334 tags = object["tag"] || []
335
336 object
337 |> Map.put("tag", tags ++ mentions)
338 end
339
340 # TODO: we should probably send mtime instead of unix epoch time for updated
341 def add_emoji_tags(object) do
342 tags = object["tag"] || []
343 emoji = object["emoji"] || []
344
345 out =
346 emoji
347 |> Enum.map(fn {name, url} ->
348 %{
349 "icon" => %{"url" => url, "type" => "Image"},
350 "name" => ":" <> name <> ":",
351 "type" => "Emoji",
352 "updated" => "1970-01-01T00:00:00Z",
353 "id" => url
354 }
355 end)
356
357 object
358 |> Map.put("tag", tags ++ out)
359 end
360
361 def set_conversation(object) do
362 Map.put(object, "conversation", object["context"])
363 end
364
365 def set_sensitive(object) do
366 tags = object["tag"] || []
367 Map.put(object, "sensitive", "nsfw" in tags)
368 end
369
370 def add_attributed_to(object) do
371 attributedTo = object["attributedTo"] || object["actor"]
372
373 object
374 |> Map.put("attributedTo", attributedTo)
375 end
376
377 def prepare_attachments(object) do
378 attachments =
379 (object["attachment"] || [])
380 |> Enum.map(fn data ->
381 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
382 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
383 end)
384
385 object
386 |> Map.put("attachment", attachments)
387 end
388
389 defp user_upgrade_task(user) do
390 old_follower_address = User.ap_followers(user)
391
392 q =
393 from(
394 u in User,
395 where: ^old_follower_address in u.following,
396 update: [
397 set: [
398 following:
399 fragment(
400 "array_replace(?,?,?)",
401 u.following,
402 ^old_follower_address,
403 ^user.follower_address
404 )
405 ]
406 ]
407 )
408
409 Repo.update_all(q, [])
410
411 maybe_retire_websub(user.ap_id)
412
413 # Only do this for recent activties, don't go through the whole db.
414 # Only look at the last 1000 activities.
415 since = (Repo.aggregate(Activity, :max, :id) || 0) - 1_000
416
417 q =
418 from(
419 a in Activity,
420 where: ^old_follower_address in a.recipients,
421 where: a.id > ^since,
422 update: [
423 set: [
424 recipients:
425 fragment(
426 "array_replace(?,?,?)",
427 a.recipients,
428 ^old_follower_address,
429 ^user.follower_address
430 )
431 ]
432 ]
433 )
434
435 Repo.update_all(q, [])
436 end
437
438 def upgrade_user_from_ap_id(ap_id, async \\ true) do
439 with %User{local: false} = user <- User.get_by_ap_id(ap_id),
440 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
441 data =
442 data
443 |> Map.put(:info, Map.merge(user.info, data[:info]))
444
445 already_ap = User.ap_enabled?(user)
446
447 {:ok, user} =
448 User.upgrade_changeset(user, data)
449 |> Repo.update()
450
451 if !already_ap do
452 # This could potentially take a long time, do it in the background
453 if async do
454 Task.start(fn ->
455 user_upgrade_task(user)
456 end)
457 else
458 user_upgrade_task(user)
459 end
460 end
461
462 {:ok, user}
463 else
464 e -> e
465 end
466 end
467
468 def maybe_retire_websub(ap_id) do
469 # some sanity checks
470 if is_binary(ap_id) && String.length(ap_id) > 8 do
471 q =
472 from(
473 ws in Pleroma.Web.Websub.WebsubClientSubscription,
474 where: fragment("? like ?", ws.topic, ^"#{ap_id}%")
475 )
476
477 Repo.delete_all(q)
478 end
479 end
480 end