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