Notifications: Make notifications save their type.
[akkoma] / lib / pleroma / web / activity_pub / side_effects.ex
index 28b5194322cb775170b352a7771a8cbaa2399126..a34bf6a054143dbfba10152f386607192a8ee6b4 100644 (file)
@@ -5,8 +5,8 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
   liked object, a `Follow` activity will add the user to the follower
   collection, and so on.
   """
-  alias Pleroma.Chat
   alias Pleroma.Activity
+  alias Pleroma.Chat
   alias Pleroma.Notification
   alias Pleroma.Object
   alias Pleroma.Repo
@@ -14,6 +14,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.ActivityPub.Pipeline
   alias Pleroma.Web.ActivityPub.Utils
+  alias Pleroma.Web.Streamer
 
   def handle(object, meta \\ [])
 
@@ -42,6 +43,24 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
     end
   end
 
+  # Tasks this handles:
+  # - Add announce to object
+  # - Set up notification
+  # - Stream out the announce
+  def handle(%{data: %{"type" => "Announce"}} = object, meta) do
+    announced_object = Object.get_by_ap_id(object.data["object"])
+    user = User.get_cached_by_ap_id(object.data["actor"])
+
+    Utils.add_announce_to_object(object, announced_object)
+
+    if !User.is_internal_user?(user) do
+      Notification.create_notifications(object)
+      ActivityPub.stream_out(object)
+    end
+
+    {:ok, object, meta}
+  end
+
   def handle(%{data: %{"type" => "Undo", "object" => undone_object}} = object, meta) do
     with undone_object <- Activity.get_by_ap_id(undone_object),
          :ok <- handle_undoing(undone_object) do
@@ -117,10 +136,15 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
       [[actor, recipient], [recipient, actor]]
       |> Enum.each(fn [user, other_user] ->
         if user.local do
-          Chat.bump_or_create(user.id, other_user.ap_id)
+          if user.ap_id == actor.ap_id do
+            Chat.get_or_create(user.id, other_user.ap_id)
+          else
+            Chat.bump_or_create(user.id, other_user.ap_id)
+          end
         end
       end)
 
+      Streamer.stream(["user", "user:pleroma_chat"], object)
       {:ok, object, meta}
     end
   end