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