Sotaro KARASAWA 9 лет назад
Родитель
Сommit
96388b11d7
4 измененных файлов с 79 добавлено и 14 удалено
  1. 26 1
      lib/models/page.js
  2. 2 11
      lib/routes/page.js
  3. 6 1
      lib/routes/search.js
  4. 45 1
      lib/util/search.js

+ 26 - 1
lib/models/page.js

@@ -666,7 +666,6 @@ module.exports = function(crowi) {
           resolve(data);
           if (!isCreate) {
             debug('pushRevision on Update');
-            pageEvent.emit('update', data, user);
           }
         });
       });
@@ -719,6 +718,32 @@ module.exports = function(crowi) {
       });
   };
 
+  pageSchema.statics.update = function(pageData, body, user, options) {
+    var Page = this
+      , Revision = crowi.model('Revision')
+      , grant = options.grant || null
+      ;
+    // update existing page
+    var newRevision = Revision.prepareRevision(pageData, body, user);
+
+    return new Promise(function(resolve, reject) {
+      Page.pushRevision(pageData, newRevision, user)
+      .then(function(revision) {
+        if (grant != pageData.grant) {
+          return Page.updateGrant(pageData, grant, user).then(function(data) {
+            resolve(data);
+            pageEvent.emit('update', data, user);
+          });
+        } else {
+          resolve(pageData);
+          pageEvent.emit('update', data, user);
+        }
+      }).catch(function(err) {
+        debug('Error on update', err);
+      });
+    });
+  };
+
   pageSchema.statics.rename = function(pageData, newPagePath, user, options) {
     var Page = this
       , Revision = crowi.model('Revision')

+ 2 - 11
lib/routes/page.js

@@ -315,10 +315,7 @@ module.exports = function(crowi, app) {
 
       if (data) {
         previousRevision = data.revision;
-        // update existing page
-        var newRevision = Revision.prepareRevision(data, body, req.user);
-        updateOrCreate = 'update';
-        return Page.pushRevision(data, newRevision, req.user);
+        return Page.update(data, body, req.user, {grant: grant});
       } else {
         // new page
         updateOrCreate = 'create';
@@ -345,13 +342,7 @@ module.exports = function(crowi, app) {
         }
       }
 
-      if (grant != data.grant) {
-        return Page.updateGrant(data, grant, req.user).then(function(data) {
-          return res.redirect(redirectPath);
-        });
-      } else {
-        return res.redirect(redirectPath);
-      }
+      return res.redirect(redirectPath);
     }).catch(function(err) {
       debug('Page create or edit error.', err);
       if (pageData && !req.form.isValid) {

+ 6 - 1
lib/routes/search.js

@@ -49,7 +49,12 @@ module.exports = function(crowi, app) {
 
         return Page.populatePageListToAnyObjects(data.data);
       }).then(function(pages) {
-        result.data = pages;
+        result.data = pages.filter(function(page) {
+          if (Object.keys(page).length < 12) { // FIXME: 12 is a number of columns.
+            return false;
+          }
+          return true;
+        });
         return res.json(ApiResponse.success(result));
       })
       .catch(function(err) {

+ 45 - 1
lib/util/search.js

@@ -100,7 +100,8 @@ SearchClient.prototype.prepareBodyForUpdate = function(body, page) {
       bookmark_count: 0, // todo
       like_count: page.liker.length || 0,
       updated_at: page.updatedAt,
-    }
+    },
+    doc_as_upsert: true,
   };
 
   body.push(command);
@@ -135,6 +136,23 @@ SearchClient.prototype.prepareBodyForCreate = function(body, page) {
   body.push(document);
 };
 
+SearchClient.prototype.prepareBodyForDelete = function(body, page) {
+  if (!Array.isArray(body)) {
+    throw new Error('Body must be an array.');
+  }
+
+  var command = {
+    delete: {
+      _index: this.index_name,
+      _type: 'pages',
+      _id: page._id.toString(),
+    }
+  };
+
+  body.push(command);
+};
+
+
 SearchClient.prototype.addPages = function(pages)
 {
   var self = this;
@@ -165,6 +183,21 @@ SearchClient.prototype.updatePages = function(pages)
   });
 };
 
+SearchClient.prototype.deletePages = function(pages)
+{
+  var self = this;
+  var body = [];
+
+  pages.map(function(page) {
+    self.prepareBodyForDelete(body, page);
+  });
+
+  debug('deletePages(): Sending Request to ES', body);
+  return this.client.bulk({
+    body: body,
+  });
+};
+
 SearchClient.prototype.addAllPages = function()
 {
   var self = this;
@@ -357,6 +390,8 @@ SearchClient.prototype.searchKeywordUnderPath = function(keyword, path, option)
 
 SearchClient.prototype.syncPageCreated = function(page, user)
 {
+  debug('SearchClient.syncPageCreated', page);
+
   if (!this.shouldIndexed(page)) {
     return ;
   }
@@ -372,8 +407,17 @@ SearchClient.prototype.syncPageCreated = function(page, user)
 
 SearchClient.prototype.syncPageUpdated = function(page, user)
 {
+  debug('SearchClient.syncPageUpdated', page);
   // TODO delete
   if (!this.shouldIndexed(page)) {
+    this.deletePages([page])
+    .then(function(res) {
+      debug('deletePages: ES Response', res);
+    })
+    .catch(function(err){
+      debug('deletePages:ES Error', err);
+    });
+
     return ;
   }