Litepub: Add ChatMessage.
[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.User
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 liked_object = Object.get_by_ap_id(object.data["object"])
21 Utils.add_like_to_object(object, liked_object)
22
23 Notification.create_notifications(object)
24
25 {:ok, object, meta}
26 end
27
28 def handle(%{data: %{"type" => "Create", "object" => object_id}} = activity, meta) do
29 object = Object.get_by_ap_id(object_id)
30
31 {:ok, _object} = handle_object_creation(object)
32
33 Notification.create_notifications(activity)
34
35 {:ok, activity, meta}
36 end
37
38 # Nothing to do
39 def handle(object, meta) do
40 {:ok, object, meta}
41 end
42
43 def handle_object_creation(%{data: %{"type" => "ChatMessage"}} = object) do
44 actor = User.get_cached_by_ap_id(object.data["actor"])
45 recipient = User.get_cached_by_ap_id(hd(object.data["to"]))
46
47 [[actor, recipient], [recipient, actor]]
48 |> Enum.each(fn [user, other_user] ->
49 if user.local do
50 Chat.bump_or_create(user.id, other_user.ap_id)
51 end
52 end)
53
54 {:ok, object}
55 end
56
57 # Nothing to do
58 def handle_object_creation(object) do
59 {:ok, object}
60 end
61 end