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