Skip to content

A platform for community discussion. Free, open, simple.

FIX: Bug setting notification level to muted/ignored on user page (#16268)

Mark VanLandinghama3563336dbb4
diff --git a/app/assets/javascripts/discourse/app/controllers/ignore-duration.js b/app/assets/javascripts/discourse/app/controllers/ignore-duration.js
index 929581be2ef..77e77f3e95e 100644
--- a/app/assets/javascripts/discourse/app/controllers/ignore-duration.js
+++ b/app/assets/javascripts/discourse/app/controllers/ignore-duration.js
@@ -18,7 +18,7 @@ export default Controller.extend(ModalFunctionality, {
this.set("loading", true);
this.model
.updateNotificationLevel({
- level: "ignored",
+ level: "ignore",
expiringAt: this.ignoredUntil,
})
.then(() => {
diff --git a/app/assets/javascripts/discourse/app/controllers/user.js b/app/assets/javascripts/discourse/app/controllers/user.js
index 49199aafd78..9567758f050 100644
--- a/app/assets/javascripts/discourse/app/controllers/user.js
+++ b/app/assets/javascripts/discourse/app/controllers/user.js
@@ -168,14 +168,19 @@ export default Controller.extend(CanCheckEmails, {
"currentUser.ignored_ids",
"model.ignored",
"model.muted",
- function () {
- if (this.get("model.ignored")) {
- return "changeToIgnored";
- } else if (this.get("model.muted")) {
- return "changeToMuted";
- } else {
- return "changeToNormal";
- }
+ {
+ get() {
+ if (this.get("model.ignored")) {
+ return "changeToIgnored";
+ } else if (this.get("model.muted")) {
+ return "changeToMuted";
+ } else {
+ return "changeToNormal";
+ }
+ },
+ set(key, value) {
+ return value;
+ },
}
),
@@ -250,7 +255,7 @@ export default Controller.extend(CanCheckEmails, {
},
updateNotificationLevel(level) {
- return this.model.updateNotificationLevel({ level });
+ return this.model.updateNotificationLevel(level);
},
},
});
diff --git a/app/assets/javascripts/discourse/app/templates/modal/ignore-duration.hbs b/app/assets/javascripts/discourse/app/templates/modal/ignore-duration.hbs
index cabbb66960a..566b60f21a7 100644
--- a/app/assets/javascripts/discourse/app/templates/modal/ignore-duration.hbs
+++ b/app/assets/javascripts/discourse/app/templates/modal/ignore-duration.hbs
@@ -11,7 +11,7 @@
{{/d-modal-body}}
<div class="modal-footer">
- {{d-button class="btn-primary"
+ {{d-button class="btn-primary ignore-duration-save"
disabled=saveDisabled
label="user.user_notifications.ignore_duration_save"
action=(action "ignore")}}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/user-test.js b/app/assets/javascripts/discourse/tests/acceptance/user-test.js
index 121ccb9e486..1485f8223a0 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/user-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/user-test.js
@@ -2,6 +2,7 @@ import EmberObject from "@ember/object";
import User from "discourse/models/user";
import selectKit from "discourse/tests/helpers/select-kit-helper";
import sinon from "sinon";
+import userFixtures from "discourse/tests/fixtures/user-fixtures";
import {
acceptance,
exists,
@@ -9,6 +10,7 @@ import {
queryAll,
} from "discourse/tests/helpers/qunit-helpers";
import { click, currentRouteName, visit } from "@ember/test-helpers";
+import { cloneJSON } from "discourse-common/lib/object";
import { test } from "qunit";
acceptance("User Routes", function (needs) {
@@ -177,3 +179,118 @@ acceptance("User - Saving user options", function (needs) {
);
});
});
+
+acceptance("User - Notification level dropdown visibility", function (needs) {
+ needs.user({ username: "eviltrout", id: 1, ignored_ids: [] });
+
+ needs.pretender((server, helper) => {
+ server.get("/u/charlie.json", () => {
+ const cloned = cloneJSON(userFixtures["/u/charlie.json"]);
+ cloned.user.can_ignore_user = false;
+ cloned.user.can_mute_user = false;
+ return helper.response(200, cloned);
+ });
+ });
+
+ test("Notification level button is not rendered for user who cannot mute or ignore another user", async function (assert) {
+ await visit("/u/charlie");
+ assert.notOk(exists(".user-notifications-dropdown"));
+ });
+});
+
+acceptance(
+ "User - Muting other user with notification level dropdown",
+ function (needs) {
+ needs.user({ username: "eviltrout", id: 1, ignored_ids: [] });
+
+ needs.pretender((server, helper) => {
+ server.get("/u/charlie.json", () => {
+ const cloned = cloneJSON(userFixtures["/u/charlie.json"]);
+ cloned.user.can_mute_user = true;
+ return helper.response(200, cloned);
+ });
+
+ server.put("/u/charlie/notification_level.json", (request) => {
+ let requestParams = new URLSearchParams(request.requestBody);
+ // Ensure the correct `notification_level` param is sent to the server
+ if (requestParams.get("notification_level") === "mute") {
+ return helper.response(200, {});
+ } else {
+ return helper.response(422, {});
+ }
+ });
+ });
+
+ test("Notification level is set to normal and can be changed to muted", async function (assert) {
+ await visit("/u/charlie");
+ assert.ok(
+ exists(".user-notifications-dropdown"),
+ "Notification level dropdown is present"
+ );
+
+ const dropdown = selectKit(".user-notifications-dropdown");
+ await dropdown.expand();
+ assert.strictEqual(dropdown.selectedRow().value(), "changeToNormal");
+
+ await dropdown.selectRowByValue("changeToMuted");
+ await dropdown.expand();
+ assert.strictEqual(dropdown.selectedRow().value(), "changeToMuted");
+ });
+ }
+);
+
+acceptance(
+ "User - Ignoring other user with notification level dropdown",
+ function (needs) {
+ needs.user({ username: "eviltrout", id: 1, ignored_ids: [] });
+
+ needs.pretender((server, helper) => {
+ server.get("/u/charlie.json", () => {
+ const cloned = cloneJSON(userFixtures["/u/charlie.json"]);
+ cloned.user.can_ignore_user = true;
+ return helper.response(200, cloned);
+ });
+
+ server.put("/u/charlie/notification_level.json", (request) => {
+ let requestParams = new URLSearchParams(request.requestBody);
+ // Ensure the correct `notification_level` param is sent to the server
+ if (requestParams.get("notification_level") === "ignore") {
+ return helper.response(200, {});
+ } else {
+ return helper.response(422, {});
+ }
+ });
+ });
+ test("Notification level can be changed to ignored", async function (assert) {
+ await visit("/u/charlie");
+ assert.ok(
+ exists(".user-notifications-dropdown"),
+ "Notification level dropdown is present"
+ );
+
+ const notificationLevelDropdown = selectKit(
+ ".user-notifications-dropdown"
+ );
+ await notificationLevelDropdown.expand();
+ assert.strictEqual(
+ notificationLevelDropdown.selectedRow().value(),
+ "changeToNormal"
+ );
+
+ await notificationLevelDropdown.selectRowByValue("changeToIgnored");
+ assert.ok(exists(".ignore-duration-modal"));
+
+ const durationDropdown = selectKit(
+ ".ignore-duration-modal .future-date-input-selector"
+ );
+ await durationDropdown.expand();
+ await durationDropdown.selectRowByIndex(0);
+ await click(".modal-footer .ignore-duration-save");
+ await notificationLevelDropdown.expand();
+ assert.strictEqual(
+ notificationLevelDropdown.selectedRow().value(),
+ "changeToIgnored"
+ );
+ });
+ }
+);
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 659a4cb221a..5c721ac698b 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1365,6 +1365,8 @@ class UsersController < ApplicationController
elsif params[:notification_level] == "normal"
MutedUser.where(user: acting_user, muted_user: target_user).delete_all
IgnoredUser.where(user: acting_user, ignored_user: target_user).delete_all
+ else
+ return render_json_error(I18n.t("notification_level.invalid_value", value: params[:notification_level]))
end
render json: success_json
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 7530935ff5b..25c7e2678df 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -5154,6 +5154,7 @@ en:
ignore_error: "Sorry, you can't ignore that user."
mute_error: "Sorry, you can't mute that user."
error: "Sorry, you cannot change the notification level for that user."
+ invalid_value: '"%{value}" is not a valid notification level.'
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 bd5cc682834..c978b8cc057 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -2876,6 +2876,16 @@ describe UsersController do
end
end
+ describe "#notification_level" do
+ it 'raises an error when `notification_level` param is not a valid value' do
+ sign_in(user)
+ invalid_arg = "invalid"
+ put "/u/#{user.username}/notification_level.json", params: { notification_level: invalid_arg }
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"].first).to eq(I18n.t("notification_level.invalid_value", value: invalid_arg))
+ end
+ end
+
describe '#ignore' do
it 'raises an error when not logged in' do
put "/u/#{user1.username}/notification_level.json", params: { notification_level: "" }