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