From 7555240413b3863e92d5e2ea56d03988d02d1beb Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 16 Feb 2024 13:25:17 -0500 Subject: [PATCH] Add summary column to documents --- app/models/Document.ts | 5 ----- .../20240216182003-add-summary-to-documents.js | 11 +++++++++++ server/models/Document.ts | 11 +++++++++++ shared/validations.ts | 3 +++ 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 server/migrations/20240216182003-add-summary-to-documents.js diff --git a/app/models/Document.ts b/app/models/Document.ts index 4dea9c234..595c864f8 100644 --- a/app/models/Document.ts +++ b/app/models/Document.ts @@ -470,11 +470,6 @@ export default class Document extends ParanoidModel { duplicate = (options?: { title?: string; recursive?: boolean }) => this.store.duplicate(this, options); - getSummary = (paragraphs = 4) => { - const result = this.text.trim().split("\n").slice(0, paragraphs).join("\n"); - return result; - }; - @computed get pinned(): boolean { return !!this.store.rootStore.pins.orderedData.find( diff --git a/server/migrations/20240216182003-add-summary-to-documents.js b/server/migrations/20240216182003-add-summary-to-documents.js new file mode 100644 index 000000000..de109a41d --- /dev/null +++ b/server/migrations/20240216182003-add-summary-to-documents.js @@ -0,0 +1,11 @@ +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.addColumn("documents", "summary", { + type: Sequelize.TEXT, + allowNull: true, + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.removeColumn("documents", "summary"); + }, +}; diff --git a/server/models/Document.ts b/server/models/Document.ts index 0255af91e..ef13aa0a6 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -217,6 +217,13 @@ class Document extends ParanoidModel< @Column title: string; + @Length({ + max: DocumentValidation.maxSummaryLength, + msg: `Document summary must be ${DocumentValidation.maxSummaryLength} characters or less`, + }) + @Column + summary: string; + @Column(DataType.ARRAY(DataType.STRING)) previousTitles: string[] = []; @@ -985,6 +992,10 @@ class Document extends ParanoidModel< getTimestamp = () => Math.round(new Date(this.updatedAt).getTime() / 1000); getSummary = () => { + if (this.summary) { + return this.summary; + } + const plainText = DocumentHelper.toPlainText(this); const lines = compact(plainText.split("\n")); const notEmpty = lines.length >= 1; diff --git a/shared/validations.ts b/shared/validations.ts index 4eb1d9aac..fefe84704 100644 --- a/shared/validations.ts +++ b/shared/validations.ts @@ -36,6 +36,9 @@ export const DocumentValidation = { /** The maximum length of the document title */ maxTitleLength: 100, + /** The maximum length of the document summary */ + maxSummaryLength: 1000, + /** The maximum size of the collaborative document state */ maxStateLength: 1500 * 1024, };