Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/improsocial/impro
An extensible Bluesky client for web impro.social
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110import { NotificationService } from "/js/notificationService.js";import { ChatNotificationService } from "/js/chatNotificationService.js";import { DesktopNotificationService } from "/js/desktopNotificationService.js";import { AppBadgeService } from "/js/appBadgeService.js";import { startActiveTabMonitor } from "/js/activeTabMonitor.js";import { PushNotificationService } from "/js/push/pushNotificationService.js";import { showToast } from "/js/toasts.js";import { html } from "/js/lib/lit-html.js";
export class NotificationServiceManager { constructor({ session, api, auth, router }) { this.router = router; this.notificationService = session ? new NotificationService(api) : null; this.chatNotificationService = session ? new ChatNotificationService(api) : null; this.pushNotificationService = session ? new PushNotificationService(api, auth) : null; this.activeTabMonitor = startActiveTabMonitor(); this.desktopNotificationService = this.notificationService && this.chatNotificationService ? new DesktopNotificationService( this.notificationService, this.chatNotificationService, router, this.activeTabMonitor, ) : null; this.appBadgeService = this.notificationService && this.chatNotificationService ? new AppBadgeService( this.notificationService, this.chatNotificationService, this.pushNotificationService, ) : null; }
startAll() { const cleanups = [ this._listenForPushMessages(), this.notificationService?.start(), this.chatNotificationService?.start(), this.desktopNotificationService?.start(), this.appBadgeService?.start(), ].filter(Boolean);
this.pushNotificationService ?.reassertIfEnabled() .then(({ newlyRevoked }) => { if (!newlyRevoked) return; showToast( html`<div class="toast-with-link"> Push notifications need to be re-authorized.<a href="/settings/notifications" >Settings</a > </div>`, { style: "warning", timeout: 8000 }, ); }) .catch((error) => { console.error("Failed to re-assert push registration", error); });
return () => { for (const cleanup of cleanups) { cleanup(); } this.activeTabMonitor.stop(); }; }
// The service worker skips the system notification when a focused window // answers its liveness check, so confirm the message arrived and refresh // the counts immediately. _listenForPushMessages() { if (!("serviceWorker" in navigator)) return null; const handleMessage = (event) => { if (event.data?.type === "push-received") { const port = event.ports?.[0]; if (port && event.data.requestId) { port.postMessage({ type: "push-received-ack", requestId: event.data.requestId, }); } this.notificationService?.fetchNumNotifications().catch(console.error); this.chatNotificationService ?.fetchNumNotifications() .catch(console.error); } else if (event.data?.type === "notification-click") { this._openNotificationUrl(event.data.url); } }; navigator.serviceWorker.addEventListener("message", handleMessage); return () => navigator.serviceWorker.removeEventListener("message", handleMessage); }
_openNotificationUrl(url) { if (url === this.router.currentPath) { window.dispatchEvent(new CustomEvent("notification-open")); } else { this.router.go(url); } }}