同步笔记成功, 还要处理

1. 笔记冲突
2. 同步图片
3. 增量同步
This commit is contained in:
life
2015-01-23 00:46:49 +08:00
parent 1d015642a6
commit f6cc411735
5 changed files with 131 additions and 19 deletions

39
node_modules/api.js generated vendored
View File

@@ -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;

4
node_modules/common.js generated vendored
View File

@@ -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;

71
node_modules/note.js generated vendored
View File

@@ -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);

12
node_modules/sync.js generated vendored
View File

@@ -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);

View File

@@ -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);
}
// 渲染