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