1 defmodule Pleroma.Web.ActivityPub.SideEffects do
3 This module looks at an inserted object and executes the side effects that it
4 implies. For example, a `Like` activity will increase the like count on the
5 liked object, a `Follow` activity will add the user to the follower
9 alias Pleroma.Notification
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.ActivityPub.Utils
16 def handle(object, meta \\ [])
19 # - Add like to object
20 # - Set up notification
21 def handle(%{data: %{"type" => "Like"}} = object, meta) do
22 liked_object = Object.get_by_ap_id(object.data["object"])
23 Utils.add_like_to_object(object, liked_object)
25 Notification.create_notifications(object)
30 def handle(%{data: %{"type" => "Undo", "object" => undone_object}} = object, meta) do
31 with undone_object <- Activity.get_by_ap_id(undone_object),
32 :ok <- handle_undoing(undone_object) do
38 # - Add reaction to object
39 # - Set up notification
40 def handle(%{data: %{"type" => "EmojiReact"}} = object, meta) do
41 reacted_object = Object.get_by_ap_id(object.data["object"])
42 Utils.add_emoji_reaction_to_object(object, reacted_object)
44 Notification.create_notifications(object)
50 # - Delete and unpins the create activity
51 # - Replace object with Tombstone
52 # - Set up notification
53 # - Reduce the user note count
54 # - Reduce the reply count
55 # - Stream out the activity
56 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
58 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
61 case deleted_object do
63 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
64 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
65 User.remove_pinnned_activity(user, activity)
67 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
69 if in_reply_to = deleted_object.data["inReplyTo"] do
70 Object.decrease_replies_count(in_reply_to)
73 ActivityPub.stream_out(object)
74 ActivityPub.stream_out_participations(deleted_object, user)
79 with {:ok, _} <- User.delete(deleted_object) do
85 Notification.create_notifications(object)
93 def handle(object, meta) do
97 def handle_undoing(%{data: %{"type" => "Like"}} = object) do
98 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
99 {:ok, _} <- Utils.remove_like_from_object(object, liked_object),
100 {:ok, _} <- Repo.delete(object) do
105 def handle_undoing(%{data: %{"type" => "EmojiReact"}} = object) do
106 with %Object{} = reacted_object <- Object.get_by_ap_id(object.data["object"]),
107 {:ok, _} <- Utils.remove_emoji_reaction_from_object(object, reacted_object),
108 {:ok, _} <- Repo.delete(object) do
113 def handle_undoing(%{data: %{"type" => "Announce"}} = object) do
114 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
115 {:ok, _} <- Utils.remove_announce_from_object(object, liked_object),
116 {:ok, _} <- Repo.delete(object) do
122 %{data: %{"type" => "Block", "actor" => blocker, "object" => blocked}} = object
124 with %User{} = blocker <- User.get_cached_by_ap_id(blocker),
125 %User{} = blocked <- User.get_cached_by_ap_id(blocked),
126 {:ok, _} <- User.unblock(blocker, blocked),
127 {:ok, _} <- Repo.delete(object) do
132 def handle_undoing(object), do: {:error, ["don't know how to handle", object]}