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