Browse Source

return three revisions and add method to get latest revision with author in revision model

yuto-o 4 years ago
parent
commit
895f48639d

+ 5 - 0
packages/app/src/client/services/PageContainer.js

@@ -495,6 +495,11 @@ export default class PageContainer extends Container {
     if (!res.ok) {
       throw new Error(res.error);
     }
+    if (res.isPageNotUptable) {
+      const err = new Error();
+      err.data = res.data;
+      throw err;
+    }
     return res;
   }
 

+ 3 - 0
packages/app/src/components/SavePageControls.jsx

@@ -47,6 +47,9 @@ class SavePageControls extends React.Component {
       await pageContainer.saveAndReload(editorContainer.getCurrentOptionsToSave());
     }
     catch (error) {
+      // TODO: display resolve conflict button when operation to update page is conflicted
+      // ref: https://estoc.weseek.co.jp/redmine/issues/78784
+      console.log(error.data);
       logger.error('failed to save', error);
       pageContainer.showErrorToastr(error);
     }

+ 5 - 0
packages/app/src/server/models/revision.js

@@ -90,5 +90,10 @@ module.exports = function(crowi) {
     }));
   };
 
+  revisionSchema.statics.findLatestRevisionByPathPopulatedWithAuthor = async(path) => {
+    const Revision = this;
+    return (await Revision.find({ path }).sort({ createdAt: -1 }).limit(1).populate('author'))[0];
+  };
+
   return mongoose.model('Revision', revisionSchema);
 };

+ 30 - 2
packages/app/src/server/routes/page.js

@@ -828,9 +828,38 @@ module.exports = function(crowi, app) {
     }
 
     // check revision
+    const Revision = crowi.model('Revision');
     let page = await Page.findByIdAndViewer(pageId, req.user);
     if (page != null && revisionId != null && !page.isUpdatable(revisionId)) {
-      return res.json(ApiResponse.error('Posted param "revisionId" is outdated.', 'outdated'));
+      // when isUpdatable is false, originRevision is the reqested revision
+      const originRevision = await Revision.findById(revisionId).populate('author');
+      let latestRevision = await Revision.find({ path: originRevision.path }).sort({ createdAt: -1 }).limit(1).populate('author');
+      latestRevision = latestRevision[0];
+
+      const revisions = {};
+
+      revisions.request = {
+        revisionID: '',
+        revisionBody: pageBody,
+        createdAt: new Date(),
+        userName: req.user.name,
+        userImgPath: req.user.imageUrlCached,
+      };
+      revisions.origin = {
+        revisionID: originRevision._id.toString(),
+        revisionBody: originRevision.body,
+        createdAt: originRevision.createdAt,
+        userName: originRevision.author.name,
+        userImgPath: originRevision.author.imageUrlCached,
+      };
+      revisions.latest = {
+        revisionID: latestRevision._id.toString(),
+        revisionBody: latestRevision.body,
+        createdAt: latestRevision.createdAt,
+        userName: latestRevision.author.name,
+        userImgPath: latestRevision.author.imageUrlCached,
+      };
+      return res.json(ApiResponse.success({ isPageNotUptable: true, data: revisions }));
     }
 
     const options = { isSyncRevisionToHackmd };
@@ -839,7 +868,6 @@ module.exports = function(crowi, app) {
       options.grantUserGroupId = grantUserGroupId;
     }
 
-    const Revision = crowi.model('Revision');
     const previousRevision = await Revision.findById(revisionId);
     try {
       page = await Page.updatePage(page, pageBody, previousRevision.body, req.user, options);