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