From 97d4ebf1ca332254bd159132b07ea2ccf49ba169 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Wed, 6 Sep 2023 18:08:09 +0800 Subject: [PATCH] Adding muting and deletion of subcriptions --- push_server | Bin 16384 -> 20480 bytes src/Subscription.ts | 3 +++ src/db.ts | 32 +++++++++++++++++++++++++++++++- src/notificationService.ts | 8 +++++++- src/subscriptionService.ts | 10 ++++++++++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/push_server b/push_server index 9b7508a73c68ab5488d295381ccaed80513a7a01..dab629f0a59367e3ed4d3c17a3acbe9e1ddf4e9b 100644 GIT binary patch delta 205 zcmZo@U~E{xI6+#FnSp_U6^NODm=VODsADY3%%GR{l9%6rftlBXf$tul5w8cY*2czd zJR1B4j2z;YmW<7+K-qs_mS9O@Qch|~YHmS(V>%1FxT-2+v*2VaepyD=$r1d^rF9gP za!X56Q^JwBt&i~ { + let result = false; + if (this.isReady) { + let result = await this.dataSource.manager.findOne(Subscription, { where: { endpoint: endpoint } }); + result.muted = state; + await this.dataSource.manager.save(Subscription, result); + + } else { + console.log(__filename, "Database not ready.") + + } + return result; + } + + + async removeSubscription(endpoint: string): Promise { + let result = null; + if (this.isReady) { + let subscription = await this.dataSource.manager.findOne(Subscription, { where: { endpoint: endpoint } }); + await this.dataSource.manager.save(Subscription, subscription); + result = true; + + } else { + console.log(__filename, "Database not ready.") + + } + return result; } diff --git a/src/notificationService.ts b/src/notificationService.ts index 310c0e3..7ccc930 100644 --- a/src/notificationService.ts +++ b/src/notificationService.ts @@ -17,13 +17,16 @@ export class NotificationService { private subscriptionService: SubscriptionService = SubscriptionService.getInstance(); private vapidService: VapidService = VapidService.getInstance(); + constructor() { } + private generateSalt(length = 16): Buffer { return crypto.randomBytes(length); } - + + async broadcast(message: Message): Promise { const subscriptions = await this.subscriptionService.fetchSubscriptions(); @@ -32,10 +35,12 @@ export class NotificationService { } } + async sendNotification(subscription: Subscription, message: Message) { await this.pushToEndpoint(subscription, message); } + private async pushToEndpoint(subscription: Subscription, message: Message): Promise { const payload = JSON.stringify(message); @@ -77,6 +82,7 @@ export class NotificationService { }); } + private encrypt(publicKey: string, auth: string, payload: string): Buffer { http_ece.keys = { 'p256dh': publicKey, diff --git a/src/subscriptionService.ts b/src/subscriptionService.ts index d9aed2d..40578a0 100644 --- a/src/subscriptionService.ts +++ b/src/subscriptionService.ts @@ -34,6 +34,16 @@ class SubscriptionService { } + async muteSubscription(endpoint: string, state: boolean): Promise { + await this.dbService.muteSubscription(endpoint, state); + } + + + async removeSubscription(endpoint: string): Promise { + await this.dbService.removeSubscription(endpoint); + } + + async fetchSubscriptions(): Promise { return this.dbService.getSubscriptions(); }