Privacy option affects all push notifications, not just Direct Messages
[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.EarmarkRenderer
11 alias Pleroma.FollowingRelationship
12 alias Pleroma.Object
13 alias Pleroma.Object.Containment
14 alias Pleroma.Repo
15 alias Pleroma.User
16 alias Pleroma.Web.ActivityPub.ActivityPub
17 alias Pleroma.Web.ActivityPub.ObjectValidator
18 alias Pleroma.Web.ActivityPub.Pipeline
19 alias Pleroma.Web.ActivityPub.Utils
20 alias Pleroma.Web.ActivityPub.Visibility
21 alias Pleroma.Web.Federator
22 alias Pleroma.Workers.TransmogrifierWorker
23
24 import Ecto.Query
25
26 require Logger
27 require Pleroma.Constants
28
29 @doc """
30 Modifies an incoming AP object (mastodon format) to our internal format.
31 """
32 def fix_object(object, options \\ []) do
33 object
34 |> strip_internal_fields
35 |> fix_actor
36 |> fix_url
37 |> fix_attachments
38 |> fix_context
39 |> fix_in_reply_to(options)
40 |> fix_emoji
41 |> fix_tag
42 |> fix_content_map
43 |> fix_addressing
44 |> fix_summary
45 |> fix_type(options)
46 |> fix_content
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 fix_content(%{"mediaType" => "text/markdown", "content" => content} = object)
362 when is_binary(content) do
363 html_content =
364 content
365 |> Earmark.as_html!(%Earmark.Options{renderer: EarmarkRenderer})
366 |> Pleroma.HTML.filter_tags()
367
368 Map.merge(object, %{"content" => html_content, "mediaType" => "text/html"})
369 end
370
371 defp fix_content(object), do: object
372
373 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
374 with true <- id =~ "follows",
375 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
376 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
377 {:ok, activity}
378 else
379 _ -> {:error, nil}
380 end
381 end
382
383 defp mastodon_follow_hack(_, _), do: {:error, nil}
384
385 defp get_follow_activity(follow_object, followed) do
386 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
387 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
388 {:ok, activity}
389 else
390 # Can't find the activity. This might a Mastodon 2.3 "Accept"
391 {:activity, nil} ->
392 mastodon_follow_hack(follow_object, followed)
393
394 _ ->
395 {:error, nil}
396 end
397 end
398
399 # Reduce the object list to find the reported user.
400 defp get_reported(objects) do
401 Enum.reduce_while(objects, nil, fn ap_id, _ ->
402 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
403 {:halt, user}
404 else
405 _ -> {:cont, nil}
406 end
407 end)
408 end
409
410 def handle_incoming(data, options \\ [])
411
412 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
413 # with nil ID.
414 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
415 with context <- data["context"] || Utils.generate_context_id(),
416 content <- data["content"] || "",
417 %User{} = actor <- User.get_cached_by_ap_id(actor),
418 # Reduce the object list to find the reported user.
419 %User{} = account <- get_reported(objects),
420 # Remove the reported user from the object list.
421 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
422 %{
423 actor: actor,
424 context: context,
425 account: account,
426 statuses: statuses,
427 content: content,
428 additional: %{"cc" => [account.ap_id]}
429 }
430 |> ActivityPub.flag()
431 end
432 end
433
434 # disallow objects with bogus IDs
435 def handle_incoming(%{"id" => nil}, _options), do: :error
436 def handle_incoming(%{"id" => ""}, _options), do: :error
437 # length of https:// = 8, should validate better, but good enough for now.
438 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
439 do: :error
440
441 # TODO: validate those with a Ecto scheme
442 # - tags
443 # - emoji
444 def handle_incoming(
445 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
446 options
447 )
448 when objtype in ["Article", "Event", "Note", "Video", "Page", "Question", "Answer", "Audio"] do
449 actor = Containment.get_actor(data)
450
451 data =
452 Map.put(data, "actor", actor)
453 |> fix_addressing
454
455 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
456 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
457 object = fix_object(object, options)
458
459 params = %{
460 to: data["to"],
461 object: object,
462 actor: user,
463 context: object["conversation"],
464 local: false,
465 published: data["published"],
466 additional:
467 Map.take(data, [
468 "cc",
469 "directMessage",
470 "id"
471 ])
472 }
473
474 with {:ok, created_activity} <- ActivityPub.create(params) do
475 reply_depth = (options[:depth] || 0) + 1
476
477 if Federator.allowed_thread_distance?(reply_depth) do
478 for reply_id <- replies(object) do
479 Pleroma.Workers.RemoteFetcherWorker.enqueue("fetch_remote", %{
480 "id" => reply_id,
481 "depth" => reply_depth
482 })
483 end
484 end
485
486 {:ok, created_activity}
487 end
488 else
489 %Activity{} = activity -> {:ok, activity}
490 _e -> :error
491 end
492 end
493
494 def handle_incoming(
495 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
496 options
497 ) do
498 actor = Containment.get_actor(data)
499
500 data =
501 Map.put(data, "actor", actor)
502 |> fix_addressing
503
504 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
505 reply_depth = (options[:depth] || 0) + 1
506 options = Keyword.put(options, :depth, reply_depth)
507 object = fix_object(object, options)
508
509 params = %{
510 to: data["to"],
511 object: object,
512 actor: user,
513 context: nil,
514 local: false,
515 published: data["published"],
516 additional: Map.take(data, ["cc", "id"])
517 }
518
519 ActivityPub.listen(params)
520 else
521 _e -> :error
522 end
523 end
524
525 def handle_incoming(
526 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
527 _options
528 ) do
529 with %User{local: true} = followed <-
530 User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
531 {:ok, %User{} = follower} <-
532 User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
533 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
534 with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
535 {_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
536 {_, false} <- {:user_locked, User.locked?(followed)},
537 {_, {:ok, follower}} <- {:follow, User.follow(follower, followed)},
538 {_, {:ok, _}} <-
539 {:follow_state_update, Utils.update_follow_state_for_all(activity, "accept")},
540 {:ok, _relationship} <-
541 FollowingRelationship.update(follower, followed, :follow_accept) do
542 ActivityPub.accept(%{
543 to: [follower.ap_id],
544 actor: followed,
545 object: data,
546 local: true
547 })
548 else
549 {:user_blocked, true} ->
550 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
551 {:ok, _relationship} = FollowingRelationship.update(follower, followed, :follow_reject)
552
553 ActivityPub.reject(%{
554 to: [follower.ap_id],
555 actor: followed,
556 object: data,
557 local: true
558 })
559
560 {:follow, {:error, _}} ->
561 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
562 {:ok, _relationship} = FollowingRelationship.update(follower, followed, :follow_reject)
563
564 ActivityPub.reject(%{
565 to: [follower.ap_id],
566 actor: followed,
567 object: data,
568 local: true
569 })
570
571 {:user_locked, true} ->
572 {:ok, _relationship} = FollowingRelationship.update(follower, followed, :follow_pending)
573 :noop
574 end
575
576 {:ok, activity}
577 else
578 _e ->
579 :error
580 end
581 end
582
583 def handle_incoming(
584 %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,
585 _options
586 ) do
587 with actor <- Containment.get_actor(data),
588 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
589 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
590 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
591 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
592 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_accept) do
593 ActivityPub.accept(%{
594 to: follow_activity.data["to"],
595 type: "Accept",
596 actor: followed,
597 object: follow_activity.data["id"],
598 local: false,
599 activity_id: id
600 })
601 else
602 _e -> :error
603 end
604 end
605
606 def handle_incoming(
607 %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
608 _options
609 ) do
610 with actor <- Containment.get_actor(data),
611 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
612 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
613 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
614 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
615 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_reject),
616 {:ok, activity} <-
617 ActivityPub.reject(%{
618 to: follow_activity.data["to"],
619 type: "Reject",
620 actor: followed,
621 object: follow_activity.data["id"],
622 local: false,
623 activity_id: id
624 }) do
625 {:ok, activity}
626 else
627 _e -> :error
628 end
629 end
630
631 @misskey_reactions %{
632 "like" => "👍",
633 "love" => "❤️",
634 "laugh" => "😆",
635 "hmm" => "🤔",
636 "surprise" => "😮",
637 "congrats" => "🎉",
638 "angry" => "💢",
639 "confused" => "😥",
640 "rip" => "😇",
641 "pudding" => "🍮",
642 "star" => "⭐"
643 }
644
645 @doc "Rewrite misskey likes into EmojiReacts"
646 def handle_incoming(
647 %{
648 "type" => "Like",
649 "_misskey_reaction" => reaction
650 } = data,
651 options
652 ) do
653 data
654 |> Map.put("type", "EmojiReact")
655 |> Map.put("content", @misskey_reactions[reaction] || reaction)
656 |> handle_incoming(options)
657 end
658
659 def handle_incoming(%{"type" => "Like"} = data, _options) do
660 with :ok <- ObjectValidator.fetch_actor_and_object(data),
661 {:ok, activity, _meta} <-
662 Pipeline.common_pipeline(data, local: false) do
663 {:ok, activity}
664 else
665 e -> {:error, e}
666 end
667 end
668
669 def handle_incoming(
670 %{
671 "type" => "EmojiReact",
672 "object" => object_id,
673 "actor" => _actor,
674 "id" => id,
675 "content" => emoji
676 } = data,
677 _options
678 ) do
679 with actor <- Containment.get_actor(data),
680 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
681 {:ok, object} <- get_obj_helper(object_id),
682 {:ok, activity, _object} <-
683 ActivityPub.react_with_emoji(actor, object, emoji, activity_id: id, local: false) do
684 {:ok, activity}
685 else
686 _e -> :error
687 end
688 end
689
690 def handle_incoming(
691 %{"type" => "Announce", "object" => object_id, "actor" => _actor, "id" => id} = data,
692 _options
693 ) do
694 with actor <- Containment.get_actor(data),
695 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
696 {:ok, object} <- get_embedded_obj_helper(object_id, actor),
697 public <- Visibility.is_public?(data),
698 {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false, public) do
699 {:ok, activity}
700 else
701 _e -> :error
702 end
703 end
704
705 def handle_incoming(
706 %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
707 data,
708 _options
709 )
710 when object_type in [
711 "Person",
712 "Application",
713 "Service",
714 "Organization"
715 ] do
716 with %User{ap_id: ^actor_id} = actor <- User.get_cached_by_ap_id(object["id"]) do
717 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
718
719 actor
720 |> User.remote_user_changeset(new_user_data)
721 |> User.update_and_set_cache()
722
723 ActivityPub.update(%{
724 local: false,
725 to: data["to"] || [],
726 cc: data["cc"] || [],
727 object: object,
728 actor: actor_id,
729 activity_id: data["id"]
730 })
731 else
732 e ->
733 Logger.error(e)
734 :error
735 end
736 end
737
738 def handle_incoming(
739 %{"type" => "Delete"} = data,
740 _options
741 ) do
742 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
743 {:ok, activity}
744 end
745 end
746
747 def handle_incoming(
748 %{
749 "type" => "Undo",
750 "object" => %{"type" => "Announce", "object" => object_id},
751 "actor" => _actor,
752 "id" => id
753 } = data,
754 _options
755 ) do
756 with actor <- Containment.get_actor(data),
757 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
758 {:ok, object} <- get_obj_helper(object_id),
759 {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
760 {:ok, activity}
761 else
762 _e -> :error
763 end
764 end
765
766 def handle_incoming(
767 %{
768 "type" => "Undo",
769 "object" => %{"type" => "Follow", "object" => followed},
770 "actor" => follower,
771 "id" => id
772 } = _data,
773 _options
774 ) do
775 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
776 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
777 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
778 User.unfollow(follower, followed)
779 {:ok, activity}
780 else
781 _e -> :error
782 end
783 end
784
785 def handle_incoming(
786 %{
787 "type" => "Undo",
788 "object" => %{"type" => "EmojiReact", "id" => reaction_activity_id},
789 "actor" => _actor,
790 "id" => id
791 } = data,
792 _options
793 ) do
794 with actor <- Containment.get_actor(data),
795 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
796 {:ok, activity, _} <-
797 ActivityPub.unreact_with_emoji(actor, reaction_activity_id,
798 activity_id: id,
799 local: false
800 ) do
801 {:ok, activity}
802 else
803 _e -> :error
804 end
805 end
806
807 def handle_incoming(
808 %{
809 "type" => "Undo",
810 "object" => %{"type" => "Block", "object" => blocked},
811 "actor" => blocker,
812 "id" => id
813 } = _data,
814 _options
815 ) do
816 with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
817 {:ok, %User{} = blocker} <- User.get_or_fetch_by_ap_id(blocker),
818 {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
819 User.unblock(blocker, blocked)
820 {:ok, activity}
821 else
822 _e -> :error
823 end
824 end
825
826 def handle_incoming(
827 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
828 _options
829 ) do
830 with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
831 {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
832 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
833 User.unfollow(blocker, blocked)
834 User.block(blocker, blocked)
835 {:ok, activity}
836 else
837 _e -> :error
838 end
839 end
840
841 def handle_incoming(
842 %{
843 "type" => "Undo",
844 "object" => %{"type" => "Like", "object" => object_id},
845 "actor" => _actor,
846 "id" => id
847 } = data,
848 _options
849 ) do
850 with actor <- Containment.get_actor(data),
851 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
852 {:ok, object} <- get_obj_helper(object_id),
853 {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
854 {:ok, activity}
855 else
856 _e -> :error
857 end
858 end
859
860 # For Undos that don't have the complete object attached, try to find it in our database.
861 def handle_incoming(
862 %{
863 "type" => "Undo",
864 "object" => object
865 } = activity,
866 options
867 )
868 when is_binary(object) do
869 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
870 activity
871 |> Map.put("object", data)
872 |> handle_incoming(options)
873 else
874 _e -> :error
875 end
876 end
877
878 def handle_incoming(
879 %{
880 "type" => "Move",
881 "actor" => origin_actor,
882 "object" => origin_actor,
883 "target" => target_actor
884 },
885 _options
886 ) do
887 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
888 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
889 true <- origin_actor in target_user.also_known_as do
890 ActivityPub.move(origin_user, target_user, false)
891 else
892 _e -> :error
893 end
894 end
895
896 def handle_incoming(_, _), do: :error
897
898 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
899 def get_obj_helper(id, options \\ []) do
900 case Object.normalize(id, true, options) do
901 %Object{} = object -> {:ok, object}
902 _ -> nil
903 end
904 end
905
906 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
907 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
908 ap_id: ap_id
909 })
910 when attributed_to == ap_id do
911 with {:ok, activity} <-
912 handle_incoming(%{
913 "type" => "Create",
914 "to" => data["to"],
915 "cc" => data["cc"],
916 "actor" => attributed_to,
917 "object" => data
918 }) do
919 {:ok, Object.normalize(activity)}
920 else
921 _ -> get_obj_helper(object_id)
922 end
923 end
924
925 def get_embedded_obj_helper(object_id, _) do
926 get_obj_helper(object_id)
927 end
928
929 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
930 with false <- String.starts_with?(in_reply_to, "http"),
931 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
932 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
933 else
934 _e -> object
935 end
936 end
937
938 def set_reply_to_uri(obj), do: obj
939
940 @doc """
941 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
942 Based on Mastodon's ActivityPub::NoteSerializer#replies.
943 """
944 def set_replies(obj_data) do
945 replies_uris =
946 with limit when limit > 0 <-
947 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
948 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
949 object
950 |> Object.self_replies()
951 |> select([o], fragment("?->>'id'", o.data))
952 |> limit(^limit)
953 |> Repo.all()
954 else
955 _ -> []
956 end
957
958 set_replies(obj_data, replies_uris)
959 end
960
961 defp set_replies(obj, []) do
962 obj
963 end
964
965 defp set_replies(obj, replies_uris) do
966 replies_collection = %{
967 "type" => "Collection",
968 "items" => replies_uris
969 }
970
971 Map.merge(obj, %{"replies" => replies_collection})
972 end
973
974 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
975 items
976 end
977
978 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
979 items
980 end
981
982 def replies(_), do: []
983
984 # Prepares the object of an outgoing create activity.
985 def prepare_object(object) do
986 object
987 |> set_sensitive
988 |> add_hashtags
989 |> add_mention_tags
990 |> add_emoji_tags
991 |> add_attributed_to
992 |> prepare_attachments
993 |> set_conversation
994 |> set_reply_to_uri
995 |> set_replies
996 |> strip_internal_fields
997 |> strip_internal_tags
998 |> set_type
999 end
1000
1001 # @doc
1002 # """
1003 # internal -> Mastodon
1004 # """
1005
1006 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
1007 when activity_type in ["Create", "Listen"] do
1008 object =
1009 object_id
1010 |> Object.normalize()
1011 |> Map.get(:data)
1012 |> prepare_object
1013
1014 data =
1015 data
1016 |> Map.put("object", object)
1017 |> Map.merge(Utils.make_json_ld_header())
1018 |> Map.delete("bcc")
1019
1020 {:ok, data}
1021 end
1022
1023 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
1024 object =
1025 object_id
1026 |> Object.normalize()
1027
1028 data =
1029 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
1030 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
1031 else
1032 data |> maybe_fix_object_url
1033 end
1034
1035 data =
1036 data
1037 |> strip_internal_fields
1038 |> Map.merge(Utils.make_json_ld_header())
1039 |> Map.delete("bcc")
1040
1041 {:ok, data}
1042 end
1043
1044 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
1045 # because of course it does.
1046 def prepare_outgoing(%{"type" => "Accept"} = data) do
1047 with follow_activity <- Activity.normalize(data["object"]) do
1048 object = %{
1049 "actor" => follow_activity.actor,
1050 "object" => follow_activity.data["object"],
1051 "id" => follow_activity.data["id"],
1052 "type" => "Follow"
1053 }
1054
1055 data =
1056 data
1057 |> Map.put("object", object)
1058 |> Map.merge(Utils.make_json_ld_header())
1059
1060 {:ok, data}
1061 end
1062 end
1063
1064 def prepare_outgoing(%{"type" => "Reject"} = data) do
1065 with follow_activity <- Activity.normalize(data["object"]) do
1066 object = %{
1067 "actor" => follow_activity.actor,
1068 "object" => follow_activity.data["object"],
1069 "id" => follow_activity.data["id"],
1070 "type" => "Follow"
1071 }
1072
1073 data =
1074 data
1075 |> Map.put("object", object)
1076 |> Map.merge(Utils.make_json_ld_header())
1077
1078 {:ok, data}
1079 end
1080 end
1081
1082 def prepare_outgoing(%{"type" => _type} = data) do
1083 data =
1084 data
1085 |> strip_internal_fields
1086 |> maybe_fix_object_url
1087 |> Map.merge(Utils.make_json_ld_header())
1088
1089 {:ok, data}
1090 end
1091
1092 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
1093 with false <- String.starts_with?(object, "http"),
1094 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
1095 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
1096 relative_object do
1097 Map.put(data, "object", external_url)
1098 else
1099 {:fetch, e} ->
1100 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
1101 data
1102
1103 _ ->
1104 data
1105 end
1106 end
1107
1108 def maybe_fix_object_url(data), do: data
1109
1110 def add_hashtags(object) do
1111 tags =
1112 (object["tag"] || [])
1113 |> Enum.map(fn
1114 # Expand internal representation tags into AS2 tags.
1115 tag when is_binary(tag) ->
1116 %{
1117 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
1118 "name" => "##{tag}",
1119 "type" => "Hashtag"
1120 }
1121
1122 # Do not process tags which are already AS2 tag objects.
1123 tag when is_map(tag) ->
1124 tag
1125 end)
1126
1127 Map.put(object, "tag", tags)
1128 end
1129
1130 def add_mention_tags(object) do
1131 {enabled_receivers, disabled_receivers} = Utils.get_notified_from_object(object)
1132 potential_receivers = enabled_receivers ++ disabled_receivers
1133 mentions = Enum.map(potential_receivers, &build_mention_tag/1)
1134
1135 tags = object["tag"] || []
1136 Map.put(object, "tag", tags ++ mentions)
1137 end
1138
1139 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
1140 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
1141 end
1142
1143 def take_emoji_tags(%User{emoji: emoji}) do
1144 emoji
1145 |> Map.to_list()
1146 |> Enum.map(&build_emoji_tag/1)
1147 end
1148
1149 # TODO: we should probably send mtime instead of unix epoch time for updated
1150 def add_emoji_tags(%{"emoji" => emoji} = object) do
1151 tags = object["tag"] || []
1152
1153 out = Enum.map(emoji, &build_emoji_tag/1)
1154
1155 Map.put(object, "tag", tags ++ out)
1156 end
1157
1158 def add_emoji_tags(object), do: object
1159
1160 defp build_emoji_tag({name, url}) do
1161 %{
1162 "icon" => %{"url" => url, "type" => "Image"},
1163 "name" => ":" <> name <> ":",
1164 "type" => "Emoji",
1165 "updated" => "1970-01-01T00:00:00Z",
1166 "id" => url
1167 }
1168 end
1169
1170 def set_conversation(object) do
1171 Map.put(object, "conversation", object["context"])
1172 end
1173
1174 def set_sensitive(%{"sensitive" => true} = object) do
1175 object
1176 end
1177
1178 def set_sensitive(object) do
1179 tags = object["tag"] || []
1180 Map.put(object, "sensitive", "nsfw" in tags)
1181 end
1182
1183 def set_type(%{"type" => "Answer"} = object) do
1184 Map.put(object, "type", "Note")
1185 end
1186
1187 def set_type(object), do: object
1188
1189 def add_attributed_to(object) do
1190 attributed_to = object["attributedTo"] || object["actor"]
1191 Map.put(object, "attributedTo", attributed_to)
1192 end
1193
1194 def prepare_attachments(object) do
1195 attachments =
1196 object
1197 |> Map.get("attachment", [])
1198 |> Enum.map(fn data ->
1199 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
1200
1201 %{
1202 "url" => href,
1203 "mediaType" => media_type,
1204 "name" => data["name"],
1205 "type" => "Document"
1206 }
1207 end)
1208
1209 Map.put(object, "attachment", attachments)
1210 end
1211
1212 def strip_internal_fields(object) do
1213 Map.drop(object, Pleroma.Constants.object_internal_fields())
1214 end
1215
1216 defp strip_internal_tags(%{"tag" => tags} = object) do
1217 tags = Enum.filter(tags, fn x -> is_map(x) end)
1218
1219 Map.put(object, "tag", tags)
1220 end
1221
1222 defp strip_internal_tags(object), do: object
1223
1224 def perform(:user_upgrade, user) do
1225 # we pass a fake user so that the followers collection is stripped away
1226 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1227
1228 from(
1229 a in Activity,
1230 where: ^old_follower_address in a.recipients,
1231 update: [
1232 set: [
1233 recipients:
1234 fragment(
1235 "array_replace(?,?,?)",
1236 a.recipients,
1237 ^old_follower_address,
1238 ^user.follower_address
1239 )
1240 ]
1241 ]
1242 )
1243 |> Repo.update_all([])
1244 end
1245
1246 def upgrade_user_from_ap_id(ap_id) do
1247 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1248 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1249 {:ok, user} <- update_user(user, data) do
1250 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1251 {:ok, user}
1252 else
1253 %User{} = user -> {:ok, user}
1254 e -> e
1255 end
1256 end
1257
1258 defp update_user(user, data) do
1259 user
1260 |> User.remote_user_changeset(data)
1261 |> User.update_and_set_cache()
1262 end
1263
1264 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1265 Map.put(data, "url", url["href"])
1266 end
1267
1268 def maybe_fix_user_url(data), do: data
1269
1270 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)
1271 end