FIX: A much nicer error message if you can't ignore/mute a user

FIX: A much nicer error message if you can’t ignore/mute a user

diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index a172450..886b0d2 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1178,6 +1178,7 @@ class UsersController < ApplicationController
     user = fetch_user_from_params
 
     if params[:notification_level] == "ignore"
+      @error_message = "ignore_error"
       guardian.ensure_can_ignore_user!(user)
       MutedUser.where(user: current_user, muted_user: user).delete_all
       ignored_user = IgnoredUser.find_by(user: current_user, ignored_user: user)
@@ -1187,6 +1188,7 @@ class UsersController < ApplicationController
         IgnoredUser.create!(user: current_user, ignored_user: user, expiring_at: Time.parse(params[:expiring_at]))
       end
     elsif params[:notification_level] == "mute"
+      @error_message = "mute_error"
       guardian.ensure_can_mute_user!(user)
       IgnoredUser.where(user: current_user, ignored_user: user).delete_all
       MutedUser.find_or_create_by!(user: current_user, muted_user: user)
@@ -1196,6 +1198,8 @@ class UsersController < ApplicationController
     end
 
     render json: success_json
+  rescue Discourse::InvalidAccess => e
+    render_json_error(I18n.t("notification_level.#{@error_message}"))
   end
 
   def read_faq
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index febb6a6..aacd7c2 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -847,7 +847,7 @@ en:
           all: "All"
           read: "Read"
           unread: "Unread"
-        ignore_duration_title: "Ignore Timer"
+        ignore_duration_title: "Ignore User"
         ignore_duration_username: "Username"
         ignore_duration_when: "Duration:"
         ignore_duration_save: "Ignore"
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 5a3bc5a..eb5ae68 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -4855,6 +4855,10 @@ en:
   email_style:
     html_missing_placeholder: "The html template must include %{placeholder}"
 
+  notification_level:
+    ignore_error: "Sorry, you can't ignore that user."
+    mute_error: "Sorry, you can't mute that user."
+
   discord:
     not_in_allowed_guild: "Authentication failed. You are not a member of a permitted Discord guild."
 
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index ec5adf4..add1d7a 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -2414,6 +2414,22 @@ describe UsersController do
       let!(:ignored_user) { Fabricate(:ignored_user, user: user, ignored_user: another_user) }
       let!(:muted_user) { Fabricate(:muted_user, user: user, muted_user: another_user) }
 
+      context "when you can't change the notification" do
+        fab!(:staff_user) { Fabricate(:admin) }
+
+        it "ignoring includes a helpful error message" do
+          put "/u/#{staff_user.username}/notification_level.json", params: { notification_level: 'ignore' }
+          expect(response.status).to eq(422)
+          expect(response.parsed_body['errors'][0]).to eq(I18n.t("notification_level.ignore_error"))
+        end
+
+        it "muting includes a helpful error message" do
+          put "/u/#{staff_user.username}/notification_level.json", params: { notification_level: 'mute' }
+          expect(response.status).to eq(422)
+          expect(response.parsed_body['errors'][0]).to eq(I18n.t("notification_level.mute_error"))
+        end
+      end
+
       context 'when changing notification level to normal' do
         it 'changes notification level to normal' do
           put "/u/#{another_user.username}/notification_level.json", params: { notification_level: "normal" }

GitHub sha: 494a27dc