Merge branch 'openapi/admin/relay' 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.FollowingRelationship
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.ObjectValidators.Types
21 alias Pleroma.Web.ActivityPub.Pipeline
22 alias Pleroma.Web.ActivityPub.Utils
23 alias Pleroma.Web.ActivityPub.Visibility
24 alias Pleroma.Web.Federator
25 alias Pleroma.Workers.TransmogrifierWorker
26
27 import Ecto.Query
28
29 require Logger
30 require Pleroma.Constants
31
32 @doc """
33 Modifies an incoming AP object (mastodon format) to our internal format.
34 """
35 def fix_object(object, options \\ []) do
36 object
37 |> strip_internal_fields
38 |> fix_actor
39 |> fix_url
40 |> fix_attachments
41 |> fix_context
42 |> fix_in_reply_to(options)
43 |> fix_emoji
44 |> fix_tag
45 |> fix_content_map
46 |> fix_addressing
47 |> fix_summary
48 |> fix_type(options)
49 |> fix_content
50 end
51
52 def fix_summary(%{"summary" => nil} = object) do
53 Map.put(object, "summary", "")
54 end
55
56 def fix_summary(%{"summary" => _} = object) do
57 # summary is present, nothing to do
58 object
59 end
60
61 def fix_summary(object), do: Map.put(object, "summary", "")
62
63 def fix_addressing_list(map, field) do
64 cond do
65 is_binary(map[field]) ->
66 Map.put(map, field, [map[field]])
67
68 is_nil(map[field]) ->
69 Map.put(map, field, [])
70
71 true ->
72 map
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 Map.put(object, "actor", Containment.get_actor(%{"actor" => actor}))
158 end
159
160 def fix_in_reply_to(object, options \\ [])
161
162 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
163 when not is_nil(in_reply_to) do
164 in_reply_to_id = prepare_in_reply_to(in_reply_to)
165 object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
166 depth = (options[:depth] || 0) + 1
167
168 if Federator.allowed_thread_distance?(depth) do
169 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
170 %Activity{} <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
171 object
172 |> Map.put("inReplyTo", replied_object.data["id"])
173 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
174 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
175 |> Map.put("context", replied_object.data["context"] || object["conversation"])
176 else
177 e ->
178 Logger.error("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
179 object
180 end
181 else
182 object
183 end
184 end
185
186 def fix_in_reply_to(object, _options), do: object
187
188 defp prepare_in_reply_to(in_reply_to) do
189 cond do
190 is_bitstring(in_reply_to) ->
191 in_reply_to
192
193 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
194 in_reply_to["id"]
195
196 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
197 Enum.at(in_reply_to, 0)
198
199 true ->
200 ""
201 end
202 end
203
204 def fix_context(object) do
205 context = object["context"] || object["conversation"] || Utils.generate_context_id()
206
207 object
208 |> Map.put("context", context)
209 |> Map.put("conversation", context)
210 end
211
212 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
213 attachments =
214 Enum.map(attachment, fn data ->
215 url =
216 cond do
217 is_list(data["url"]) -> List.first(data["url"])
218 is_map(data["url"]) -> data["url"]
219 true -> nil
220 end
221
222 media_type =
223 cond do
224 is_map(url) && is_binary(url["mediaType"]) -> url["mediaType"]
225 is_binary(data["mediaType"]) -> data["mediaType"]
226 is_binary(data["mimeType"]) -> data["mimeType"]
227 true -> nil
228 end
229
230 href =
231 cond do
232 is_map(url) && is_binary(url["href"]) -> url["href"]
233 is_binary(data["url"]) -> data["url"]
234 is_binary(data["href"]) -> data["href"]
235 end
236
237 attachment_url =
238 %{"href" => href}
239 |> Maps.put_if_present("mediaType", media_type)
240 |> Maps.put_if_present("type", Map.get(url || %{}, "type"))
241
242 %{"url" => [attachment_url]}
243 |> Maps.put_if_present("mediaType", media_type)
244 |> Maps.put_if_present("type", data["type"])
245 |> Maps.put_if_present("name", data["name"])
246 end)
247
248 Map.put(object, "attachment", attachments)
249 end
250
251 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
252 object
253 |> Map.put("attachment", [attachment])
254 |> fix_attachments()
255 end
256
257 def fix_attachments(object), do: object
258
259 def fix_url(%{"url" => url} = object) when is_map(url) do
260 Map.put(object, "url", url["href"])
261 end
262
263 def fix_url(%{"type" => object_type, "url" => url} = object)
264 when object_type in ["Video", "Audio"] and is_list(url) do
265 first_element = Enum.at(url, 0)
266
267 link_element = Enum.find(url, fn x -> is_map(x) and x["mimeType"] == "text/html" end)
268
269 object
270 |> Map.put("attachment", [first_element])
271 |> Map.put("url", link_element["href"])
272 end
273
274 def fix_url(%{"type" => object_type, "url" => url} = object)
275 when object_type != "Video" and is_list(url) do
276 first_element = Enum.at(url, 0)
277
278 url_string =
279 cond do
280 is_bitstring(first_element) -> first_element
281 is_map(first_element) -> first_element["href"] || ""
282 true -> ""
283 end
284
285 Map.put(object, "url", url_string)
286 end
287
288 def fix_url(object), do: object
289
290 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
291 emoji =
292 tags
293 |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
294 |> Enum.reduce(%{}, fn data, mapping ->
295 name = String.trim(data["name"], ":")
296
297 Map.put(mapping, name, data["icon"]["url"])
298 end)
299
300 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
301 emoji = Map.merge(object["emoji"] || %{}, emoji)
302
303 Map.put(object, "emoji", emoji)
304 end
305
306 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
307 name = String.trim(tag["name"], ":")
308 emoji = %{name => tag["icon"]["url"]}
309
310 Map.put(object, "emoji", emoji)
311 end
312
313 def fix_emoji(object), do: object
314
315 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
316 tags =
317 tag
318 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
319 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
320
321 Map.put(object, "tag", tag ++ tags)
322 end
323
324 def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
325 combined = [tag, String.slice(hashtag, 1..-1)]
326
327 Map.put(object, "tag", combined)
328 end
329
330 def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
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 defp fix_content(%{"mediaType" => "text/markdown", "content" => content} = object)
359 when is_binary(content) do
360 html_content =
361 content
362 |> Earmark.as_html!(%Earmark.Options{renderer: EarmarkRenderer})
363 |> Pleroma.HTML.filter_tags()
364
365 Map.merge(object, %{"content" => html_content, "mediaType" => "text/html"})
366 end
367
368 defp fix_content(object), do: object
369
370 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
371 with true <- id =~ "follows",
372 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
373 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
374 {:ok, activity}
375 else
376 _ -> {:error, nil}
377 end
378 end
379
380 defp mastodon_follow_hack(_, _), do: {:error, nil}
381
382 defp get_follow_activity(follow_object, followed) do
383 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
384 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
385 {:ok, activity}
386 else
387 # Can't find the activity. This might a Mastodon 2.3 "Accept"
388 {:activity, nil} ->
389 mastodon_follow_hack(follow_object, followed)
390
391 _ ->
392 {:error, nil}
393 end
394 end
395
396 # Reduce the object list to find the reported user.
397 defp get_reported(objects) do
398 Enum.reduce_while(objects, nil, fn ap_id, _ ->
399 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
400 {:halt, user}
401 else
402 _ -> {:cont, nil}
403 end
404 end)
405 end
406
407 def handle_incoming(data, options \\ [])
408
409 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
410 # with nil ID.
411 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
412 with context <- data["context"] || Utils.generate_context_id(),
413 content <- data["content"] || "",
414 %User{} = actor <- User.get_cached_by_ap_id(actor),
415 # Reduce the object list to find the reported user.
416 %User{} = account <- get_reported(objects),
417 # Remove the reported user from the object list.
418 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
419 %{
420 actor: actor,
421 context: context,
422 account: account,
423 statuses: statuses,
424 content: content,
425 additional: %{"cc" => [account.ap_id]}
426 }
427 |> ActivityPub.flag()
428 end
429 end
430
431 # disallow objects with bogus IDs
432 def handle_incoming(%{"id" => nil}, _options), do: :error
433 def handle_incoming(%{"id" => ""}, _options), do: :error
434 # length of https:// = 8, should validate better, but good enough for now.
435 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
436 do: :error
437
438 # TODO: validate those with a Ecto scheme
439 # - tags
440 # - emoji
441 def handle_incoming(
442 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
443 options
444 )
445 when objtype in ["Article", "Event", "Note", "Video", "Page", "Question", "Answer", "Audio"] do
446 actor = Containment.get_actor(data)
447
448 data =
449 Map.put(data, "actor", actor)
450 |> fix_addressing
451
452 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
453 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
454 object = fix_object(object, options)
455
456 params = %{
457 to: data["to"],
458 object: object,
459 actor: user,
460 context: object["conversation"],
461 local: false,
462 published: data["published"],
463 additional:
464 Map.take(data, [
465 "cc",
466 "directMessage",
467 "id"
468 ])
469 }
470
471 with {:ok, created_activity} <- ActivityPub.create(params) do
472 reply_depth = (options[:depth] || 0) + 1
473
474 if Federator.allowed_thread_distance?(reply_depth) do
475 for reply_id <- replies(object) do
476 Pleroma.Workers.RemoteFetcherWorker.enqueue("fetch_remote", %{
477 "id" => reply_id,
478 "depth" => reply_depth
479 })
480 end
481 end
482
483 {:ok, created_activity}
484 end
485 else
486 %Activity{} = activity -> {:ok, activity}
487 _e -> :error
488 end
489 end
490
491 def handle_incoming(
492 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
493 options
494 ) do
495 actor = Containment.get_actor(data)
496
497 data =
498 Map.put(data, "actor", actor)
499 |> fix_addressing
500
501 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
502 reply_depth = (options[:depth] || 0) + 1
503 options = Keyword.put(options, :depth, reply_depth)
504 object = fix_object(object, options)
505
506 params = %{
507 to: data["to"],
508 object: object,
509 actor: user,
510 context: nil,
511 local: false,
512 published: data["published"],
513 additional: Map.take(data, ["cc", "id"])
514 }
515
516 ActivityPub.listen(params)
517 else
518 _e -> :error
519 end
520 end
521
522 def handle_incoming(
523 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
524 _options
525 ) do
526 with %User{local: true} = followed <-
527 User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
528 {:ok, %User{} = follower} <-
529 User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
530 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
531 with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
532 {_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
533 {_, false} <- {:user_locked, User.locked?(followed)},
534 {_, {:ok, follower}} <- {:follow, User.follow(follower, followed)},
535 {_, {:ok, _}} <-
536 {:follow_state_update, Utils.update_follow_state_for_all(activity, "accept")},
537 {:ok, _relationship} <-
538 FollowingRelationship.update(follower, followed, :follow_accept) do
539 ActivityPub.accept(%{
540 to: [follower.ap_id],
541 actor: followed,
542 object: data,
543 local: true
544 })
545 else
546 {:user_blocked, true} ->
547 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
548 {:ok, _relationship} = FollowingRelationship.update(follower, followed, :follow_reject)
549
550 ActivityPub.reject(%{
551 to: [follower.ap_id],
552 actor: followed,
553 object: data,
554 local: true
555 })
556
557 {:follow, {:error, _}} ->
558 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
559 {:ok, _relationship} = FollowingRelationship.update(follower, followed, :follow_reject)
560
561 ActivityPub.reject(%{
562 to: [follower.ap_id],
563 actor: followed,
564 object: data,
565 local: true
566 })
567
568 {:user_locked, true} ->
569 {:ok, _relationship} = FollowingRelationship.update(follower, followed, :follow_pending)
570 :noop
571 end
572
573 {:ok, activity}
574 else
575 _e ->
576 :error
577 end
578 end
579
580 def handle_incoming(
581 %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,
582 _options
583 ) do
584 with actor <- Containment.get_actor(data),
585 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
586 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
587 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
588 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
589 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_accept) do
590 User.update_follower_count(followed)
591 User.update_following_count(follower)
592
593 ActivityPub.accept(%{
594 to: follow_activity.data["to"],
595 type: "Accept",
596 actor: followed,
597 object: follow_activity.data["id"],
598 local: false,
599 activity_id: id
600 })
601 else
602 _e ->
603 :error
604 end
605 end
606
607 def handle_incoming(
608 %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
609 _options
610 ) do
611 with actor <- Containment.get_actor(data),
612 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
613 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
614 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
615 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
616 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_reject),
617 {:ok, activity} <-
618 ActivityPub.reject(%{
619 to: follow_activity.data["to"],
620 type: "Reject",
621 actor: followed,
622 object: follow_activity.data["id"],
623 local: false,
624 activity_id: id
625 }) do
626 {:ok, activity}
627 else
628 _e -> :error
629 end
630 end
631
632 @misskey_reactions %{
633 "like" => "👍",
634 "love" => "❤️",
635 "laugh" => "😆",
636 "hmm" => "🤔",
637 "surprise" => "😮",
638 "congrats" => "🎉",
639 "angry" => "💢",
640 "confused" => "😥",
641 "rip" => "😇",
642 "pudding" => "🍮",
643 "star" => "⭐"
644 }
645
646 @doc "Rewrite misskey likes into EmojiReacts"
647 def handle_incoming(
648 %{
649 "type" => "Like",
650 "_misskey_reaction" => reaction
651 } = data,
652 options
653 ) do
654 data
655 |> Map.put("type", "EmojiReact")
656 |> Map.put("content", @misskey_reactions[reaction] || reaction)
657 |> handle_incoming(options)
658 end
659
660 def handle_incoming(%{"type" => type} = data, _options)
661 when type in ["Like", "EmojiReact", "Announce"] do
662 with :ok <- ObjectValidator.fetch_actor_and_object(data),
663 {:ok, activity, _meta} <-
664 Pipeline.common_pipeline(data, local: false) do
665 {:ok, activity}
666 else
667 e -> {:error, e}
668 end
669 end
670
671 def handle_incoming(
672 %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
673 data,
674 _options
675 )
676 when object_type in [
677 "Person",
678 "Application",
679 "Service",
680 "Organization"
681 ] do
682 with %User{ap_id: ^actor_id} = actor <- User.get_cached_by_ap_id(object["id"]) do
683 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
684
685 actor
686 |> User.remote_user_changeset(new_user_data)
687 |> User.update_and_set_cache()
688
689 ActivityPub.update(%{
690 local: false,
691 to: data["to"] || [],
692 cc: data["cc"] || [],
693 object: object,
694 actor: actor_id,
695 activity_id: data["id"]
696 })
697 else
698 e ->
699 Logger.error(e)
700 :error
701 end
702 end
703
704 def handle_incoming(
705 %{"type" => "Delete"} = data,
706 _options
707 ) do
708 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
709 {:ok, activity}
710 else
711 {:error, {:validate_object, _}} = e ->
712 # Check if we have a create activity for this
713 with {:ok, object_id} <- Types.ObjectID.cast(data["object"]),
714 %Activity{data: %{"actor" => actor}} <-
715 Activity.create_by_object_ap_id(object_id) |> Repo.one(),
716 # We have one, insert a tombstone and retry
717 {:ok, tombstone_data, _} <- Builder.tombstone(actor, object_id),
718 {:ok, _tombstone} <- Object.create(tombstone_data) do
719 handle_incoming(data)
720 else
721 _ -> e
722 end
723 end
724 end
725
726 def handle_incoming(
727 %{
728 "type" => "Undo",
729 "object" => %{"type" => "Follow", "object" => followed},
730 "actor" => follower,
731 "id" => id
732 } = _data,
733 _options
734 ) do
735 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
736 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
737 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
738 User.unfollow(follower, followed)
739 {:ok, activity}
740 else
741 _e -> :error
742 end
743 end
744
745 def handle_incoming(
746 %{
747 "type" => "Undo",
748 "object" => %{"type" => type}
749 } = data,
750 _options
751 )
752 when type in ["Like", "EmojiReact", "Announce", "Block"] do
753 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
754 {:ok, activity}
755 end
756 end
757
758 # For Undos that don't have the complete object attached, try to find it in our database.
759 def handle_incoming(
760 %{
761 "type" => "Undo",
762 "object" => object
763 } = activity,
764 options
765 )
766 when is_binary(object) do
767 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
768 activity
769 |> Map.put("object", data)
770 |> handle_incoming(options)
771 else
772 _e -> :error
773 end
774 end
775
776 def handle_incoming(
777 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
778 _options
779 ) do
780 with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
781 {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
782 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
783 User.unfollow(blocker, blocked)
784 User.block(blocker, blocked)
785 {:ok, activity}
786 else
787 _e -> :error
788 end
789 end
790
791 def handle_incoming(
792 %{
793 "type" => "Move",
794 "actor" => origin_actor,
795 "object" => origin_actor,
796 "target" => target_actor
797 },
798 _options
799 ) do
800 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
801 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
802 true <- origin_actor in target_user.also_known_as do
803 ActivityPub.move(origin_user, target_user, false)
804 else
805 _e -> :error
806 end
807 end
808
809 def handle_incoming(_, _), do: :error
810
811 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
812 def get_obj_helper(id, options \\ []) do
813 case Object.normalize(id, true, options) do
814 %Object{} = object -> {:ok, object}
815 _ -> nil
816 end
817 end
818
819 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
820 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
821 ap_id: ap_id
822 })
823 when attributed_to == ap_id do
824 with {:ok, activity} <-
825 handle_incoming(%{
826 "type" => "Create",
827 "to" => data["to"],
828 "cc" => data["cc"],
829 "actor" => attributed_to,
830 "object" => data
831 }) do
832 {:ok, Object.normalize(activity)}
833 else
834 _ -> get_obj_helper(object_id)
835 end
836 end
837
838 def get_embedded_obj_helper(object_id, _) do
839 get_obj_helper(object_id)
840 end
841
842 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
843 with false <- String.starts_with?(in_reply_to, "http"),
844 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
845 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
846 else
847 _e -> object
848 end
849 end
850
851 def set_reply_to_uri(obj), do: obj
852
853 @doc """
854 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
855 Based on Mastodon's ActivityPub::NoteSerializer#replies.
856 """
857 def set_replies(obj_data) do
858 replies_uris =
859 with limit when limit > 0 <-
860 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
861 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
862 object
863 |> Object.self_replies()
864 |> select([o], fragment("?->>'id'", o.data))
865 |> limit(^limit)
866 |> Repo.all()
867 else
868 _ -> []
869 end
870
871 set_replies(obj_data, replies_uris)
872 end
873
874 defp set_replies(obj, []) do
875 obj
876 end
877
878 defp set_replies(obj, replies_uris) do
879 replies_collection = %{
880 "type" => "Collection",
881 "items" => replies_uris
882 }
883
884 Map.merge(obj, %{"replies" => replies_collection})
885 end
886
887 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
888 items
889 end
890
891 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
892 items
893 end
894
895 def replies(_), do: []
896
897 # Prepares the object of an outgoing create activity.
898 def prepare_object(object) do
899 object
900 |> set_sensitive
901 |> add_hashtags
902 |> add_mention_tags
903 |> add_emoji_tags
904 |> add_attributed_to
905 |> prepare_attachments
906 |> set_conversation
907 |> set_reply_to_uri
908 |> set_replies
909 |> strip_internal_fields
910 |> strip_internal_tags
911 |> set_type
912 end
913
914 # @doc
915 # """
916 # internal -> Mastodon
917 # """
918
919 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
920 when activity_type in ["Create", "Listen"] do
921 object =
922 object_id
923 |> Object.normalize()
924 |> Map.get(:data)
925 |> prepare_object
926
927 data =
928 data
929 |> Map.put("object", object)
930 |> Map.merge(Utils.make_json_ld_header())
931 |> Map.delete("bcc")
932
933 {:ok, data}
934 end
935
936 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
937 object =
938 object_id
939 |> Object.normalize()
940
941 data =
942 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
943 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
944 else
945 data |> maybe_fix_object_url
946 end
947
948 data =
949 data
950 |> strip_internal_fields
951 |> Map.merge(Utils.make_json_ld_header())
952 |> Map.delete("bcc")
953
954 {:ok, data}
955 end
956
957 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
958 # because of course it does.
959 def prepare_outgoing(%{"type" => "Accept"} = data) do
960 with follow_activity <- Activity.normalize(data["object"]) do
961 object = %{
962 "actor" => follow_activity.actor,
963 "object" => follow_activity.data["object"],
964 "id" => follow_activity.data["id"],
965 "type" => "Follow"
966 }
967
968 data =
969 data
970 |> Map.put("object", object)
971 |> Map.merge(Utils.make_json_ld_header())
972
973 {:ok, data}
974 end
975 end
976
977 def prepare_outgoing(%{"type" => "Reject"} = data) do
978 with follow_activity <- Activity.normalize(data["object"]) do
979 object = %{
980 "actor" => follow_activity.actor,
981 "object" => follow_activity.data["object"],
982 "id" => follow_activity.data["id"],
983 "type" => "Follow"
984 }
985
986 data =
987 data
988 |> Map.put("object", object)
989 |> Map.merge(Utils.make_json_ld_header())
990
991 {:ok, data}
992 end
993 end
994
995 def prepare_outgoing(%{"type" => _type} = data) do
996 data =
997 data
998 |> strip_internal_fields
999 |> maybe_fix_object_url
1000 |> Map.merge(Utils.make_json_ld_header())
1001
1002 {:ok, data}
1003 end
1004
1005 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
1006 with false <- String.starts_with?(object, "http"),
1007 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
1008 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
1009 relative_object do
1010 Map.put(data, "object", external_url)
1011 else
1012 {:fetch, e} ->
1013 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
1014 data
1015
1016 _ ->
1017 data
1018 end
1019 end
1020
1021 def maybe_fix_object_url(data), do: data
1022
1023 def add_hashtags(object) do
1024 tags =
1025 (object["tag"] || [])
1026 |> Enum.map(fn
1027 # Expand internal representation tags into AS2 tags.
1028 tag when is_binary(tag) ->
1029 %{
1030 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
1031 "name" => "##{tag}",
1032 "type" => "Hashtag"
1033 }
1034
1035 # Do not process tags which are already AS2 tag objects.
1036 tag when is_map(tag) ->
1037 tag
1038 end)
1039
1040 Map.put(object, "tag", tags)
1041 end
1042
1043 # TODO These should be added on our side on insertion, it doesn't make much
1044 # sense to regenerate these all the time
1045 def add_mention_tags(object) do
1046 to = object["to"] || []
1047 cc = object["cc"] || []
1048 mentioned = User.get_users_from_set(to ++ cc, local_only: false)
1049
1050 mentions = Enum.map(mentioned, &build_mention_tag/1)
1051
1052 tags = object["tag"] || []
1053 Map.put(object, "tag", tags ++ mentions)
1054 end
1055
1056 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
1057 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
1058 end
1059
1060 def take_emoji_tags(%User{emoji: emoji}) do
1061 emoji
1062 |> Map.to_list()
1063 |> Enum.map(&build_emoji_tag/1)
1064 end
1065
1066 # TODO: we should probably send mtime instead of unix epoch time for updated
1067 def add_emoji_tags(%{"emoji" => emoji} = object) do
1068 tags = object["tag"] || []
1069
1070 out = Enum.map(emoji, &build_emoji_tag/1)
1071
1072 Map.put(object, "tag", tags ++ out)
1073 end
1074
1075 def add_emoji_tags(object), do: object
1076
1077 defp build_emoji_tag({name, url}) do
1078 %{
1079 "icon" => %{"url" => url, "type" => "Image"},
1080 "name" => ":" <> name <> ":",
1081 "type" => "Emoji",
1082 "updated" => "1970-01-01T00:00:00Z",
1083 "id" => url
1084 }
1085 end
1086
1087 def set_conversation(object) do
1088 Map.put(object, "conversation", object["context"])
1089 end
1090
1091 def set_sensitive(%{"sensitive" => true} = object) do
1092 object
1093 end
1094
1095 def set_sensitive(object) do
1096 tags = object["tag"] || []
1097 Map.put(object, "sensitive", "nsfw" in tags)
1098 end
1099
1100 def set_type(%{"type" => "Answer"} = object) do
1101 Map.put(object, "type", "Note")
1102 end
1103
1104 def set_type(object), do: object
1105
1106 def add_attributed_to(object) do
1107 attributed_to = object["attributedTo"] || object["actor"]
1108 Map.put(object, "attributedTo", attributed_to)
1109 end
1110
1111 def prepare_attachments(object) do
1112 attachments =
1113 object
1114 |> Map.get("attachment", [])
1115 |> Enum.map(fn data ->
1116 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
1117
1118 %{
1119 "url" => href,
1120 "mediaType" => media_type,
1121 "name" => data["name"],
1122 "type" => "Document"
1123 }
1124 end)
1125
1126 Map.put(object, "attachment", attachments)
1127 end
1128
1129 def strip_internal_fields(object) do
1130 Map.drop(object, Pleroma.Constants.object_internal_fields())
1131 end
1132
1133 defp strip_internal_tags(%{"tag" => tags} = object) do
1134 tags = Enum.filter(tags, fn x -> is_map(x) end)
1135
1136 Map.put(object, "tag", tags)
1137 end
1138
1139 defp strip_internal_tags(object), do: object
1140
1141 def perform(:user_upgrade, user) do
1142 # we pass a fake user so that the followers collection is stripped away
1143 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1144
1145 from(
1146 a in Activity,
1147 where: ^old_follower_address in a.recipients,
1148 update: [
1149 set: [
1150 recipients:
1151 fragment(
1152 "array_replace(?,?,?)",
1153 a.recipients,
1154 ^old_follower_address,
1155 ^user.follower_address
1156 )
1157 ]
1158 ]
1159 )
1160 |> Repo.update_all([])
1161 end
1162
1163 def upgrade_user_from_ap_id(ap_id) do
1164 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1165 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1166 {:ok, user} <- update_user(user, data) do
1167 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1168 {:ok, user}
1169 else
1170 %User{} = user -> {:ok, user}
1171 e -> e
1172 end
1173 end
1174
1175 defp update_user(user, data) do
1176 user
1177 |> User.remote_user_changeset(data)
1178 |> User.update_and_set_cache()
1179 end
1180
1181 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1182 Map.put(data, "url", url["href"])
1183 end
1184
1185 def maybe_fix_user_url(data), do: data
1186
1187 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)
1188 end