summaryrefslogtreecommitdiff
path: root/lib/models/note.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/models/note.js')
-rw-r--r--lib/models/note.js26
1 files changed, 21 insertions, 5 deletions
diff --git a/lib/models/note.js b/lib/models/note.js
index 2b51c87c..ace072a3 100644
--- a/lib/models/note.js
+++ b/lib/models/note.js
@@ -52,6 +52,9 @@ module.exports = function (sequelize, DataTypes) {
},
lastchangeAt: {
type: DataTypes.DATE
+ },
+ savedAt: {
+ type: DataTypes.DATE
}
}, {
classMethods: {
@@ -66,6 +69,10 @@ module.exports = function (sequelize, DataTypes) {
as: "lastchangeuser",
constraints: false
});
+ Note.hasMany(models.Revision, {
+ foreignKey: "noteId",
+ constraints: false
+ });
},
checkFileExist: function (filePath) {
try {
@@ -100,11 +107,15 @@ module.exports = function (sequelize, DataTypes) {
var dbModifiedTime = moment(note.lastchangeAt || note.createdAt);
if (fsModifiedTime.isAfter(dbModifiedTime)) {
var body = fs.readFileSync(filePath, 'utf8');
- note.title = LZString.compressToBase64(Note.parseNoteTitle(body));
- note.content = LZString.compressToBase64(body);
- note.lastchangeAt = fsModifiedTime;
- note.save().then(function (note) {
- return callback(null, note.id);
+ note.update({
+ title: LZString.compressToBase64(Note.parseNoteTitle(body)),
+ content: LZString.compressToBase64(body),
+ lastchangeAt: fsModifiedTime
+ }).then(function (note) {
+ sequelize.models.Revision.saveNoteRevision(note, function (err, revision) {
+ if (err) return _callback(err, null);
+ return callback(null, note.id);
+ });
}).catch(function (err) {
return _callback(err, null);
});
@@ -224,6 +235,11 @@ module.exports = function (sequelize, DataTypes) {
}
}
return callback(null, note);
+ },
+ afterCreate: function (note, options, callback) {
+ sequelize.models.Revision.saveNoteRevision(note, function (err, revision) {
+ callback(err, note);
+ });
}
}
});