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

This commit is contained in:
Robin Ward 2020-06-18 13:41:27 -04:00
parent 6cb6faff29
commit 494a27dc27
4 changed files with 25 additions and 1 deletions

View File

@ -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

View File

@ -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"

View File

@ -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."

View File

@ -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" }