1 defmodule Pleroma.Web.ActivityPub.SideEffects do
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
9 alias Pleroma.Notification
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.ActivityPub.Pipeline
15 alias Pleroma.Web.ActivityPub.Utils
17 def handle(object, meta \\ [])
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)
26 Notification.create_notifications(object)
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)
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
53 Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
56 case deleted_object do
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)
62 {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
64 if in_reply_to = deleted_object.data["inReplyTo"] do
65 Object.decrease_replies_count(in_reply_to)
68 ActivityPub.stream_out(object)
69 ActivityPub.stream_out_participations(deleted_object, user)
74 with {:ok, _} <- User.delete(deleted_object) do
80 Notification.create_notifications(object)
88 def handle(object, meta) do
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"]))
97 [[actor, recipient], [recipient, actor]]
98 |> Enum.each(fn [user, other_user] ->
100 Chat.bump_or_create(user.id, other_user.ap_id)
109 def handle_object_creation(object) do