mirror of
https://github.com/molvqingtai/WebChat.git
synced 2025-11-25 03:15:08 +08:00
perf(notification): improve notification logic and smart filtering
- refactor notification domain logic to use early returns pattern - add self-message filtering to prevent notifications from own messages - implement smart notification control to avoid notifications when user is active on same site - remove debug console.log and improve variable naming - maintain backward compatibility for atUsers field handling
This commit is contained in:
@@ -73,24 +73,27 @@ const NotificationDomain = Remesh.domain({
|
||||
const onMessage$ = merge(onTextMessage$).pipe(
|
||||
map((message) => {
|
||||
const notificationEnabled = get(IsEnabledQuery())
|
||||
if (notificationEnabled) {
|
||||
// Compatible with old versions, without the atUsers field
|
||||
if (message.atUsers) {
|
||||
const userInfo = get(userInfoDomain.query.UserInfoQuery())
|
||||
const hasAtSelf = message.atUsers.find((user) => user.userId === userInfo?.id)
|
||||
if (userInfo?.notificationType === 'all') {
|
||||
return PushCommand(message)
|
||||
}
|
||||
if (userInfo?.notificationType === 'at' && hasAtSelf) {
|
||||
return PushCommand(message)
|
||||
}
|
||||
return null
|
||||
} else {
|
||||
return PushCommand(message)
|
||||
}
|
||||
} else {
|
||||
|
||||
if (!notificationEnabled) {
|
||||
return null
|
||||
}
|
||||
|
||||
const userInfo = get(userInfoDomain.query.UserInfoQuery())
|
||||
if (message.userId === userInfo?.id) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (userInfo?.notificationType === 'all') {
|
||||
return PushCommand(message)
|
||||
}
|
||||
|
||||
if (userInfo?.notificationType === 'at') {
|
||||
const hasAtSelf = message.atUsers.find((user) => user.userId === userInfo?.id)
|
||||
if (hasAtSelf) {
|
||||
return PushCommand(message)
|
||||
}
|
||||
}
|
||||
return null
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
@@ -35,15 +35,23 @@ export class Notification implements NotificationExternType {
|
||||
})
|
||||
}
|
||||
async push(message: ChatRoomTextMessage & { meta?: { tab?: MessageTab } }) {
|
||||
const tab = message.meta?.tab
|
||||
console.log(tab)
|
||||
const messageTab = message.meta?.tab
|
||||
const tabs = await browser.tabs.query({ active: true })
|
||||
const hasActiveSameSiteTab =
|
||||
messageTab?.url &&
|
||||
tabs.some((tab) => {
|
||||
return tab.url && new URL(messageTab.url!).origin === new URL(tab.url).origin
|
||||
})
|
||||
|
||||
if (hasActiveSameSiteTab) return
|
||||
|
||||
const id = await browser.notifications.create({
|
||||
type: 'basic',
|
||||
iconUrl: message.userAvatar,
|
||||
title: message.username,
|
||||
message: message.body,
|
||||
contextMessage: tab?.url
|
||||
contextMessage: messageTab?.url
|
||||
})
|
||||
tab && this.historyNotificationTabs.set(id, tab)
|
||||
messageTab && this.historyNotificationTabs.set(id, messageTab)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user