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