83 lines
2.8 KiB
JavaScript
83 lines
2.8 KiB
JavaScript
import { hasRemoteConfig } from '@/utils/runtime-config.js'
|
|
import {
|
|
normalizeNotificationDetail,
|
|
normalizeNotifications
|
|
} from './notification-contract.js'
|
|
import { normalizeResourcePathId } from './request-normalizers.js'
|
|
import {
|
|
createRequestError,
|
|
requestStrict
|
|
} from './request-client.js'
|
|
|
|
const requireRemoteNotifications = (label, operation = '读取') => {
|
|
if (hasRemoteConfig()) return
|
|
throw createRequestError(
|
|
`${label}${operation}需要真实服务,当前本地预览不会伪造结果`,
|
|
operation === '读取' ? 'REMOTE_READ_REQUIRED' : 'REMOTE_WRITE_REQUIRED'
|
|
)
|
|
}
|
|
|
|
export const notificationApi = {
|
|
async getNotifications(requestOptions = {}) {
|
|
requireRemoteNotifications('消息通知')
|
|
const notificationRows = await requestStrict({
|
|
url: '/genealogy/app/notifications',
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeNotifications(notificationRows)
|
|
},
|
|
|
|
async getNotificationDetail(notificationId, requestOptions = {}) {
|
|
const normalizedNotificationId = normalizeResourcePathId(notificationId, '通知标识')
|
|
requireRemoteNotifications('通知详情')
|
|
const notificationDetail = await requestStrict({
|
|
url: `/genealogy/app/notifications/${normalizedNotificationId}`,
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return normalizeNotificationDetail(notificationDetail, normalizedNotificationId)
|
|
},
|
|
|
|
async getUnreadNotificationCount(requestOptions = {}) {
|
|
requireRemoteNotifications('未读通知数量')
|
|
const unreadCount = await requestStrict({
|
|
url: '/genealogy/app/notifications/unread-count',
|
|
method: 'GET'
|
|
}, {
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
if (!Number.isSafeInteger(unreadCount) || unreadCount < 0) {
|
|
throw createRequestError('未读通知数量响应无效', 'NOTIFICATION_COUNT_RESPONSE_INVALID')
|
|
}
|
|
return unreadCount
|
|
},
|
|
|
|
async markNotificationRead(notificationId, requestOptions = {}) {
|
|
const normalizedNotificationId = normalizeResourcePathId(notificationId, '通知标识')
|
|
requireRemoteNotifications('通知已读状态', '写入')
|
|
await requestStrict({
|
|
url: `/genealogy/app/notifications/${normalizedNotificationId}/read`,
|
|
method: 'POST'
|
|
}, {
|
|
requireData: false,
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return null
|
|
},
|
|
|
|
async markAllNotificationsRead(requestOptions = {}) {
|
|
requireRemoteNotifications('全部通知已读状态', '写入')
|
|
await requestStrict({
|
|
url: '/genealogy/app/notifications/read-all',
|
|
method: 'POST'
|
|
}, {
|
|
requireData: false,
|
|
requestController: requestOptions.requestController ?? null
|
|
})
|
|
return null
|
|
}
|
|
}
|