diff --git a/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts b/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts new file mode 100644 index 0000000000..282f522f7f --- /dev/null +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts @@ -0,0 +1,296 @@ +import { ConfigService } from '@nestjs/config'; +import { copyFile, mkdir, mkdtemp, readdir, readFile, rm, writeFile } from 'fs/promises'; +import { tmpdir } from 'os'; +import { join } from 'path'; + +import type { FSWatcher } from 'chokidar'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +import { pubsub, PUBSUB_CHANNEL } from '@app/core/pubsub.js'; +import { + NotificationImportance, + NotificationType, +} from '@app/unraid-api/graph/resolvers/notifications/notifications.model.js'; +import { NotificationsResolver } from '@app/unraid-api/graph/resolvers/notifications/notifications.resolver.js'; +import { NotificationsService } from '@app/unraid-api/graph/resolvers/notifications/notifications.service.js'; + +// Native macOS watching drops events during rename bursts; polling observes every change. +vi.mock('@app/environment.js', async (importOriginal) => ({ + ...(await importOriginal()), + CHOKIDAR_USEPOLLING: true, +})); + +const { UNREAD, ARCHIVE } = NotificationType; +const COUNT = 120; + +describe.sequential('bulk notification moves', () => { + let basePath: string; + let service: NotificationsService; + let subscription: number; + let overviewPublishes: number; + let ids: string[]; + + const file = (type: NotificationType, id: string) => join(basePath, type.toLowerCase(), id); + + beforeEach(async () => { + basePath = await mkdtemp(join(tmpdir(), 'notification-bulk-')); + await Promise.all( + Object.values(NotificationType).map((type) => mkdir(join(basePath, type.toLowerCase()))) + ); + ids = Array.from({ length: COUNT }, (_, index) => `docker-update-${index}.notify`); + for (const id of ids) { + await writeFile( + file(UNREAD, id), + 'timestamp=1700000000\nevent=Docker\nsubject=Update\ndescription=Update\nimportance=normal\n' + ); + await copyFile(file(UNREAD, id), file(ARCHIVE, id)); + } + subscription = await pubsub.subscribe(PUBSUB_CHANNEL.NOTIFICATION_OVERVIEW, () => { + overviewPublishes++; + }); + service = new NotificationsService( + new ConfigService({ store: { dynamix: { notify: { path: basePath } } } }) + ); + await Reflect.get(service, 'initialization'); + overviewPublishes = 0; + }); + + afterEach(async () => { + const watcher: FSWatcher | null = Reflect.get(NotificationsService, 'watcher'); + await watcher?.close(); + Reflect.set(NotificationsService, 'watcher', null); + await Reflect.get(service, 'overviewUpdate'); + await Reflect.get(service, 'warningsUpdate'); + pubsub.unsubscribe(subscription); + await rm(basePath, { recursive: true, force: true }); + }); + + it('archives every unread notification with a bounded number of overview broadcasts', async () => { + const { overview } = await service.archiveAll(); + + expect(await readdir(join(basePath, 'unread'))).toEqual([]); + expect(overview.unread.total).toBe(0); + expect(overview.archive.total).toBe(COUNT); + expect(overviewPublishes).toBeLessThan(COUNT / 10); + }); + + it('moves the remaining ids when some ids in a bulk request are missing', async () => { + const result = await service.archiveIds([...ids, 'missing.notify']); + + expect(result.successes).toBe(COUNT); + expect(result.errors).toHaveLength(1); + expect(service.getOverview().archive.total).toBe(COUNT); + + await service.unarchiveAll(); + expect(service.getOverview().unread.total).toBe(COUNT); + expect(service.getOverview().archive.total).toBe(0); + }); + + it('moves each requested id once even if the list contains duplicates', async () => { + const [id] = ids; + const resolver = new NotificationsResolver(service); + + await expect(resolver.archiveNotifications([id, id])).resolves.toMatchObject({ + archive: { total: 1 }, + }); + await expect(resolver.unarchiveNotifications([id, id])).resolves.toMatchObject({ + archive: { total: 0 }, + unread: { total: COUNT }, + }); + }); + + it('leaves unread entries that cannot be loaded in place', async () => { + const directory = 'directory.notify'; + await mkdir(file(UNREAD, directory)); + + const result = await service.archiveIds([directory, ids[0]]); + + expect(result.successes).toBe(1); + expect(result.errors).toHaveLength(1); + expect(await readdir(join(basePath, 'unread'))).toContain(directory); + expect(await readdir(join(basePath, 'archive'))).not.toContain(directory); + }); + + it('reports a reconciliation failure through the bulk mutation', async () => { + const [id] = ids; + const internal = service as unknown as { + readNotificationCopy(id: string, type: NotificationType): Promise; + }; + const read = internal.readNotificationCopy.bind(service); + const spy = vi.spyOn(internal, 'readNotificationCopy').mockImplementation((entry, type) => { + if (entry === id && type === ARCHIVE) return Promise.reject(new Error('read failed')); + return read(entry, type); + }); + + try { + const resolver = new NotificationsResolver(service); + await expect(resolver.archiveNotifications([id])).rejects.toThrow( + 'Bulk notification move did not complete' + ); + expect(await readdir(join(basePath, 'unread'))).not.toContain(id); + } finally { + spy.mockRestore(); + } + }); + + it('still applies an external edit made right after the service moved the file', async () => { + const [id] = ids; + await service.archiveNotification({ id }); + await writeFile( + file(ARCHIVE, id), + 'timestamp=1700000001\nevent=Docker\nsubject=Update\ndescription=Edited\nimportance=alert\n' + ); + + await vi.waitFor(() => expect(service.getOverview().archive.alert).toBe(1), { + timeout: 10_000, + }); + expect(service.getOverview().archive.total).toBe(1); + }, 20_000); + + it('restores in bulk without announcing each restored notification', async () => { + const added: string[] = []; + const addedSubscription = await pubsub.subscribe( + PUBSUB_CHANNEL.NOTIFICATION_ADDED, + (payload: { notificationAdded: { id: string } }) => added.push(payload.notificationAdded.id) + ); + const watcher: FSWatcher = Reflect.get(NotificationsService, 'watcher'); + let unreadAdds = 0; + let unreadUnlinks = 0; + let lastEventAt = Date.now(); + watcher.on('all', (event, path) => { + lastEventAt = Date.now(); + if (!path.startsWith(join(basePath, 'unread'))) return; + if (event === 'add') unreadAdds++; + if (event === 'unlink') unreadUnlinks++; + }); + try { + await service.archiveAll(); + await vi.waitFor(() => expect(unreadUnlinks).toBe(COUNT), { timeout: 10_000 }); + await service.unarchiveAll(); + expect(service.getOverview().unread.total).toBe(COUNT); + + await vi.waitFor(() => expect(unreadAdds).toBe(COUNT), { timeout: 10_000 }); + await vi.waitFor(() => expect(Date.now() - lastEventAt).toBeGreaterThan(1_000), { + timeout: 15_000, + interval: 100, + }); + await Reflect.get(service, 'overviewUpdate'); + expect(added).toEqual([]); + + const [id] = ids; + await service.archiveNotification({ id }); + await vi.waitFor(() => expect(unreadUnlinks).toBe(COUNT + 1), { timeout: 10_000 }); + await service.markAsUnread({ id }); + await vi.waitFor(() => expect(added).toEqual([id]), { timeout: 10_000 }); + } finally { + pubsub.unsubscribe(addedSubscription); + } + }, 30_000); + + it('restores archive-only notifications without overwriting newer unread copies', async () => { + const [legacy, archived] = ids; + await service.archiveNotification({ id: archived }); + await writeFile( + file(UNREAD, legacy), + 'timestamp=1700000003\nevent=Docker\nsubject=Update\ndescription=Edited\nimportance=alert\n' + ); + + for (const importance of [NotificationImportance.INFO, undefined]) { + const result = await service.unarchiveAll(importance); + expect(result.errorOccurred).toBe(false); + } + + expect(await readFile(file(UNREAD, legacy), 'utf8')).toContain('importance=alert'); + expect(await readdir(join(basePath, 'archive'))).toContain(legacy); + expect(await readdir(join(basePath, 'unread'))).toContain(archived); + }); + + it('preserves an unread copy that appeared after bulk restore selection', async () => { + const [id, restorable] = ids; + await service.archiveIds([id, restorable]); + const internal = service as unknown as { + selectIdsToMove(type: NotificationType): Promise; + }; + const select = internal.selectIdsToMove.bind(service); + const selection = vi.spyOn(internal, 'selectIdsToMove').mockImplementationOnce(async (type) => { + const selected = await select(type); + await writeFile( + file(UNREAD, id), + 'timestamp=1700000003\nevent=Docker\nsubject=Newer\ndescription=Edited\nimportance=alert\n' + ); + return selected; + }); + + try { + await expect(service.unarchiveAll()).resolves.toMatchObject({ + successes: 1, + errorOccurred: false, + overview: { unread: { alert: 1, total: COUNT }, archive: { total: 0 } }, + }); + await expect(service.unarchiveIds([id])).resolves.toMatchObject({ + successes: 0, + errorOccurred: false, + }); + expect(await readFile(file(UNREAD, id), 'utf8')).toContain('subject=Newer'); + expect(await readFile(file(ARCHIVE, id), 'utf8')).toContain('subject=Update'); + expect(await readdir(join(basePath, 'archive'))).not.toContain(restorable); + } finally { + selection.mockRestore(); + } + }); + + it('reports files that cannot be read while filtering a bulk move by importance', async () => { + await mkdir(file(UNREAD, 'unreadable.notify')); + + const result = await service.archiveAll(NotificationImportance.INFO); + + expect(result.successes).toBe(COUNT); + expect(result.errors).toHaveLength(1); + expect(result.errorOccurred).toBe(true); + await expect( + new NotificationsResolver(service).archiveAll(NotificationImportance.INFO) + ).rejects.toThrow(); + }); + + it('does not fail a delete because an unrelated id in the same batch failed', async () => { + const internal = service as unknown as { + enqueueOverviewUpdate(action: () => Promise): Promise; + synchronizeNotifications(ids: Iterable): Promise; + readNotificationCopy(id: string, type: NotificationType): Promise; + }; + const read = internal.readNotificationCopy.bind(service); + const spy = vi + .spyOn(internal, 'readNotificationCopy') + .mockImplementation((id, type) => + id === 'unrelated.notify' ? Promise.reject(new Error('read failed')) : read(id, type) + ); + const synchronize = internal.synchronizeNotifications.bind(service); + let deletionJoined = false; + const joinSpy = vi + .spyOn(internal, 'synchronizeNotifications') + .mockImplementation((requested: Iterable) => { + const list = [...requested]; + // Watcher events request one id at a time; only the deletion requests the whole folder. + if (list.length === COUNT) deletionJoined = true; + return synchronize(list); + }); + let release: (() => void) | undefined; + const blocker = internal.enqueueOverviewUpdate( + () => new Promise((resolve) => (release = resolve)) + ); + try { + await vi.waitFor(() => expect(release).toBeDefined()); + const unrelated = synchronize(['unrelated.notify']); + const deletion = service.deleteNotifications(ARCHIVE); + await vi.waitFor(() => expect(deletionJoined).toBe(true)); + release?.(); + await blocker; + await expect(deletion).resolves.toBeDefined(); + await expect(unrelated).resolves.toBeDefined(); + } finally { + release?.(); + joinSpy.mockRestore(); + spy.mockRestore(); + } + }); +}); diff --git a/api/src/unraid-api/graph/resolvers/notifications/notifications.resolver.ts b/api/src/unraid-api/graph/resolvers/notifications/notifications.resolver.ts index 1152af190f..6713c50249 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.resolver.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.resolver.ts @@ -21,6 +21,15 @@ import { NotificationsService } from '@app/unraid-api/graph/resolvers/notificati export class NotificationsResolver { constructor(readonly notificationsService: NotificationsService) {} + private checkBulkMove({ errorOccurred, successes }: { errorOccurred: boolean; successes: number }) { + if (errorOccurred) { + throw new AppError( + `Bulk notification move did not complete. ${successes} notification(s) moved and reconciled. Refresh notifications to see the current state.`, + 500 + ); + } + } + /**============================================ * Queries *=============================================**/ @@ -131,7 +140,8 @@ export class NotificationsResolver { @Args('ids', { type: () => [PrefixedID] }) ids: string[] ): Promise { - await this.notificationsService.archiveIds(ids); + const result = await this.notificationsService.archiveIds(ids); + this.checkBulkMove(result); return this.notificationsService.getOverview(); } @@ -160,8 +170,9 @@ export class NotificationsResolver { @Args('importance', { type: () => NotificationImportance, nullable: true }) importance?: NotificationImportance ): Promise { - const { overview } = await this.notificationsService.archiveAll(importance); - return overview; + const result = await this.notificationsService.archiveAll(importance); + this.checkBulkMove(result); + return result.overview; } @UsePermissions({ @@ -185,7 +196,8 @@ export class NotificationsResolver { @Args('ids', { type: () => [PrefixedID] }) ids: string[] ): Promise { - await this.notificationsService.unarchiveIds(ids); + const result = await this.notificationsService.unarchiveIds(ids); + this.checkBulkMove(result); return this.notificationsService.getOverview(); } @@ -198,8 +210,9 @@ export class NotificationsResolver { @Args('importance', { type: () => NotificationImportance, nullable: true }) importance?: NotificationImportance ): Promise { - const { overview } = await this.notificationsService.unarchiveAll(importance); - return overview; + const result = await this.notificationsService.unarchiveAll(importance); + this.checkBulkMove(result); + return result.overview; } @UsePermissions({ diff --git a/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts b/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts index 35a3149d09..249ca66498 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts @@ -35,10 +35,16 @@ import { batchProcess, formatDatetime, isFulfilled, isRejected, unraidTimestamp type NotificationFileState = Partial>; type NotificationFileEvent = 'add' | 'change' | 'unlink'; +type NotificationSyncResult = { unread: Notification | null } | { error: unknown }; +type PendingSynchronization = { + ids: Set; + result: Promise>; +}; @Injectable() export class NotificationsService { private static readonly FILE_IO_BATCH_SIZE = 32; + private static readonly QUIET_RESTORE_TTL_MS = 60_000; private logger = new Logger(NotificationsService.name); private static watcher: FSWatcher | null = null; /** @@ -53,6 +59,9 @@ export class NotificationsService { private notificationFiles = new Map(); private isHydratingOverview = false; private pendingNotificationEvents = new Map(); + private pendingSynchronization: PendingSynchronization | null = null; + /** Ids restored in bulk, mapped to when their "added" announcement suppression expires. */ + private quietRestores = new Map(); private static overview: NotificationOverview = { unread: { @@ -221,15 +230,34 @@ export class NotificationsService { return; } - const { unread } = await this.synchronizeNotification(basename(path)); + const id = basename(path); + const { unread } = await this.synchronizeNotification(id); if (type === NotificationType.UNREAD) { - if (event === 'add' && unread) { + if (event === 'add' && unread && !this.isQuietRestore(id)) { await pubsub.publish(PUBSUB_CHANNEL.NOTIFICATION_ADDED, { notificationAdded: unread }); } void this.publishWarningsAndAlerts(); } } + /** + * Bulk restores reuse existing ids, so their watcher "add" events are not new notifications. + * Counts still update; only the per-file announcement (a toast in the web UI) is skipped. + */ + private markQuietRestores(ids: string[]) { + const now = Date.now(); + for (const [id, expiresAt] of this.quietRestores) { + if (expiresAt <= now) this.quietRestores.delete(id); + } + const expiresAt = now + NotificationsService.QUIET_RESTORE_TTL_MS; + for (const id of ids) this.quietRestores.set(id, expiresAt); + } + + private isQuietRestore(id: string) { + const expiresAt = this.quietRestores.get(id); + return expiresAt !== undefined && expiresAt > Date.now(); + } + private enqueueOverviewUpdate(action: () => Promise): Promise { const update = this.overviewUpdate.catch(() => undefined).then(action); this.overviewUpdate = update; @@ -245,19 +273,58 @@ export class NotificationsService { } } - private synchronizeNotification(id: string) { - return this.enqueueOverviewUpdate(async () => { - const [unread, archive] = await Promise.all([ + private async synchronizeNotification(id: string) { + const result = (await this.synchronizeNotifications([id])).get(id); + if (!result) throw new Error(`Notification was not synchronized: ${id}`); + if ('error' in result) throw result.error; + return result; + } + + /** + * Re-reads both copies of each id and applies the differences to the overview. + * + * Requests made before the queued update starts join the same batch, so a burst of + * watcher events or a bulk move costs one queue slot and at most one overview broadcast. + */ + private synchronizeNotifications(ids: Iterable) { + if (!this.pendingSynchronization) { + const batch = new Set(); + const result = this.enqueueOverviewUpdate(async () => { + if (this.pendingSynchronization?.ids === batch) this.pendingSynchronization = null; + return this.applyNotificationStates([...batch]); + }); + this.pendingSynchronization = { ids: batch, result }; + } + const pending = this.pendingSynchronization; + for (const id of ids) pending.ids.add(id); + return pending.result; + } + + private async applyNotificationStates(ids: string[]) { + const reads = await this.settleInBatches(ids, (id) => + Promise.all([ this.readNotificationCopy(id, NotificationType.UNREAD), this.readNotificationCopy(id, NotificationType.ARCHIVE), - ]); - const changed = this.updateNotificationCounts(id, { - UNREAD: unread?.importance, - ARCHIVE: archive?.importance, - }); - if (changed) await this.publishOverview(); - return { unread }; + ]) + ); + const results = new Map(); + let changed = false; + reads.forEach((read, index) => { + const id = ids[index]; + if (isRejected(read)) { + results.set(id, { error: read.reason }); + return; + } + const [unread, archive] = read.value; + changed = + this.updateNotificationCounts(id, { + UNREAD: unread?.importance, + ARCHIVE: archive?.importance, + }) || changed; + results.set(id, { unread }); }); + if (changed) await this.publishOverview(); + return results; } private updateNotificationCounts(id: string, next: NotificationFileState): boolean { @@ -530,7 +597,12 @@ export class NotificationsService { if (copies[type] !== undefined) ids.add(id); } await emptyDir(folder); - await Promise.all([...ids].map((id) => this.synchronizeNotification(id))); + const synchronized = await this.synchronizeNotifications(ids); + // The batch can include unrelated watcher requests; only this deletion's ids matter here. + for (const id of ids) { + const result = synchronized.get(id); + if (result && 'error' in result) throw result.error; + } if (type === NotificationType.UNREAD) { void this.publishWarningsAndAlerts(); } @@ -553,17 +625,81 @@ export class NotificationsService { * CRUD: Updating Notifications *------------------------------------------------------------------------**/ - private moveNotification(params: { from: NotificationType; to: NotificationType }) { - const { from, to } = params; - return async (notification: Notification) => { - const currentPath = this.notificationPath(notification.id, from); - const targetPath = this.notificationPath(notification.id, to); - await rename(currentPath, targetPath); - // A legacy archive copy may already exist, so rename need not emit an add event. - await this.synchronizeNotification(notification.id); + /** + * Moves the given ids between folders with bounded file I/O, then reconciles the + * overview once for the whole set instead of once per file. + */ + private async moveNotifications(ids: string[], from: NotificationType, to: NotificationType) { + const uniqueIds = [...new Set(ids)]; + if (to === NotificationType.UNREAD) this.markQuietRestores(uniqueIds); + const results = + to === NotificationType.UNREAD + ? await this.restoreWithoutReplacing(uniqueIds) + : await this.settleInBatches(uniqueIds, async (id) => { + const source = this.notificationPath(id, from); + await this.loadNotificationFile(source, from); + await rename(source, this.notificationPath(id, to)); + return true; + }); + const synchronizedIds = uniqueIds.filter((_, index) => isFulfilled(results[index])); + const synchronized = await this.synchronizeNotifications(synchronizedIds); + void this.publishWarningsAndAlerts(); + + const errors = results.filter(isRejected).map((result) => result.reason); + let successes = 0; + for (const [index, move] of results.entries()) { + if (isRejected(move)) continue; + const id = uniqueIds[index]; + const result = synchronized.get(id); + if (!result) { + errors.push(new Error(`Notification was not synchronized: ${id}`)); + } else if ('error' in result) { + errors.push(result.error); + } else if (move.value) { + successes += 1; + } + } + return { + successes, + errors, + errorOccurred: errors.length > 0, }; } + private async restoreWithoutReplacing(ids: string[]) { + const { UNREAD: unread, ARCHIVE: archive } = this.paths(); + const batchSize = NotificationsService.FILE_IO_BATCH_SIZE; + const results: PromiseSettledResult[] = []; + for (let index = 0; index < ids.length; index += batchSize) { + const batch = ids.slice(index, index + batchSize); + const sources = await Promise.allSettled( + batch.map(async (id) => { + const source = resolveFileInDirectory(archive, id); + await this.loadNotificationFile(source, NotificationType.ARCHIVE); + return source; + }) + ); + const paths = sources.filter(isFulfilled).map(({ value }) => value); + // GNU mv uses a no-replace rename on Unraid, including for notifications stored on FAT. + const move = paths.length + ? await execa('mv', ['-n', '--', ...paths, unread + '/'], { reject: false }) + : undefined; + const restored = await Promise.allSettled( + sources.map(async (source) => { + if (isRejected(source)) throw source.reason; + const sourceStillExists = await fileExists(source.value); + // GNU mv versions differ on whether a skipped move exits with an error. + if (move?.failed && !(await fileExists(join(unread, basename(source.value))))) { + throw move; + } + return !sourceStillExists; + }) + ); + results.push(...restored); + } + return results; + } + public async archiveNotification({ id }: Pick): Promise { await this.waitForInitialization(); const unreadPath = this.notificationPath(id, NotificationType.UNREAD); @@ -576,11 +712,8 @@ export class NotificationsService { } const notification = await this.loadNotificationFile(unreadPath, NotificationType.UNREAD); - const moveToArchive = this.moveNotification({ - from: NotificationType.UNREAD, - to: NotificationType.ARCHIVE, - }); - await moveToArchive(notification); + await rename(unreadPath, this.notificationPath(id, NotificationType.ARCHIVE)); + await this.synchronizeNotification(id); void this.publishWarningsAndAlerts(); @@ -600,12 +733,10 @@ export class NotificationsService { } const notification = await this.loadNotificationFile(archivePath, NotificationType.ARCHIVE); - const moveToUnread = this.moveNotification({ - from: NotificationType.ARCHIVE, - to: NotificationType.UNREAD, - }); - - await moveToUnread(notification); + // A single restore is announced even if an earlier bulk restore quieted this id. + this.quietRestores.delete(id); + await rename(archivePath, this.notificationPath(id, NotificationType.UNREAD)); + await this.synchronizeNotification(id); void this.publishWarningsAndAlerts(); return { ...notification, @@ -615,45 +746,55 @@ export class NotificationsService { public async archiveAll(importance?: NotificationImportance) { await this.waitForInitialization(); - const { UNREAD } = this.paths(); - - if (!importance) { - await readdir(UNREAD).then((ids) => this.archiveIds(ids)); - return { overview: NotificationsService.overview }; - } - - const unreads = await this.listFilesInFolder(UNREAD); - const [notifications] = await this.loadNotificationsFromPaths(unreads, { importance }); - const archive = this.moveNotification({ - from: NotificationType.UNREAD, - to: NotificationType.ARCHIVE, - }); - - const stats = await batchProcess(notifications, archive); - void this.publishWarningsAndAlerts(); - return { ...stats, overview: this.getOverview() }; + return this.moveAll(NotificationType.UNREAD, NotificationType.ARCHIVE, importance); } public async unarchiveAll(importance?: NotificationImportance) { await this.waitForInitialization(); - const { ARCHIVE } = this.paths(); + return this.moveAll(NotificationType.ARCHIVE, NotificationType.UNREAD, importance); + } + + private async moveAll( + from: NotificationType, + to: NotificationType, + importance?: NotificationImportance + ) { + const selection = await this.selectIdsToMove(from, importance); + const stats = await this.moveNotifications(selection.ids, from, to); + const errors = [...selection.errors, ...stats.errors]; + return { + successes: stats.successes, + errors, + errorOccurred: errors.length > 0, + overview: this.getOverview(), + }; + } + /** + * Lists the ids a bulk move should take from `from`, plus any files that could not be read + * while filtering by importance. + * + * Archive copies whose id is still unread are hidden legacy copies, so they are skipped: + * restoring one would overwrite the newer unread file. + */ + private async selectIdsToMove(from: NotificationType, importance?: NotificationImportance) { + const { UNREAD } = this.paths(); + const unread = + from === NotificationType.ARCHIVE ? new Set(await readdir(UNREAD)) : new Set(); + const visible = (ids: string[]) => ids.filter((id) => !unread.has(id)); if (!importance) { - // use arrow function to preserve `this` - await readdir(ARCHIVE).then((ids) => this.unarchiveIds(ids)); - return { overview: NotificationsService.overview }; + return { ids: visible(await readdir(this.paths()[from])), errors: [] as unknown[] }; } - const archives = await this.listFilesInFolder(ARCHIVE); - const [notifications] = await this.loadNotificationsFromPaths(archives, { importance }); - const unArchive = this.moveNotification({ - from: NotificationType.ARCHIVE, - to: NotificationType.UNREAD, + const files = await this.listFilesInFolder(this.paths()[from], visible); + const [notifications, failures] = await this.loadNotificationsFromPaths(files, { + importance, + type: from, }); - - const stats = await batchProcess(notifications, unArchive); - void this.publishWarningsAndAlerts(); - return { ...stats, overview: this.getOverview() }; + return { + ids: notifications.map((notification) => notification.id), + errors: failures.filter((error) => !this.isMissingFileError(error)), + }; } /** @@ -666,8 +807,9 @@ export class NotificationsService { * @param ids a list of '*.notify' id's, which correspond to id files * @returns */ - public archiveIds(ids: string[]) { - return batchProcess(ids, (id) => this.archiveNotification({ id })); + public async archiveIds(ids: string[]) { + await this.waitForInitialization(); + return this.moveNotifications(ids, NotificationType.UNREAD, NotificationType.ARCHIVE); } /** @@ -680,8 +822,9 @@ export class NotificationsService { * @param ids a list of '*.notify' id's, which correspond to id files * @returns */ - public unarchiveIds(ids: string[]) { - return batchProcess(ids, (id) => this.markAsUnread({ id })); + public async unarchiveIds(ids: string[]) { + await this.waitForInitialization(); + return this.moveNotifications(ids, NotificationType.ARCHIVE, NotificationType.UNREAD); } /**------------------------------------------------------------------------ diff --git a/web/__test__/helpers/apollo-cache/index.test.ts b/web/__test__/helpers/apollo-cache/index.test.ts index 8dba28ca89..9c31fc1444 100644 --- a/web/__test__/helpers/apollo-cache/index.test.ts +++ b/web/__test__/helpers/apollo-cache/index.test.ts @@ -1,11 +1,17 @@ +import { provideApolloClient } from '@vue/apollo-composable'; + +import { ApolloClient, ApolloLink, Observable } from '@apollo/client/core'; import { createApolloCache } from '~/helpers/apollo-cache'; +import { GraphQLError } from 'graphql'; import { describe, expect, it } from 'vitest'; import { archiveNotification, + getNotifications, NOTIFICATION_FRAGMENT, notificationsOverview, } from '~/components/Notifications/graphql/notification.query'; +import { useArchiveAllNotifications } from '~/composables/api/use-notifications'; import { makeFragmentData } from '~/composables/gql/fragment-masking'; import { NotificationImportance, NotificationType } from '~/composables/gql/graphql'; @@ -56,6 +62,64 @@ const writeArchiveResult = (cache: ReturnType) => const readOverview = (cache: ReturnType) => cache.readQuery({ query: notificationsOverview })?.notifications.overview; +describe('Archive All cache invalidation', () => { + it('refetches the unread and archived lists after a partially completed archive', async () => { + const cache = createApolloCache(); + cache.writeQuery({ query: notificationsOverview, data: overview(1, 0) }); + let archiveAttempted = false; + const link = new ApolloLink( + (operation) => + new Observable((observer) => { + if (operation.operationName === 'ArchiveAllNotifications') { + archiveAttempted = true; + cache.writeQuery({ query: notificationsOverview, data: overview(0, 1) }); + observer.next({ data: null, errors: [new GraphQLError('Partial completion')] }); + } else { + const archive = operation.variables.filter.type === NotificationType.ARCHIVE; + const notification = archive + ? archivedNotification + : { ...archivedNotification, type: NotificationType.UNREAD }; + observer.next({ + data: { + notifications: { + __typename: 'Notifications', + id: 'notifications', + list: archive === archiveAttempted ? [notification] : [], + }, + }, + }); + } + observer.complete(); + }) + ); + const client = new ApolloClient({ cache, link }); + const { mutate } = provideApolloClient(client)(useArchiveAllNotifications); + const watchList = (type: NotificationType) => { + const list: { total?: number } = {}; + const subscription = client + .watchQuery({ query: getNotifications, variables: { filter: { type, limit: 15, offset: 0 } } }) + .subscribe(({ data }) => { + list.total = data.notifications?.list.length; + }); + return { list, subscription }; + }; + const unread = watchList(NotificationType.UNREAD); + const archive = watchList(NotificationType.ARCHIVE); + + try { + await expect.poll(() => [unread.list.total, archive.list.total]).toEqual([1, 0]); + + await expect(mutate()).rejects.toThrow('Partial completion'); + + await expect.poll(() => [unread.list.total, archive.list.total]).toEqual([0, 1]); + } finally { + unread.subscription.unsubscribe(); + archive.subscription.unsubscribe(); + client.stop(); + } + }); +}); + describe('archiveNotification cache policy', () => { it('keeps the server overview when the subscription arrives before the mutation result', () => { const cache = createApolloCache(); diff --git a/web/src/components/Notifications/Sidebar.vue b/web/src/components/Notifications/Sidebar.vue index 858f9bbae6..8b83e36278 100644 --- a/web/src/components/Notifications/Sidebar.vue +++ b/web/src/components/Notifications/Sidebar.vue @@ -23,7 +23,6 @@ import { Settings } from 'lucide-vue-next'; import ConfirmDialog from '~/components/ConfirmDialog.vue'; import { - archiveAllNotifications, deleteArchivedNotifications, NOTIFICATION_FRAGMENT, notificationsOverview, @@ -35,12 +34,15 @@ import { } from '~/components/Notifications/graphql/notification.subscription'; import NotificationsIndicator from '~/components/Notifications/Indicator.vue'; import NotificationsList from '~/components/Notifications/List.vue'; -import { useTrackLatestSeenNotification } from '~/composables/api/use-notifications'; +import { + useArchiveAllNotifications, + useTrackLatestSeenNotification, +} from '~/composables/api/use-notifications'; import { useFragment } from '~/composables/gql'; import { NotificationImportance as Importance, NotificationType } from '~/composables/gql/graphql'; import { useConfirm } from '~/composables/useConfirm'; -const { mutate: archiveAll, loading: loadingArchiveAll } = useMutation(archiveAllNotifications); +const { mutate: archiveAll, loading: loadingArchiveAll } = useArchiveAllNotifications(); const { mutate: deleteArchives, loading: loadingDeleteAll } = useMutation(deleteArchivedNotifications); const { mutate: recalculateOverview } = useMutation(resetOverview); const { confirm } = useConfirm(); diff --git a/web/src/composables/api/use-notifications.ts b/web/src/composables/api/use-notifications.ts index 754496c903..55b9f02812 100644 --- a/web/src/composables/api/use-notifications.ts +++ b/web/src/composables/api/use-notifications.ts @@ -1,10 +1,12 @@ import { computed, onUnmounted, ref, watchEffect } from 'vue'; -import { useQuery } from '@vue/apollo-composable'; +import { useApolloClient, useMutation, useQuery } from '@vue/apollo-composable'; import { useStorage, watchOnce } from '@vueuse/core'; import { NotificationType } from '@/composables/gql/graphql'; +import { invalidateNotificationQueries } from '~/helpers/apollo-cache'; import { + archiveAllNotifications, getNotifications, NOTIFICATION_FRAGMENT, } from '~/components/Notifications/graphql/notification.query'; @@ -13,6 +15,13 @@ import { useFragment } from '~/composables/gql/fragment-masking'; const LATEST_SEEN_TIMESTAMP_KEY = 'latest-seen-notification-timestamp'; const HAVE_SEEN_NOTIFICATIONS_KEY = 'have-seen-notifications'; +export function useArchiveAllNotifications() { + const { client } = useApolloClient(); + const mutation = useMutation(archiveAllNotifications, { throws: 'always' }); + mutation.onError(() => invalidateNotificationQueries(client.cache)); + return mutation; +} + /** * Composable for managing user's state of having seen notifications. * diff --git a/web/src/helpers/apollo-cache/index.ts b/web/src/helpers/apollo-cache/index.ts index 78a8f59d1b..8ab598154e 100644 --- a/web/src/helpers/apollo-cache/index.ts +++ b/web/src/helpers/apollo-cache/index.ts @@ -1,7 +1,7 @@ import { InMemoryCache } from '@apollo/client/core'; import { mergeAndDedup } from '~/helpers/apollo-cache/merge'; -import type { InMemoryCacheConfig } from '@apollo/client/core'; +import type { ApolloCache, InMemoryCacheConfig } from '@apollo/client/core'; import type { NotificationOverview } from '~/composables/gql/graphql'; import { getNotifications } from '~/components/Notifications/graphql/notification.query'; @@ -13,6 +13,11 @@ const isDefined = (value: T | null | undefined): value is T => { return value !== null && value !== undefined; }; +export function invalidateNotificationQueries(cache: Pick, 'evict' | 'gc'>) { + cache.evict({ fieldName: 'notifications' }); + cache.gc(); +} + /**------------------------------------------------------------------------ * ! Understanding Cache Type Policies * @@ -111,8 +116,7 @@ const defaultCacheConfig: InMemoryCacheConfig = { * @returns the value to cache for this operation */ merge(_, incoming, { cache }) { - cache.evict({ fieldName: 'notifications' }); - cache.gc(); // Run garbage collection to prevent orphans & incorrect cache state + invalidateNotificationQueries(cache); return incoming; // Return the incoming data so Apollo knows the result of the mutation }, },