DeleteValidator: Only allow deletion of certain types.
[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.ActivityPub
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 {: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 # - Reduce the reply count
39 # - Stream out the activity
40 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
41 deleted_object =
42 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
43
44 result =
45 case deleted_object do
46 %Object{} ->
47 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
48 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
49 User.remove_pinnned_activity(user, activity)
50
51 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
52
53 if in_reply_to = deleted_object.data["inReplyTo"] do
54 Object.decrease_replies_count(in_reply_to)
55 end
56
57 ActivityPub.stream_out(object)
58 ActivityPub.stream_out_participations(deleted_object, user)
59 :ok
60 end
61
62 %User{} ->
63 with {:ok, _} <- User.delete(deleted_object) do
64 :ok
65 end
66 end
67
68 if result == :ok do
69 Notification.create_notifications(object)
70 {:ok, object, meta}
71 else
72 {:error, result}
73 end
74 end
75
76 # Nothing to do
77 def handle(object, meta) do
78 {:ok, object, meta}
79 end
80 end