diff options
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/revision.js | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/lib/models/revision.js b/lib/models/revision.js index 5d66f9fe..b497f0ed 100644 --- a/lib/models/revision.js +++ b/lib/models/revision.js @@ -167,7 +167,7 @@ module.exports = function (sequelize, DataTypes) { checkAllNotesRevision: function (callback) { Revision.saveAllNotesRevision(function (err, notes) { if (err) return callback(err, null); - if (notes.length <= 0) { + if (!notes || notes.length <= 0) { return callback(null, notes); } else { Revision.checkAllNotesRevision(callback); @@ -176,6 +176,7 @@ module.exports = function (sequelize, DataTypes) { }, saveAllNotesRevision: function (callback) { sequelize.models.Note.findAll({ + // query all notes that need to save for revision where: { $and: [ { @@ -201,11 +202,30 @@ module.exports = function (sequelize, DataTypes) { } }).then(function (notes) { if (notes.length <= 0) return callback(null, notes); + var savedNotes = []; async.each(notes, function (note, _callback) { - Revision.saveNoteRevision(note, _callback); + // revision saving policy: note not been modified for 5 mins or not save for 10 mins + if (note.lastchangeAt && note.savedAt) { + var lastchangeAt = moment(note.lastchangeAt); + var savedAt = moment(note.savedAt); + if (moment().isAfter(lastchangeAt.add(1, 'minutes'))) { + savedNotes.push(note); + Revision.saveNoteRevision(note, _callback); + } else if (lastchangeAt.isAfter(savedAt.add(2, 'minutes'))) { + savedNotes.push(note); + Revision.saveNoteRevision(note, _callback); + } else { + return _callback(null, null); + } + } else { + savedNotes.push(note); + Revision.saveNoteRevision(note, _callback); + } }, function (err) { if (err) return callback(err, null); - return callback(null, notes); + // return null when no notes need saving at this moment but have delayed tasks to be done + var result = ((savedNotes.length == 0) && (notes.length > savedNotes.length)) ? null : savedNotes; + return callback(null, result); }); }).catch(function (err) { return callback(err, null); |