Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[akkoma] / lib / pleroma / web / activity_pub / side_effects.ex
index 5981e754567e0c098340a1c77395b4402c87dc1f..ebe3071b08fc55ffe48dd5225ed950a517654489 100644 (file)
@@ -5,8 +5,10 @@ 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.Notification
   alias Pleroma.Object
+  alias Pleroma.User
   alias Pleroma.Web.ActivityPub.Utils
 
   def handle(object, meta \\ [])
@@ -28,8 +30,37 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
     result
   end
 
+  def handle(%{data: %{"type" => "Create", "object" => object_id}} = activity, meta) do
+    object = Object.get_by_ap_id(object_id)
+
+    {:ok, _object} = handle_object_creation(object)
+
+    Notification.create_notifications(activity)
+
+    {:ok, activity, meta}
+  end
+
   # Nothing to do
   def handle(object, meta) do
     {:ok, object, meta}
   end
+
+  def handle_object_creation(%{data: %{"type" => "ChatMessage"}} = object) do
+    actor = User.get_cached_by_ap_id(object.data["actor"])
+    recipient = User.get_cached_by_ap_id(hd(object.data["to"]))
+
+    [[actor, recipient], [recipient, actor]]
+    |> Enum.each(fn [user, other_user] ->
+      if user.local do
+        Chat.bump_or_create(user.id, other_user.ap_id)
+      end
+    end)
+
+    {:ok, object}
+  end
+
+  # Nothing to do
+  def handle_object_creation(object) do
+    {:ok, object}
+  end
 end