A platform for community discussion. Free, open, simple.
FIX: correctly check for disabled notifications, tab and is idle (#27127)
Joffrey JAFFEUX52125d849fd8
diff --git a/app/assets/javascripts/discourse/app/lib/desktop-notifications.js b/app/assets/javascripts/discourse/app/lib/desktop-notifications.jsindex 6cf190f001b..54765ed6386 100644--- a/app/assets/javascripts/discourse/app/lib/desktop-notifications.js+++ b/app/assets/javascripts/discourse/app/lib/desktop-notifications.js@@ -136,31 +136,48 @@ function setupNotifications(appEvents) {appEvents.on("page:changed", resetIdle);}-export function resetIdle() {+function resetIdle() {lastAction = Date.now();}+function isIdle() {return lastAction + idleThresholdTime < Date.now();}-// Call-in point from message bus-async function onNotification(data, siteSettings, user, appEvents) {- if (!liveEnabled) {- return;- }+function setLastAction(time) {+ lastAction = time;+}++function canUserReceiveNotifications(user) {if (!primaryTab) {- return;+ return false;}+if (!isIdle()) {- return;+ return false;}+if (user.isInDoNotDisturb()) {- return;+ return false;}+if (keyValueStore.getItem("notifications-disabled")) {+ return false;+ }++ return true;+}++// Call-in point from message bus+async function onNotification(data, siteSettings, user, appEvents) {+ if (!canUserReceiveNotifications(user)) {return;}+ if (!liveEnabled) {+ return false;+ }+const notificationTitle =data.translated_title ||I18n.t(i18nKey(data.notification_type), {@@ -244,4 +261,7 @@ export {alertChannel,confirmNotification,disable,+ canUserReceiveNotifications,+ resetIdle,+ setLastAction,};diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-channel-notification-sound.js b/plugins/chat/assets/javascripts/discourse/services/chat-channel-notification-sound.jsindex 1969e0e1788..304255540ee 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat-channel-notification-sound.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat-channel-notification-sound.js@@ -1,4 +1,5 @@import Service, { service } from "@ember/service";+import { canUserReceiveNotifications } from "discourse/lib/desktop-notifications";export default class ChatChannelNotificationSound extends Service {@service chat;@@ -7,15 +8,15 @@ export default class ChatChannelNotificationSound extends Service {@service site;async play(channel) {- if (channel.isCategoryChannel) {+ if (!canUserReceiveNotifications(this.currentUser)) {return false;}- if (channel.chatable.group) {+ if (channel.isCategoryChannel) {return false;}- if (this.currentUser.isInDoNotDisturb()) {+ if (channel.chatable.group) {return false;}diff --git a/plugins/chat/test/javascripts/unit/services/chat-channel-notification-sound-test.js b/plugins/chat/test/javascripts/unit/services/chat-channel-notification-sound-test.jsindex d0947b13e42..9b8400c99af 100644--- a/plugins/chat/test/javascripts/unit/services/chat-channel-notification-sound-test.js+++ b/plugins/chat/test/javascripts/unit/services/chat-channel-notification-sound-test.js@@ -1,5 +1,11 @@import { getOwner } from "@ember/application";import { test } from "qunit";+import {+ disable,+ init,+ resetIdle,+ setLastAction,+} from "discourse/lib/desktop-notifications";import {acceptance,updateCurrentUser,@@ -44,6 +50,13 @@ acceptance(});updateCurrentUser({ chat_sound: "ding" });++ init(+ this.container.lookup("service:message-bus"),+ this.container.lookup("service:app-events")+ );++ setLastAction(moment().subtract(1, "hour").valueOf());});needs.user();@@ -115,5 +128,19 @@ acceptance(assert.deepEqual(await this.subject.play(channel), false);});++ test("not idle", async function (assert) {+ const channel = buildDirectMessageChannel(getOwner(this));+ resetIdle();++ assert.deepEqual(await this.subject.play(channel), false);+ });++ test("notifications disabled", async function (assert) {+ const channel = buildDirectMessageChannel(getOwner(this));+ disable();++ assert.deepEqual(await this.subject.play(channel), false);+ });});