Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1x 1x 1x 1x 1x 1x 133x 133x 133x 133x 133x 133x 1x 1x 55x 55x 55x 55x 55x 55x 55x 55x 1x 1x 3x 3x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 3x 1x | import {Resources} from "../../lib/resources/main.js"; import webpush from 'web-push'; import {State} from "./state.js"; export class NotificationPusher { constructor() { webpush.setVapidDetails( `mailto:${Resources.manager().username}`, Resources.manager().notificationVapidCredentials.publicKey, Resources.manager().notificationVapidCredentials.privateKey ); } async sendNotifications(state: State): Promise<void[]> { const notificationSubscriptions = Resources.manager().notificationSubscriptions; Resources.logger(this).info(`State changed to: ${state.serialize()}, sending notifications to ${notificationSubscriptions.length} listeners.`); return Promise.all( notificationSubscriptions.map( subscription => this.sendPushNotification(subscription, state) ) ) } async sendPushNotification(subscription: webpush.PushSubscription, state: State) { try { Resources.logger(this).debug(`Sending notification to ${subscription.endpoint}`); await webpush.sendNotification(subscription, JSON.stringify(state.serialize())); } catch (err) { Resources.logger(this).error(`Failed to send notification to subscription: ${err.message}`); if (err instanceof webpush.WebPushError) { Resources.logger(this).error(`WebPush error: ${err.statusCode} - ${err.body}`); if (err.statusCode === 410) { // Subscription is no longer valid, remove it Resources.logger(this).warn(`Removing subscription, because it has expired`); Resources.manager().removeNotificationSubscription(subscription); } } } } } |