Initial commit

This commit is contained in:
2025-03-06 12:24:20 +01:00
commit 7f3cefdc22
37 changed files with 6581 additions and 0 deletions

60
src/notif.ts Normal file
View File

@@ -0,0 +1,60 @@
import { PushSubscription, RequestOptions, SendResult, sendNotification } from "web-push";
import { readFileSync } from "fs";
import Notification from "./schemas/Notification";
import { allNotif, groupNotif, roomNotif, userNotif } from "./pipelines/notif";
export class NotifcationHelper {
private options: RequestOptions
constructor () {
let keys = JSON.parse(readFileSync("./config/keys.json", 'utf-8'))
this.options = {
vapidDetails: {
subject: "CHANGE ME",
privateKey: keys.privateKey,
publicKey: keys.publicKey
}
}
}
private async send(message: string, subscriptions: PushSubscription[]) {
var count = 0;
var subslen = subscriptions.length
for (const v of subscriptions) {
var result
try {
result = await sendNotification(v, message, this.options)
count++
} catch (error) {
if (error.statusCode == 410) {
console.log("GONE")
await Notification.findOneAndDelete({endpoint: v.endpoint, keys: v.keys})
subslen--
}
else console.log(error)
}
}
return {sent: count, possible: subslen}
}
private rcpt(message: string) {
return {
user: async (uname: string) => {
return await this.send(message, await Notification.aggregate(userNotif(uname)))
},
room: async (room: number) => {
return await this.send(message, await Notification.aggregate(roomNotif(room)))
},
group: async (group: string) => {
return await this.send(message, await Notification.aggregate(groupNotif(group)))
},
withRoom: async () => {
return await this.send(message, await Notification.aggregate(allNotif()))
}
}
}
simpleMessage(title: string, body: string) {
return this.rcpt(JSON.stringify({notification: {title: title, body: body}}))
}
}