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,11 +22,7 @@ export class ChatInfoComponent {
protected readonly getServerTextColor = getServerTextColor; protected readonly getServerTextColor = getServerTextColor;
public enableNotifications() { public enableNotifications() {
this.notificationService.requestPermission().then( this.notificationService.enableNotifications(this.selectedServer());
() => {
this.notificationService.enableNotifications(this.selectedServer());
}
);
} }
public disableNotifications() { public disableNotifications() {

View File

@ -8,43 +8,13 @@ export class NotificationService {
private readonly _notificationEnabled = signal<Record<string, boolean>>( private readonly _notificationEnabled = signal<Record<string, boolean>>(
this.getStoredNotificationSettings() this.getStoredNotificationSettings()
); );
public readonly notificationEnabled = this._notificationEnabled.asReadonly();
async requestPermission(): Promise<boolean> { public notify(server: string) {
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) {
if (!this.isNotificationEnabled(server)) { if (!this.isNotificationEnabled(server)) {
return; return;
} }
if (Notification.permission === 'granted') { this.playSound();
if (body === null) {
new Notification(title);
} else {
new Notification(title, {body});
}
this.playSound();
}
} }
private enableSound() { private enableSound() {
@ -72,11 +42,12 @@ export class NotificationService {
return this._notificationEnabled()[server]; return this._notificationEnabled()[server];
} }
enableNotifications(server: string) { public enableNotifications(server: string) {
this.setNotificationEnabled(server, true); this.setNotificationEnabled(server, true);
this.enableSound()
} }
disableNotifications(server: string) { public disableNotifications(server: string) {
this.setNotificationEnabled(server, false); 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 {ChatServer} from '@pages/altitude/chat/objects/chat-server.object';
import {NotificationService} from '@pages/altitude/chat/service/chat-notification.service'; import {NotificationService} from '@pages/altitude/chat/service/chat-notification.service';
export interface ChatEvent {
type: string;
data: any;
}
interface SsePayloadEvent { interface SsePayloadEvent {
data: string; data: string;
} }
@ -60,14 +55,10 @@ export class ChatService implements OnDestroy {
find.unreadMessages++; find.unreadMessages++;
} }
}) })
const messagesByServer = chatMessages.reduce<Record<string, number>>((acc, message) => { new Set(chatMessages.map(message => message.server))
acc[message.server] = (acc[message.server] ?? 0) + 1; .forEach((server) => {
return acc; this.notificationService.notify(server);
}, {}); });
Object.entries(messagesByServer).forEach(([server, messageCount]) => {
this.notificationService.notify(server, `New messages`, `(${messageCount}) new messages in ${server}`);
});
}); });
source.onerror = (err) => { source.onerror = (err) => {