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