Refactor mutes removing in CommonAPI and User
authorRoman Chvanikov <chvanikoff@pm.me>
Sun, 20 Sep 2020 17:51:20 +0000 (20:51 +0300)
committerRoman Chvanikov <chvanikoff@pm.me>
Sun, 20 Sep 2020 17:58:17 +0000 (20:58 +0300)
lib/pleroma/user.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 83eb4d5ffa58e76be414942fa09a6e493b94a0e3..83e89a12ce436d01c6baee5a2dfdc0d099767dc7 100644 (file)
@@ -1385,6 +1385,20 @@ defmodule Pleroma.User do
     end
   end
 
+  def unmute(muter_id, mutee_id) do
+    with {:muter, %User{} = muter} <- {:muter, User.get_by_id(muter_id)},
+         {:mutee, %User{} = mutee} <- {:mutee, User.get_by_id(mutee_id)} do
+      unmute(muter, mutee)
+    else
+      {who, result} = error ->
+        Logger.warn(
+          "User.unmute/2 failed. #{who}: #{result}, muter_id: #{muter_id}, mutee_id: #{mutee_id}"
+        )
+
+        {:error, error}
+    end
+  end
+
   def subscribe(%User{} = subscriber, %User{} = target) do
     deny_follow_blocked = Config.get([:user, :deny_follow_blocked])
 
index fca9246a56c02a92b08c79cad2f758a5e43a57b4..aa4c6ddabe335cb735c4b8057db156d95d6bb4f3 100644 (file)
@@ -472,11 +472,27 @@ defmodule Pleroma.Web.CommonAPI do
     end
   end
 
-  def remove_mute(user, activity) do
+  def remove_mute(%User{} = user, %Activity{} = activity) do
     ThreadMute.remove_mute(user.id, activity.data["context"])
     {:ok, activity}
   end
 
+  def remove_mute(user_id, activity_id) do
+    with {:user, %User{} = user} <- {:user, User.get_by_id(user_id)},
+         {:activity, %Activity{} = activity} <- {:activity, Activity.get_by_id(activity_id)} do
+      remove_mute(user, activity)
+    else
+      {what, result} = error ->
+        Logger.warn(
+          "CommonAPI.remove_mute/2 failed. #{what}: #{result}, user_id: #{user_id}, activity_id: #{
+            activity_id
+          }"
+        )
+
+        {:error, error}
+    end
+  end
+
   def thread_muted?(%User{id: user_id}, %{data: %{"context" => context}})
       when is_binary(context) do
     ThreadMute.exists?(user_id, context)
index c8b44894e91f9a110e66329a10a9da98feb75e1a..32a12ba8559024ab666ccf412b53d9958de1026a 100644 (file)
@@ -7,18 +7,14 @@ defmodule Pleroma.Workers.MuteExpireWorker do
 
   @impl Oban.Worker
   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)
+    Pleroma.User.unmute(muter_id, mutee_id)
     :ok
   end
 
   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)
+    Pleroma.Web.CommonAPI.remove_mute(user_id, activity_id)
     :ok
   end
 end
index ce0d4d38bad1aa09a0b20cee199a71bf4c5b7459..79c8b76b8b1dc6f647cdfe703369d41ff6e7f7aa 100644 (file)
@@ -1034,6 +1034,17 @@ defmodule Pleroma.UserTest do
       refute User.muted_notifications?(user, muted_user)
     end
 
+    test "it unmutes users by id" do
+      user = insert(:user)
+      muted_user = insert(:user)
+
+      {:ok, _user_relationships} = User.mute(user, muted_user)
+      {:ok, _user_mute} = User.unmute(user.id, muted_user.id)
+
+      refute User.mutes?(user, muted_user)
+      refute User.muted_notifications?(user, muted_user)
+    end
+
     test "it mutes user without notifications" do
       user = insert(:user)
       muted_user = insert(:user)
index bf4353a57bbbf0f07a8aaf807fcb81db74010718..45ab1e0bdf2949ab3fdbf220c1c1bcbd7009bd49 100644 (file)
@@ -913,6 +913,12 @@ defmodule Pleroma.Web.CommonAPITest do
       refute CommonAPI.thread_muted?(user, activity)
     end
 
+    test "remove mute by ids", %{user: user, activity: activity} do
+      CommonAPI.add_mute(user, activity)
+      {:ok, _} = CommonAPI.remove_mute(user.id, activity.id)
+      refute CommonAPI.thread_muted?(user, activity)
+    end
+
     test "check that mutes can't be duplicate", %{user: user, activity: activity} do
       CommonAPI.add_mute(user, activity)
       {:error, _} = CommonAPI.add_mute(user, activity)