From e76f0dcf8ee561710f72a704f79523e5af7eb10d Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 24 Oct 2026 07:24:06 -0400 Subject: [PATCH] perf: Load only ids when resolving membership user ids (#14275) --- server/models/Collection.ts | 1 + server/models/Document.test.ts | 46 ++++++++++++++++++++++++++++++++++ server/models/Document.ts | 1 + 3 files changed, 48 insertions(+) diff --git a/server/models/Collection.ts b/server/models/Collection.ts index fe4b9ca741..cfe8c33a45 100644 --- a/server/models/Collection.ts +++ b/server/models/Collection.ts @@ -644,6 +644,7 @@ class Collection extends ParanoidModel< ) { const collection = await this.scope("withAllMemberships").findOne({ where: { id: collectionId }, + attributes: ["id"], include: [ { association: "memberships", diff --git a/server/models/Document.test.ts b/server/models/Document.test.ts index 168ab0b388..a2b2002141 100644 --- a/server/models/Document.test.ts +++ b/server/models/Document.test.ts @@ -440,6 +440,52 @@ describe("#findAllParentDocumentIds", () => { }); }); +describe("#membershipUserIds", () => { + it("should return unique users with direct and group memberships", async () => { + const team = await buildTeam(); + const owner = await buildUser({ teamId: team.id }); + const [direct, grouped, both] = await Promise.all([ + buildUser({ teamId: team.id }), + buildUser({ teamId: team.id }), + buildUser({ teamId: team.id }), + ]); + const document = await buildDocument({ + teamId: team.id, + userId: owner.id, + }); + const group = await buildGroup({ teamId: team.id }); + await group.$add("user", grouped, { through: { createdById: owner.id } }); + await group.$add("user", both, { through: { createdById: owner.id } }); + await GroupMembership.create({ + createdById: owner.id, + groupId: group.id, + documentId: document.id, + permission: DocumentPermission.Read, + }); + await UserMembership.create({ + createdById: owner.id, + documentId: document.id, + userId: direct.id, + permission: DocumentPermission.ReadWrite, + }); + await UserMembership.create({ + createdById: owner.id, + documentId: document.id, + userId: both.id, + permission: DocumentPermission.Read, + }); + + const userIds = await Document.membershipUserIds(document.id); + expect(userIds.sort()).toEqual([direct.id, grouped.id, both.id].sort()); + + const writerIds = await Document.membershipUserIds( + document.id, + DocumentPermission.ReadWrite + ); + expect(writerIds).toEqual([direct.id]); + }); +}); + describe("#membershipDocumentIds", () => { it("should return empty array when the user has no document memberships", async () => { const user = await buildUser(); diff --git a/server/models/Document.ts b/server/models/Document.ts index 3919f8c576..a3809603f5 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -780,6 +780,7 @@ class Document extends ArchivableModel< ) { const document = await this.scope("withAllMemberships").findOne({ where: { id: documentId }, + attributes: ["id"], include: [ { association: "memberships",