From f6cc41173545866388790a2d29b50f320b07c01d Mon Sep 17 00:00:00 2001 From: life Date: Fri, 23 Jan 2015 00:46:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E7=AC=94=E8=AE=B0=E6=88=90?= =?UTF-8?q?=E5=8A=9F,=20=E8=BF=98=E8=A6=81=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 笔记冲突 2. 同步图片 3. 增量同步 --- node_modules/api.js | 41 ++++++++++++++++++++++-- node_modules/common.js | 4 +-- node_modules/note.js | 71 +++++++++++++++++++++++++++++++++++++++++- node_modules/sync.js | 20 ++++++------ public/js/app/note.js | 14 ++++++--- 5 files changed, 131 insertions(+), 19 deletions(-) diff --git a/node_modules/api.js b/node_modules/api.js index 838c2fca..a3b16534 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -59,7 +59,7 @@ var Api = { }); }, // get图片 - getImage: function(callback) { + getImageTest: function(callback) { needle.get('http://localhost:9000/images/logo.png', function(err, resp) { // log(resp.body); /* @@ -106,6 +106,43 @@ var Api = { callback && callback(false); } }); - } + }, + // 获取笔记内容, 获取之后保存到笔记中 + getNoteContent: function(noteId, callback) { + var me = this; + var url = this.getUrl('note/getNoteContent', {noteId: noteId}); + log(url); + needle.get(url, function(error, response) { + var ret = response.body; + log('--------') + log(ret); + if(Common.isOk(ret)) { // {Content: 'xx', NoteId: 'xxx'} + // Note.updateNoteContentForce(noteId, ret.Content, function() { + callback && callback(ret); + // }); + } else { + callback && callback(false); + } + }); + }, + + // TODO + // get图片, 获取内容后, 得到所有图片链接, 异步去获取图片, 并修改图片链接, + // 将https://leanote.com/api/resource/getImage?imageId=xx + // 转成app://leanote/public/files, 内部可以是个服务器吗? 请求内部的controller + getImage: function(fileId, callback) { + needle.get('http://localhost:9000/images/logo.png', function(err, resp) { + // log(resp.body); + /* + { 'accept-ranges': 'bytes', + 'content-disposition': 'inline; filename="logo.png"', + 'content-length': '8583', + 'content-type': 'image/png', + date: 'Mon, 19 Jan 2015 15:01:47 GMT', + */ + // log(resp.headers); + fs.writeFile('/Users/life/Desktop/aa.png', resp.body); + }); + }, }; module.exports = Api; \ No newline at end of file diff --git a/node_modules/common.js b/node_modules/common.js index 75ba0a9c..b2403f99 100644 --- a/node_modules/common.js +++ b/node_modules/common.js @@ -26,8 +26,8 @@ var Common = { if('length' in ret) { return true; } - if(ret == 'object') { - if(!ret.Ok) { // 指明了Ok + if(typeof ret == 'object') { + if('Ok' in ret && !ret.Ok) { // 指明了Ok return false; } return true; diff --git a/node_modules/note.js b/node_modules/note.js index db8b2a99..7e9fb9d1 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -2,6 +2,8 @@ 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) { @@ -34,6 +36,9 @@ var Note = { log('update'); var date = new Date(); noteOrContent.UpdatedTime = date; + + noteOrContent['IsDirty'] = true; // 已修改 + // 新建笔记, IsNew还是保存着 if(noteOrContent.IsNew) { noteOrContent.CreatedTime = date; @@ -69,6 +74,10 @@ var Note = { needUpdate = true; } } + if('Content' in updateFields) { + // 内容更新了, 因为内容是以后才从远程获取的, 获取内容后改变ContentIsDirty=false + noteOrContent['ContentIsDirty'] = true; + } if(needUpdate) { updates.UpdatedTime = date; // Set an existing field's value @@ -87,6 +96,20 @@ var Note = { } } }, + + + // 远程修改本地内容 + 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; @@ -131,6 +154,50 @@ var Note = { }); }, + // 同步内容 + 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); + } + } + }); + }, + //---------------- // 同步 //---------------- @@ -149,6 +216,7 @@ var Note = { // 添加笔记本, 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); @@ -158,9 +226,10 @@ var Note = { } }); }, - // 更新笔记本 + // 更新笔记本, 合并之, 内容要重新获取 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); diff --git a/node_modules/sync.js b/node_modules/sync.js index b375a93c..a4c4c656 100644 --- a/node_modules/sync.js +++ b/node_modules/sync.js @@ -181,7 +181,7 @@ var Sync = { } }, - // 同步笔记本 + // 同步笔记到本地 _syncNoteToLocal: function(notes, callback) { var me = this; function canCall() { @@ -228,19 +228,21 @@ var Sync = { canCall(); }); } else { - // 2.2 本地是否修改了, 需要合并, 使用服务端的数据 + // 2.2 本地是否修改了, 冲突, 报告给前端, 前端处理 + // 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的 if(note.IsDirty) { log('冲突....') + me._syncInfo.note.conflicts.push(note); // 2.3 服务器是最新的, 用服务器的 } else { + // 服务器是最新的, 本地没动过, 则覆盖之 + Note.updateNoteForce(note, function(note) { + if(note) { + me._syncInfo.note.updates.push(note); + } + canCall(); + }); } - // 这里都是用服务器端的数据, 不处理冲突 - Note.updateNoteForce(note, function(note) { - if(note) { - me._syncInfo.note.updates.push(note); - } - canCall(); - }) } }); })(note); diff --git a/public/js/app/note.js b/public/js/app/note.js index d7605f89..ff59f534 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -624,6 +624,12 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) { Note.contentAjaxSeq++; var seq = Note.contentAjaxSeq; function setContent(ret) { + if(ret) { + cacheNote.InitSync = false; + } + ret = ret || {}; + log(">>") + log(ret); Note.contentAjax = null; if(seq != Note.contentAjaxSeq) { return; @@ -641,7 +647,8 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) { callback && callback(ret); } - if(cacheNote.Content) { + // 不是刚同步过来的, 且有内容 + if(!cacheNote.InitSync && cacheNote.Content) { setContent(cacheNote); return; } @@ -654,10 +661,7 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) { } self.showContentLoading(); - if(Note.contentAjax != null) { - Note.contentAjax.abort(); - } - Note.contentAjax = ajaxGet(url, param, setContent); + Service.noteService.getNoteContent(cacheNote.NoteId, setContent); // ajaxGet(url, param, setContent); } // 渲染