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