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
17 alias Pleroma.Web.Streamer
19 def handle(object, meta \\ [])
22 # - Add like to object
23 # - Set up notification
24 def handle(%{data: %{"type" => "Like"}} = object, meta) do
25 liked_object = Object.get_by_ap_id(object.data["object"])
26 Utils.add_like_to_object(object, liked_object)
28 Notification.create_notifications(object)
34 # - Actually create object
35 # - Rollback if we couldn't create it
36 # - Set up notifications
37 def handle(%{data: %{"type" => "Create"}} = activity, meta) do
38 with {:ok, _object, _meta} <- handle_object_creation(meta[:object_data], meta) do
39 Notification.create_notifications(activity)
47 # - Add announce to object
48 # - Set up notification
49 # - Stream out the announce
50 def handle(%{data: %{"type" => "Announce"}} = object, meta) do
51 announced_object = Object.get_by_ap_id(object.data["object"])
52 user = User.get_cached_by_ap_id(object.data["actor"])
54 Utils.add_announce_to_object(object, announced_object)
56 if !User.is_internal_user?(user) do
57 Notification.create_notifications(object)
58 ActivityPub.stream_out(object)
64 def handle(%{data: %{"type" => "Undo", "object" => undone_object}} = object, meta) do
65 with undone_object <- Activity.get_by_ap_id(undone_object),
66 :ok <- handle_undoing(undone_object) do
72 # - Add reaction to object
73 # - Set up notification
74 def handle(%{data: %{"type" => "EmojiReact"}} = object, meta) do
75 reacted_object = Object.get_by_ap_id(object.data["object"])
76 Utils.add_emoji_reaction_to_object(object, reacted_object)
78 Notification.create_notifications(object)
84 # - Delete and unpins the create activity
85 # - Replace object with Tombstone
86 # - Set up notification
87 # - Reduce the user note count
88 # - Reduce the reply count
89 # - Stream out the activity
90 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
92 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
95 case deleted_object do
97 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
98 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
99 User.remove_pinnned_activity(user, activity)
101 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
103 if in_reply_to = deleted_object.data["inReplyTo"] do
104 Object.decrease_replies_count(in_reply_to)
107 ActivityPub.stream_out(object)
108 ActivityPub.stream_out_participations(deleted_object, user)
113 with {:ok, _} <- User.delete(deleted_object) do
119 Notification.create_notifications(object)
127 def handle(object, meta) do
131 def handle_object_creation(%{"type" => "ChatMessage"} = object, meta) do
132 with {:ok, object, meta} <- Pipeline.common_pipeline(object, meta) do
133 actor = User.get_cached_by_ap_id(object.data["actor"])
134 recipient = User.get_cached_by_ap_id(hd(object.data["to"]))
136 [[actor, recipient], [recipient, actor]]
137 |> Enum.each(fn [user, other_user] ->
139 if user.ap_id == actor.ap_id do
140 Chat.get_or_create(user.id, other_user.ap_id)
142 Chat.bump_or_create(user.id, other_user.ap_id)
147 Streamer.stream(["user", "user:pleroma_chat"], object)
153 def handle_object_creation(object) do
157 def handle_undoing(%{data: %{"type" => "Like"}} = object) do
158 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
159 {:ok, _} <- Utils.remove_like_from_object(object, liked_object),
160 {:ok, _} <- Repo.delete(object) do
165 def handle_undoing(%{data: %{"type" => "EmojiReact"}} = object) do
166 with %Object{} = reacted_object <- Object.get_by_ap_id(object.data["object"]),
167 {:ok, _} <- Utils.remove_emoji_reaction_from_object(object, reacted_object),
168 {:ok, _} <- Repo.delete(object) do
173 def handle_undoing(%{data: %{"type" => "Announce"}} = object) do
174 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
175 {:ok, _} <- Utils.remove_announce_from_object(object, liked_object),
176 {:ok, _} <- Repo.delete(object) do
182 %{data: %{"type" => "Block", "actor" => blocker, "object" => blocked}} = object
184 with %User{} = blocker <- User.get_cached_by_ap_id(blocker),
185 %User{} = blocked <- User.get_cached_by_ap_id(blocked),
186 {:ok, _} <- User.unblock(blocker, blocked),
187 {:ok, _} <- Repo.delete(object) do
192 def handle_undoing(object), do: {:error, ["don't know how to handle", object]}