优化后端鉴权与日志安全
This commit is contained in:
@@ -7,6 +7,13 @@ const NOTIFICATION_CONFIG_FILE_PATH = path.join(PROJECT_ROOT, 'data', 'notificat
|
||||
const DEFAULT_BARK_SERVER_URL = 'https://api.day.app'
|
||||
|
||||
type JsonObject = Record<string, any>
|
||||
type NotificationRecipient = {
|
||||
id: string
|
||||
name: string
|
||||
deviceKey?: string
|
||||
apiKey?: string
|
||||
enabled: boolean
|
||||
}
|
||||
|
||||
export function getNotificationConfigFilePath() {
|
||||
return NOTIFICATION_CONFIG_FILE_PATH
|
||||
@@ -26,7 +33,7 @@ export function listEnabledBarkRecipients(config: JsonObject = getNotificationCo
|
||||
}
|
||||
|
||||
return (Array.isArray(config.channels?.bark?.recipients) ? config.channels.bark.recipients : [])
|
||||
.filter((item) => item.enabled !== false && String(item.deviceKey || '').trim())
|
||||
.filter((item: JsonObject) => item.enabled !== false && String(item.deviceKey || '').trim())
|
||||
}
|
||||
|
||||
export function listEnabledWpushRecipients(config: JsonObject = getNotificationConfig()) {
|
||||
@@ -35,7 +42,7 @@ export function listEnabledWpushRecipients(config: JsonObject = getNotificationC
|
||||
}
|
||||
|
||||
return (Array.isArray(config.channels?.wpush?.recipients) ? config.channels.wpush.recipients : [])
|
||||
.filter((item) => item.enabled !== false && String(item.apiKey || item.apikey || '').trim())
|
||||
.filter((item: JsonObject) => item.enabled !== false && String(item.apiKey || item.apikey || '').trim())
|
||||
}
|
||||
|
||||
function loadNotificationConfigFromFile() {
|
||||
@@ -58,20 +65,20 @@ export function normalizeNotificationConfig(rawValue: unknown) {
|
||||
enabled: typeof bark.enabled === 'boolean' ? bark.enabled : true,
|
||||
serverUrl: normalizeBarkServerUrl(bark.serverUrl),
|
||||
recipients: (Array.isArray(bark.recipients) ? bark.recipients : [])
|
||||
.map((item) => normalizeBarkRecipient(item))
|
||||
.map((item: unknown) => normalizeBarkRecipient(item))
|
||||
.filter(Boolean),
|
||||
},
|
||||
wpush: {
|
||||
enabled: typeof wpush.enabled === 'boolean' ? wpush.enabled : true,
|
||||
recipients: (Array.isArray(wpush.recipients) ? wpush.recipients : [])
|
||||
.map((item) => normalizeWpushRecipient(item))
|
||||
.map((item: unknown) => normalizeWpushRecipient(item))
|
||||
.filter(Boolean),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeBarkRecipient(rawValue: unknown) {
|
||||
function normalizeBarkRecipient(rawValue: unknown): NotificationRecipient | null {
|
||||
if (!isPlainObject(rawValue)) {
|
||||
return null
|
||||
}
|
||||
@@ -97,7 +104,7 @@ function normalizeBarkServerUrl(value: unknown) {
|
||||
return normalized.replace(/\/+$/, '')
|
||||
}
|
||||
|
||||
function normalizeWpushRecipient(rawValue: unknown) {
|
||||
function normalizeWpushRecipient(rawValue: unknown): NotificationRecipient | null {
|
||||
if (!isPlainObject(rawValue)) {
|
||||
return null
|
||||
}
|
||||
@@ -125,11 +132,11 @@ export function createDefaultNotificationConfig() {
|
||||
bark: {
|
||||
enabled: true,
|
||||
serverUrl: DEFAULT_BARK_SERVER_URL,
|
||||
recipients: [],
|
||||
recipients: [] as NotificationRecipient[],
|
||||
},
|
||||
wpush: {
|
||||
enabled: true,
|
||||
recipients: [],
|
||||
recipients: [] as NotificationRecipient[],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import { sendBarkNotification } from './bark-service.js'
|
||||
import { sendWpushNotification } from './wpush-service.js'
|
||||
|
||||
type JsonObject = Record<string, any>
|
||||
type NotificationResult = ReturnType<typeof mapNotificationResult>
|
||||
type NotificationInput = {
|
||||
title?: string
|
||||
body?: string
|
||||
@@ -31,12 +32,12 @@ export async function sendInternalNotification(input: NotificationInput = {}) {
|
||||
successCount: 0,
|
||||
failedCount: 0,
|
||||
skippedCount: barkRecipients.length + wpushRecipients.length,
|
||||
results: [],
|
||||
results: [] as NotificationResult[],
|
||||
}
|
||||
}
|
||||
|
||||
const results = [
|
||||
...(await Promise.all(barkRecipients.map(async (recipient) => {
|
||||
...(await Promise.all(barkRecipients.map(async (recipient: JsonObject) => {
|
||||
try {
|
||||
const result = await sendBarkNotification({
|
||||
serverUrl: bark.serverUrl,
|
||||
@@ -64,7 +65,7 @@ export async function sendInternalNotification(input: NotificationInput = {}) {
|
||||
)
|
||||
}
|
||||
}))),
|
||||
...(await Promise.all(wpushRecipients.map(async (recipient) => {
|
||||
...(await Promise.all(wpushRecipients.map(async (recipient: JsonObject) => {
|
||||
try {
|
||||
const result = await sendWpushNotification({
|
||||
recipient,
|
||||
|
||||
Reference in New Issue
Block a user