4fec3a797c8086c9532a446a1552269a4022bffa
[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 # - Reduce the user note count
38 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
39 deleted_object =
40 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
41
42 result =
43 case deleted_object do
44 %Object{} ->
45 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
46 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
47 User.remove_pinnned_activity(user, activity)
48
49 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
50 ActivityPub.stream_out(object)
51 ActivityPub.stream_out_participations(deleted_object, user)
52 :ok
53 end
54
55 %User{} ->
56 with {:ok, _} <- User.delete(deleted_object) do
57 :ok
58 end
59 end
60
61 if result == :ok do
62 Notification.create_notifications(object)
63 {:ok, object, meta}
64 else
65 {:error, result}
66 end
67 end
68
69 # Nothing to do
70 def handle(object, meta) do
71 {:ok, object, meta}
72 end
73 end