Privacy option affects all push notifications, not just Direct Messages
[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 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 # Tasks this handles:
29 # - Delete and unpins the create activity
30 # - Replace object with Tombstone
31 # - Set up notification
32 # - Reduce the user note count
33 # - Reduce the reply count
34 # - Stream out the activity
35 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
36 deleted_object =
37 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
38
39 result =
40 case deleted_object do
41 %Object{} ->
42 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
43 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
44 User.remove_pinnned_activity(user, activity)
45
46 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
47
48 if in_reply_to = deleted_object.data["inReplyTo"] do
49 Object.decrease_replies_count(in_reply_to)
50 end
51
52 ActivityPub.stream_out(object)
53 ActivityPub.stream_out_participations(deleted_object, user)
54 :ok
55 end
56
57 %User{} ->
58 with {:ok, _} <- User.delete(deleted_object) do
59 :ok
60 end
61 end
62
63 if result == :ok do
64 Notification.create_notifications(object)
65 {:ok, object, meta}
66 else
67 {:error, result}
68 end
69 end
70
71 # Nothing to do
72 def handle(object, meta) do
73 {:ok, object, meta}
74 end
75 end