d260e00698428f58faf04b78e3b1068c3a8fd23f
[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 alias Pleroma.Web.ActivityPub.ActivityPub
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 {:ok, result} =
21 Pleroma.Repo.transaction(fn ->
22 liked_object = Object.get_by_ap_id(object.data["object"])
23 Utils.add_like_to_object(object, liked_object)
24
25 Notification.create_notifications(object)
26
27 {:ok, object, meta}
28 end)
29
30 result
31 end
32
33 # Tasks this handles:
34 # - Delete and unpins the create activity
35 # - Replace object with Tombstone
36 # - Set up notification
37 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
38 deleted_object =
39 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
40
41 result =
42 case deleted_object do
43 %Object{} ->
44 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
45 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
46 User.remove_pinnned_activity(user, activity)
47
48 ActivityPub.stream_out(object)
49 ActivityPub.stream_out_participations(deleted_object, user)
50 :ok
51 end
52
53 %User{} ->
54 with {:ok, _} <- User.delete(deleted_object) do
55 :ok
56 end
57 end
58
59 if result == :ok do
60 Notification.create_notifications(object)
61 {:ok, object, meta}
62 else
63 {:error, result}
64 end
65 end
66
67 # Nothing to do
68 def handle(object, meta) do
69 {:ok, object, meta}
70 end
71 end