Просмотр исходного кода

WIP: GC-610 transplant global notification

refactor actions
Yuki Takei 7 лет назад
Родитель
Сommit
0a7721043d
1 измененных файлов с 30 добавлено и 20 удалено
  1. 30 20
      lib/routes/page.js

+ 30 - 20
lib/routes/page.js

@@ -844,7 +844,7 @@ module.exports = function(crowi, app) {
    * @apiParam {String} path
    * @apiParam {String} grant
    */
-  api.create = function(req, res) {
+  api.create = async function(req, res) {
     const body = req.body.body || null;
     const pagePath = req.body.path || null;
     const grant = req.body.grant || null;
@@ -854,9 +854,8 @@ module.exports = function(crowi, app) {
       return res.json(ApiResponse.error('Parameters body and path are required.'));
     }
 
-    let createdPage = undefined;
     const ignoreNotFound = true;
-    Page.findPage(pagePath, req.user, null, ignoreNotFound)
+    const createdPage = await Page.findPage(pagePath, req.user, null, ignoreNotFound)
       .then(function(data) {
         if (data !== null) {
           throw new Error('Page exists');
@@ -868,8 +867,7 @@ module.exports = function(crowi, app) {
         if (!data) {
           throw new Error('Failed to create page.');
         }
-        createdPage = data.toObject();
-        const result = { page: createdPage };
+        const result = { page: data.toObject() };
 
         result.page.lastUpdateUser = User.filterToPublicFields(data.lastUpdateUser);
         result.page.creator = User.filterToPublicFields(data.creator);
@@ -877,12 +875,15 @@ module.exports = function(crowi, app) {
       })
       .catch(function(err) {
         return res.json(ApiResponse.error(err));
-      })
-      .then(() => {
-        // global notification
-        globalNotificationService.notifyPageCreate(createdPage);
       });
 
+    // global notification
+    try {
+      await globalNotificationService.notifyPageCreate(createdPage);
+    }
+    catch (err) {
+      logger.error(err);
+    }
   };
 
   /**
@@ -899,7 +900,7 @@ module.exports = function(crowi, app) {
    * - If revision_id is specified => update the page,
    * - If revision_id is not specified => force update by the new contents.
    */
-  api.update = function(req, res) {
+  api.update = async function(req, res) {
     const pageBody = req.body.body || null;
     const pageId = req.body.page_id || null;
     const revisionId = req.body.revision_id || null;
@@ -910,8 +911,8 @@ module.exports = function(crowi, app) {
       return res.json(ApiResponse.error('page_id and body are required.'));
     }
 
-    let updatedPage = undefined;
-    Page.findPageByIdAndGrantedUser(pageId, req.user)
+    let previousRevision = undefined;
+    let updatedPage = await Page.findPageByIdAndGrantedUser(pageId, req.user)
       .then(function(pageData) {
         if (pageData && revisionId !== null && !pageData.isUpdatable(revisionId)) {
           throw new Error('Revision error.');
@@ -924,23 +925,32 @@ module.exports = function(crowi, app) {
         if (grantUserGroupId != null) {
           grantOption.grantUserGroupId = grantUserGroupId;
         }
+
+        // store previous revision
+        previousRevision = pageData.revision;
+
         return Page.updatePage(pageData, pageBody, req.user, grantOption);
       })
       .then(function(pageData) {
-        updatedPage = pageData.toObject();
-        const result = { page: updatedPage };
+        const result = { page: pageData.toObject() };
 
         result.page.lastUpdateUser = User.filterToPublicFields(result.page.lastUpdateUser);
-        return res.json(ApiResponse.success(result));
+        res.json(ApiResponse.success(result));
+
+        return pageData;
       })
       .catch(function(err) {
         debug('error on _api/pages.update', err);
-        return res.json(ApiResponse.error(err));
-      })
-      .then(() => {
-        // global notification
-        globalNotificationService.notifyPageEdit(updatedPage);
+        res.json(ApiResponse.error(err));
       });
+
+        // global notification
+    try {
+      await globalNotificationService.notifyPageEdit(updatedPage);
+    }
+    catch (err) {
+        logger.error(err);
+    }
   };
 
   /**