From e3f845b24363cd867ab85b7297f2d34bfa16b13f Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 8 Sep 2020 15:13:50 +0300 Subject: [PATCH] Add expiring mutes for activities --- lib/pleroma/user.ex | 6 ++--- .../api_spec/operations/status_operation.ex | 22 ++++++++++++++++++- lib/pleroma/web/common_api/common_api.ex | 12 +++++++++- lib/pleroma/workers/mute_expire_worker.ex | 10 ++++++--- test/user_test.exs | 12 ++++++++-- test/web/common_api/common_api_test.exs | 18 +++++++++++++++ 6 files changed, 70 insertions(+), 10 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 040db8d80..46e03553c 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1366,10 +1366,10 @@ def mute(%User{} = muter, %User{} = mutee, params \\ %{}) do {:ok, user_notification_mute} <- (notifications? && UserRelationship.create_notification_mute(muter, mutee)) || {:ok, nil} do - with seconds when seconds > 0 <- expires_in do + if expires_in > 0 do Pleroma.Workers.MuteExpireWorker.enqueue( - "unmute", - %{"muter" => muter.id, "mutee" => mutee.id}, + "unmute_user", + %{"muter_id" => muter.id, "mutee_id" => mutee.id}, schedule_in: expires_in ) end diff --git a/lib/pleroma/web/api_spec/operations/status_operation.ex b/lib/pleroma/web/api_spec/operations/status_operation.ex index 5bd4619d5..6589a16f3 100644 --- a/lib/pleroma/web/api_spec/operations/status_operation.ex +++ b/lib/pleroma/web/api_spec/operations/status_operation.ex @@ -223,7 +223,27 @@ def mute_conversation_operation do security: [%{"oAuth" => ["write:mutes"]}], description: "Do not receive notifications for the thread that this status is part of.", operationId: "StatusController.mute_conversation", - parameters: [id_param()], + requestBody: + request_body("Parameters", %Schema{ + type: :object, + properties: %{ + expires_in: %Schema{ + type: :integer, + nullable: true, + description: "Expire the mute in `expires_in` seconds. Default 0 for infinity", + default: 0 + } + } + }), + parameters: [ + id_param(), + Operation.parameter( + :expires_in, + :query, + %Schema{type: :integer, default: 0}, + "Expire the mute in `expires_in` seconds. Default 0 for infinity" + ) + ], responses: %{ 200 => status_response(), 400 => Operation.response("Error", "application/json", ApiError) diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index 4ab533658..b217c4d10 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -451,9 +451,19 @@ def unpin(id, user) do end end - def add_mute(user, activity) do + def add_mute(user, activity, params \\ %{}) do + expires_in = Map.get(params, :expires_in, 0) + with {:ok, _} <- ThreadMute.add_mute(user.id, activity.data["context"]), _ <- Pleroma.Notification.mark_context_as_read(user, activity.data["context"]) do + if expires_in > 0 do + Pleroma.Workers.MuteExpireWorker.enqueue( + "unmute_conversation", + %{"user_id" => user.id, "activity_id" => activity.id}, + schedule_in: expires_in + ) + end + {:ok, activity} else {:error, _} -> {:error, dgettext("errors", "conversation is already muted")} diff --git a/lib/pleroma/workers/mute_expire_worker.ex b/lib/pleroma/workers/mute_expire_worker.ex index b8ec939a9..622fdbadd 100644 --- a/lib/pleroma/workers/mute_expire_worker.ex +++ b/lib/pleroma/workers/mute_expire_worker.ex @@ -8,15 +8,19 @@ defmodule Pleroma.Workers.MuteExpireWorker do require Logger @impl Oban.Worker - def perform(%Job{args: %{"op" => "unmute", "muter" => muter_id, "mutee" => mutee_id}}) do + def perform(%Job{args: %{"op" => "unmute_user", "muter_id" => muter_id, "mutee_id" => mutee_id}}) do muter = Pleroma.User.get_by_id(muter_id) mutee = Pleroma.User.get_by_id(mutee_id) Pleroma.User.unmute(muter, mutee) :ok end - def perform(any) do - Logger.error("Got call to perform(#{inspect(any)})") + def perform(%Job{ + args: %{"op" => "unmute_conversation", "user_id" => user_id, "activity_id" => activity_id} + }) do + user = Pleroma.User.get_by_id(user_id) + activity = Pleroma.Activity.get_by_id(activity_id) + Pleroma.Web.CommonAPI.remove_mute(user, activity) :ok end end diff --git a/test/user_test.exs b/test/user_test.exs index 83c017ec5..d49afb35a 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -970,10 +970,18 @@ test "expiring" do {:ok, _user_relationships} = User.mute(user, muted_user, %{expires_in: 60}) assert User.mutes?(user, muted_user) + worker = Pleroma.Workers.MuteExpireWorker + args = %{"op" => "unmute_user", "muter_id" => user.id, "mutee_id" => muted_user.id} + assert_enqueued( - worker: Pleroma.Workers.MuteExpireWorker, - args: %{"op" => "unmute", "muter" => user.id, "mutee" => muted_user.id} + worker: worker, + args: args ) + + assert :ok = perform_job(worker, args) + + refute User.mutes?(user, muted_user) + refute User.muted_notifications?(user, muted_user) end test "it unmutes users" do diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 800db9a20..7ceb7ec7f 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -3,7 +3,9 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.CommonAPITest do + use Oban.Testing, repo: Pleroma.Repo use Pleroma.DataCase + alias Pleroma.Activity alias Pleroma.Chat alias Pleroma.Conversation.Participation @@ -878,6 +880,22 @@ test "add mute", %{user: user, activity: activity} do assert CommonAPI.thread_muted?(user, activity) end + test "add expiring mute", %{user: user, activity: activity} do + {:ok, _} = CommonAPI.add_mute(user, activity, %{expires_in: 60}) + assert CommonAPI.thread_muted?(user, activity) + + worker = Pleroma.Workers.MuteExpireWorker + args = %{"op" => "unmute_conversation", "user_id" => user.id, "activity_id" => activity.id} + + assert_enqueued( + worker: worker, + args: args + ) + + assert :ok = perform_job(worker, args) + refute CommonAPI.thread_muted?(user, activity) + end + test "remove mute", %{user: user, activity: activity} do CommonAPI.add_mute(user, activity) {:ok, _} = CommonAPI.remove_mute(user, activity)