ac1d4c2223d63c187a606d5b9fce62b8ce09e789
[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.Notification
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.Utils
12
13 def handle(object, meta \\ [])
14
15 # Tasks this handles:
16 # - Add like to object
17 # - Set up notification
18 def handle(%{data: %{"type" => "Like"}} = object, meta) do
19 {:ok, result} =
20 Pleroma.Repo.transaction(fn ->
21 liked_object = Object.get_by_ap_id(object.data["object"])
22 Utils.add_like_to_object(object, liked_object)
23
24 Notification.create_notifications(object)
25
26 {:ok, object, meta}
27 end)
28
29 result
30 end
31
32 # Tasks this handles:
33 # - Delete create activity
34 # - Replace object with Tombstone
35 # - Set up notification
36 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
37 deleted_object =
38 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
39
40 result =
41 case deleted_object do
42 %Object{} ->
43 with {:ok, _, _} <- Object.delete(deleted_object) do
44 :ok
45 end
46
47 %User{} ->
48 with {:ok, _} <- User.delete(deleted_object) do
49 :ok
50 end
51 end
52
53 if result == :ok do
54 Notification.create_notifications(object)
55 {:ok, object, meta}
56 else
57 {:error, result}
58 end
59 end
60
61 # Nothing to do
62 def handle(object, meta) do
63 {:ok, object, meta}
64 end
65 end