Simplify notification logic by removing permission handling, consolidating methods, and refining server-based notification management.
This commit is contained in:
parent
660fd4eb9c
commit
564582a6f9
|
|
@ -22,12 +22,8 @@ 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() {
|
||||||
this.notificationService.disableNotifications(this.selectedServer());
|
this.notificationService.disableNotifications(this.selectedServer());
|
||||||
|
|
|
||||||
|
|
@ -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') {
|
|
||||||
if (body === null) {
|
|
||||||
new Notification(title);
|
|
||||||
} else {
|
|
||||||
new Notification(title, {body});
|
|
||||||
}
|
|
||||||
this.playSound();
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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,13 +55,9 @@ 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}`);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user