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