FIX: allows to change sound when no sound was ever set (#19136)

It fixes a bug which was only allowing users with a sound to change it. Users with `none` could not change it again after a full page reset.
This commit is contained in:
Joffrey JAFFEUX 2022-11-22 08:57:06 +01:00 committed by GitHub
parent 564292bfc1
commit d127d2acdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 7 deletions

View File

@ -34,7 +34,7 @@ export default class PreferencesChatController extends Controller {
@action @action
onChangeChatSound(sound) { onChangeChatSound(sound) {
if (sound && !isTesting()) { if (sound) {
this.chatAudioManager.playImmediately(sound); this.chatAudioManager.playImmediately(sound);
} }
this.model.set("user_option.chat_sound", sound); this.model.set("user_option.chat_sound", sound);

View File

@ -8,10 +8,9 @@ export default {
name: "chat-audio", name: "chat-audio",
initialize(container) { initialize(container) {
const currentUser = container.lookup("service:current-user");
const chatService = container.lookup("service:chat"); const chatService = container.lookup("service:chat");
if (!chatService.userCanChat || !currentUser?.chat_sound) { if (!chatService.userCanChat) {
return; return;
} }
@ -20,6 +19,10 @@ export default {
withPluginApi("0.12.1", (api) => { withPluginApi("0.12.1", (api) => {
api.registerDesktopNotificationHandler((data, siteSettings, user) => { api.registerDesktopNotificationHandler((data, siteSettings, user) => {
if (!user.chat_sound) {
return;
}
if (CHAT_NOTIFICATION_TYPES.includes(data.notification_type)) { if (CHAT_NOTIFICATION_TYPES.includes(data.notification_type)) {
chatAudioManager.play(user.chat_sound); chatAudioManager.play(user.chat_sound);
} }

View File

@ -1,5 +1,6 @@
import Service from "@ember/service"; import Service from "@ember/service";
import { debounce } from "discourse-common/utils/decorators"; import { debounce } from "discourse-common/utils/decorators";
import { isTesting } from "discourse-common/config/environment";
const AUDIO_DEBOUNCE_DELAY = 3000; const AUDIO_DEBOUNCE_DELAY = 3000;
@ -49,6 +50,8 @@ export default class ChatAudioManager extends Service {
const audio = const audio =
this._audioCache[soundName] || this._audioCache[DEFAULT_SOUND_NAME]; this._audioCache[soundName] || this._audioCache[DEFAULT_SOUND_NAME];
audio.muted = isTesting();
if (!audio.paused) { if (!audio.paused) {
audio.pause(); audio.pause();
if (typeof audio.fastSeek === "function") { if (typeof audio.fastSeek === "function") {
@ -59,10 +62,12 @@ export default class ChatAudioManager extends Service {
} }
return audio.play().catch(() => { return audio.play().catch(() => {
if (!isTesting()) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.info( console.info(
"[chat] User needs to interact with DOM before we can play notification sounds." "[chat] User needs to interact with DOM before we can play notification sounds."
); );
}
}); });
} }
} }

View File

@ -0,0 +1,34 @@
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { visit } from "@ember/test-helpers";
import { test } from "qunit";
import { chatChannels } from "discourse/plugins/chat/chat-fixtures";
import selectKit from "discourse/tests/helpers/select-kit-helper";
import { CHAT_SOUNDS } from "discourse/plugins/chat/discourse/services/chat-audio-manager";
function preferencesPretender(server, helper) {
server.get("/u/eviltrout/activity.json", () => helper.response({}));
server.get("/chat/chat_channels.json", () => helper.response(chatChannels));
}
acceptance("Discourse Chat | User Preferences", function (needs) {
needs.user({ can_chat: true, has_chat_enabled: true });
needs.settings({ chat_enabled: true });
needs.pretender(preferencesPretender);
test("when user has not chat sound set", async function (assert) {
const sounds = Object.keys(CHAT_SOUNDS);
await visit("/u/eviltrout/preferences/chat");
const dropdown = selectKit("#user_chat_sounds");
assert.strictEqual(dropdown.header().value(), null, "it displays no sound");
await dropdown.expand();
await dropdown.selectRowByValue(sounds[1]);
assert.strictEqual(
dropdown.header().value(),
sounds[1],
"it selects the sound"
);
});
});