SideEffects: Run in transaction.
[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.Web.ActivityPub.Utils
11
12 def handle(object, meta \\ [])
13
14 # Tasks this handles:
15 # - Add like to object
16 # - Set up notification
17 def handle(%{data: %{"type" => "Like"}} = object, meta) do
18 Pleroma.Repo.transaction(fn ->
19 liked_object = Object.get_by_ap_id(object.data["object"])
20 Utils.add_like_to_object(object, liked_object)
21
22 Notification.create_notifications(object)
23
24 {:ok, object, meta}
25 end)
26 |> (fn {:ok, res} -> res end).()
27 end
28
29 # Nothing to do
30 def handle(object, meta) do
31 {:ok, object, meta}
32 end
33 end