Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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.Chat
10 alias Pleroma.Notification
11 alias Pleroma.Object
12 alias Pleroma.Repo
13 alias Pleroma.User
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Pipeline
16 alias Pleroma.Web.ActivityPub.Utils
17
18 def handle(object, meta \\ [])
19
20 # Tasks this handles:
21 # - Add like to object
22 # - Set up notification
23 def handle(%{data: %{"type" => "Like"}} = object, meta) do
24 liked_object = Object.get_by_ap_id(object.data["object"])
25 Utils.add_like_to_object(object, liked_object)
26
27 Notification.create_notifications(object)
28
29 {:ok, object, meta}
30 end
31
32 # Tasks this handles
33 # - Actually create object
34 # - Rollback if we couldn't create it
35 # - Set up notifications
36 def handle(%{data: %{"type" => "Create"}} = activity, meta) do
37 with {:ok, _object, _meta} <- handle_object_creation(meta[:object_data], meta) do
38 Notification.create_notifications(activity)
39 {:ok, activity, meta}
40 else
41 e -> Repo.rollback(e)
42 end
43 end
44
45 # Tasks this handles:
46 # - Add announce to object
47 # - Set up notification
48 # - Stream out the announce
49 def handle(%{data: %{"type" => "Announce"}} = object, meta) do
50 announced_object = Object.get_by_ap_id(object.data["object"])
51
52 Utils.add_announce_to_object(object, announced_object)
53
54 Notification.create_notifications(object)
55 ActivityPub.stream_out(object)
56
57 {:ok, object, meta}
58 end
59
60 def handle(%{data: %{"type" => "Undo", "object" => undone_object}} = object, meta) do
61 with undone_object <- Activity.get_by_ap_id(undone_object),
62 :ok <- handle_undoing(undone_object) do
63 {:ok, object, meta}
64 end
65 end
66
67 # Tasks this handles:
68 # - Add reaction to object
69 # - Set up notification
70 def handle(%{data: %{"type" => "EmojiReact"}} = object, meta) do
71 reacted_object = Object.get_by_ap_id(object.data["object"])
72 Utils.add_emoji_reaction_to_object(object, reacted_object)
73
74 Notification.create_notifications(object)
75
76 {:ok, object, meta}
77 end
78
79 # Tasks this handles:
80 # - Delete and unpins the create activity
81 # - Replace object with Tombstone
82 # - Set up notification
83 # - Reduce the user note count
84 # - Reduce the reply count
85 # - Stream out the activity
86 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
87 deleted_object =
88 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
89
90 result =
91 case deleted_object do
92 %Object{} ->
93 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
94 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
95 User.remove_pinnned_activity(user, activity)
96
97 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
98
99 if in_reply_to = deleted_object.data["inReplyTo"] do
100 Object.decrease_replies_count(in_reply_to)
101 end
102
103 ActivityPub.stream_out(object)
104 ActivityPub.stream_out_participations(deleted_object, user)
105 :ok
106 end
107
108 %User{} ->
109 with {:ok, _} <- User.delete(deleted_object) do
110 :ok
111 end
112 end
113
114 if result == :ok do
115 Notification.create_notifications(object)
116 {:ok, object, meta}
117 else
118 {:error, result}
119 end
120 end
121
122 # Nothing to do
123 def handle(object, meta) do
124 {:ok, object, meta}
125 end
126
127 def handle_object_creation(%{"type" => "ChatMessage"} = object, meta) do
128 with {:ok, object, meta} <- Pipeline.common_pipeline(object, meta) do
129 actor = User.get_cached_by_ap_id(object.data["actor"])
130 recipient = User.get_cached_by_ap_id(hd(object.data["to"]))
131
132 [[actor, recipient], [recipient, actor]]
133 |> Enum.each(fn [user, other_user] ->
134 if user.local do
135 if user.ap_id == actor.ap_id do
136 Chat.get_or_create(user.id, other_user.ap_id)
137 else
138 Chat.bump_or_create(user.id, other_user.ap_id)
139 end
140 end
141 end)
142
143 {:ok, object, meta}
144 end
145 end
146
147 # Nothing to do
148 def handle_object_creation(object) do
149 {:ok, object}
150 end
151
152 def handle_undoing(%{data: %{"type" => "Like"}} = object) do
153 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
154 {:ok, _} <- Utils.remove_like_from_object(object, liked_object),
155 {:ok, _} <- Repo.delete(object) do
156 :ok
157 end
158 end
159
160 def handle_undoing(%{data: %{"type" => "EmojiReact"}} = object) do
161 with %Object{} = reacted_object <- Object.get_by_ap_id(object.data["object"]),
162 {:ok, _} <- Utils.remove_emoji_reaction_from_object(object, reacted_object),
163 {:ok, _} <- Repo.delete(object) do
164 :ok
165 end
166 end
167
168 def handle_undoing(%{data: %{"type" => "Announce"}} = object) do
169 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
170 {:ok, _} <- Utils.remove_announce_from_object(object, liked_object),
171 {:ok, _} <- Repo.delete(object) do
172 :ok
173 end
174 end
175
176 def handle_undoing(
177 %{data: %{"type" => "Block", "actor" => blocker, "object" => blocked}} = object
178 ) do
179 with %User{} = blocker <- User.get_cached_by_ap_id(blocker),
180 %User{} = blocked <- User.get_cached_by_ap_id(blocked),
181 {:ok, _} <- User.unblock(blocker, blocked),
182 {:ok, _} <- Repo.delete(object) do
183 :ok
184 end
185 end
186
187 def handle_undoing(object), do: {:error, ["don't know how to handle", object]}
188 end