Add expiring mutes for activities
authorRoman Chvanikov <chvanikoff@pm.me>
Tue, 8 Sep 2020 12:13:50 +0000 (15:13 +0300)
committerRoman Chvanikov <chvanikoff@pm.me>
Tue, 8 Sep 2020 12:13:50 +0000 (15:13 +0300)
lib/pleroma/user.ex
lib/pleroma/web/api_spec/operations/status_operation.ex
lib/pleroma/web/common_api/common_api.ex
lib/pleroma/workers/mute_expire_worker.ex
test/user_test.exs
test/web/common_api/common_api_test.exs

index 040db8d80c46c941d983e971a7437806ef57966b..46e03553ceae0668348c66d6372806834720a980 100644 (file)
@@ -1366,10 +1366,10 @@ defmodule Pleroma.User 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
index 5bd4619d519dba85b043fcc95b3ac1d7091be548..6589a16f3a90318db8272ff954dc6d17a3e49eed 100644 (file)
@@ -223,7 +223,27 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation 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)
index 4ab533658e41342571c72d1ee64e2d9650c55908..b217c4d10fbbe928f79e66cd46b45a86f657c363 100644 (file)
@@ -451,9 +451,19 @@ defmodule Pleroma.Web.CommonAPI 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")}
index b8ec939a9353fbd4c15fe225a298f4cb62a7f0be..622fdbaddfe4d8ac92b4978c0e6c8dd72ba3f093 100644 (file)
@@ -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
index 83c017ec54404584edb605466ee387d5fb25c96b..d49afb35ac2d0e91aaea058b8fe538a4541e56fa 100644 (file)
@@ -970,10 +970,18 @@ defmodule Pleroma.UserTest 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
index 800db9a207207921b31b6da87e4433abe6519ee8..7ceb7ec7fb065b80292a2ffbca62680f9d2b8552 100644 (file)
@@ -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 @@ defmodule Pleroma.Web.CommonAPITest 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)