fix(core): not revalidate notification count when logged out (#11617)

This commit is contained in:
EYHN
2025-04-10 19:54:36 +08:00
committed by GitHub
parent ffad5d0a2e
commit 0f39ab4ea4
2 changed files with 25 additions and 6 deletions

View File

@@ -6,7 +6,12 @@ export { NotificationType } from './stores/notification';
import type { Framework } from '@toeverything/infra'; import type { Framework } from '@toeverything/infra';
import { GraphQLService, ServerScope, ServerService } from '../cloud'; import {
AuthService,
GraphQLService,
ServerScope,
ServerService,
} from '../cloud';
import { GlobalSessionState } from '../storage'; import { GlobalSessionState } from '../storage';
import { NotificationCountService } from './services/count'; import { NotificationCountService } from './services/count';
import { NotificationListService } from './services/list'; import { NotificationListService } from './services/list';
@@ -17,7 +22,7 @@ export function configureNotificationModule(framework: Framework) {
framework framework
.scope(ServerScope) .scope(ServerScope)
.service(NotificationService, [NotificationStore]) .service(NotificationService, [NotificationStore])
.service(NotificationCountService, [NotificationStore]) .service(NotificationCountService, [NotificationStore, AuthService])
.service(NotificationListService, [ .service(NotificationListService, [
NotificationStore, NotificationStore,
NotificationCountService, NotificationCountService,

View File

@@ -12,17 +12,24 @@ import {
} from '@toeverything/infra'; } from '@toeverything/infra';
import { EMPTY, mergeMap, switchMap, timer } from 'rxjs'; import { EMPTY, mergeMap, switchMap, timer } from 'rxjs';
import { AccountChanged, type AuthService } from '../../cloud';
import { ServerStarted } from '../../cloud/events/server-started'; import { ServerStarted } from '../../cloud/events/server-started';
import { ApplicationFocused } from '../../lifecycle'; import { ApplicationFocused } from '../../lifecycle';
import type { NotificationStore } from '../stores/notification'; import type { NotificationStore } from '../stores/notification';
@OnEvent(ApplicationFocused, s => s.handleApplicationFocused) @OnEvent(ApplicationFocused, s => s.handleApplicationFocused)
@OnEvent(ServerStarted, s => s.handleServerStarted) @OnEvent(ServerStarted, s => s.handleServerStarted)
@OnEvent(AccountChanged, s => s.handleAccountChanged)
export class NotificationCountService extends Service { export class NotificationCountService extends Service {
constructor(private readonly store: NotificationStore) { constructor(
private readonly store: NotificationStore,
private readonly authService: AuthService
) {
super(); super();
} }
loggedIn$ = this.authService.session.status$.map(v => v === 'authenticated');
readonly count$ = LiveData.from(this.store.watchNotificationCountCache(), 0); readonly count$ = LiveData.from(this.store.watchNotificationCountCache(), 0);
readonly isLoading$ = new LiveData(false); readonly isLoading$ = new LiveData(false);
readonly error$ = new LiveData<any>(null); readonly error$ = new LiveData<any>(null);
@@ -32,9 +39,12 @@ export class NotificationCountService extends Service {
return timer(0, 30000); // revalidate every 30 seconds return timer(0, 30000); // revalidate every 30 seconds
}), }),
exhaustMapWithTrailing(() => { exhaustMapWithTrailing(() => {
return fromPromise(signal => return fromPromise(signal => {
this.store.getNotificationCount(signal) if (!this.loggedIn$.value) {
).pipe( return Promise.resolve(0);
}
return this.store.getNotificationCount(signal);
}).pipe(
mergeMap(result => { mergeMap(result => {
this.setCount(result ?? 0); this.setCount(result ?? 0);
return EMPTY; return EMPTY;
@@ -57,6 +67,10 @@ export class NotificationCountService extends Service {
this.revalidate(); this.revalidate();
} }
handleAccountChanged() {
this.revalidate();
}
setCount(count: number) { setCount(count: number) {
this.store.setNotificationCountCache(count); this.store.setNotificationCountCache(count);
} }