mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-03 19:15:37 +08:00
refactor(backend): move db repos to typescript and update imports
This commit is contained in:
42
server/src/modules/push-sub/push-sub.services.ts
Normal file
42
server/src/modules/push-sub/push-sub.services.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import webPush from 'web-push';
|
||||
|
||||
import { vapidKeysDb } from '@/shared/database/repositories/vapid-keys.js';
|
||||
|
||||
type VapidKeyPair = {
|
||||
publicKey: string;
|
||||
privateKey: string;
|
||||
};
|
||||
|
||||
let cachedKeys: VapidKeyPair | null = null;
|
||||
|
||||
function ensureVapidKeys(): VapidKeyPair {
|
||||
if (cachedKeys) return cachedKeys;
|
||||
|
||||
const existingKeys = vapidKeysDb.getVapidKeys();
|
||||
if (existingKeys) {
|
||||
cachedKeys = existingKeys;
|
||||
return existingKeys;
|
||||
}
|
||||
|
||||
const generatedKeys = webPush.generateVAPIDKeys();
|
||||
vapidKeysDb.createVapidKeys(generatedKeys.publicKey, generatedKeys.privateKey);
|
||||
cachedKeys = generatedKeys;
|
||||
return generatedKeys;
|
||||
}
|
||||
|
||||
function getPublicKey(): string {
|
||||
return ensureVapidKeys().publicKey;
|
||||
}
|
||||
|
||||
function configureWebPush(): void {
|
||||
const keys = ensureVapidKeys();
|
||||
webPush.setVapidDetails(
|
||||
'mailto:noreply@claudecodeui.local',
|
||||
keys.publicKey,
|
||||
keys.privateKey
|
||||
);
|
||||
console.log('Web Push notifications configured');
|
||||
}
|
||||
|
||||
export { ensureVapidKeys, getPublicKey, configureWebPush };
|
||||
|
||||
Reference in New Issue
Block a user