From 0f39ab4ea415dc55e3e63080daba7e64db152428 Mon Sep 17 00:00:00 2001 From: EYHN Date: Thu, 10 Apr 2025 19:54:36 +0800 Subject: [PATCH] fix(core): not revalidate notification count when logged out (#11617) --- .../core/src/modules/notification/index.ts | 9 ++++++-- .../modules/notification/services/count.ts | 22 +++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/frontend/core/src/modules/notification/index.ts b/packages/frontend/core/src/modules/notification/index.ts index a1f5f97e34..f81d423a63 100644 --- a/packages/frontend/core/src/modules/notification/index.ts +++ b/packages/frontend/core/src/modules/notification/index.ts @@ -6,7 +6,12 @@ export { NotificationType } from './stores/notification'; import type { Framework } from '@toeverything/infra'; -import { GraphQLService, ServerScope, ServerService } from '../cloud'; +import { + AuthService, + GraphQLService, + ServerScope, + ServerService, +} from '../cloud'; import { GlobalSessionState } from '../storage'; import { NotificationCountService } from './services/count'; import { NotificationListService } from './services/list'; @@ -17,7 +22,7 @@ export function configureNotificationModule(framework: Framework) { framework .scope(ServerScope) .service(NotificationService, [NotificationStore]) - .service(NotificationCountService, [NotificationStore]) + .service(NotificationCountService, [NotificationStore, AuthService]) .service(NotificationListService, [ NotificationStore, NotificationCountService, diff --git a/packages/frontend/core/src/modules/notification/services/count.ts b/packages/frontend/core/src/modules/notification/services/count.ts index 8c57aff083..1861f039ad 100644 --- a/packages/frontend/core/src/modules/notification/services/count.ts +++ b/packages/frontend/core/src/modules/notification/services/count.ts @@ -12,17 +12,24 @@ import { } from '@toeverything/infra'; import { EMPTY, mergeMap, switchMap, timer } from 'rxjs'; +import { AccountChanged, type AuthService } from '../../cloud'; import { ServerStarted } from '../../cloud/events/server-started'; import { ApplicationFocused } from '../../lifecycle'; import type { NotificationStore } from '../stores/notification'; @OnEvent(ApplicationFocused, s => s.handleApplicationFocused) @OnEvent(ServerStarted, s => s.handleServerStarted) +@OnEvent(AccountChanged, s => s.handleAccountChanged) export class NotificationCountService extends Service { - constructor(private readonly store: NotificationStore) { + constructor( + private readonly store: NotificationStore, + private readonly authService: AuthService + ) { super(); } + loggedIn$ = this.authService.session.status$.map(v => v === 'authenticated'); + readonly count$ = LiveData.from(this.store.watchNotificationCountCache(), 0); readonly isLoading$ = new LiveData(false); readonly error$ = new LiveData(null); @@ -32,9 +39,12 @@ export class NotificationCountService extends Service { return timer(0, 30000); // revalidate every 30 seconds }), exhaustMapWithTrailing(() => { - return fromPromise(signal => - this.store.getNotificationCount(signal) - ).pipe( + return fromPromise(signal => { + if (!this.loggedIn$.value) { + return Promise.resolve(0); + } + return this.store.getNotificationCount(signal); + }).pipe( mergeMap(result => { this.setCount(result ?? 0); return EMPTY; @@ -57,6 +67,10 @@ export class NotificationCountService extends Service { this.revalidate(); } + handleAccountChanged() { + this.revalidate(); + } + setCount(count: number) { this.store.setNotificationCountCache(count); }