增加xpush
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import { logWarn } from '../../utils/logger.js'
|
||||
import { getNotificationConfig, listEnabledBarkRecipients } from './config-service.js'
|
||||
import {
|
||||
getNotificationConfig,
|
||||
listEnabledBarkRecipients,
|
||||
listEnabledWpushRecipients,
|
||||
} from './config-service.js'
|
||||
import { sendBarkNotification } from './bark-service.js'
|
||||
import { sendWpushNotification } from './wpush-service.js'
|
||||
|
||||
type JsonObject = Record<string, any>
|
||||
type NotificationInput = {
|
||||
@@ -13,7 +18,8 @@ type NotificationInput = {
|
||||
export async function sendInternalNotification(input: NotificationInput = {}) {
|
||||
const config = getNotificationConfig()
|
||||
const bark = (config.channels?.bark || {}) as { serverUrl?: string }
|
||||
const recipients = listEnabledBarkRecipients(config)
|
||||
const barkRecipients = listEnabledBarkRecipients(config)
|
||||
const wpushRecipients = listEnabledWpushRecipients(config)
|
||||
const title = String(input.title || '订单系统通知').trim() || '订单系统通知'
|
||||
const body = String(input.body || '').trim()
|
||||
const category = String(input.category || 'system').trim() || 'system'
|
||||
@@ -21,44 +27,73 @@ export async function sendInternalNotification(input: NotificationInput = {}) {
|
||||
if (config.enabled === false) {
|
||||
return {
|
||||
enabled: false,
|
||||
channel: 'bark',
|
||||
channel: 'internal',
|
||||
successCount: 0,
|
||||
failedCount: 0,
|
||||
skippedCount: recipients.length,
|
||||
skippedCount: barkRecipients.length + wpushRecipients.length,
|
||||
results: [],
|
||||
}
|
||||
}
|
||||
|
||||
const results = await Promise.all(recipients.map(async (recipient) => {
|
||||
try {
|
||||
const result = await sendBarkNotification({
|
||||
serverUrl: bark.serverUrl,
|
||||
recipient,
|
||||
title,
|
||||
body,
|
||||
group: `订单系统/${category}`,
|
||||
url: input.url,
|
||||
})
|
||||
return mapNotificationResult(recipient, true, '', result.status, result.response)
|
||||
} catch (error) {
|
||||
logWarn('[notification/bark]', 'Bark 内部通知发送失败', {
|
||||
recipientId: recipient.id,
|
||||
recipientName: recipient.name,
|
||||
error,
|
||||
})
|
||||
return mapNotificationResult(
|
||||
recipient,
|
||||
false,
|
||||
error instanceof Error ? error.message : 'Bark 内部通知发送失败',
|
||||
isPlainObject(error) ? Number(error.statusCode || 0) || 0 : 0,
|
||||
isPlainObject(error) ? error.context || null : null,
|
||||
)
|
||||
}
|
||||
}))
|
||||
const results = [
|
||||
...(await Promise.all(barkRecipients.map(async (recipient) => {
|
||||
try {
|
||||
const result = await sendBarkNotification({
|
||||
serverUrl: bark.serverUrl,
|
||||
recipient,
|
||||
title,
|
||||
body,
|
||||
group: `订单系统/${category}`,
|
||||
url: input.url,
|
||||
})
|
||||
return mapNotificationResult('bark', recipient, recipient.deviceKey, true, '', result.status, result.response)
|
||||
} catch (error) {
|
||||
logWarn('[notification/bark]', 'Bark 内部通知发送失败', {
|
||||
recipientId: recipient.id,
|
||||
recipientName: recipient.name,
|
||||
error,
|
||||
})
|
||||
return mapNotificationResult(
|
||||
'bark',
|
||||
recipient,
|
||||
recipient.deviceKey,
|
||||
false,
|
||||
error instanceof Error ? error.message : 'Bark 内部通知发送失败',
|
||||
isPlainObject(error) ? Number(error.statusCode || 0) || 0 : 0,
|
||||
isPlainObject(error) ? error.context || null : null,
|
||||
)
|
||||
}
|
||||
}))),
|
||||
...(await Promise.all(wpushRecipients.map(async (recipient) => {
|
||||
try {
|
||||
const result = await sendWpushNotification({
|
||||
recipient,
|
||||
title,
|
||||
body,
|
||||
})
|
||||
return mapNotificationResult('wpush', recipient, recipient.apiKey, true, '', result.status, result.response)
|
||||
} catch (error) {
|
||||
logWarn('[notification/wpush]', 'WPush 内部通知发送失败', {
|
||||
recipientId: recipient.id,
|
||||
recipientName: recipient.name,
|
||||
error,
|
||||
})
|
||||
return mapNotificationResult(
|
||||
'wpush',
|
||||
recipient,
|
||||
recipient.apiKey,
|
||||
false,
|
||||
error instanceof Error ? error.message : 'WPush 内部通知发送失败',
|
||||
isPlainObject(error) ? Number(error.statusCode || 0) || 0 : 0,
|
||||
isPlainObject(error) ? error.context || null : null,
|
||||
)
|
||||
}
|
||||
}))),
|
||||
]
|
||||
|
||||
return {
|
||||
enabled: true,
|
||||
channel: 'bark',
|
||||
channel: 'internal',
|
||||
successCount: results.filter((item) => item.ok).length,
|
||||
failedCount: results.filter((item) => !item.ok).length,
|
||||
skippedCount: 0,
|
||||
@@ -67,16 +102,19 @@ export async function sendInternalNotification(input: NotificationInput = {}) {
|
||||
}
|
||||
|
||||
function mapNotificationResult(
|
||||
channel: string,
|
||||
recipient: JsonObject,
|
||||
recipientKey: unknown,
|
||||
ok: boolean,
|
||||
errorMessage: string,
|
||||
status: number,
|
||||
response: unknown,
|
||||
) {
|
||||
return {
|
||||
channel,
|
||||
recipientId: String(recipient.id || '').trim(),
|
||||
recipientName: String(recipient.name || '').trim(),
|
||||
recipientKeyMasked: maskDeviceKey(recipient.deviceKey),
|
||||
recipientKeyMasked: maskSecretKey(recipientKey),
|
||||
ok,
|
||||
status,
|
||||
errorMessage,
|
||||
@@ -84,7 +122,7 @@ function mapNotificationResult(
|
||||
}
|
||||
}
|
||||
|
||||
function maskDeviceKey(value: unknown) {
|
||||
function maskSecretKey(value: unknown) {
|
||||
const normalized = String(value || '').trim()
|
||||
if (!normalized) {
|
||||
return ''
|
||||
|
||||
Reference in New Issue
Block a user