From 7eaeb2c273f04e1fa459f942b13c1f2c0f8fbe06 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Thu, 19 Nov 2026 15:52:44 +0000 Subject: [PATCH 1/7] fix(api): keep bulk notification archive from stalling the API Archive All and the other bulk moves handled each notification as a separate archive. Every file was reconciled on its own and broadcast a new overview, so 800 notifications produced 800 overview broadcasts and several thousand file reads, with all moves running at once. Bulk moves now validate and rename files in bounded batches, reconcile the overview once for the whole set, and broadcast once. Watcher events that arrive while a reconciliation is queued join the same batch. The GraphQL bulk mutations report an error when a move or reconciliation fails instead of returning stale counts. --- .../notifications/notifications.bulk.spec.ts | 133 ++++++++++++ .../notifications/notifications.resolver.ts | 25 ++- .../notifications/notifications.service.ts | 189 +++++++++++------- 3 files changed, 274 insertions(+), 73 deletions(-) create mode 100644 api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts 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..e9da16666c --- /dev/null +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts @@ -0,0 +1,133 @@ +import { ConfigService } from '@nestjs/config'; +import { copyFile, mkdir, mkdtemp, readdir, 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 { 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('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); +}); 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..bbed677ad9 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts @@ -35,6 +35,11 @@ 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 { @@ -53,6 +58,7 @@ export class NotificationsService { private notificationFiles = new Map(); private isHydratingOverview = false; private pendingNotificationEvents = new Map(); + private pendingSynchronization: PendingSynchronization | null = null; private static overview: NotificationOverview = { unread: { @@ -221,7 +227,8 @@ 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) { await pubsub.publish(PUBSUB_CHANNEL.NOTIFICATION_ADDED, { notificationAdded: unread }); @@ -245,19 +252,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 +576,10 @@ 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); + for (const result of synchronized.values()) { + if ('error' in result) throw result.error; + } if (type === NotificationType.UNREAD) { void this.publishWarningsAndAlerts(); } @@ -553,14 +602,37 @@ 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); + /** + * Renames 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 results = await this.settleInBatches(ids, async (id) => { + const source = this.notificationPath(id, from); + await this.loadNotificationFile(source, from); + await rename(source, this.notificationPath(id, to)); + }); + const moved = ids.filter((_, index) => isFulfilled(results[index])); + // A legacy archive copy may already exist, so rename need not emit an add event. + const synchronized = await this.synchronizeNotifications(moved); + void this.publishWarningsAndAlerts(); + + const errors = results.filter(isRejected).map((result) => result.reason); + let successes = 0; + for (const id of moved) { + 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 { + successes += 1; + } + } + return { + successes, + errors, + errorOccurred: errors.length > 0, }; } @@ -576,11 +648,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 +669,8 @@ export class NotificationsService { } const notification = await this.loadNotificationFile(archivePath, NotificationType.ARCHIVE); - const moveToUnread = this.moveNotification({ - from: NotificationType.ARCHIVE, - to: NotificationType.UNREAD, - }); - - await moveToUnread(notification); + await rename(archivePath, this.notificationPath(id, NotificationType.UNREAD)); + await this.synchronizeNotification(id); void this.publishWarningsAndAlerts(); return { ...notification, @@ -615,45 +680,33 @@ 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(); + const ids = await this.listIdsToMove(NotificationType.UNREAD, importance); + const stats = await this.moveNotifications( + ids, + NotificationType.UNREAD, + NotificationType.ARCHIVE + ); return { ...stats, overview: this.getOverview() }; } public async unarchiveAll(importance?: NotificationImportance) { await this.waitForInitialization(); - const { ARCHIVE } = this.paths(); + const ids = await this.listIdsToMove(NotificationType.ARCHIVE, importance); + const stats = await this.moveNotifications( + ids, + NotificationType.ARCHIVE, + NotificationType.UNREAD + ); + return { ...stats, overview: this.getOverview() }; + } - if (!importance) { - // use arrow function to preserve `this` - await readdir(ARCHIVE).then((ids) => this.unarchiveIds(ids)); - return { overview: NotificationsService.overview }; - } + private async listIdsToMove(from: NotificationType, importance?: NotificationImportance) { + const folder = this.paths()[from]; + if (!importance) return readdir(folder); - const archives = await this.listFilesInFolder(ARCHIVE); - const [notifications] = await this.loadNotificationsFromPaths(archives, { importance }); - const unArchive = this.moveNotification({ - from: NotificationType.ARCHIVE, - to: NotificationType.UNREAD, - }); - - const stats = await batchProcess(notifications, unArchive); - void this.publishWarningsAndAlerts(); - return { ...stats, overview: this.getOverview() }; + const files = await this.listFilesInFolder(folder); + const [notifications] = await this.loadNotificationsFromPaths(files, { importance }); + return notifications.map((notification) => notification.id); } /** @@ -666,8 +719,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 +734,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); } /**------------------------------------------------------------------------ From 0d1718c3829cc47ebb497476ba17478f0454932b Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Thu, 19 Nov 2026 15:53:09 +0000 Subject: [PATCH 2/7] fix(api): stop bulk notification restores from toasting every file Unarchive All and the bulk unarchive mutation move files back into the unread folder. The watcher saw each file as new and published notificationAdded for it, so the web UI showed one toast per restored notification. Bulk restores now mark their ids for a short window. The watcher still reconciles counts for those files but does not announce them. A single restore clears the mark for its id, so it keeps its toast. --- .../notifications/notifications.bulk.spec.ts | 40 +++++++++++++++++++ .../notifications/notifications.service.ts | 26 +++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) 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 index e9da16666c..188df84f9b 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts @@ -130,4 +130,44 @@ describe.sequential('bulk notification moves', () => { }); 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); }); 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 bbed677ad9..6a375bb01b 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts @@ -44,6 +44,7 @@ type PendingSynchronization = { @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; /** @@ -59,6 +60,8 @@ export class NotificationsService { 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: { @@ -230,13 +233,31 @@ export class NotificationsService { 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; @@ -607,6 +628,7 @@ export class NotificationsService { * overview once for the whole set instead of once per file. */ private async moveNotifications(ids: string[], from: NotificationType, to: NotificationType) { + if (to === NotificationType.UNREAD) this.markQuietRestores(ids); const results = await this.settleInBatches(ids, async (id) => { const source = this.notificationPath(id, from); await this.loadNotificationFile(source, from); @@ -669,6 +691,8 @@ export class NotificationsService { } const notification = await this.loadNotificationFile(archivePath, NotificationType.ARCHIVE); + // 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(); From 50bbb94ffa640097689093f5e4c0ecde3d9167e7 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Thu, 19 Nov 2026 17:41:38 +0000 Subject: [PATCH 3/7] fix(api): tighten bulk notification selection and error reporting Restore All selected archive copies whose id was still unread. These are hidden legacy copies, and restoring one renamed it over the newer unread file. Bulk selection now skips them and restores archive-only ids. Importance-filtered Archive All and Restore All dropped files that could not be read during selection. Those failures are now part of the bulk result, so the GraphQL mutation reports them. Deleting a category checked every result in the shared reconciliation batch, so an unrelated watcher read failure could fail the delete. It now checks only the ids it deleted. --- .../notifications/notifications.bulk.spec.ts | 72 ++++++++++++++++++- .../notifications/notifications.service.ts | 68 ++++++++++++------ 2 files changed, 116 insertions(+), 24 deletions(-) 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 index 188df84f9b..ba67f3f5a7 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts @@ -1,5 +1,5 @@ import { ConfigService } from '@nestjs/config'; -import { copyFile, mkdir, mkdtemp, readdir, rm, writeFile } from 'fs/promises'; +import { copyFile, mkdir, mkdtemp, readdir, readFile, rm, writeFile } from 'fs/promises'; import { tmpdir } from 'os'; import { join } from 'path'; @@ -7,7 +7,10 @@ import type { FSWatcher } from 'chokidar'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { pubsub, PUBSUB_CHANNEL } from '@app/core/pubsub.js'; -import { NotificationType } from '@app/unraid-api/graph/resolvers/notifications/notifications.model.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'; @@ -170,4 +173,69 @@ describe.sequential('bulk notification moves', () => { 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('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) + ); + let release: (() => void) | undefined; + const blocker = internal.enqueueOverviewUpdate( + () => new Promise((resolve) => (release = resolve)) + ); + try { + await vi.waitFor(() => expect(release).toBeDefined()); + const unrelated = internal.synchronizeNotifications(['unrelated.notify']); + const deletion = service.deleteNotifications(ARCHIVE); + await vi.waitFor(() => + expect( + (Reflect.get(service, 'pendingSynchronization') as { ids: Set }).ids.size + ).toBeGreaterThan(1) + ); + release?.(); + await blocker; + await expect(deletion).resolves.toBeDefined(); + await expect(unrelated).resolves.toBeDefined(); + } finally { + spy.mockRestore(); + } + }); }); 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 6a375bb01b..134a7612d3 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts @@ -598,8 +598,10 @@ export class NotificationsService { } await emptyDir(folder); const synchronized = await this.synchronizeNotifications(ids); - for (const result of synchronized.values()) { - if ('error' in result) throw result.error; + // 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(); @@ -704,33 +706,55 @@ export class NotificationsService { public async archiveAll(importance?: NotificationImportance) { await this.waitForInitialization(); - const ids = await this.listIdsToMove(NotificationType.UNREAD, importance); - const stats = await this.moveNotifications( - ids, - NotificationType.UNREAD, - NotificationType.ARCHIVE - ); - return { ...stats, overview: this.getOverview() }; + return this.moveAll(NotificationType.UNREAD, NotificationType.ARCHIVE, importance); } public async unarchiveAll(importance?: NotificationImportance) { await this.waitForInitialization(); - const ids = await this.listIdsToMove(NotificationType.ARCHIVE, importance); - const stats = await this.moveNotifications( - ids, - NotificationType.ARCHIVE, - NotificationType.UNREAD - ); - return { ...stats, overview: this.getOverview() }; + 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(), + }; } - private async listIdsToMove(from: NotificationType, importance?: NotificationImportance) { - const folder = this.paths()[from]; - if (!importance) return readdir(folder); + /** + * 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) { + return { ids: visible(await readdir(this.paths()[from])), errors: [] as unknown[] }; + } - const files = await this.listFilesInFolder(folder); - const [notifications] = await this.loadNotificationsFromPaths(files, { importance }); - return notifications.map((notification) => notification.id); + const files = await this.listFilesInFolder(this.paths()[from], visible); + const [notifications, failures] = await this.loadNotificationsFromPaths(files, { + importance, + type: from, + }); + return { + ids: notifications.map((notification) => notification.id), + errors: failures.filter((error) => !this.isMissingFileError(error)), + }; } /** From 5e29f53588c4615dd944efa1579da66bb3fe91d0 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Thu, 19 Nov 2026 17:54:10 +0000 Subject: [PATCH 4/7] test(api): make the shared-batch delete test wait for the deletion to join The test released its queue blocker once the batch held any second id, which a stray watcher event could satisfy. It now waits until a deleted id is in the blocked batch, and releases the blocker in finally so a failure cannot hang cleanup. --- .../resolvers/notifications/notifications.bulk.spec.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 index ba67f3f5a7..59d79b49b6 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts @@ -225,16 +225,20 @@ describe.sequential('bulk notification moves', () => { await vi.waitFor(() => expect(release).toBeDefined()); const unrelated = internal.synchronizeNotifications(['unrelated.notify']); const deletion = service.deleteNotifications(ARCHIVE); + // The deletion has joined only once its own ids are in the blocked batch. await vi.waitFor(() => expect( - (Reflect.get(service, 'pendingSynchronization') as { ids: Set }).ids.size - ).toBeGreaterThan(1) + ( + Reflect.get(service, 'pendingSynchronization') as { ids: Set } | null + )?.ids.has(ids[0]) + ).toBe(true) ); release?.(); await blocker; await expect(deletion).resolves.toBeDefined(); await expect(unrelated).resolves.toBeDefined(); } finally { + release?.(); spy.mockRestore(); } }); From c02f9433c1683e98746638d08bbebb80a0153a73 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Thu, 19 Nov 2026 18:05:20 +0000 Subject: [PATCH 5/7] test(api): detect the deletion's own synchronization in the shared-batch test Watcher unlink events from emptyDir can add a deleted id to the blocked batch before deleteNotifications synchronizes. The test now waits for the deletion's own call, the only one that requests the whole folder at once. --- .../notifications/notifications.bulk.spec.ts | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) 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 index 59d79b49b6..e9137e918e 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts @@ -217,28 +217,32 @@ describe.sequential('bulk notification moves', () => { .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 = internal.synchronizeNotifications(['unrelated.notify']); + const unrelated = synchronize(['unrelated.notify']); const deletion = service.deleteNotifications(ARCHIVE); - // The deletion has joined only once its own ids are in the blocked batch. - await vi.waitFor(() => - expect( - ( - Reflect.get(service, 'pendingSynchronization') as { ids: Set } | null - )?.ids.has(ids[0]) - ).toBe(true) - ); + 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(); } }); From 4764c92b62ec736cfee2b8f48fca870c10e9a650 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Fri, 20 Nov 2026 13:53:12 +0000 Subject: [PATCH 6/7] fix(api): preserve unread copies during bulk restores --- .../notifications/notifications.bulk.spec.ts | 47 ++++++++++++++ .../notifications/notifications.service.ts | 64 +++++++++++++++---- 2 files changed, 99 insertions(+), 12 deletions(-) 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 index e9137e918e..282f522f7f 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts @@ -86,6 +86,19 @@ describe.sequential('bulk notification moves', () => { 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)); @@ -192,6 +205,40 @@ describe.sequential('bulk notification moves', () => { 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')); 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 134a7612d3..249ca66498 100644 --- a/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts +++ b/api/src/unraid-api/graph/resolvers/notifications/notifications.service.ts @@ -626,30 +626,36 @@ export class NotificationsService { *------------------------------------------------------------------------**/ /** - * Renames the given ids between folders with bounded file I/O, then reconciles the + * 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) { - if (to === NotificationType.UNREAD) this.markQuietRestores(ids); - const results = await this.settleInBatches(ids, async (id) => { - const source = this.notificationPath(id, from); - await this.loadNotificationFile(source, from); - await rename(source, this.notificationPath(id, to)); - }); - const moved = ids.filter((_, index) => isFulfilled(results[index])); - // A legacy archive copy may already exist, so rename need not emit an add event. - const synchronized = await this.synchronizeNotifications(moved); + 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 id of moved) { + 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 { + } else if (move.value) { successes += 1; } } @@ -660,6 +666,40 @@ export class NotificationsService { }; } + 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); From 715abb478373cb9327f8df22ad3573c36754fd0f Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Fri, 20 Nov 2026 13:53:44 +0000 Subject: [PATCH 7/7] fix(web): refresh notifications after a failed bulk archive --- .../helpers/apollo-cache/index.test.ts | 64 +++++++++++++++++++ web/src/components/Notifications/Sidebar.vue | 8 ++- web/src/composables/api/use-notifications.ts | 11 +++- web/src/helpers/apollo-cache/index.ts | 10 ++- 4 files changed, 86 insertions(+), 7 deletions(-) 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 }, },