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