var db = require('db'); var User = require('user'); var Notebook = require('notebook'); var Tag = require('tag'); var Api = require('api'); var Common = require('common'); var Notes = db.notes; function log(o) { console.log(o); } // 笔记服务 var Note = { /* type NoteOrContent struct { NotebookId string NoteId string UserId string Title string Desc string ImgSrc string Tags []string Content string Abstract string IsNew bool IsMarkdown bool FromUserId string // 为共享而新建 IsBlog bool // 是否是blog, 更新note不需要修改, 添加note时才有可能用到, 此时需要判断notebook是否设为Blog } */ // 更新笔记 updateNoteOrContent: function(noteOrContent, callback) { var userId = User.getCurActiveUserId(); noteOrContent['UserId'] = userId; log('update'); var date = new Date(); noteOrContent.UpdatedTime = date; noteOrContent['IsDirty'] = true; // 已修改 // 新建笔记, IsNew还是保存着 if(noteOrContent.IsNew) { noteOrContent.CreatedTime = date; noteOrContent['IsTrash'] = false; delete noteOrContent['IsNew']; noteOrContent['LocalIsNew'] = true; Notes.insert(noteOrContent, function (err, newDoc) { // Callback is optional if(err) { console.log(err); callback && callback(false); } else { // 为什么又设置成true, 因为js的对象是共享的, callback用到了noteOrContent.IsNew来做判断 noteOrContent['IsNew'] = true; callback && callback(newDoc); // 重新统计笔记本的笔记数量 Notebook.reCountNotebookNumberNotes(noteOrContent.NotebookId); // 标签 if(noteOrContent.Tags && noteOrContent.Tags.length > 0) { Tag.addTags(noteOrContent.Tags); } } }); // 更新笔记 } else { var updateFields = ['Desc', 'ImgSrc', 'Title', 'Tags', 'Content']; var updates = {}; var needUpdate = false; for(var i in updateFields) { var field = updateFields[i]; if(field in noteOrContent) { updates[field] = noteOrContent[field]; needUpdate = true; } } if('Content' in updateFields) { // 内容更新了, 因为内容是以后才从远程获取的, 获取内容后改变ContentIsDirty=false noteOrContent['ContentIsDirty'] = true; } if(needUpdate) { updates.UpdatedTime = date; // Set an existing field's value Notes.update({NoteId: noteOrContent.NoteId}, { $set: updates }, {}, function (err, numReplaced) { if(err) { log(err); callback && callback(false); } else { callback && callback(noteOrContent); // 标签 if(noteOrContent.Tags && noteOrContent.Tags.length > 0) { Tag.addTags(noteOrContent.Tags); } } }); } } }, // 远程修改本地内容 updateNoteContentForce: function(noteId, content) { Notes.update({NoteId: noteId}, { $set: {Content: content, IsContentDirty: false} }, {}, function (err, numReplaced) { if(err) { log(err); callback && callback(false); } else { callback && callback(true); } }); }, // 获取笔记列表 getNotes: function(notebookId, callback) { var me = this; me._getNotes(notebookId, false, callback); }, // 获取trash笔记 getTrashNotes: function(callback) { var me = this; me._getNotes('', true, callback); }, _getNotes: function(notebookId, isTrash, callback) { var userId = User.getCurActiveUserId(); var query = { UserId: userId, IsTrash: false, }; if(notebookId) { query['NotebookId'] = notebookId; } if(isTrash) { query['IsTrash'] = true; } Notes.find(query).sort({'UpdatedTime': -1}).exec(function(err, notes) { if(err) { log(err); return callback && callback(false); } return callback && callback(notes); }); }, // 得到笔记 getNote: function(noteId, callback) { var me = this; Notes.findOne({NoteId: noteId}, function(err, doc) { if(err || !doc) { log('不存在'); callback && callback(false); } else { callback && callback(doc); } }); }, // 同步内容 updateNoteContentForce: function(noteId, content, callback) { Notes.update({NoteId: noteId}, { $set: {Content: content, InitSync: false} }, {}, function (err, numReplaced) { if(err) { log(err); callback && callback(false); } else { callback && callback(content); } }); }, // 得到笔记内容 getNoteContent: function(noteId, callback) { var me = this; log('getNoteContent------') me.getNote(noteId, function(note) { if(!Common.isOk(note)) { log('not ok'); log(note); callback && callback(false); } else { // 如果笔记是刚同步过来的, 那么内容要重新获取 if(note.InitSync) { log('need load from server'); // 远程获取 Api.getNoteContent(noteId, function(noteContent) { // 同步到本地 if(Common.isOk(noteContent)) { me.updateNoteContentForce(noteId, noteContent.Content, function(ret) { callback && callback(noteContent); }); } else { callback && callback(false); } }); } else { log('not need'); callback && callback(note); } } }); }, //---------------- // 同步 //---------------- // 强制删除 deleteNoteForce: function(noteId, callback) { var me = this; Notes.remove({NoteId: noteId}, function(err, n) { if(err) { callback && callback(false); } else { callback && callback(true); } }); }, // 添加笔记本, note object addNoteForce: function(note, callback) { note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步 note.IsDirty = false; Notes.insert(note, function (err, newDoc) { // Callback is optional if(err) { console.log(err); callback && callback(false); } else { callback && callback(newDoc); } }); }, // 更新笔记本, 合并之, 内容要重新获取 updateNoteForce: function(note, callback) { note.IsDirty = false; note.InitSync = true; Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, updates) { // Callback is optional if(err) { console.log(err); callback && callback(false); } else { callback && callback(note); } }); }, }; module.exports = Note;