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 { 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,

View File

@@ -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<any>(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);
}