Merge branch 'docs/storing-remote-media' 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 def handle(%{data: %{"type" => "Undo", "object" => undone_object}} = object, meta) do
31 with undone_object <- Activity.get_by_ap_id(undone_object),
32 :ok <- handle_undoing(undone_object) do
33 {:ok, object, meta}
34 end
35 end
36
37 # Tasks this handles:
38 # - Add reaction to object
39 # - Set up notification
40 def handle(%{data: %{"type" => "EmojiReact"}} = object, meta) do
41 reacted_object = Object.get_by_ap_id(object.data["object"])
42 Utils.add_emoji_reaction_to_object(object, reacted_object)
43
44 Notification.create_notifications(object)
45
46 {:ok, object, meta}
47 end
48
49 # Tasks this handles:
50 # - Delete and unpins the create activity
51 # - Replace object with Tombstone
52 # - Set up notification
53 # - Reduce the user note count
54 # - Reduce the reply count
55 # - Stream out the activity
56 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
57 deleted_object =
58 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
59
60 result =
61 case deleted_object do
62 %Object{} ->
63 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
64 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
65 User.remove_pinnned_activity(user, activity)
66
67 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
68
69 if in_reply_to = deleted_object.data["inReplyTo"] do
70 Object.decrease_replies_count(in_reply_to)
71 end
72
73 ActivityPub.stream_out(object)
74 ActivityPub.stream_out_participations(deleted_object, user)
75 :ok
76 end
77
78 %User{} ->
79 with {:ok, _} <- User.delete(deleted_object) do
80 :ok
81 end
82 end
83
84 if result == :ok do
85 Notification.create_notifications(object)
86 {:ok, object, meta}
87 else
88 {:error, result}
89 end
90 end
91
92 # Nothing to do
93 def handle(object, meta) do
94 {:ok, object, meta}
95 end
96
97 def handle_undoing(%{data: %{"type" => "Like"}} = object) do
98 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
99 {:ok, _} <- Utils.remove_like_from_object(object, liked_object),
100 {:ok, _} <- Repo.delete(object) do
101 :ok
102 end
103 end
104
105 def handle_undoing(%{data: %{"type" => "EmojiReact"}} = object) do
106 with %Object{} = reacted_object <- Object.get_by_ap_id(object.data["object"]),
107 {:ok, _} <- Utils.remove_emoji_reaction_from_object(object, reacted_object),
108 {:ok, _} <- Repo.delete(object) do
109 :ok
110 end
111 end
112
113 def handle_undoing(%{data: %{"type" => "Announce"}} = object) do
114 with %Object{} = liked_object <- Object.get_by_ap_id(object.data["object"]),
115 {:ok, _} <- Utils.remove_announce_from_object(object, liked_object),
116 {:ok, _} <- Repo.delete(object) do
117 :ok
118 end
119 end
120
121 def handle_undoing(
122 %{data: %{"type" => "Block", "actor" => blocker, "object" => blocked}} = object
123 ) do
124 with %User{} = blocker <- User.get_cached_by_ap_id(blocker),
125 %User{} = blocked <- User.get_cached_by_ap_id(blocked),
126 {:ok, _} <- User.unblock(blocker, blocked),
127 {:ok, _} <- Repo.delete(object) do
128 :ok
129 end
130 end
131
132 def handle_undoing(object), do: {:error, ["don't know how to handle", object]}
133 end