SideEffects: Handle strange deletion case.
authorlain <lain@soykaf.club>
Tue, 11 Aug 2020 08:54:38 +0000 (10:54 +0200)
committerlain <lain@soykaf.club>
Tue, 11 Aug 2020 08:54:38 +0000 (10:54 +0200)
lib/pleroma/web/activity_pub/side_effects.ex
test/web/activity_pub/side_effects_test.exs

index 5104d38eeafbcbda067abfdf5339eee93c75112b..bff7c66290e3086cf93650da5b24c1f6bfa40e50 100644 (file)
@@ -22,6 +22,8 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
   alias Pleroma.Web.Streamer
   alias Pleroma.Workers.BackgroundWorker
 
+  require Logger
+
   def handle(object, meta \\ [])
 
   # Tasks this handle
@@ -217,13 +219,15 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
   # - Stream out the activity
   def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
     deleted_object =
-      Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
+      Object.normalize(deleted_object, false) ||
+        User.get_cached_by_ap_id(deleted_object)
 
     result =
       case deleted_object do
         %Object{} ->
           with {:ok, deleted_object, activity} <- Object.delete(deleted_object),
-               %User{} = user <- User.get_cached_by_ap_id(deleted_object.data["actor"]) do
+               {_, actor} when is_binary(actor) <- {:actor, deleted_object.data["actor"]},
+               %User{} = user <- User.get_cached_by_ap_id(actor) do
             User.remove_pinnned_activity(user, activity)
 
             {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
@@ -237,6 +241,10 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
             ActivityPub.stream_out(object)
             ActivityPub.stream_out_participations(deleted_object, user)
             :ok
+          else
+            {:actor, _} ->
+              Logger.error("The object doesn't have an actor: #{inspect(deleted_object)}")
+              :no_object_actor
           end
 
         %User{} ->
index 4a08eb7ee4c36329423e201b7794db9b242e126d..9efbaad045cf329a2dba81e9fbd60d928c8b4782 100644 (file)
@@ -19,8 +19,9 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
   alias Pleroma.Web.ActivityPub.SideEffects
   alias Pleroma.Web.CommonAPI
 
-  import Pleroma.Factory
+  import ExUnit.CaptureLog
   import Mock
+  import Pleroma.Factory
 
   describe "handle_after_transaction" do
     test "it streams out notifications and streams" do
@@ -221,6 +222,22 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
 
       assert User.get_cached_by_ap_id(user.ap_id).deactivated
     end
+
+    test "it logs issues with objects deletion", %{
+      delete: delete,
+      object: object
+    } do
+      {:ok, object} =
+        object
+        |> Object.change(%{data: Map.delete(object.data, "actor")})
+        |> Repo.update()
+
+      Object.invalid_object_cache(object)
+
+      assert capture_log(fn ->
+               {:error, :no_object_actor} = SideEffects.handle(delete)
+             end) =~ "object doesn't have an actor"
+    end
   end
 
   describe "EmojiReact objects" do