From ee3d4504e2433065cd62fa9662f043adf015dace Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 23 Mar 2025 11:03:22 -0400 Subject: [PATCH] Move notification event writing to model layer fix: Bulk notification action does not reflect on other clients --- server/commands/notificationUpdater.test.ts | 182 ------------------ server/commands/notificationUpdater.ts | 53 ----- server/models/base/Model.ts | 5 +- .../routes/api/notifications/notifications.ts | 39 ++-- 4 files changed, 29 insertions(+), 250 deletions(-) delete mode 100644 server/commands/notificationUpdater.test.ts delete mode 100644 server/commands/notificationUpdater.ts diff --git a/server/commands/notificationUpdater.test.ts b/server/commands/notificationUpdater.test.ts deleted file mode 100644 index bb889758c0..0000000000 --- a/server/commands/notificationUpdater.test.ts +++ /dev/null @@ -1,182 +0,0 @@ -import { NotificationEventType } from "@shared/types"; -import { Event } from "@server/models"; -import { - buildUser, - buildNotification, - buildDocument, - buildCollection, -} from "@server/test/factories"; -import { withAPIContext } from "@server/test/support"; -import notificationUpdater from "./notificationUpdater"; - -describe("notificationUpdater", () => { - it("should mark the notification as viewed", async () => { - const user = await buildUser(); - const actor = await buildUser({ - teamId: user.teamId, - }); - const collection = await buildCollection({ - teamId: user.teamId, - createdById: actor.id, - }); - const document = await buildDocument({ - teamId: user.teamId, - collectionId: collection.id, - createdById: actor.id, - }); - const notification = await buildNotification({ - actorId: actor.id, - event: NotificationEventType.UpdateDocument, - userId: user.id, - teamId: user.teamId, - documentId: document.id, - collectionId: collection.id, - }); - - expect(notification.archivedAt).toBe(null); - expect(notification.viewedAt).toBe(null); - - await withAPIContext(user, (ctx) => - notificationUpdater(ctx, { - notification, - viewedAt: new Date(), - }) - ); - const event = await Event.findLatest({ - teamId: user.teamId, - }); - - expect(notification.viewedAt).not.toBe(null); - expect(notification.archivedAt).toBe(null); - expect(event!.name).toEqual("notifications.update"); - expect(event!.modelId).toEqual(notification.id); - }); - - it("should mark the notification as unseen", async () => { - const user = await buildUser(); - const actor = await buildUser({ - teamId: user.teamId, - }); - const collection = await buildCollection({ - teamId: user.teamId, - createdById: actor.id, - }); - const document = await buildDocument({ - teamId: user.teamId, - collectionId: collection.id, - createdById: actor.id, - }); - const notification = await buildNotification({ - actorId: actor.id, - event: NotificationEventType.UpdateDocument, - userId: user.id, - teamId: user.teamId, - documentId: document.id, - collectionId: collection.id, - viewedAt: new Date(), - }); - - expect(notification.archivedAt).toBe(null); - expect(notification.viewedAt).not.toBe(null); - - await withAPIContext(user, (ctx) => - notificationUpdater(ctx, { - notification, - viewedAt: null, - }) - ); - const event = await Event.findLatest({ - teamId: user.teamId, - }); - - expect(notification.viewedAt).toBe(null); - expect(notification.archivedAt).toBe(null); - expect(event!.name).toEqual("notifications.update"); - expect(event!.modelId).toEqual(notification.id); - }); - - it("should archive the notification", async () => { - const user = await buildUser(); - const actor = await buildUser({ - teamId: user.teamId, - }); - const collection = await buildCollection({ - teamId: user.teamId, - createdById: actor.id, - }); - const document = await buildDocument({ - teamId: user.teamId, - collectionId: collection.id, - createdById: actor.id, - }); - const notification = await buildNotification({ - actorId: actor.id, - event: NotificationEventType.UpdateDocument, - userId: user.id, - teamId: user.teamId, - documentId: document.id, - collectionId: collection.id, - }); - - expect(notification.archivedAt).toBe(null); - expect(notification.viewedAt).toBe(null); - - await withAPIContext(user, (ctx) => - notificationUpdater(ctx, { - notification, - archivedAt: new Date(), - }) - ); - const event = await Event.findLatest({ - teamId: user.teamId, - }); - - expect(notification.viewedAt).toBe(null); - expect(notification.archivedAt).not.toBe(null); - expect(event!.name).toEqual("notifications.update"); - expect(event!.modelId).toEqual(notification.id); - }); - - it("should unarchive the notification", async () => { - const user = await buildUser(); - const actor = await buildUser({ - teamId: user.teamId, - }); - const collection = await buildCollection({ - teamId: user.teamId, - createdById: actor.id, - }); - const document = await buildDocument({ - teamId: user.teamId, - collectionId: collection.id, - createdById: actor.id, - }); - const notification = await buildNotification({ - actorId: actor.id, - event: NotificationEventType.UpdateDocument, - userId: user.id, - teamId: user.teamId, - documentId: document.id, - collectionId: collection.id, - archivedAt: new Date(), - }); - - expect(notification.archivedAt).not.toBe(null); - expect(notification.viewedAt).toBe(null); - - await withAPIContext(user, (ctx) => - notificationUpdater(ctx, { - notification, - archivedAt: null, - }) - ); - const event = await Event.findLatest({ - teamId: user.teamId, - }); - - expect(notification.viewedAt).toBe(null); - expect(notification.archivedAt).toBeNull(); - expect(event!.name).toEqual("notifications.update"); - expect(event!.modelId).toEqual(notification.id); - }); -}); diff --git a/server/commands/notificationUpdater.ts b/server/commands/notificationUpdater.ts deleted file mode 100644 index 6f85f8de3b..0000000000 --- a/server/commands/notificationUpdater.ts +++ /dev/null @@ -1,53 +0,0 @@ -import isUndefined from "lodash/isUndefined"; -import { Event, Notification } from "@server/models"; -import { APIContext } from "@server/types"; - -type Props = { - /** Notification to be updated */ - notification: Notification; - /** Time at which notification was viewed */ - viewedAt?: Date | null; - /** Time at which notification was archived */ - archivedAt?: Date | null; -}; - -/** - * This command updates notification properties. - * - * @param ctx The originating request context - * @param Props The properties of the notification to update - * @returns Notification The updated notification - */ -export default async function notificationUpdater( - ctx: APIContext, - { notification, viewedAt, archivedAt }: Props -): Promise { - const { transaction } = ctx.state; - - if (!isUndefined(viewedAt)) { - notification.viewedAt = viewedAt; - } - if (!isUndefined(archivedAt)) { - notification.archivedAt = archivedAt; - } - const changed = notification.changed(); - if (changed) { - await notification.save({ transaction }); - - await Event.createFromContext( - ctx, - { - name: "notifications.update", - userId: notification.userId, - modelId: notification.id, - documentId: notification.documentId, - }, - { - actorId: notification.userId, - teamId: notification.teamId, - } - ); - } - - return notification; -} diff --git a/server/models/base/Model.ts b/server/models/base/Model.ts index e27c416f85..52ef3bec04 100644 --- a/server/models/base/Model.ts +++ b/server/models/base/Model.ts @@ -280,6 +280,7 @@ class Model< * * @param query The query options. * @param callback The function to call for each batch of results + * @return The total number of results processed. */ static async findAllInBatches( query: Replace, "limit", "batchLimit"> & { @@ -287,7 +288,7 @@ class Model< totalLimit?: number; }, callback: (results: Array, query: FindOptions) => Promise - ) { + ): Promise { const mappedQuery = { ...query, offset: query.offset ?? 0, @@ -305,6 +306,8 @@ class Model< results.length >= mappedQuery.limit && (mappedQuery.totalLimit ?? Infinity) > mappedQuery.offset ); + + return mappedQuery.offset; } /** diff --git a/server/routes/api/notifications/notifications.ts b/server/routes/api/notifications/notifications.ts index 9a1d5c5785..e1686ce264 100644 --- a/server/routes/api/notifications/notifications.ts +++ b/server/routes/api/notifications/notifications.ts @@ -1,10 +1,9 @@ import Router from "koa-router"; -import { isNil } from "lodash"; +import isNil from "lodash/isNil"; import isNull from "lodash/isNull"; import isUndefined from "lodash/isUndefined"; import { WhereOptions, Op } from "sequelize"; import { NotificationEventType } from "@shared/types"; -import notificationUpdater from "@server/commands/notificationUpdater"; import env from "@server/env"; import { AuthenticationError } from "@server/errors"; import auth from "@server/middlewares/authentication"; @@ -152,10 +151,8 @@ router.get( } if (!notification.viewedAt) { - await notificationUpdater(ctx, { - notification, - viewedAt: new Date(), - }); + notification.viewedAt = new Date(); + await notification.saveWithCtx(ctx); } ctx.response.set("Content-Type", "image/gif"); @@ -171,15 +168,22 @@ router.post( async (ctx: APIContext) => { const { id, viewedAt, archivedAt } = ctx.input.body; const { user } = ctx.state.auth; + const { transaction } = ctx.state; - const notification = await Notification.findByPk(id); + const notification = await Notification.findByPk(id, { + lock: transaction.LOCK.UPDATE, + rejectOnEmpty: true, + transaction, + }); authorize(user, "update", notification); - await notificationUpdater(ctx, { - notification, - viewedAt, - archivedAt, - }); + if (!isUndefined(viewedAt)) { + notification.viewedAt = viewedAt; + } + if (!isUndefined(archivedAt)) { + notification.archivedAt = archivedAt; + } + await notification.saveWithCtx(ctx); ctx.body = { data: await presentNotification(ctx, notification), @@ -196,7 +200,7 @@ router.post( const { viewedAt, archivedAt } = ctx.input.body; const { user } = ctx.state.auth; - const values: { [x: string]: any } = {}; + const values: Partial = {}; let where: WhereOptions = { teamId: user.teamId, userId: user.id, @@ -216,7 +220,14 @@ router.post( }; } - const [total] = await Notification.update(values, { where }); + const total = await Notification.findAllInBatches( + { where }, + async (results) => { + await Promise.all( + results.map((notification) => notification.updateWithCtx(ctx, values)) + ); + } + ); ctx.body = { success: true,