Merge remote-tracking branch 'remotes/origin/develop' into 1505-threads-federation
[akkoma] / lib / pleroma / web / activity_pub / transmogrifier.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.ActivityPub.Transmogrifier do
6 @moduledoc """
7 A module to handle coding from internal to wire ActivityPub and back.
8 """
9 alias Pleroma.Activity
10 alias Pleroma.FollowingRelationship
11 alias Pleroma.Object
12 alias Pleroma.Object.Containment
13 alias Pleroma.Repo
14 alias Pleroma.User
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Utils
17 alias Pleroma.Web.ActivityPub.Visibility
18 alias Pleroma.Web.Federator
19 alias Pleroma.Workers.TransmogrifierWorker
20
21 import Ecto.Query
22
23 require Logger
24 require Pleroma.Constants
25
26 @doc """
27 Modifies an incoming AP object (mastodon format) to our internal format.
28 """
29 def fix_object(object, options \\ []) do
30 object
31 |> strip_internal_fields
32 |> fix_actor
33 |> fix_url
34 |> fix_attachments
35 |> fix_context
36 |> fix_in_reply_to(options)
37 |> fix_emoji
38 |> fix_tag
39 |> fix_content_map
40 |> fix_addressing
41 |> fix_summary
42 |> fix_type(options)
43 end
44
45 def fix_summary(%{"summary" => nil} = object) do
46 Map.put(object, "summary", "")
47 end
48
49 def fix_summary(%{"summary" => _} = object) do
50 # summary is present, nothing to do
51 object
52 end
53
54 def fix_summary(object), do: Map.put(object, "summary", "")
55
56 def fix_addressing_list(map, field) do
57 cond do
58 is_binary(map[field]) ->
59 Map.put(map, field, [map[field]])
60
61 is_nil(map[field]) ->
62 Map.put(map, field, [])
63
64 true ->
65 map
66 end
67 end
68
69 def fix_explicit_addressing(
70 %{"to" => to, "cc" => cc} = object,
71 explicit_mentions,
72 follower_collection
73 ) do
74 explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
75
76 explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
77
78 final_cc =
79 (cc ++ explicit_cc)
80 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
81 |> Enum.uniq()
82
83 object
84 |> Map.put("to", explicit_to)
85 |> Map.put("cc", final_cc)
86 end
87
88 def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
89
90 # if directMessage flag is set to true, leave the addressing alone
91 def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
92
93 def fix_explicit_addressing(object) do
94 explicit_mentions = Utils.determine_explicit_mentions(object)
95
96 %User{follower_address: follower_collection} =
97 object
98 |> Containment.get_actor()
99 |> User.get_cached_by_ap_id()
100
101 explicit_mentions =
102 explicit_mentions ++
103 [
104 Pleroma.Constants.as_public(),
105 follower_collection
106 ]
107
108 fix_explicit_addressing(object, explicit_mentions, follower_collection)
109 end
110
111 # if as:Public is addressed, then make sure the followers collection is also addressed
112 # so that the activities will be delivered to local users.
113 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
114 recipients = to ++ cc
115
116 if followers_collection not in recipients do
117 cond do
118 Pleroma.Constants.as_public() in cc ->
119 to = to ++ [followers_collection]
120 Map.put(object, "to", to)
121
122 Pleroma.Constants.as_public() in to ->
123 cc = cc ++ [followers_collection]
124 Map.put(object, "cc", cc)
125
126 true ->
127 object
128 end
129 else
130 object
131 end
132 end
133
134 def fix_implicit_addressing(object, _), do: object
135
136 def fix_addressing(object) do
137 {:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"])
138 followers_collection = User.ap_followers(user)
139
140 object
141 |> fix_addressing_list("to")
142 |> fix_addressing_list("cc")
143 |> fix_addressing_list("bto")
144 |> fix_addressing_list("bcc")
145 |> fix_explicit_addressing()
146 |> fix_implicit_addressing(followers_collection)
147 end
148
149 def fix_actor(%{"attributedTo" => actor} = object) do
150 Map.put(object, "actor", Containment.get_actor(%{"actor" => actor}))
151 end
152
153 def fix_in_reply_to(object, options \\ [])
154
155 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
156 when not is_nil(in_reply_to) do
157 in_reply_to_id = prepare_in_reply_to(in_reply_to)
158 object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
159
160 if Federator.allowed_incoming_reply_depth?(options[:depth]) do
161 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
162 %Activity{} = _ <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
163 object
164 |> Map.put("inReplyTo", replied_object.data["id"])
165 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
166 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
167 |> Map.put("context", replied_object.data["context"] || object["conversation"])
168 else
169 e ->
170 Logger.error("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
171 object
172 end
173 else
174 object
175 end
176 end
177
178 def fix_in_reply_to(object, _options), do: object
179
180 defp prepare_in_reply_to(in_reply_to) do
181 cond do
182 is_bitstring(in_reply_to) ->
183 in_reply_to
184
185 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
186 in_reply_to["id"]
187
188 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
189 Enum.at(in_reply_to, 0)
190
191 true ->
192 ""
193 end
194 end
195
196 def fix_context(object) do
197 context = object["context"] || object["conversation"] || Utils.generate_context_id()
198
199 object
200 |> Map.put("context", context)
201 |> Map.put("conversation", context)
202 end
203
204 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
205 attachments =
206 Enum.map(attachment, fn data ->
207 media_type = data["mediaType"] || data["mimeType"]
208 href = data["url"] || data["href"]
209 url = [%{"type" => "Link", "mediaType" => media_type, "href" => href}]
210
211 data
212 |> Map.put("mediaType", media_type)
213 |> Map.put("url", url)
214 end)
215
216 Map.put(object, "attachment", attachments)
217 end
218
219 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
220 object
221 |> Map.put("attachment", [attachment])
222 |> fix_attachments()
223 end
224
225 def fix_attachments(object), do: object
226
227 def fix_url(%{"url" => url} = object) when is_map(url) do
228 Map.put(object, "url", url["href"])
229 end
230
231 def fix_url(%{"type" => "Video", "url" => url} = object) when is_list(url) do
232 first_element = Enum.at(url, 0)
233
234 link_element = Enum.find(url, fn x -> is_map(x) and x["mimeType"] == "text/html" end)
235
236 object
237 |> Map.put("attachment", [first_element])
238 |> Map.put("url", link_element["href"])
239 end
240
241 def fix_url(%{"type" => object_type, "url" => url} = object)
242 when object_type != "Video" and is_list(url) do
243 first_element = Enum.at(url, 0)
244
245 url_string =
246 cond do
247 is_bitstring(first_element) -> first_element
248 is_map(first_element) -> first_element["href"] || ""
249 true -> ""
250 end
251
252 Map.put(object, "url", url_string)
253 end
254
255 def fix_url(object), do: object
256
257 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
258 emoji =
259 tags
260 |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
261 |> Enum.reduce(%{}, fn data, mapping ->
262 name = String.trim(data["name"], ":")
263
264 Map.put(mapping, name, data["icon"]["url"])
265 end)
266
267 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
268 emoji = Map.merge(object["emoji"] || %{}, emoji)
269
270 Map.put(object, "emoji", emoji)
271 end
272
273 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
274 name = String.trim(tag["name"], ":")
275 emoji = %{name => tag["icon"]["url"]}
276
277 Map.put(object, "emoji", emoji)
278 end
279
280 def fix_emoji(object), do: object
281
282 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
283 tags =
284 tag
285 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
286 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
287
288 Map.put(object, "tag", tag ++ tags)
289 end
290
291 def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
292 combined = [tag, String.slice(hashtag, 1..-1)]
293
294 Map.put(object, "tag", combined)
295 end
296
297 def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
298
299 def fix_tag(object), do: object
300
301 # content map usually only has one language so this will do for now.
302 def fix_content_map(%{"contentMap" => content_map} = object) do
303 content_groups = Map.to_list(content_map)
304 {_, content} = Enum.at(content_groups, 0)
305
306 Map.put(object, "content", content)
307 end
308
309 def fix_content_map(object), do: object
310
311 def fix_type(object, options \\ [])
312
313 def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
314 when is_binary(reply_id) do
315 with true <- Federator.allowed_incoming_reply_depth?(options[:depth]),
316 {:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
317 Map.put(object, "type", "Answer")
318 else
319 _ -> object
320 end
321 end
322
323 def fix_type(object, _), do: object
324
325 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
326 with true <- id =~ "follows",
327 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
328 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
329 {:ok, activity}
330 else
331 _ -> {:error, nil}
332 end
333 end
334
335 defp mastodon_follow_hack(_, _), do: {:error, nil}
336
337 defp get_follow_activity(follow_object, followed) do
338 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
339 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
340 {:ok, activity}
341 else
342 # Can't find the activity. This might a Mastodon 2.3 "Accept"
343 {:activity, nil} ->
344 mastodon_follow_hack(follow_object, followed)
345
346 _ ->
347 {:error, nil}
348 end
349 end
350
351 # Reduce the object list to find the reported user.
352 defp get_reported(objects) do
353 Enum.reduce_while(objects, nil, fn ap_id, _ ->
354 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
355 {:halt, user}
356 else
357 _ -> {:cont, nil}
358 end
359 end)
360 end
361
362 def handle_incoming(data, options \\ [])
363
364 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
365 # with nil ID.
366 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
367 with context <- data["context"] || Utils.generate_context_id(),
368 content <- data["content"] || "",
369 %User{} = actor <- User.get_cached_by_ap_id(actor),
370 # Reduce the object list to find the reported user.
371 %User{} = account <- get_reported(objects),
372 # Remove the reported user from the object list.
373 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
374 %{
375 actor: actor,
376 context: context,
377 account: account,
378 statuses: statuses,
379 content: content,
380 additional: %{"cc" => [account.ap_id]}
381 }
382 |> ActivityPub.flag()
383 end
384 end
385
386 # disallow objects with bogus IDs
387 def handle_incoming(%{"id" => nil}, _options), do: :error
388 def handle_incoming(%{"id" => ""}, _options), do: :error
389 # length of https:// = 8, should validate better, but good enough for now.
390 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
391 do: :error
392
393 # TODO: validate those with a Ecto scheme
394 # - tags
395 # - emoji
396 def handle_incoming(
397 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
398 options
399 )
400 when objtype in ["Article", "Event", "Note", "Video", "Page", "Question", "Answer"] do
401 actor = Containment.get_actor(data)
402
403 data =
404 Map.put(data, "actor", actor)
405 |> fix_addressing
406
407 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
408 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
409 options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
410 object = fix_object(data["object"], options)
411
412 params = %{
413 to: data["to"],
414 object: object,
415 actor: user,
416 context: object["conversation"],
417 local: false,
418 published: data["published"],
419 additional:
420 Map.take(data, [
421 "cc",
422 "directMessage",
423 "id"
424 ])
425 }
426
427 with {:ok, created_activity} <- ActivityPub.create(params) do
428 for reply_id <- replies(object) do
429 Pleroma.Workers.RemoteFetcherWorker.enqueue("fetch_remote", %{"id" => reply_id})
430 end
431
432 {:ok, created_activity}
433 end
434 else
435 %Activity{} = activity -> {:ok, activity}
436 _e -> :error
437 end
438 end
439
440 def handle_incoming(
441 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
442 options
443 ) do
444 actor = Containment.get_actor(data)
445
446 data =
447 Map.put(data, "actor", actor)
448 |> fix_addressing
449
450 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
451 options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
452 object = fix_object(object, options)
453
454 params = %{
455 to: data["to"],
456 object: object,
457 actor: user,
458 context: nil,
459 local: false,
460 published: data["published"],
461 additional: Map.take(data, ["cc", "id"])
462 }
463
464 ActivityPub.listen(params)
465 else
466 _e -> :error
467 end
468 end
469
470 def handle_incoming(
471 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
472 _options
473 ) do
474 with %User{local: true} = followed <-
475 User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
476 {:ok, %User{} = follower} <-
477 User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
478 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
479 with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
480 {_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
481 {_, false} <- {:user_locked, User.locked?(followed)},
482 {_, {:ok, follower}} <- {:follow, User.follow(follower, followed)},
483 {_, {:ok, _}} <-
484 {:follow_state_update, Utils.update_follow_state_for_all(activity, "accept")},
485 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, "accept") do
486 ActivityPub.accept(%{
487 to: [follower.ap_id],
488 actor: followed,
489 object: data,
490 local: true
491 })
492 else
493 {:user_blocked, true} ->
494 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
495 {:ok, _relationship} = FollowingRelationship.update(follower, followed, "reject")
496
497 ActivityPub.reject(%{
498 to: [follower.ap_id],
499 actor: followed,
500 object: data,
501 local: true
502 })
503
504 {:follow, {:error, _}} ->
505 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
506 {:ok, _relationship} = FollowingRelationship.update(follower, followed, "reject")
507
508 ActivityPub.reject(%{
509 to: [follower.ap_id],
510 actor: followed,
511 object: data,
512 local: true
513 })
514
515 {:user_locked, true} ->
516 {:ok, _relationship} = FollowingRelationship.update(follower, followed, "pending")
517 :noop
518 end
519
520 {:ok, activity}
521 else
522 _e ->
523 :error
524 end
525 end
526
527 def handle_incoming(
528 %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,
529 _options
530 ) do
531 with actor <- Containment.get_actor(data),
532 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
533 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
534 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
535 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
536 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, "accept") do
537 ActivityPub.accept(%{
538 to: follow_activity.data["to"],
539 type: "Accept",
540 actor: followed,
541 object: follow_activity.data["id"],
542 local: false,
543 activity_id: id
544 })
545 else
546 _e -> :error
547 end
548 end
549
550 def handle_incoming(
551 %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
552 _options
553 ) do
554 with actor <- Containment.get_actor(data),
555 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
556 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
557 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
558 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
559 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, "reject"),
560 {:ok, activity} <-
561 ActivityPub.reject(%{
562 to: follow_activity.data["to"],
563 type: "Reject",
564 actor: followed,
565 object: follow_activity.data["id"],
566 local: false,
567 activity_id: id
568 }) do
569 {:ok, activity}
570 else
571 _e -> :error
572 end
573 end
574
575 @misskey_reactions %{
576 "like" => "👍",
577 "love" => "❤️",
578 "laugh" => "😆",
579 "hmm" => "🤔",
580 "surprise" => "😮",
581 "congrats" => "🎉",
582 "angry" => "💢",
583 "confused" => "😥",
584 "rip" => "😇",
585 "pudding" => "🍮",
586 "star" => "⭐"
587 }
588
589 @doc "Rewrite misskey likes into EmojiReacts"
590 def handle_incoming(
591 %{
592 "type" => "Like",
593 "_misskey_reaction" => reaction
594 } = data,
595 options
596 ) do
597 data
598 |> Map.put("type", "EmojiReact")
599 |> Map.put("content", @misskey_reactions[reaction] || reaction)
600 |> handle_incoming(options)
601 end
602
603 def handle_incoming(
604 %{"type" => "Like", "object" => object_id, "actor" => _actor, "id" => id} = data,
605 _options
606 ) do
607 with actor <- Containment.get_actor(data),
608 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
609 {:ok, object} <- get_obj_helper(object_id),
610 {:ok, activity, _object} <- ActivityPub.like(actor, object, id, false) do
611 {:ok, activity}
612 else
613 _e -> :error
614 end
615 end
616
617 def handle_incoming(
618 %{
619 "type" => "EmojiReact",
620 "object" => object_id,
621 "actor" => _actor,
622 "id" => id,
623 "content" => emoji
624 } = data,
625 _options
626 ) do
627 with actor <- Containment.get_actor(data),
628 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
629 {:ok, object} <- get_obj_helper(object_id),
630 {:ok, activity, _object} <-
631 ActivityPub.react_with_emoji(actor, object, emoji, activity_id: id, local: false) do
632 {:ok, activity}
633 else
634 _e -> :error
635 end
636 end
637
638 def handle_incoming(
639 %{"type" => "Announce", "object" => object_id, "actor" => _actor, "id" => id} = data,
640 _options
641 ) do
642 with actor <- Containment.get_actor(data),
643 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
644 {:ok, object} <- get_embedded_obj_helper(object_id, actor),
645 public <- Visibility.is_public?(data),
646 {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false, public) do
647 {:ok, activity}
648 else
649 _e -> :error
650 end
651 end
652
653 def handle_incoming(
654 %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
655 data,
656 _options
657 )
658 when object_type in [
659 "Person",
660 "Application",
661 "Service",
662 "Organization"
663 ] do
664 with %User{ap_id: ^actor_id} = actor <- User.get_cached_by_ap_id(object["id"]) do
665 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
666
667 actor
668 |> User.upgrade_changeset(new_user_data, true)
669 |> User.update_and_set_cache()
670
671 ActivityPub.update(%{
672 local: false,
673 to: data["to"] || [],
674 cc: data["cc"] || [],
675 object: object,
676 actor: actor_id,
677 activity_id: data["id"]
678 })
679 else
680 e ->
681 Logger.error(e)
682 :error
683 end
684 end
685
686 # TODO: We presently assume that any actor on the same origin domain as the object being
687 # deleted has the rights to delete that object. A better way to validate whether or not
688 # the object should be deleted is to refetch the object URI, which should return either
689 # an error or a tombstone. This would allow us to verify that a deletion actually took
690 # place.
691 def handle_incoming(
692 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => id} = data,
693 _options
694 ) do
695 object_id = Utils.get_ap_id(object_id)
696
697 with actor <- Containment.get_actor(data),
698 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
699 {:ok, object} <- get_obj_helper(object_id),
700 :ok <- Containment.contain_origin(actor.ap_id, object.data),
701 {:ok, activity} <-
702 ActivityPub.delete(object, local: false, activity_id: id, actor: actor.ap_id) do
703 {:ok, activity}
704 else
705 nil ->
706 case User.get_cached_by_ap_id(object_id) do
707 %User{ap_id: ^actor} = user ->
708 User.delete(user)
709
710 nil ->
711 :error
712 end
713
714 _e ->
715 :error
716 end
717 end
718
719 def handle_incoming(
720 %{
721 "type" => "Undo",
722 "object" => %{"type" => "Announce", "object" => object_id},
723 "actor" => _actor,
724 "id" => id
725 } = data,
726 _options
727 ) do
728 with actor <- Containment.get_actor(data),
729 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
730 {:ok, object} <- get_obj_helper(object_id),
731 {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
732 {:ok, activity}
733 else
734 _e -> :error
735 end
736 end
737
738 def handle_incoming(
739 %{
740 "type" => "Undo",
741 "object" => %{"type" => "Follow", "object" => followed},
742 "actor" => follower,
743 "id" => id
744 } = _data,
745 _options
746 ) do
747 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
748 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
749 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
750 User.unfollow(follower, followed)
751 {:ok, activity}
752 else
753 _e -> :error
754 end
755 end
756
757 def handle_incoming(
758 %{
759 "type" => "Undo",
760 "object" => %{"type" => "EmojiReact", "id" => reaction_activity_id},
761 "actor" => _actor,
762 "id" => id
763 } = data,
764 _options
765 ) do
766 with actor <- Containment.get_actor(data),
767 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
768 {:ok, activity, _} <-
769 ActivityPub.unreact_with_emoji(actor, reaction_activity_id,
770 activity_id: id,
771 local: false
772 ) do
773 {:ok, activity}
774 else
775 _e -> :error
776 end
777 end
778
779 def handle_incoming(
780 %{
781 "type" => "Undo",
782 "object" => %{"type" => "Block", "object" => blocked},
783 "actor" => blocker,
784 "id" => id
785 } = _data,
786 _options
787 ) do
788 with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
789 {:ok, %User{} = blocker} <- User.get_or_fetch_by_ap_id(blocker),
790 {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
791 User.unblock(blocker, blocked)
792 {:ok, activity}
793 else
794 _e -> :error
795 end
796 end
797
798 def handle_incoming(
799 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
800 _options
801 ) do
802 with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
803 {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
804 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
805 User.unfollow(blocker, blocked)
806 User.block(blocker, blocked)
807 {:ok, activity}
808 else
809 _e -> :error
810 end
811 end
812
813 def handle_incoming(
814 %{
815 "type" => "Undo",
816 "object" => %{"type" => "Like", "object" => object_id},
817 "actor" => _actor,
818 "id" => id
819 } = data,
820 _options
821 ) do
822 with actor <- Containment.get_actor(data),
823 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
824 {:ok, object} <- get_obj_helper(object_id),
825 {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
826 {:ok, activity}
827 else
828 _e -> :error
829 end
830 end
831
832 # For Undos that don't have the complete object attached, try to find it in our database.
833 def handle_incoming(
834 %{
835 "type" => "Undo",
836 "object" => object
837 } = activity,
838 options
839 )
840 when is_binary(object) do
841 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
842 activity
843 |> Map.put("object", data)
844 |> handle_incoming(options)
845 else
846 _e -> :error
847 end
848 end
849
850 def handle_incoming(
851 %{
852 "type" => "Move",
853 "actor" => origin_actor,
854 "object" => origin_actor,
855 "target" => target_actor
856 },
857 _options
858 ) do
859 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
860 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
861 true <- origin_actor in target_user.also_known_as do
862 ActivityPub.move(origin_user, target_user, false)
863 else
864 _e -> :error
865 end
866 end
867
868 def handle_incoming(_, _), do: :error
869
870 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
871 def get_obj_helper(id, options \\ []) do
872 case Object.normalize(id, true, options) do
873 %Object{} = object -> {:ok, object}
874 _ -> nil
875 end
876 end
877
878 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
879 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
880 ap_id: ap_id
881 })
882 when attributed_to == ap_id do
883 with {:ok, activity} <-
884 handle_incoming(%{
885 "type" => "Create",
886 "to" => data["to"],
887 "cc" => data["cc"],
888 "actor" => attributed_to,
889 "object" => data
890 }) do
891 {:ok, Object.normalize(activity)}
892 else
893 _ -> get_obj_helper(object_id)
894 end
895 end
896
897 def get_embedded_obj_helper(object_id, _) do
898 get_obj_helper(object_id)
899 end
900
901 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
902 with false <- String.starts_with?(in_reply_to, "http"),
903 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
904 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
905 else
906 _e -> object
907 end
908 end
909
910 def set_reply_to_uri(obj), do: obj
911
912 @doc """
913 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
914 Based on Mastodon's ActivityPub::NoteSerializer#replies.
915 """
916 def set_replies(obj) do
917 limit = Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0)
918
919 replies_uris =
920 with true <- limit > 0 || nil,
921 %Activity{} = activity <- Activity.get_create_by_object_ap_id(obj["id"]) do
922 activity
923 |> Activity.self_replies()
924 |> select([a], fragment("?->>'id'", a.data))
925 |> limit(^limit)
926 |> Repo.all()
927 end
928
929 set_replies(obj, replies_uris || [])
930 end
931
932 defp set_replies(obj, replies_uris) when replies_uris in [nil, []] do
933 obj
934 end
935
936 defp set_replies(obj, replies_uris) do
937 # Note: stubs (Mastodon doesn't make separate requests via those URIs in FetchRepliesService)
938 masto_replies_uri = nil
939 masto_replies_next_page_uri = nil
940
941 replies_collection = %{
942 "type" => "Collection",
943 "id" => masto_replies_uri,
944 "first" => %{
945 "type" => "Collection",
946 "part_of" => masto_replies_uri,
947 "items" => replies_uris,
948 "next" => masto_replies_next_page_uri
949 }
950 }
951
952 Map.merge(obj, %{"replies" => replies_collection})
953 end
954
955 def replies(%{"replies" => replies = %{}}) do
956 replies =
957 if is_map(replies["first"]) do
958 replies["first"]
959 else
960 replies
961 end
962
963 replies["items"] || []
964 end
965
966 def replies(_), do: []
967
968 # Prepares the object of an outgoing create activity.
969 def prepare_object(object) do
970 object
971 |> set_sensitive
972 |> add_hashtags
973 |> add_mention_tags
974 |> add_emoji_tags
975 |> add_attributed_to
976 |> prepare_attachments
977 |> set_conversation
978 |> set_reply_to_uri
979 |> set_replies
980 |> strip_internal_fields
981 |> strip_internal_tags
982 |> set_type
983 end
984
985 # @doc
986 # """
987 # internal -> Mastodon
988 # """
989
990 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
991 when activity_type in ["Create", "Listen"] do
992 object =
993 object_id
994 |> Object.normalize()
995 |> Map.get(:data)
996 |> prepare_object
997
998 data =
999 data
1000 |> Map.put("object", object)
1001 |> Map.merge(Utils.make_json_ld_header())
1002 |> Map.delete("bcc")
1003
1004 {:ok, data}
1005 end
1006
1007 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
1008 object =
1009 object_id
1010 |> Object.normalize()
1011
1012 data =
1013 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
1014 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
1015 else
1016 data |> maybe_fix_object_url
1017 end
1018
1019 data =
1020 data
1021 |> strip_internal_fields
1022 |> Map.merge(Utils.make_json_ld_header())
1023 |> Map.delete("bcc")
1024
1025 {:ok, data}
1026 end
1027
1028 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
1029 # because of course it does.
1030 def prepare_outgoing(%{"type" => "Accept"} = data) do
1031 with follow_activity <- Activity.normalize(data["object"]) do
1032 object = %{
1033 "actor" => follow_activity.actor,
1034 "object" => follow_activity.data["object"],
1035 "id" => follow_activity.data["id"],
1036 "type" => "Follow"
1037 }
1038
1039 data =
1040 data
1041 |> Map.put("object", object)
1042 |> Map.merge(Utils.make_json_ld_header())
1043
1044 {:ok, data}
1045 end
1046 end
1047
1048 def prepare_outgoing(%{"type" => "Reject"} = data) do
1049 with follow_activity <- Activity.normalize(data["object"]) do
1050 object = %{
1051 "actor" => follow_activity.actor,
1052 "object" => follow_activity.data["object"],
1053 "id" => follow_activity.data["id"],
1054 "type" => "Follow"
1055 }
1056
1057 data =
1058 data
1059 |> Map.put("object", object)
1060 |> Map.merge(Utils.make_json_ld_header())
1061
1062 {:ok, data}
1063 end
1064 end
1065
1066 def prepare_outgoing(%{"type" => _type} = data) do
1067 data =
1068 data
1069 |> strip_internal_fields
1070 |> maybe_fix_object_url
1071 |> Map.merge(Utils.make_json_ld_header())
1072
1073 {:ok, data}
1074 end
1075
1076 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
1077 with false <- String.starts_with?(object, "http"),
1078 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
1079 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
1080 relative_object do
1081 Map.put(data, "object", external_url)
1082 else
1083 {:fetch, e} ->
1084 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
1085 data
1086
1087 _ ->
1088 data
1089 end
1090 end
1091
1092 def maybe_fix_object_url(data), do: data
1093
1094 def add_hashtags(object) do
1095 tags =
1096 (object["tag"] || [])
1097 |> Enum.map(fn
1098 # Expand internal representation tags into AS2 tags.
1099 tag when is_binary(tag) ->
1100 %{
1101 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
1102 "name" => "##{tag}",
1103 "type" => "Hashtag"
1104 }
1105
1106 # Do not process tags which are already AS2 tag objects.
1107 tag when is_map(tag) ->
1108 tag
1109 end)
1110
1111 Map.put(object, "tag", tags)
1112 end
1113
1114 def add_mention_tags(object) do
1115 mentions =
1116 object
1117 |> Utils.get_notified_from_object()
1118 |> Enum.map(&build_mention_tag/1)
1119
1120 tags = object["tag"] || []
1121
1122 Map.put(object, "tag", tags ++ mentions)
1123 end
1124
1125 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
1126 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
1127 end
1128
1129 def take_emoji_tags(%User{emoji: emoji}) do
1130 emoji
1131 |> Enum.flat_map(&Map.to_list/1)
1132 |> Enum.map(&build_emoji_tag/1)
1133 end
1134
1135 # TODO: we should probably send mtime instead of unix epoch time for updated
1136 def add_emoji_tags(%{"emoji" => emoji} = object) do
1137 tags = object["tag"] || []
1138
1139 out = Enum.map(emoji, &build_emoji_tag/1)
1140
1141 Map.put(object, "tag", tags ++ out)
1142 end
1143
1144 def add_emoji_tags(object), do: object
1145
1146 defp build_emoji_tag({name, url}) do
1147 %{
1148 "icon" => %{"url" => url, "type" => "Image"},
1149 "name" => ":" <> name <> ":",
1150 "type" => "Emoji",
1151 "updated" => "1970-01-01T00:00:00Z",
1152 "id" => url
1153 }
1154 end
1155
1156 def set_conversation(object) do
1157 Map.put(object, "conversation", object["context"])
1158 end
1159
1160 def set_sensitive(object) do
1161 tags = object["tag"] || []
1162 Map.put(object, "sensitive", "nsfw" in tags)
1163 end
1164
1165 def set_type(%{"type" => "Answer"} = object) do
1166 Map.put(object, "type", "Note")
1167 end
1168
1169 def set_type(object), do: object
1170
1171 def add_attributed_to(object) do
1172 attributed_to = object["attributedTo"] || object["actor"]
1173 Map.put(object, "attributedTo", attributed_to)
1174 end
1175
1176 def prepare_attachments(object) do
1177 attachments =
1178 (object["attachment"] || [])
1179 |> Enum.map(fn data ->
1180 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
1181 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
1182 end)
1183
1184 Map.put(object, "attachment", attachments)
1185 end
1186
1187 def strip_internal_fields(object) do
1188 object
1189 |> Map.drop(Pleroma.Constants.object_internal_fields())
1190 end
1191
1192 defp strip_internal_tags(%{"tag" => tags} = object) do
1193 tags = Enum.filter(tags, fn x -> is_map(x) end)
1194
1195 Map.put(object, "tag", tags)
1196 end
1197
1198 defp strip_internal_tags(object), do: object
1199
1200 def perform(:user_upgrade, user) do
1201 # we pass a fake user so that the followers collection is stripped away
1202 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1203
1204 from(
1205 a in Activity,
1206 where: ^old_follower_address in a.recipients,
1207 update: [
1208 set: [
1209 recipients:
1210 fragment(
1211 "array_replace(?,?,?)",
1212 a.recipients,
1213 ^old_follower_address,
1214 ^user.follower_address
1215 )
1216 ]
1217 ]
1218 )
1219 |> Repo.update_all([])
1220 end
1221
1222 def upgrade_user_from_ap_id(ap_id) do
1223 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1224 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1225 already_ap <- User.ap_enabled?(user),
1226 {:ok, user} <- upgrade_user(user, data) do
1227 if not already_ap do
1228 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1229 end
1230
1231 {:ok, user}
1232 else
1233 %User{} = user -> {:ok, user}
1234 e -> e
1235 end
1236 end
1237
1238 defp upgrade_user(user, data) do
1239 user
1240 |> User.upgrade_changeset(data, true)
1241 |> User.update_and_set_cache()
1242 end
1243
1244 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1245 Map.put(data, "url", url["href"])
1246 end
1247
1248 def maybe_fix_user_url(data), do: data
1249
1250 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)
1251 end