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.Chat
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.Pipeline
15 alias Pleroma.Web.ActivityPub.Utils
16
17 def handle(object, meta \\ [])
18
19 # Tasks this handles:
20 # - Add like to object
21 # - Set up notification
22 def handle(%{data: %{"type" => "Like"}} = object, meta) do
23 liked_object = Object.get_by_ap_id(object.data["object"])
24 Utils.add_like_to_object(object, liked_object)
25
26 Notification.create_notifications(object)
27
28 {:ok, object, meta}
29 end
30
31 # Tasks this handles
32 # - Actually create object
33 # - Rollback if we couldn't create it
34 # - Set up notifications
35 def handle(%{data: %{"type" => "Create"}} = activity, meta) do
36 with {:ok, _object, _meta} <- handle_object_creation(meta[:object_data], meta) do
37 Notification.create_notifications(activity)
38 {:ok, activity, meta}
39 else
40 e -> Repo.rollback(e)
41 end
42 end
43
44 # Tasks this handles:
45 # - Delete and unpins the create activity
46 # - Replace object with Tombstone
47 # - Set up notification
48 # - Reduce the user note count
49 # - Reduce the reply count
50 # - Stream out the activity
51 def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
52 deleted_object =
53 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
54
55 result =
56 case deleted_object do
57 %Object{} ->
58 with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
59 %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
60 User.remove_pinnned_activity(user, activity)
61
62 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
63
64 if in_reply_to = deleted_object.data["inReplyTo"] do
65 Object.decrease_replies_count(in_reply_to)
66 end
67
68 ActivityPub.stream_out(object)
69 ActivityPub.stream_out_participations(deleted_object, user)
70 :ok
71 end
72
73 %User{} ->
74 with {:ok, _} <- User.delete(deleted_object) do
75 :ok
76 end
77 end
78
79 if result == :ok do
80 Notification.create_notifications(object)
81 {:ok, object, meta}
82 else
83 {:error, result}
84 end
85 end
86
87 # Nothing to do
88 def handle(object, meta) do
89 {:ok, object, meta}
90 end
91
92 def handle_object_creation(%{"type" => "ChatMessage"} = object, meta) do
93 with {:ok, object, meta} <- Pipeline.common_pipeline(object, meta) do
94 actor = User.get_cached_by_ap_id(object.data["actor"])
95 recipient = User.get_cached_by_ap_id(hd(object.data["to"]))
96
97 [[actor, recipient], [recipient, actor]]
98 |> Enum.each(fn [user, other_user] ->
99 if user.local do
100 Chat.bump_or_create(user.id, other_user.ap_id)
101 end
102 end)
103
104 {:ok, object, meta}
105 end
106 end
107
108 # Nothing to do
109 def handle_object_creation(object) do
110 {:ok, object}
111 end
112 end