Broadcast deleted activity id on deletion to conform to MastoAPI streamig spec
authoreugenijm <eugenijm@protonmail.com>
Sat, 9 Mar 2019 11:12:15 +0000 (14:12 +0300)
committereugenijm <eugenijm@protonmail.com>
Mon, 11 Mar 2019 06:02:03 +0000 (09:02 +0300)
lib/pleroma/activity.ex
lib/pleroma/object.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
lib/pleroma/web/streamer.ex
test/web/streamer_test.exs

index 66854dc2dd2920208261f4da1849420bd60c874a..18d5b70deb2425aeb40c384948dc0dfc4d526ad7 100644 (file)
@@ -107,6 +107,18 @@ defmodule Pleroma.Activity do
 
   def get_in_reply_to_activity(_), do: nil
 
+  def delete_by_ap_id(id) when is_binary(id) do
+    by_object_ap_id(id)
+    |> Repo.delete_all(returning: true)
+    |> elem(1)
+    |> Enum.find(fn
+      %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
+      _ -> nil
+    end)
+  end
+
+  def delete_by_ap_id(_), do: nil
+
   for {ap_type, type} <- @mastodon_notification_types do
     def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
       do: unquote(type)
index 5f1fc801b592d70da0534fdb9defd34e99880758..7510a09bbe27777b9729c3bd3ba7634278bba0c0 100644 (file)
@@ -86,9 +86,9 @@ defmodule Pleroma.Object do
 
   def delete(%Object{data: %{"id" => id}} = object) do
     with {:ok, _obj} = swap_object_with_tombstone(object),
-         Repo.delete_all(Activity.by_object_ap_id(id)),
+         deleted_activity = Activity.delete_by_ap_id(id),
          {:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do
-      {:ok, object}
+      {:ok, object, deleted_activity}
     end
   end
 
index adb42b9abe6c2c2f5b6e2a16627a88fdac570285..2f11f8984a1063db622dd96ace6a9dbb75576459 100644 (file)
@@ -311,14 +311,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     user = User.get_cached_by_ap_id(actor)
     to = object.data["to"] || [] ++ object.data["cc"] || []
 
-    data = %{
-      "type" => "Delete",
-      "actor" => actor,
-      "object" => id,
-      "to" => to
-    }
-
-    with {:ok, _} <- Object.delete(object),
+    with {:ok, object, activity} <- Object.delete(object),
+         data <- %{
+           "type" => "Delete",
+           "actor" => actor,
+           "object" => id,
+           "to" => to,
+           "deleted_activity_id" => activity && activity.id
+         },
          {:ok, activity} <- insert(data, local),
          # Changing note count prior to enqueuing federation task in order to avoid race conditions on updating user.info
          {:ok, _actor} <- decrease_note_count_if_public(user, object),
index 88007aa162b35654738259c787e06335d0640c85..27d223a3e3bcb78bef46b8ae9b548c642192bf16 100644 (file)
@@ -736,6 +736,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   def prepare_outgoing(%{"type" => _type} = data) do
     data =
       data
+      |> strip_internal_fields
       |> maybe_fix_object_url
       |> Map.merge(Utils.make_json_ld_header())
 
@@ -870,7 +871,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
       "announcements",
       "announcement_count",
       "emoji",
-      "context_id"
+      "context_id",
+      "deleted_activity_id"
     ])
   end
 
index 27e8020f4ce198f005ada07b2cdeddae51452805..ad888c36151675111f4326e950fd91a19adf0a4c 100644 (file)
@@ -211,15 +211,19 @@ defmodule Pleroma.Web.Streamer do
     end)
   end
 
-  def push_to_socket(topics, topic, %Activity{id: id, data: %{"type" => "Delete"}}) do
+  def push_to_socket(topics, topic, %Activity{
+        data: %{"type" => "Delete", "deleted_activity_id" => deleted_activity_id}
+      }) do
     Enum.each(topics[topic] || [], fn socket ->
       send(
         socket.transport_pid,
-        {:text, %{event: "delete", payload: to_string(id)} |> Jason.encode!()}
+        {:text, %{event: "delete", payload: to_string(deleted_activity_id)} |> Jason.encode!()}
       )
     end)
   end
 
+  def push_to_socket(_topics, _topic, %Activity{data: %{"type" => "Delete"}}), do: :noop
+
   def push_to_socket(topics, topic, item) do
     Enum.each(topics[topic] || [], fn socket ->
       # Get the current user so we have up-to-date blocks etc.
index 16d7b9c24d7d7351a6f0f5435720a05a64b66ad6..a0969e1d7c49c03cc4aa0ec7c39b12778ae06934 100644 (file)
@@ -39,7 +39,15 @@ defmodule Pleroma.Web.StreamerTest do
 
     task =
       Task.async(fn ->
-        assert_receive {:text, _}, 4_000
+        expected_event =
+          %{
+            "event" => "delete",
+            "payload" => activity.id
+          }
+          |> Jason.encode!()
+
+        assert_receive {:text, received_event}, 4_000
+        assert received_event == expected_event
       end)
 
     fake_socket = %{