Browse Source

Adding muting and deletion of subcriptions

custom-readme
Matthew Raymer 1 year ago
parent
commit
97d4ebf1ca
  1. BIN
      push_server
  2. 3
      src/Subscription.ts
  3. 32
      src/db.ts
  4. 8
      src/notificationService.ts
  5. 10
      src/subscriptionService.ts

BIN
push_server

Binary file not shown.

3
src/Subscription.ts

@ -14,4 +14,7 @@ export class Subscription {
@Column()
keys_auth: string;
@Column({ type: 'boolean', default: false })
muted: boolean;
}

32
src/db.ts

@ -41,7 +41,37 @@ class DBService {
subscription.endpoint = endpoint;
subscription.keys_auth = keys_auth;
subscription.keys_p256dh = keys_p256dh;
return await this.dataSource.manager.save(subscription);
return await this.dataSource.manager.save(Subscription, subscription);
}
async muteSubscription(endpoint: string, state: boolean): Promise<boolean> {
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<boolean> {
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;
}

8
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<void> {
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<void> {
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,

10
src/subscriptionService.ts

@ -34,6 +34,16 @@ class SubscriptionService {
}
async muteSubscription(endpoint: string, state: boolean): Promise<void> {
await this.dbService.muteSubscription(endpoint, state);
}
async removeSubscription(endpoint: string): Promise<void> {
await this.dbService.removeSubscription(endpoint);
}
async fetchSubscriptions(): Promise<Subscription[]> {
return this.dbService.getSubscriptions();
}

Loading…
Cancel
Save