From 86ae00f9da4d9e39d8f635d51b1139529b6fb9dc Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 20 Jul 2024 14:53:53 -0400 Subject: [PATCH] Support cancelling jobs when Unfavoriting --- changelog.d/oban-cancel-federation.add | 2 +- lib/pleroma/web/common_api.ex | 1 + test/pleroma/web/common_api_test.exs | 44 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/changelog.d/oban-cancel-federation.add b/changelog.d/oban-cancel-federation.add index 39473e898..aae8bd84f 100644 --- a/changelog.d/oban-cancel-federation.add +++ b/changelog.d/oban-cancel-federation.add @@ -1 +1 @@ -Deleting a post will cancel queued publishing jobs for the deleted activity. +Deleting or Unfavoriting a post will cancel undelivered publishing jobs for the original activity. diff --git a/lib/pleroma/web/common_api.ex b/lib/pleroma/web/common_api.ex index d43c46520..9f730d811 100644 --- a/lib/pleroma/web/common_api.ex +++ b/lib/pleroma/web/common_api.ex @@ -277,6 +277,7 @@ defmodule Pleroma.Web.CommonAPI do {:find_activity, Activity.get_by_id(id)}, %Object{} = note <- Object.normalize(activity, fetch: false), %Activity{} = like <- Utils.get_existing_like(user.ap_id, note), + {_, {:ok, _}} <- {:cancel_jobs, maybe_cancel_jobs(like)}, {:ok, undo, _} <- Builder.undo(user, like), {:ok, activity, _} <- Pipeline.common_pipeline(undo, local: true) do {:ok, activity} diff --git a/test/pleroma/web/common_api_test.exs b/test/pleroma/web/common_api_test.exs index 75a30191a..500c4ba3a 100644 --- a/test/pleroma/web/common_api_test.exs +++ b/test/pleroma/web/common_api_test.exs @@ -1993,5 +1993,49 @@ defmodule Pleroma.Web.CommonAPITest do assert length(cancelled_jobs) == 2 end + + test "when unfavoriting posts", %{ + local_user: local_user, + remote_one: remote_user + } do + {:ok, activity} = + CommonAPI.post(remote_user, %{status: "I like turtles!"}) + + {:ok, %{data: %{"id" => ap_id}} = _favorite} = + CommonAPI.favorite(local_user, activity.id) + + # Generate the publish_one jobs + ObanHelpers.perform_all() + + publish_one_jobs = + all_enqueued() + |> Enum.filter(fn job -> + match?( + %{ + state: "available", + queue: "federator_outgoing", + worker: "Pleroma.Workers.PublisherWorker", + args: %{"op" => "publish_one", "params" => %{"id" => ^ap_id}} + }, + job + ) + end) + + assert length(publish_one_jobs) == 1 + + # The unfavorite should have triggered cancelling the publish_one jobs + assert {:ok, _unfavorite} = CommonAPI.unfavorite(activity.id, local_user) + + # all_enqueued/1 will not return cancelled jobs + cancelled_jobs = + Oban.Job + |> where([j], j.worker == "Pleroma.Workers.PublisherWorker") + |> where([j], j.state == "cancelled") + |> where([j], j.args["op"] == "publish_one") + |> where([j], j.args["params"]["id"] == ^ap_id) + |> Pleroma.Repo.all() + + assert length(cancelled_jobs) == 1 + end end end