KI-AGENT: Mobile Push Registrierung anbinden
This commit is contained in:
@@ -3,6 +3,7 @@ import webPush from "web-push"
|
||||
import { and, desc, eq, inArray, isNull, sql } from "drizzle-orm"
|
||||
import {
|
||||
authUsers,
|
||||
notificationMobilePushDevices,
|
||||
notificationPushSubscriptions,
|
||||
notificationsEventTypes,
|
||||
notificationsItems,
|
||||
@@ -10,6 +11,7 @@ import {
|
||||
notificationsPreferencesDefaults,
|
||||
} from "../../db/schema"
|
||||
import { secrets } from "../utils/secrets"
|
||||
import { pushServerClient } from "./push-server.client"
|
||||
|
||||
export type NotificationChannel = "inapp" | "email" | "push" | "webhook" | "sms"
|
||||
export type NotificationStatus = "queued" | "sent" | "failed" | "read"
|
||||
@@ -280,18 +282,8 @@ export class NotificationService {
|
||||
}
|
||||
|
||||
private async deliverPush(item: typeof notificationsItems.$inferSelect) {
|
||||
if (!secrets.WEB_PUSH_PUBLIC_KEY || !secrets.WEB_PUSH_PRIVATE_KEY) {
|
||||
await this.markFailed(item.id, "Web Push ist nicht konfiguriert")
|
||||
return { success: false, id: item.id, channel: item.channel }
|
||||
}
|
||||
|
||||
webPush.setVapidDetails(
|
||||
secrets.WEB_PUSH_SUBJECT || "mailto:admin@example.com",
|
||||
secrets.WEB_PUSH_PUBLIC_KEY,
|
||||
secrets.WEB_PUSH_PRIVATE_KEY
|
||||
)
|
||||
|
||||
const subscriptions = await this.server.db
|
||||
const subscriptions = secrets.WEB_PUSH_PUBLIC_KEY && secrets.WEB_PUSH_PRIVATE_KEY
|
||||
? await this.server.db
|
||||
.select()
|
||||
.from(notificationPushSubscriptions)
|
||||
.where(and(
|
||||
@@ -299,8 +291,18 @@ export class NotificationService {
|
||||
eq(notificationPushSubscriptions.userId, item.userId),
|
||||
isNull(notificationPushSubscriptions.disabledAt)
|
||||
))
|
||||
: []
|
||||
|
||||
if (!subscriptions.length) {
|
||||
const mobileDevices = await this.server.db
|
||||
.select({ centralDeviceId: notificationMobilePushDevices.centralDeviceId })
|
||||
.from(notificationMobilePushDevices)
|
||||
.where(and(
|
||||
eq(notificationMobilePushDevices.tenantId, item.tenantId),
|
||||
eq(notificationMobilePushDevices.userId, item.userId),
|
||||
isNull(notificationMobilePushDevices.disabledAt)
|
||||
))
|
||||
|
||||
if (!subscriptions.length && !mobileDevices.length) {
|
||||
await this.markFailed(item.id, "Keine aktive Push-Subscription")
|
||||
return { success: false, id: item.id, channel: item.channel }
|
||||
}
|
||||
@@ -315,6 +317,37 @@ export class NotificationService {
|
||||
let delivered = 0
|
||||
const errors: string[] = []
|
||||
|
||||
if (mobileDevices.length) {
|
||||
try {
|
||||
const result = await pushServerClient.sendPush({
|
||||
idempotencyKey: `notification:${item.id}`,
|
||||
devices: mobileDevices.map((device) => device.centralDeviceId),
|
||||
priority: "high",
|
||||
ttlSeconds: 3600,
|
||||
notification: {
|
||||
title: item.title,
|
||||
body: item.message,
|
||||
},
|
||||
data: {
|
||||
notificationId: item.id,
|
||||
...(typeof item.payload === "object" && item.payload !== null ? item.payload as Record<string, unknown> : {}),
|
||||
},
|
||||
})
|
||||
delivered += result.accepted
|
||||
if (result.rejected) errors.push(`${result.rejected} mobile Geräte vom Push-Server abgelehnt`)
|
||||
} catch (error: any) {
|
||||
errors.push(error?.message || String(error))
|
||||
}
|
||||
}
|
||||
|
||||
if (subscriptions.length) {
|
||||
webPush.setVapidDetails(
|
||||
secrets.WEB_PUSH_SUBJECT || "mailto:admin@example.com",
|
||||
secrets.WEB_PUSH_PUBLIC_KEY!,
|
||||
secrets.WEB_PUSH_PRIVATE_KEY!
|
||||
)
|
||||
}
|
||||
|
||||
for (const subscription of subscriptions) {
|
||||
try {
|
||||
await webPush.sendNotification({
|
||||
|
||||
Reference in New Issue
Block a user