From f21ec80ea2b0567fc9943830db8dcd9eb6b76c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 1 Dec 2022 00:34:39 +0100 Subject: [PATCH] Fix race condition, update tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/web/activity_pub/side_effects.ex | 3 ++- lib/pleroma/web/common_api.ex | 8 ++++++-- .../controllers/event_controller_test.exs | 14 +++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/pleroma/web/activity_pub/side_effects.ex b/lib/pleroma/web/activity_pub/side_effects.ex index 302d87b284..3ff0e47d01 100644 --- a/lib/pleroma/web/activity_pub/side_effects.ex +++ b/lib/pleroma/web/activity_pub/side_effects.ex @@ -401,7 +401,8 @@ def handle(%{data: %{"type" => "Join"}} = object, meta) do {:ok, _activity, _} = Pipeline.common_pipeline(accept_data, local: true) end - if Object.local?(joined_event) and joined_event.data["joinMode"] != "free" do + if Object.local?(joined_event) and joined_event.data["joinMode"] != "free" and + object.data["actor"] != joined_event.data["actor"] do Utils.update_participation_request_count_in_object(joined_event) end diff --git a/lib/pleroma/web/common_api.ex b/lib/pleroma/web/common_api.ex index f576801d87..cff5b196e6 100644 --- a/lib/pleroma/web/common_api.ex +++ b/lib/pleroma/web/common_api.ex @@ -380,8 +380,12 @@ def accept_join_request(%User{} = user, %User{ap_id: participant_ap_id} = partic with %Activity{} = join_activity <- Utils.get_existing_join(participant_ap_id, event_id), {:ok, accept_data, _} <- Builder.accept(user, join_activity), {:ok, _activity, _} <- Pipeline.common_pipeline(accept_data, local: true), - event <- Object.get_by_ap_id(event_id), - {:ok, _} <- Utils.update_participation_request_count_in_object(event) do + event <- Object.get_by_ap_id(event_id) do + if Object.local?(event) and event.data["joinMode"] != "free" and + join_activity.data["actor"] == event.data["actor"] do + Utils.update_participation_request_count_in_object(event) + end + {:ok, participant} end end diff --git a/test/pleroma/web/pleroma_api/controllers/event_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/event_controller_test.exs index f2a648c0f4..688063e212 100644 --- a/test/pleroma/web/pleroma_api/controllers/event_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/event_controller_test.exs @@ -111,7 +111,7 @@ test "GET /api/v1/pleroma/events/:id/participations" do |> get("/api/v1/pleroma/events/#{activity.id}/participations") assert response = json_response_and_validate_schema(conn, 200) - assert length(response) == 2 + assert length(response) == 3 end describe "GET /api/v1/pleroma/events/:id/participation_requests" do @@ -175,7 +175,7 @@ test "don't display requests if not an author", %{conn: conn} do [user: user, conn: conn] end - test "joins an event", %{conn: conn} do + test "joins an event", %{conn: conn, user: user} do other_user = insert(:user) {:ok, activity} = @@ -193,12 +193,12 @@ test "joins an event", %{conn: conn} do assert %{ data: %{ - "participation_count" => 1 + "participation_count" => 2 } } = Object.get_by_ap_id(activity.data["object"]) end - test "can't participate in your own event", %{conn: conn, user: user} do + test "can't join your own event", %{conn: conn, user: user} do {:ok, activity} = CommonAPI.event(user, %{ name: "test event", @@ -243,7 +243,7 @@ test "leaves an event", %{conn: conn, user: user} do assert %{ data: %{ - "participation_count" => 0 + "participation_count" => 1 } } = Object.get_by_ap_id(activity.data["object"]) end @@ -298,8 +298,8 @@ test "accepts a participation request", %{user: user, conn: conn} do assert %{ data: %{ - "participations" => [^ap_id], - "participation_count" => 1 + "participations" => [^ap_id, _], + "participation_count" => 2 } } = Object.get_by_ap_id(activity.data["object"])