From dde6c3e4435faeecee251871856deb3d8c23683f Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 18 Nov 2020 19:48:57 -0800 Subject: [PATCH] Relax documents.update endpoint fix: documents.update should not trigger event if nothing changes --- app/models/Document.js | 1 + app/scenes/Document/components/Document.js | 6 +- server/api/documents.js | 110 +++++++++++---------- 3 files changed, 62 insertions(+), 55 deletions(-) diff --git a/app/models/Document.js b/app/models/Document.js index 96d2aaf781..02ff5a32bc 100644 --- a/app/models/Document.js +++ b/app/models/Document.js @@ -77,6 +77,7 @@ export default class Document extends BaseModel { @computed get isNew(): boolean { return ( + !!this.publishedAt && !this.lastViewedAt && differenceInDays(new Date(), new Date(this.createdAt)) < 14 ); diff --git a/app/scenes/Document/components/Document.js b/app/scenes/Document/components/Document.js index 82ae10805e..1cc80b647a 100644 --- a/app/scenes/Document/components/Document.js +++ b/app/scenes/Document/components/Document.js @@ -464,7 +464,11 @@ class DocumentScene extends React.Component { key={[injectTemplate, disableEmbeds].join("-")} title={revision ? revision.title : this.title} document={document} - value={readOnly ? value : undefined} + value={ + team && !team.multiplayerEditor && readOnly + ? value + : undefined + } defaultValue={team && team.multiplayerEditor ? "" : value} disableEmbeds={disableEmbeds} onImageUploadStart={this.onImageUploadStart} diff --git a/server/api/documents.js b/server/api/documents.js index 7048e1fe30..94fced378f 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -887,7 +887,6 @@ router.post("documents.update", auth(), async (ctx) => { const editorVersion = ctx.headers["x-editor-version"]; ctx.assertPresent(id, "id is required"); - ctx.assertPresent(title || text, "title or text is required"); if (append) ctx.assertPresent(text, "Text is required while appending"); const user = ctx.state.user; @@ -899,6 +898,7 @@ router.post("documents.update", auth(), async (ctx) => { } const previousTitle = document.title; + const willPublish = publish && !document.published; // Update document if (title) document.title = title; @@ -913,67 +913,69 @@ router.post("documents.update", auth(), async (ctx) => { document.lastModifiedById = user.id; const { collection } = document; - let transaction; - try { - transaction = await sequelize.transaction(); + if (document.changed() || willPublish) { + let transaction; + try { + transaction = await sequelize.transaction(); + + if (publish) { + await document.publish({ transaction }); + } else { + await document.save({ autosave, transaction }); + } + await transaction.commit(); + } catch (err) { + if (transaction) { + await transaction.rollback(); + } + throw err; + } if (publish) { - await document.publish({ transaction }); + await Event.create({ + name: "documents.publish", + documentId: document.id, + collectionId: document.collectionId, + teamId: document.teamId, + actorId: user.id, + data: { title: document.title }, + ip: ctx.request.ip, + }); } else { - await document.save({ autosave, transaction }); + await Event.create({ + name: "documents.update", + documentId: document.id, + collectionId: document.collectionId, + teamId: document.teamId, + actorId: user.id, + data: { + autosave, + done, + title: document.title, + }, + ip: ctx.request.ip, + }); } - await transaction.commit(); - } catch (err) { - if (transaction) { - await transaction.rollback(); + + if (document.title !== previousTitle) { + Event.add({ + name: "documents.title_change", + documentId: document.id, + collectionId: document.collectionId, + teamId: document.teamId, + actorId: user.id, + data: { + previousTitle, + title: document.title, + }, + ip: ctx.request.ip, + }); } - throw err; - } - if (publish) { - await Event.create({ - name: "documents.publish", - documentId: document.id, - collectionId: document.collectionId, - teamId: document.teamId, - actorId: user.id, - data: { title: document.title }, - ip: ctx.request.ip, - }); - } else { - await Event.create({ - name: "documents.update", - documentId: document.id, - collectionId: document.collectionId, - teamId: document.teamId, - actorId: user.id, - data: { - autosave, - done, - title: document.title, - }, - ip: ctx.request.ip, - }); + document.updatedBy = user; + document.collection = collection; } - if (document.title !== previousTitle) { - Event.add({ - name: "documents.title_change", - documentId: document.id, - collectionId: document.collectionId, - teamId: document.teamId, - actorId: user.id, - data: { - previousTitle, - title: document.title, - }, - ip: ctx.request.ip, - }); - } - - document.updatedBy = user; - document.collection = collection; - ctx.body = { data: await presentDocument(document), policies: presentPolicies(user, [document]),