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