discourse/plugins/chat/assets/javascripts/discourse/services/chat-notification-manager.js
Joffrey JAFFEUX b4406861ae
FIX: simplify desktop notifications behavior (#29957)
Historically the behavior of this file has been complexified to attempt to answer this use case:

A user has two tabs open, tab 1 is on a topic, tab 2 is on a chat channel. If your active tab is tab 1 and someones sends you a mention in chat. We will show a desktop notification, but in which tab the channel should open if you click it? The changes made years ago said: in tab 2.

I think this is complexifying too much this codepath and is also confusing. You might wonder why this discourse notification you clicked opened in some of your 50 tabs in the background when you had a discourse tab active currently in front of you.

Moreover, a recent change has made the notification to only happen on desktop, but all the subscription stuff was happening regardless of mobile or desktop.
2024-11-27 17:33:31 +01:00

60 lines
1.3 KiB
JavaScript

import Service, { service } from "@ember/service";
import {
alertChannel,
onNotification as onDesktopNotification,
} from "discourse/lib/desktop-notifications";
import { isTesting } from "discourse-common/config/environment";
import { bind } from "discourse-common/utils/decorators";
export default class ChatNotificationManager extends Service {
@service chat;
@service currentUser;
@service appEvents;
@service site;
willDestroy() {
super.willDestroy(...arguments);
if (!this.#shouldRun) {
return;
}
this.messageBus.unsubscribe(this.messageBusChannel, this.onMessage);
}
start() {
if (!this.#shouldRun) {
return;
}
this.messageBus.subscribe(this.messageBusChannel, this.onMessage);
}
get messageBusChannel() {
return `/chat${alertChannel(this.currentUser)}`;
}
@bind
async onMessage(data) {
// if the user is currently focused on this tab and channel,
// we don't want to show a desktop notification
if (
this.session.hasFocus &&
data.channel_id === this.chat.activeChannel?.id
) {
return;
}
return onDesktopNotification(
data,
this.siteSettings,
this.currentUser,
this.appEvents
);
}
get #shouldRun() {
return this.site.desktopView && this.chat.userCanChat && !isTesting();
}
}