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