FIX: correctly check for disabled notifications, tab and is idle (#27127)

This commit reuses the existing codepath in desktop-notifications and make it available to use to chat.

primaryTab was too hard to test if not impossible in this service test, however isIdle and disabled notifications are correctly tested.
This commit is contained in:
Joffrey JAFFEUX 2024-05-22 10:08:05 +02:00 committed by GitHub
parent 322a20a9f4
commit 52125d849f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 18 deletions

View File

@ -136,29 +136,46 @@ function setupNotifications(appEvents) {
appEvents.on("page:changed", resetIdle); appEvents.on("page:changed", resetIdle);
} }
export function resetIdle() { function resetIdle() {
lastAction = Date.now(); lastAction = Date.now();
} }
function isIdle() { function isIdle() {
return lastAction + idleThresholdTime < Date.now(); return lastAction + idleThresholdTime < Date.now();
} }
function setLastAction(time) {
lastAction = time;
}
function canUserReceiveNotifications(user) {
if (!primaryTab) {
return false;
}
if (!isIdle()) {
return false;
}
if (user.isInDoNotDisturb()) {
return false;
}
if (keyValueStore.getItem("notifications-disabled")) {
return false;
}
return true;
}
// Call-in point from message bus // Call-in point from message bus
async function onNotification(data, siteSettings, user, appEvents) { async function onNotification(data, siteSettings, user, appEvents) {
if (!canUserReceiveNotifications(user)) {
return;
}
if (!liveEnabled) { if (!liveEnabled) {
return; return false;
}
if (!primaryTab) {
return;
}
if (!isIdle()) {
return;
}
if (user.isInDoNotDisturb()) {
return;
}
if (keyValueStore.getItem("notifications-disabled")) {
return;
} }
const notificationTitle = const notificationTitle =
@ -244,4 +261,7 @@ export {
alertChannel, alertChannel,
confirmNotification, confirmNotification,
disable, disable,
canUserReceiveNotifications,
resetIdle,
setLastAction,
}; };

View File

@ -1,4 +1,5 @@
import Service, { service } from "@ember/service"; import Service, { service } from "@ember/service";
import { canUserReceiveNotifications } from "discourse/lib/desktop-notifications";
export default class ChatChannelNotificationSound extends Service { export default class ChatChannelNotificationSound extends Service {
@service chat; @service chat;
@ -7,6 +8,10 @@ export default class ChatChannelNotificationSound extends Service {
@service site; @service site;
async play(channel) { async play(channel) {
if (!canUserReceiveNotifications(this.currentUser)) {
return false;
}
if (channel.isCategoryChannel) { if (channel.isCategoryChannel) {
return false; return false;
} }
@ -15,10 +20,6 @@ export default class ChatChannelNotificationSound extends Service {
return false; return false;
} }
if (this.currentUser.isInDoNotDisturb()) {
return false;
}
if (!this.currentUser.chat_sound) { if (!this.currentUser.chat_sound) {
return false; return false;
} }

View File

@ -1,5 +1,11 @@
import { getOwner } from "@ember/application"; import { getOwner } from "@ember/application";
import { test } from "qunit"; import { test } from "qunit";
import {
disable,
init,
resetIdle,
setLastAction,
} from "discourse/lib/desktop-notifications";
import { import {
acceptance, acceptance,
updateCurrentUser, updateCurrentUser,
@ -44,6 +50,13 @@ acceptance(
}); });
updateCurrentUser({ chat_sound: "ding" }); 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(); needs.user();
@ -115,5 +128,19 @@ acceptance(
assert.deepEqual(await this.subject.play(channel), false); 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);
});
} }
); );