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