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
10 alias Pleroma.Notification
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Pipeline
16 alias Pleroma.Web.ActivityPub.Utils
18 def handle(object, meta \\ [])
21 # - Add like to object
22 # - Set up notification
23 def handle(%{data: %{"type" => "Like"}} = object, meta) do
24 liked_object = Object.get_by_ap_id(object.data["object"])
25 Utils.add_like_to_object(object, liked_object)
27 Notification.create_notifications(object)
33 # - Actually create object
34 # - Rollback if we couldn't create it
35 # - Set up notifications
36 def handle(%{data: %{"type" => "Create"}} = activity, meta) do
37 with {:ok, _object, _meta} <- handle_object_creation(meta[:object_data], meta) do
38 Notification.create_notifications(activity)
46 # - Add announce to object
47 # - Set up notification
48 # - Stream out the announce
49 def handle(%{data: %{"type" => "Announce"}} = object, meta) do
50 announced_object = Object.get_by_ap_id(object.data["object"])
52 Utils.add_announce_to_object(object, announced_object)
54 Notification.create_notifications(object)
55 ActivityPub.stream_out(object)
60 def handle(%{data: %{"type" => "Undo", "object" => undone_object}} = object, meta) do
61 with undone_object <- Activity.get_by_ap_id(undone_object),
62 :ok <- handle_undoing(undone_object) do
68 # - Add reaction to object
69 # - Set up notification
70 def handle(%{data: %{"type" => "EmojiReact"}} = object, meta) do
71 reacted_object = Object.get_by_ap_id(object.data["object"])
72 Utils.add_emoji_reaction_to_object(object, reacted_object)
74 Notification.create_notifications(object)
80 # - Delete and unpins the create activity
81 # - Replace object with Tombstone
82 # - Set up notification
83 # - Reduce the user note count
84 # - Reduce the reply count
85 # - Stream out the activity
86 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
88 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
91 case deleted_object do
93 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
94 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
95 User.remove_pinnned_activity(user, activity)
97 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
99 if in_reply_to = deleted_object.data["inReplyTo"] do
100 Object.decrease_replies_count(in_reply_to)
103 ActivityPub.stream_out(object)
104 ActivityPub.stream_out_participations(deleted_object, user)
109 with {:ok, _} <- User.delete(deleted_object) do
115 Notification.create_notifications(object)
123 def handle(object, meta) do
127 def handle_object_creation(%{"type" => "ChatMessage"} = object, meta) do
128 with {:ok, object, meta} <- Pipeline.common_pipeline(object, meta) do
129 actor = User.get_cached_by_ap_id(object.data["actor"])
130 recipient = User.get_cached_by_ap_id(hd(object.data["to"]))
132 [[actor, recipient], [recipient, actor]]
133 |> Enum.each(fn [user, other_user] ->
135 if user.ap_id == actor.ap_id do
136 Chat.get_or_create(user.id, other_user.ap_id)
138 Chat.bump_or_create(user.id, other_user.ap_id)
148 def handle_object_creation(object) do
152 def handle_undoing(%{data: %{"type" => "Like"}} = object) do
153 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
154 {:ok, _} <- Utils.remove_like_from_object(object, liked_object),
155 {:ok, _} <- Repo.delete(object) do
160 def handle_undoing(%{data: %{"type" => "EmojiReact"}} = object) do
161 with %Object{} = reacted_object <- Object.get_by_ap_id(object.data["object"]),
162 {:ok, _} <- Utils.remove_emoji_reaction_from_object(object, reacted_object),
163 {:ok, _} <- Repo.delete(object) do
168 def handle_undoing(%{data: %{"type" => "Announce"}} = object) do
169 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
170 {:ok, _} <- Utils.remove_announce_from_object(object, liked_object),
171 {:ok, _} <- Repo.delete(object) do
177 %{data: %{"type" => "Block", "actor" => blocker, "object" => blocked}} = object
179 with %User{} = blocker <- User.get_cached_by_ap_id(blocker),
180 %User{} = blocked <- User.get_cached_by_ap_id(blocked),
181 {:ok, _} <- User.unblock(blocker, blocked),
182 {:ok, _} <- Repo.delete(object) do
187 def handle_undoing(object), do: {:error, ["don't know how to handle", object]}