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:
molvqingtai
2025-10-03 20:13:14 +08:00
parent 53af7ef096
commit b2432dfc66
2 changed files with 31 additions and 20 deletions

View File

@@ -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
})
)

View File

@@ -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)
}
}