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)
31 # - Add announce to object
32 # - Set up notification
33 # - Stream out the announce
34 def handle(%{data: %{"type" => "Announce"}} = object, meta) do
35 announced_object = Object.get_by_ap_id(object.data["object"])
36 user = User.get_cached_by_ap_id(object.data["actor"])
38 Utils.add_announce_to_object(object, announced_object)
40 if !User.is_internal_user?(user) do
41 Notification.create_notifications(object)
42 ActivityPub.stream_out(object)
48 def handle(%{data: %{"type" => "Undo", "object" => undone_object}} = object, meta) do
49 with undone_object <- Activity.get_by_ap_id(undone_object),
50 :ok <- handle_undoing(undone_object) do
56 # - Add reaction to object
57 # - Set up notification
58 def handle(%{data: %{"type" => "EmojiReact"}} = object, meta) do
59 reacted_object = Object.get_by_ap_id(object.data["object"])
60 Utils.add_emoji_reaction_to_object(object, reacted_object)
62 Notification.create_notifications(object)
68 # - Delete and unpins the create activity
69 # - Replace object with Tombstone
70 # - Set up notification
71 # - Reduce the user note count
72 # - Reduce the reply count
73 # - Stream out the activity
74 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
76 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
79 case deleted_object do
81 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
82 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
83 User.remove_pinnned_activity(user, activity)
85 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
87 if in_reply_to = deleted_object.data["inReplyTo"] do
88 Object.decrease_replies_count(in_reply_to)
91 ActivityPub.stream_out(object)
92 ActivityPub.stream_out_participations(deleted_object, user)
97 with {:ok, _} <- User.delete(deleted_object) do
103 Notification.create_notifications(object)
111 def handle(object, meta) do
115 def handle_undoing(%{data: %{"type" => "Like"}} = object) do
116 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
117 {:ok, _} <- Utils.remove_like_from_object(object, liked_object),
118 {:ok, _} <- Repo.delete(object) do
123 def handle_undoing(%{data: %{"type" => "EmojiReact"}} = object) do
124 with %Object{} = reacted_object <- Object.get_by_ap_id(object.data["object"]),
125 {:ok, _} <- Utils.remove_emoji_reaction_from_object(object, reacted_object),
126 {:ok, _} <- Repo.delete(object) do
131 def handle_undoing(%{data: %{"type" => "Announce"}} = object) do
132 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
133 {:ok, _} <- Utils.remove_announce_from_object(object, liked_object),
134 {:ok, _} <- Repo.delete(object) do
140 %{data: %{"type" => "Block", "actor" => blocker, "object" => blocked}} = object
142 with %User{} = blocker <- User.get_cached_by_ap_id(blocker),
143 %User{} = blocked <- User.get_cached_by_ap_id(blocked),
144 {:ok, _} <- User.unblock(blocker, blocked),
145 {:ok, _} <- Repo.delete(object) do
150 def handle_undoing(object), do: {:error, ["don't know how to handle", object]}