Merge branch 'issue/1936' into 'develop'
[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.EarmarkRenderer
11 alias Pleroma.EctoType.ActivityPub.ObjectValidators
12 alias Pleroma.Maps
13 alias Pleroma.Object
14 alias Pleroma.Object.Containment
15 alias Pleroma.Repo
16 alias Pleroma.User
17 alias Pleroma.Web.ActivityPub.ActivityPub
18 alias Pleroma.Web.ActivityPub.Builder
19 alias Pleroma.Web.ActivityPub.ObjectValidator
20 alias Pleroma.Web.ActivityPub.Pipeline
21 alias Pleroma.Web.ActivityPub.Utils
22 alias Pleroma.Web.ActivityPub.Visibility
23 alias Pleroma.Web.Federator
24 alias Pleroma.Workers.TransmogrifierWorker
25
26 import Ecto.Query
27
28 require Logger
29 require Pleroma.Constants
30
31 @doc """
32 Modifies an incoming AP object (mastodon format) to our internal format.
33 """
34 def fix_object(object, options \\ []) do
35 object
36 |> strip_internal_fields
37 |> fix_actor
38 |> fix_url
39 |> fix_attachments
40 |> fix_context
41 |> fix_in_reply_to(options)
42 |> fix_emoji
43 |> fix_tag
44 |> fix_content_map
45 |> fix_addressing
46 |> fix_summary
47 |> fix_type(options)
48 |> fix_content
49 end
50
51 def fix_summary(%{"summary" => nil} = object) do
52 Map.put(object, "summary", "")
53 end
54
55 def fix_summary(%{"summary" => _} = object) do
56 # summary is present, nothing to do
57 object
58 end
59
60 def fix_summary(object), do: Map.put(object, "summary", "")
61
62 def fix_addressing_list(map, field) do
63 addrs = map[field]
64
65 cond do
66 is_list(addrs) ->
67 Map.put(map, field, Enum.filter(addrs, &is_binary/1))
68
69 is_binary(addrs) ->
70 Map.put(map, field, [addrs])
71
72 true ->
73 Map.put(map, field, [])
74 end
75 end
76
77 def fix_explicit_addressing(
78 %{"to" => to, "cc" => cc} = object,
79 explicit_mentions,
80 follower_collection
81 ) do
82 explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
83
84 explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
85
86 final_cc =
87 (cc ++ explicit_cc)
88 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
89 |> Enum.uniq()
90
91 object
92 |> Map.put("to", explicit_to)
93 |> Map.put("cc", final_cc)
94 end
95
96 def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
97
98 # if directMessage flag is set to true, leave the addressing alone
99 def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
100
101 def fix_explicit_addressing(object) do
102 explicit_mentions = Utils.determine_explicit_mentions(object)
103
104 %User{follower_address: follower_collection} =
105 object
106 |> Containment.get_actor()
107 |> User.get_cached_by_ap_id()
108
109 explicit_mentions =
110 explicit_mentions ++
111 [
112 Pleroma.Constants.as_public(),
113 follower_collection
114 ]
115
116 fix_explicit_addressing(object, explicit_mentions, follower_collection)
117 end
118
119 # if as:Public is addressed, then make sure the followers collection is also addressed
120 # so that the activities will be delivered to local users.
121 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
122 recipients = to ++ cc
123
124 if followers_collection not in recipients do
125 cond do
126 Pleroma.Constants.as_public() in cc ->
127 to = to ++ [followers_collection]
128 Map.put(object, "to", to)
129
130 Pleroma.Constants.as_public() in to ->
131 cc = cc ++ [followers_collection]
132 Map.put(object, "cc", cc)
133
134 true ->
135 object
136 end
137 else
138 object
139 end
140 end
141
142 def fix_implicit_addressing(object, _), do: object
143
144 def fix_addressing(object) do
145 {:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"])
146 followers_collection = User.ap_followers(user)
147
148 object
149 |> fix_addressing_list("to")
150 |> fix_addressing_list("cc")
151 |> fix_addressing_list("bto")
152 |> fix_addressing_list("bcc")
153 |> fix_explicit_addressing()
154 |> fix_implicit_addressing(followers_collection)
155 end
156
157 def fix_actor(%{"attributedTo" => actor} = object) do
158 actor = Containment.get_actor(%{"actor" => actor})
159
160 # TODO: Remove actor field for Objects
161 object
162 |> Map.put("actor", actor)
163 |> Map.put("attributedTo", actor)
164 end
165
166 def fix_in_reply_to(object, options \\ [])
167
168 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
169 when not is_nil(in_reply_to) do
170 in_reply_to_id = prepare_in_reply_to(in_reply_to)
171 object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
172 depth = (options[:depth] || 0) + 1
173
174 if Federator.allowed_thread_distance?(depth) do
175 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
176 %Activity{} <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
177 object
178 |> Map.put("inReplyTo", replied_object.data["id"])
179 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
180 |> Map.put("context", replied_object.data["context"] || object["conversation"])
181 |> Map.drop(["conversation"])
182 else
183 e ->
184 Logger.warn("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
185 object
186 end
187 else
188 object
189 end
190 end
191
192 def fix_in_reply_to(object, _options), do: object
193
194 defp prepare_in_reply_to(in_reply_to) do
195 cond do
196 is_bitstring(in_reply_to) ->
197 in_reply_to
198
199 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
200 in_reply_to["id"]
201
202 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
203 Enum.at(in_reply_to, 0)
204
205 true ->
206 ""
207 end
208 end
209
210 def fix_context(object) do
211 context = object["context"] || object["conversation"] || Utils.generate_context_id()
212
213 object
214 |> Map.put("context", context)
215 |> Map.drop(["conversation"])
216 end
217
218 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
219 attachments =
220 Enum.map(attachment, fn data ->
221 url =
222 cond do
223 is_list(data["url"]) -> List.first(data["url"])
224 is_map(data["url"]) -> data["url"]
225 true -> nil
226 end
227
228 media_type =
229 cond do
230 is_map(url) && MIME.valid?(url["mediaType"]) -> url["mediaType"]
231 MIME.valid?(data["mediaType"]) -> data["mediaType"]
232 MIME.valid?(data["mimeType"]) -> data["mimeType"]
233 true -> nil
234 end
235
236 href =
237 cond do
238 is_map(url) && is_binary(url["href"]) -> url["href"]
239 is_binary(data["url"]) -> data["url"]
240 is_binary(data["href"]) -> data["href"]
241 true -> nil
242 end
243
244 if href do
245 attachment_url =
246 %{
247 "href" => href,
248 "type" => Map.get(url || %{}, "type", "Link")
249 }
250 |> Maps.put_if_present("mediaType", media_type)
251
252 %{
253 "url" => [attachment_url],
254 "type" => data["type"] || "Document"
255 }
256 |> Maps.put_if_present("mediaType", media_type)
257 |> Maps.put_if_present("name", data["name"])
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(%{"type" => object_type, "url" => url} = object)
280 when object_type in ["Video", "Audio"] and is_list(url) do
281 attachment =
282 Enum.find(url, fn x ->
283 media_type = x["mediaType"] || x["mimeType"] || ""
284
285 is_map(x) and String.starts_with?(media_type, ["audio/", "video/"])
286 end)
287
288 link_element =
289 Enum.find(url, fn x -> is_map(x) and (x["mediaType"] || x["mimeType"]) == "text/html" end)
290
291 object
292 |> Map.put("attachment", [attachment])
293 |> Map.put("url", link_element["href"])
294 end
295
296 def fix_url(%{"type" => object_type, "url" => url} = object)
297 when object_type != "Video" and is_list(url) do
298 first_element = Enum.at(url, 0)
299
300 url_string =
301 cond do
302 is_bitstring(first_element) -> first_element
303 is_map(first_element) -> first_element["href"] || ""
304 true -> ""
305 end
306
307 Map.put(object, "url", url_string)
308 end
309
310 def fix_url(object), do: object
311
312 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
313 emoji =
314 tags
315 |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
316 |> Enum.reduce(%{}, fn data, mapping ->
317 name = String.trim(data["name"], ":")
318
319 Map.put(mapping, name, data["icon"]["url"])
320 end)
321
322 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
323 emoji = Map.merge(object["emoji"] || %{}, emoji)
324
325 Map.put(object, "emoji", emoji)
326 end
327
328 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
329 name = String.trim(tag["name"], ":")
330 emoji = %{name => tag["icon"]["url"]}
331
332 Map.put(object, "emoji", emoji)
333 end
334
335 def fix_emoji(object), do: object
336
337 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
338 tags =
339 tag
340 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
341 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
342
343 Map.put(object, "tag", tag ++ tags)
344 end
345
346 def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
347 combined = [tag, String.slice(hashtag, 1..-1)]
348
349 Map.put(object, "tag", combined)
350 end
351
352 def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
353
354 def fix_tag(object), do: object
355
356 # content map usually only has one language so this will do for now.
357 def fix_content_map(%{"contentMap" => content_map} = object) do
358 content_groups = Map.to_list(content_map)
359 {_, content} = Enum.at(content_groups, 0)
360
361 Map.put(object, "content", content)
362 end
363
364 def fix_content_map(object), do: object
365
366 def fix_type(object, options \\ [])
367
368 def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
369 when is_binary(reply_id) do
370 with true <- Federator.allowed_thread_distance?(options[:depth]),
371 {:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
372 Map.put(object, "type", "Answer")
373 else
374 _ -> object
375 end
376 end
377
378 def fix_type(object, _), do: object
379
380 defp fix_content(%{"mediaType" => "text/markdown", "content" => content} = object)
381 when is_binary(content) do
382 html_content =
383 content
384 |> Earmark.as_html!(%Earmark.Options{renderer: EarmarkRenderer})
385 |> Pleroma.HTML.filter_tags()
386
387 Map.merge(object, %{"content" => html_content, "mediaType" => "text/html"})
388 end
389
390 defp fix_content(object), do: object
391
392 # Reduce the object list to find the reported user.
393 defp get_reported(objects) do
394 Enum.reduce_while(objects, nil, fn ap_id, _ ->
395 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
396 {:halt, user}
397 else
398 _ -> {:cont, nil}
399 end
400 end)
401 end
402
403 # Compatibility wrapper for Mastodon votes
404 defp handle_create(%{"object" => %{"type" => "Answer"}} = data, _user) do
405 handle_incoming(data)
406 end
407
408 defp handle_create(%{"object" => object} = data, user) do
409 %{
410 to: data["to"],
411 object: object,
412 actor: user,
413 context: object["context"],
414 local: false,
415 published: data["published"],
416 additional:
417 Map.take(data, [
418 "cc",
419 "directMessage",
420 "id"
421 ])
422 }
423 |> ActivityPub.create()
424 end
425
426 def handle_incoming(data, options \\ [])
427
428 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
429 # with nil ID.
430 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
431 with context <- data["context"] || Utils.generate_context_id(),
432 content <- data["content"] || "",
433 %User{} = actor <- User.get_cached_by_ap_id(actor),
434 # Reduce the object list to find the reported user.
435 %User{} = account <- get_reported(objects),
436 # Remove the reported user from the object list.
437 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
438 %{
439 actor: actor,
440 context: context,
441 account: account,
442 statuses: statuses,
443 content: content,
444 additional: %{"cc" => [account.ap_id]}
445 }
446 |> ActivityPub.flag()
447 end
448 end
449
450 # disallow objects with bogus IDs
451 def handle_incoming(%{"id" => nil}, _options), do: :error
452 def handle_incoming(%{"id" => ""}, _options), do: :error
453 # length of https:// = 8, should validate better, but good enough for now.
454 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
455 do: :error
456
457 # TODO: validate those with a Ecto scheme
458 # - tags
459 # - emoji
460 def handle_incoming(
461 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
462 options
463 )
464 when objtype in ["Article", "Event", "Note", "Video", "Page", "Audio"] do
465 actor = Containment.get_actor(data)
466
467 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
468 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(actor) do
469 data =
470 data
471 |> Map.put("object", fix_object(object, options))
472 |> Map.put("actor", actor)
473 |> fix_addressing()
474
475 with {:ok, created_activity} <- handle_create(data, user) do
476 reply_depth = (options[:depth] || 0) + 1
477
478 if Federator.allowed_thread_distance?(reply_depth) do
479 for reply_id <- replies(object) do
480 Pleroma.Workers.RemoteFetcherWorker.enqueue("fetch_remote", %{
481 "id" => reply_id,
482 "depth" => reply_depth
483 })
484 end
485 end
486
487 {:ok, created_activity}
488 end
489 else
490 %Activity{} = activity -> {:ok, activity}
491 _e -> :error
492 end
493 end
494
495 def handle_incoming(
496 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
497 options
498 ) do
499 actor = Containment.get_actor(data)
500
501 data =
502 Map.put(data, "actor", actor)
503 |> fix_addressing
504
505 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
506 reply_depth = (options[:depth] || 0) + 1
507 options = Keyword.put(options, :depth, reply_depth)
508 object = fix_object(object, options)
509
510 params = %{
511 to: data["to"],
512 object: object,
513 actor: user,
514 context: nil,
515 local: false,
516 published: data["published"],
517 additional: Map.take(data, ["cc", "id"])
518 }
519
520 ActivityPub.listen(params)
521 else
522 _e -> :error
523 end
524 end
525
526 @misskey_reactions %{
527 "like" => "👍",
528 "love" => "❤️",
529 "laugh" => "😆",
530 "hmm" => "🤔",
531 "surprise" => "😮",
532 "congrats" => "🎉",
533 "angry" => "💢",
534 "confused" => "😥",
535 "rip" => "😇",
536 "pudding" => "🍮",
537 "star" => "⭐"
538 }
539
540 @doc "Rewrite misskey likes into EmojiReacts"
541 def handle_incoming(
542 %{
543 "type" => "Like",
544 "_misskey_reaction" => reaction
545 } = data,
546 options
547 ) do
548 data
549 |> Map.put("type", "EmojiReact")
550 |> Map.put("content", @misskey_reactions[reaction] || reaction)
551 |> handle_incoming(options)
552 end
553
554 def handle_incoming(
555 %{"type" => "Create", "object" => %{"type" => objtype}} = data,
556 _options
557 )
558 when objtype in ["Question", "Answer", "ChatMessage"] do
559 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
560 {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
561 {:ok, activity}
562 end
563 end
564
565 def handle_incoming(%{"type" => type} = data, _options)
566 when type in ~w{Like EmojiReact Announce} do
567 with :ok <- ObjectValidator.fetch_actor_and_object(data),
568 {:ok, activity, _meta} <-
569 Pipeline.common_pipeline(data, local: false) do
570 {:ok, activity}
571 else
572 e -> {:error, e}
573 end
574 end
575
576 def handle_incoming(
577 %{"type" => type} = data,
578 _options
579 )
580 when type in ~w{Update Block Follow Accept Reject} do
581 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
582 {:ok, activity, _} <-
583 Pipeline.common_pipeline(data, local: false) do
584 {:ok, activity}
585 end
586 end
587
588 def handle_incoming(
589 %{"type" => "Delete"} = data,
590 _options
591 ) do
592 with {:ok, activity, _} <-
593 Pipeline.common_pipeline(data, local: false) do
594 {:ok, activity}
595 else
596 {:error, {:validate_object, _}} = e ->
597 # Check if we have a create activity for this
598 with {:ok, object_id} <- ObjectValidators.ObjectID.cast(data["object"]),
599 %Activity{data: %{"actor" => actor}} <-
600 Activity.create_by_object_ap_id(object_id) |> Repo.one(),
601 # We have one, insert a tombstone and retry
602 {:ok, tombstone_data, _} <- Builder.tombstone(actor, object_id),
603 {:ok, _tombstone} <- Object.create(tombstone_data) do
604 handle_incoming(data)
605 else
606 _ -> e
607 end
608 end
609 end
610
611 def handle_incoming(
612 %{
613 "type" => "Undo",
614 "object" => %{"type" => "Follow", "object" => followed},
615 "actor" => follower,
616 "id" => id
617 } = _data,
618 _options
619 ) do
620 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
621 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
622 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
623 User.unfollow(follower, followed)
624 {:ok, activity}
625 else
626 _e -> :error
627 end
628 end
629
630 def handle_incoming(
631 %{
632 "type" => "Undo",
633 "object" => %{"type" => type}
634 } = data,
635 _options
636 )
637 when type in ["Like", "EmojiReact", "Announce", "Block"] do
638 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
639 {:ok, activity}
640 end
641 end
642
643 # For Undos that don't have the complete object attached, try to find it in our database.
644 def handle_incoming(
645 %{
646 "type" => "Undo",
647 "object" => object
648 } = activity,
649 options
650 )
651 when is_binary(object) do
652 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
653 activity
654 |> Map.put("object", data)
655 |> handle_incoming(options)
656 else
657 _e -> :error
658 end
659 end
660
661 def handle_incoming(
662 %{
663 "type" => "Move",
664 "actor" => origin_actor,
665 "object" => origin_actor,
666 "target" => target_actor
667 },
668 _options
669 ) do
670 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
671 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
672 true <- origin_actor in target_user.also_known_as do
673 ActivityPub.move(origin_user, target_user, false)
674 else
675 _e -> :error
676 end
677 end
678
679 def handle_incoming(_, _), do: :error
680
681 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
682 def get_obj_helper(id, options \\ []) do
683 case Object.normalize(id, true, options) do
684 %Object{} = object -> {:ok, object}
685 _ -> nil
686 end
687 end
688
689 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
690 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
691 ap_id: ap_id
692 })
693 when attributed_to == ap_id do
694 with {:ok, activity} <-
695 handle_incoming(%{
696 "type" => "Create",
697 "to" => data["to"],
698 "cc" => data["cc"],
699 "actor" => attributed_to,
700 "object" => data
701 }) do
702 {:ok, Object.normalize(activity)}
703 else
704 _ -> get_obj_helper(object_id)
705 end
706 end
707
708 def get_embedded_obj_helper(object_id, _) do
709 get_obj_helper(object_id)
710 end
711
712 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
713 with false <- String.starts_with?(in_reply_to, "http"),
714 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
715 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
716 else
717 _e -> object
718 end
719 end
720
721 def set_reply_to_uri(obj), do: obj
722
723 @doc """
724 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
725 Based on Mastodon's ActivityPub::NoteSerializer#replies.
726 """
727 def set_replies(obj_data) do
728 replies_uris =
729 with limit when limit > 0 <-
730 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
731 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
732 object
733 |> Object.self_replies()
734 |> select([o], fragment("?->>'id'", o.data))
735 |> limit(^limit)
736 |> Repo.all()
737 else
738 _ -> []
739 end
740
741 set_replies(obj_data, replies_uris)
742 end
743
744 defp set_replies(obj, []) do
745 obj
746 end
747
748 defp set_replies(obj, replies_uris) do
749 replies_collection = %{
750 "type" => "Collection",
751 "items" => replies_uris
752 }
753
754 Map.merge(obj, %{"replies" => replies_collection})
755 end
756
757 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
758 items
759 end
760
761 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
762 items
763 end
764
765 def replies(_), do: []
766
767 # Prepares the object of an outgoing create activity.
768 def prepare_object(object) do
769 object
770 |> set_sensitive
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()
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()
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" => 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_sensitive(%{"sensitive" => true} = object) do
962 object
963 end
964
965 def set_sensitive(object) do
966 tags = object["tag"] || []
967 Map.put(object, "sensitive", "nsfw" in tags)
968 end
969
970 def set_type(%{"type" => "Answer"} = object) do
971 Map.put(object, "type", "Note")
972 end
973
974 def set_type(object), do: object
975
976 def add_attributed_to(object) do
977 attributed_to = object["attributedTo"] || object["actor"]
978 Map.put(object, "attributedTo", attributed_to)
979 end
980
981 # TODO: Revisit this
982 def prepare_attachments(%{"type" => "ChatMessage"} = object), do: object
983
984 def prepare_attachments(object) do
985 attachments =
986 object
987 |> Map.get("attachment", [])
988 |> Enum.map(fn data ->
989 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
990
991 %{
992 "url" => href,
993 "mediaType" => media_type,
994 "name" => data["name"],
995 "type" => "Document"
996 }
997 end)
998
999 Map.put(object, "attachment", attachments)
1000 end
1001
1002 def strip_internal_fields(object) do
1003 Map.drop(object, Pleroma.Constants.object_internal_fields())
1004 end
1005
1006 defp strip_internal_tags(%{"tag" => tags} = object) do
1007 tags = Enum.filter(tags, fn x -> is_map(x) end)
1008
1009 Map.put(object, "tag", tags)
1010 end
1011
1012 defp strip_internal_tags(object), do: object
1013
1014 def perform(:user_upgrade, user) do
1015 # we pass a fake user so that the followers collection is stripped away
1016 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1017
1018 from(
1019 a in Activity,
1020 where: ^old_follower_address in a.recipients,
1021 update: [
1022 set: [
1023 recipients:
1024 fragment(
1025 "array_replace(?,?,?)",
1026 a.recipients,
1027 ^old_follower_address,
1028 ^user.follower_address
1029 )
1030 ]
1031 ]
1032 )
1033 |> Repo.update_all([])
1034 end
1035
1036 def upgrade_user_from_ap_id(ap_id) do
1037 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1038 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1039 {:ok, user} <- update_user(user, data) do
1040 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1041 {:ok, user}
1042 else
1043 %User{} = user -> {:ok, user}
1044 e -> e
1045 end
1046 end
1047
1048 defp update_user(user, data) do
1049 user
1050 |> User.remote_user_changeset(data)
1051 |> User.update_and_set_cache()
1052 end
1053
1054 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1055 Map.put(data, "url", url["href"])
1056 end
1057
1058 def maybe_fix_user_url(data), do: data
1059
1060 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)
1061 end