diff --git a/server/models/Document.test.ts b/server/models/Document.test.ts index 929589bf9a..168ab0b388 100644 --- a/server/models/Document.test.ts +++ b/server/models/Document.test.ts @@ -793,6 +793,43 @@ describe("#findByPk", () => { }); }); +describe("#withMembershipScope", () => { + it("should not load text for documents with content", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + text: "Hello world", + }); + + const [loaded] = await Document.withMembershipScope(user.id).findAll({ + where: { id: document.id }, + }); + + expect(loaded.content).toBeTruthy(); + expect(loaded.text).toBeNull(); + }); + + it("should load text for documents without content or state", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + text: "Hello world", + }); + await sequelize.query( + `UPDATE documents SET content = NULL, state = NULL WHERE id = :id`, + { replacements: { id: document.id } } + ); + + const [loaded] = await Document.withMembershipScope(user.id).findAll({ + where: { id: document.id }, + }); + + expect(loaded.text).toEqual("Hello world"); + }); +}); + describe("findByIds", () => { test("should return documents by ids", async () => { const document1 = await buildDocument(); diff --git a/server/models/Document.ts b/server/models/Document.ts index 31332a29d6..c12e1f6bdd 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -97,6 +97,15 @@ const stateIfContentEmpty: ProjectionAlias = [ "state", ]; +// The markdown text column is only needed to deserialize documents that have +// neither content nor state. +const textIfContentAndStateEmpty: ProjectionAlias = [ + Sequelize.literal( + `CASE WHEN document.content IS NULL AND document.state IS NULL THEN document.text ELSE NULL END` + ), + "text", +]; + type AdditionalFindOptions = { /** The user ID to load associated permissions for. */ userId?: string; @@ -166,6 +175,12 @@ const publishedWhere: WhereOptions = { include: [stateIfContentEmpty], }, }, + withoutStateOrText: { + attributes: { + exclude: ["state", "text"], + include: [stateIfContentEmpty, textIfContentAndStateEmpty], + }, + }, withoutContent: { attributes: { exclude: ["state", "content", "text"], @@ -890,7 +905,7 @@ class Document extends ArchivableModel< ) { return this.scope([ options?.includeDrafts ? "withDrafts" : "defaultScope", - "withoutState", + "withoutStateOrText", { method: ["withViews", userId], }, diff --git a/server/models/Notification.ts b/server/models/Notification.ts index ce07248493..54e7f615f5 100644 --- a/server/models/Notification.ts +++ b/server/models/Notification.ts @@ -79,7 +79,8 @@ let baseDomain; @DefaultScope(() => ({ include: [ { - association: "document", + model: Document.scope(["defaultScope", "withoutStateOrText"]), + as: "document", required: false, }, { diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index b4f6d25285..5527c24565 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -624,7 +624,7 @@ router.post( { model: Document.scope([ "withDrafts", - "withoutState", + "withoutStateOrText", { method: ["withMembership", userId] }, ]), required: true,