Merge branch 'feature/status-by-id-account-view' into 'develop'
[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.Activity
9 alias Pleroma.Notification
10 alias Pleroma.Object
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.ActivityPub.Utils
15
16 def handle(object, meta \\ [])
17
18 # Tasks this handles:
19 # - Add like to object
20 # - Set up notification
21 def handle(%{data: %{"type" => "Like"}} = object, meta) do
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 # Tasks this handles:
31 # - Add announce to object
32 # - Set up notification
33 # - Stream out the announce
34 def handle(%{data: %{"type" => "Announce"}} = object, meta) do
35 announced_object = Object.get_by_ap_id(object.data["object"])
36 user = User.get_cached_by_ap_id(object.data["actor"])
37
38 Utils.add_announce_to_object(object, announced_object)
39
40 if !User.is_internal_user?(user) do
41 Notification.create_notifications(object)
42 ActivityPub.stream_out(object)
43 end
44
45 {:ok, object, meta}
46 end
47
48 def handle(%{data: %{"type" => "Undo", "object" => undone_object}} = object, meta) do
49 with undone_object <- Activity.get_by_ap_id(undone_object),
50 :ok <- handle_undoing(undone_object) do
51 {:ok, object, meta}
52 end
53 end
54
55 # Tasks this handles:
56 # - Add reaction to object
57 # - Set up notification
58 def handle(%{data: %{"type" => "EmojiReact"}} = object, meta) do
59 reacted_object = Object.get_by_ap_id(object.data["object"])
60 Utils.add_emoji_reaction_to_object(object, reacted_object)
61
62 Notification.create_notifications(object)
63
64 {:ok, object, meta}
65 end
66
67 # Tasks this handles:
68 # - Delete and unpins the create activity
69 # - Replace object with Tombstone
70 # - Set up notification
71 # - Reduce the user note count
72 # - Reduce the reply count
73 # - Stream out the activity
74 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
75 deleted_object =
76 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
77
78 result =
79 case deleted_object do
80 %Object{} ->
81 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
82 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
83 User.remove_pinnned_activity(user, activity)
84
85 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
86
87 if in_reply_to = deleted_object.data["inReplyTo"] do
88 Object.decrease_replies_count(in_reply_to)
89 end
90
91 ActivityPub.stream_out(object)
92 ActivityPub.stream_out_participations(deleted_object, user)
93 :ok
94 end
95
96 %User{} ->
97 with {:ok, _} <- User.delete(deleted_object) do
98 :ok
99 end
100 end
101
102 if result == :ok do
103 Notification.create_notifications(object)
104 {:ok, object, meta}
105 else
106 {:error, result}
107 end
108 end
109
110 # Nothing to do
111 def handle(object, meta) do
112 {:ok, object, meta}
113 end
114
115 def handle_undoing(%{data: %{"type" => "Like"}} = object) do
116 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
117 {:ok, _} <- Utils.remove_like_from_object(object, liked_object),
118 {:ok, _} <- Repo.delete(object) do
119 :ok
120 end
121 end
122
123 def handle_undoing(%{data: %{"type" => "EmojiReact"}} = object) do
124 with %Object{} = reacted_object <- Object.get_by_ap_id(object.data["object"]),
125 {:ok, _} <- Utils.remove_emoji_reaction_from_object(object, reacted_object),
126 {:ok, _} <- Repo.delete(object) do
127 :ok
128 end
129 end
130
131 def handle_undoing(%{data: %{"type" => "Announce"}} = object) do
132 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
133 {:ok, _} <- Utils.remove_announce_from_object(object, liked_object),
134 {:ok, _} <- Repo.delete(object) do
135 :ok
136 end
137 end
138
139 def handle_undoing(
140 %{data: %{"type" => "Block", "actor" => blocker, "object" => blocked}} = object
141 ) do
142 with %User{} = blocker <- User.get_cached_by_ap_id(blocker),
143 %User{} = blocked <- User.get_cached_by_ap_id(blocked),
144 {:ok, _} <- User.unblock(blocker, blocked),
145 {:ok, _} <- Repo.delete(object) do
146 :ok
147 end
148 end
149
150 def handle_undoing(object), do: {:error, ["don't know how to handle", object]}
151 end