Undoing: Move undoing reactions to the pipeline everywhere.
[akkoma] / lib / pleroma / web / activity_pub / side_effects.ex
1 defmodule Pleroma.Web.ActivityPub.SideEffects do
2 @moduledoc """
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
6 collection, and so on.
7 """
8 alias Pleroma.Activity
9 alias Pleroma.Notification
10 alias Pleroma.Object
11 alias Pleroma.Repo
12 alias Pleroma.Web.ActivityPub.Utils
13
14 def handle(object, meta \\ [])
15
16 # Tasks this handles:
17 # - Add like to object
18 # - Set up notification
19 def handle(%{data: %{"type" => "Like"}} = object, meta) do
20 liked_object = Object.get_by_ap_id(object.data["object"])
21 Utils.add_like_to_object(object, liked_object)
22
23 Notification.create_notifications(object)
24
25 {:ok, object, meta}
26 end
27
28 def handle(%{data: %{"type" => "Undo", "object" => undone_object}} = object, meta) do
29 with undone_object <- Activity.get_by_ap_id(undone_object),
30 :ok <- handle_undoing(undone_object) do
31 {:ok, object, meta}
32 end
33 end
34
35 # Nothing to do
36 def handle(object, meta) do
37 {:ok, object, meta}
38 end
39
40 def handle_undoing(%{data: %{"type" => "Like"}} = object) do
41 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
42 {:ok, _} <- Utils.remove_like_from_object(object, liked_object),
43 {:ok, _} <- Repo.delete(object) do
44 :ok
45 end
46 end
47
48 def handle_undoing(%{data: %{"type" => "EmojiReact"}} = object) do
49 with %Object{} = reacted_object <- Object.get_by_ap_id(object.data["object"]),
50 {:ok, _} <- Utils.remove_emoji_reaction_from_object(object, reacted_object),
51 {:ok, _} <- Repo.delete(object) do
52 :ok
53 end
54 end
55
56 def handle_undoing(object), do: {:error, ["don't know how to handle", object]}
57 end