mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 17:06:31 -06:00
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:
parent
322a20a9f4
commit
52125d849f
@ -136,29 +136,46 @@ function setupNotifications(appEvents) {
|
||||
appEvents.on("page:changed", resetIdle);
|
||||
}
|
||||
|
||||
export function resetIdle() {
|
||||
function resetIdle() {
|
||||
lastAction = Date.now();
|
||||
}
|
||||
|
||||
function isIdle() {
|
||||
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
|
||||
async function onNotification(data, siteSettings, user, appEvents) {
|
||||
if (!canUserReceiveNotifications(user)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!liveEnabled) {
|
||||
return;
|
||||
}
|
||||
if (!primaryTab) {
|
||||
return;
|
||||
}
|
||||
if (!isIdle()) {
|
||||
return;
|
||||
}
|
||||
if (user.isInDoNotDisturb()) {
|
||||
return;
|
||||
}
|
||||
if (keyValueStore.getItem("notifications-disabled")) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
const notificationTitle =
|
||||
@ -244,4 +261,7 @@ export {
|
||||
alertChannel,
|
||||
confirmNotification,
|
||||
disable,
|
||||
canUserReceiveNotifications,
|
||||
resetIdle,
|
||||
setLastAction,
|
||||
};
|
||||
|
@ -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,6 +8,10 @@ export default class ChatChannelNotificationSound extends Service {
|
||||
@service site;
|
||||
|
||||
async play(channel) {
|
||||
if (!canUserReceiveNotifications(this.currentUser)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (channel.isCategoryChannel) {
|
||||
return false;
|
||||
}
|
||||
@ -15,10 +20,6 @@ export default class ChatChannelNotificationSound extends Service {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.currentUser.isInDoNotDisturb()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.currentUser.chat_sound) {
|
||||
return false;
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user