Announcements: Prevent race condition.
authorlain <lain@soykaf.club>
Fri, 24 Apr 2020 12:37:53 +0000 (14:37 +0200)
committerlain <lain@soykaf.club>
Fri, 24 Apr 2020 12:37:53 +0000 (14:37 +0200)
lib/pleroma/web/activity_pub/activity_pub.ex
test/web/common_api/common_api_test.exs

index c67b3335d39747efb30afd98a3a72cdb96844112..4cce4f13c1505c492202b9d4b7743f21411129b7 100644 (file)
@@ -438,6 +438,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
   defp do_announce(user, object, activity_id, local, public) do
     with true <- is_announceable?(object, user, public),
+         object <- Object.get_by_id(object.id),
          announce_data <- make_announce_data(user, object, activity_id, public),
          {:ok, activity} <- insert(announce_data, local),
          {:ok, object} <- add_announce_to_object(activity, object),
index e87193c83bbcd6f7e6910a0d958a770a428d5da0..1758662b0c69b3caccd8289a33e4a916f8413dde 100644 (file)
@@ -48,6 +48,33 @@ defmodule Pleroma.Web.CommonAPITest do
     assert object.data["like_count"] == 20
   end
 
+  test "repeating race condition" do
+    user = insert(:user)
+    users_serial = insert_list(10, :user)
+    users = insert_list(10, :user)
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "."})
+
+    users_serial
+    |> Enum.map(fn user ->
+      CommonAPI.repeat(activity.id, user)
+    end)
+
+    object = Object.get_by_ap_id(activity.data["object"])
+    assert object.data["announcement_count"] == 10
+
+    users
+    |> Enum.map(fn user ->
+      Task.async(fn ->
+        CommonAPI.repeat(activity.id, user)
+      end)
+    end)
+    |> Enum.map(&Task.await/1)
+
+    object = Object.get_by_ap_id(activity.data["object"])
+    assert object.data["announcement_count"] == 20
+  end
+
   test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
     user = insert(:user)
     {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})