mirror of
https://github.com/discourse/discourse.git
synced 2025-05-31 01:57:15 +08:00
DEV: use service worker for chat sound (#29388)
This change makes use of service workers to determine if we should play chat sounds in the current browser tab. Since users can have multiple tabs open, we currently attempt to play sound across all active tabs. With this change we iterate over all clients and check if client.focused is true (ie. the current tab/window we have open), if so we allow playing the audio in the current tab and for all other hidden tabs/windows we return false. --------- Co-authored-by: Bianca Nenciu <nbianca@users.noreply.github.com>
This commit is contained in:
@ -19,14 +19,19 @@ module("Discourse Chat | Unit | chat-audio", function (hooks) {
|
||||
this.siteSettings = getOwner(this).lookup("service:site-settings");
|
||||
this.siteSettings.chat_enabled = true;
|
||||
|
||||
this.currentUser.chat_sound = "ding";
|
||||
this.currentUser.user_option.has_chat_enabled = true;
|
||||
this.currentUser.user_option.chat_sound = "ding";
|
||||
this.currentUser.user_option.chat_header_indicator_preference = "all_new";
|
||||
|
||||
withPluginApi("0.12.1", async (api) => {
|
||||
this.stub = sinon.spy(api, "registerDesktopNotificationHandler");
|
||||
chatAudioInitializer.initialize(getOwner(this));
|
||||
|
||||
// stub the service worker response
|
||||
sinon
|
||||
.stub(chatAudioInitializer, "canPlaySound")
|
||||
.returns(Promise.resolve(true));
|
||||
|
||||
this.notificationHandler = this.stub.getCall(0).callback;
|
||||
this.playStub = sinon.stub(chatAudioManager, "play");
|
||||
|
||||
@ -43,58 +48,65 @@ module("Discourse Chat | Unit | chat-audio", function (hooks) {
|
||||
assert.ok(this.stub.calledOnce);
|
||||
});
|
||||
|
||||
test("it plays chat sound", function (assert) {
|
||||
this.handleNotification();
|
||||
test("it plays chat sound", async function (assert) {
|
||||
await this.handleNotification();
|
||||
|
||||
assert.ok(this.playStub.calledOnce);
|
||||
});
|
||||
|
||||
test("it skips chat sound for user in DND mode", function (assert) {
|
||||
test("it skips chat sound for user in DND mode", async function (assert) {
|
||||
this.currentUser.isInDoNotDisturb = () => true;
|
||||
this.handleNotification();
|
||||
await this.handleNotification();
|
||||
|
||||
assert.ok(this.playStub.notCalled);
|
||||
});
|
||||
|
||||
test("it skips chat sound for user with no chat sound set", function (assert) {
|
||||
this.currentUser.user_option.chat_sound = null;
|
||||
this.handleNotification();
|
||||
test("it skips chat sound for user with no chat sound set", async function (assert) {
|
||||
this.currentUser.chat_sound = null;
|
||||
await this.handleNotification();
|
||||
|
||||
assert.ok(this.playStub.notCalled);
|
||||
});
|
||||
|
||||
test("it plays a chat sound for mentions", function (assert) {
|
||||
test("it plays a chat sound for mentions", async function (assert) {
|
||||
this.currentUser.user_option.chat_header_indicator_preference =
|
||||
"only_mentions";
|
||||
|
||||
this.handleNotification({ notification_type: 29 });
|
||||
await this.handleNotification({ notification_type: 29 });
|
||||
|
||||
assert.ok(this.playStub.calledOnce);
|
||||
});
|
||||
|
||||
test("it skips chat sound for non-mentions", function (assert) {
|
||||
test("it skips chat sound for non-mentions", async function (assert) {
|
||||
this.currentUser.user_option.chat_header_indicator_preference =
|
||||
"only_mentions";
|
||||
|
||||
this.handleNotification();
|
||||
await this.handleNotification();
|
||||
|
||||
assert.ok(this.playStub.notCalled);
|
||||
});
|
||||
|
||||
test("it plays a chat sound for DMs", function (assert) {
|
||||
test("it plays a chat sound for DMs", async function (assert) {
|
||||
this.currentUser.user_option.chat_header_indicator_preference =
|
||||
"dm_and_mentions";
|
||||
|
||||
this.handleNotification({ is_direct_message_channel: true });
|
||||
await this.handleNotification({ is_direct_message_channel: true });
|
||||
|
||||
assert.ok(this.playStub.calledOnce);
|
||||
});
|
||||
|
||||
test("it skips chat sound for non-DM messages", function (assert) {
|
||||
test("it skips chat sound for non-DM messages", async function (assert) {
|
||||
this.currentUser.user_option.chat_header_indicator_preference =
|
||||
"dm_and_mentions";
|
||||
|
||||
this.handleNotification({ is_direct_message_channel: false });
|
||||
await this.handleNotification({ is_direct_message_channel: false });
|
||||
|
||||
assert.ok(this.playStub.notCalled);
|
||||
});
|
||||
|
||||
test("it skips chat sound when service worker returns false", async function (assert) {
|
||||
chatAudioInitializer.canPlaySound.returns(Promise.resolve(false));
|
||||
await this.handleNotification();
|
||||
|
||||
assert.ok(this.playStub.notCalled);
|
||||
});
|
||||
|
Reference in New Issue
Block a user