Simplify notification logic by removing permission handling, consolidating methods, and refining server-based notification management.

This commit is contained in:
akastijn 2026-07-19 19:08:12 +02:00
parent 660fd4eb9c
commit 564582a6f9
3 changed files with 11 additions and 53 deletions

View File

@ -22,12 +22,8 @@ export class ChatInfoComponent {
protected readonly getServerTextColor = getServerTextColor;
public enableNotifications() {
this.notificationService.requestPermission().then(
() => {
this.notificationService.enableNotifications(this.selectedServer());
}
);
}
public disableNotifications() {
this.notificationService.disableNotifications(this.selectedServer());

View File

@ -8,43 +8,13 @@ export class NotificationService {
private readonly _notificationEnabled = signal<Record<string, boolean>>(
this.getStoredNotificationSettings()
);
public readonly notificationEnabled = this._notificationEnabled.asReadonly();
async requestPermission(): Promise<boolean> {
if (!('Notification' in window)) {
console.log('This browser does not support notifications');
return false;
}
if (Notification.permission === 'granted') {
return true;
}
if (Notification.permission === 'denied') {
console.log('Notification permission was denied');
return false;
}
const permission = await Notification.requestPermission();
if (permission === 'granted') {
this.enableSound()
return true
}
return false;
}
notify(server: string, title: string, body: string | null) {
public notify(server: string) {
if (!this.isNotificationEnabled(server)) {
return;
}
if (Notification.permission === 'granted') {
if (body === null) {
new Notification(title);
} else {
new Notification(title, {body});
}
this.playSound();
}
}
private enableSound() {
@ -72,11 +42,12 @@ export class NotificationService {
return this._notificationEnabled()[server];
}
enableNotifications(server: string) {
public enableNotifications(server: string) {
this.setNotificationEnabled(server, true);
this.enableSound()
}
disableNotifications(server: string) {
public disableNotifications(server: string) {
this.setNotificationEnabled(server, false);
}

View File

@ -7,11 +7,6 @@ import {normalizeComponent} from '@pages/altitude/chat/mini-message/normalize.ut
import {ChatServer} from '@pages/altitude/chat/objects/chat-server.object';
import {NotificationService} from '@pages/altitude/chat/service/chat-notification.service';
export interface ChatEvent {
type: string;
data: any;
}
interface SsePayloadEvent {
data: string;
}
@ -60,13 +55,9 @@ export class ChatService implements OnDestroy {
find.unreadMessages++;
}
})
const messagesByServer = chatMessages.reduce<Record<string, number>>((acc, message) => {
acc[message.server] = (acc[message.server] ?? 0) + 1;
return acc;
}, {});
Object.entries(messagesByServer).forEach(([server, messageCount]) => {
this.notificationService.notify(server, `New messages`, `(${messageCount}) new messages in ${server}`);
new Set(chatMessages.map(message => message.server))
.forEach((server) => {
this.notificationService.notify(server);
});
});