mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-17 00:15:12 +00:00
同步笔记成功, 还要处理
1. 笔记冲突 2. 同步图片 3. 增量同步
This commit is contained in:
41
node_modules/api.js
generated
vendored
41
node_modules/api.js
generated
vendored
@@ -59,7 +59,7 @@ var Api = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
// get图片
|
// get图片
|
||||||
getImage: function(callback) {
|
getImageTest: function(callback) {
|
||||||
needle.get('http://localhost:9000/images/logo.png', function(err, resp) {
|
needle.get('http://localhost:9000/images/logo.png', function(err, resp) {
|
||||||
// log(resp.body);
|
// log(resp.body);
|
||||||
/*
|
/*
|
||||||
@@ -106,6 +106,43 @@ var Api = {
|
|||||||
callback && callback(false);
|
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;
|
module.exports = Api;
|
||||||
4
node_modules/common.js
generated
vendored
4
node_modules/common.js
generated
vendored
@@ -26,8 +26,8 @@ var Common = {
|
|||||||
if('length' in ret) {
|
if('length' in ret) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if(ret == 'object') {
|
if(typeof ret == 'object') {
|
||||||
if(!ret.Ok) { // 指明了Ok
|
if('Ok' in ret && !ret.Ok) { // 指明了Ok
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
71
node_modules/note.js
generated
vendored
71
node_modules/note.js
generated
vendored
@@ -2,6 +2,8 @@ var db = require('db');
|
|||||||
var User = require('user');
|
var User = require('user');
|
||||||
var Notebook = require('notebook');
|
var Notebook = require('notebook');
|
||||||
var Tag = require('tag');
|
var Tag = require('tag');
|
||||||
|
var Api = require('api');
|
||||||
|
var Common = require('common');
|
||||||
var Notes = db.notes;
|
var Notes = db.notes;
|
||||||
|
|
||||||
function log(o) {
|
function log(o) {
|
||||||
@@ -34,6 +36,9 @@ var Note = {
|
|||||||
log('update');
|
log('update');
|
||||||
var date = new Date();
|
var date = new Date();
|
||||||
noteOrContent.UpdatedTime = date;
|
noteOrContent.UpdatedTime = date;
|
||||||
|
|
||||||
|
noteOrContent['IsDirty'] = true; // 已修改
|
||||||
|
|
||||||
// 新建笔记, IsNew还是保存着
|
// 新建笔记, IsNew还是保存着
|
||||||
if(noteOrContent.IsNew) {
|
if(noteOrContent.IsNew) {
|
||||||
noteOrContent.CreatedTime = date;
|
noteOrContent.CreatedTime = date;
|
||||||
@@ -69,6 +74,10 @@ var Note = {
|
|||||||
needUpdate = true;
|
needUpdate = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if('Content' in updateFields) {
|
||||||
|
// 内容更新了, 因为内容是以后才从远程获取的, 获取内容后改变ContentIsDirty=false
|
||||||
|
noteOrContent['ContentIsDirty'] = true;
|
||||||
|
}
|
||||||
if(needUpdate) {
|
if(needUpdate) {
|
||||||
updates.UpdatedTime = date;
|
updates.UpdatedTime = date;
|
||||||
// Set an existing field's value
|
// 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) {
|
getNotes: function(notebookId, callback) {
|
||||||
var me = this;
|
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
|
// 添加笔记本, note object
|
||||||
addNoteForce: function(note, callback) {
|
addNoteForce: function(note, callback) {
|
||||||
note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步
|
note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步
|
||||||
|
note.IsDirty = false;
|
||||||
Notes.insert(note, function (err, newDoc) { // Callback is optional
|
Notes.insert(note, function (err, newDoc) { // Callback is optional
|
||||||
if(err) {
|
if(err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
@@ -158,9 +226,10 @@ var Note = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
// 更新笔记本
|
// 更新笔记本, 合并之, 内容要重新获取
|
||||||
updateNoteForce: function(note, callback) {
|
updateNoteForce: function(note, callback) {
|
||||||
note.IsDirty = false;
|
note.IsDirty = false;
|
||||||
|
note.InitSync = true;
|
||||||
Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, updates) { // Callback is optional
|
Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, updates) { // Callback is optional
|
||||||
if(err) {
|
if(err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
|||||||
20
node_modules/sync.js
generated
vendored
20
node_modules/sync.js
generated
vendored
@@ -181,7 +181,7 @@ var Sync = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// 同步笔记本
|
// 同步笔记到本地
|
||||||
_syncNoteToLocal: function(notes, callback) {
|
_syncNoteToLocal: function(notes, callback) {
|
||||||
var me = this;
|
var me = this;
|
||||||
function canCall() {
|
function canCall() {
|
||||||
@@ -228,19 +228,21 @@ var Sync = {
|
|||||||
canCall();
|
canCall();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// 2.2 本地是否修改了, 需要合并, 使用服务端的数据
|
// 2.2 本地是否修改了, 冲突, 报告给前端, 前端处理
|
||||||
|
// 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的
|
||||||
if(note.IsDirty) {
|
if(note.IsDirty) {
|
||||||
log('冲突....')
|
log('冲突....')
|
||||||
|
me._syncInfo.note.conflicts.push(note);
|
||||||
// 2.3 服务器是最新的, 用服务器的
|
// 2.3 服务器是最新的, 用服务器的
|
||||||
} else {
|
} 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);
|
})(note);
|
||||||
|
|||||||
@@ -624,6 +624,12 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) {
|
|||||||
Note.contentAjaxSeq++;
|
Note.contentAjaxSeq++;
|
||||||
var seq = Note.contentAjaxSeq;
|
var seq = Note.contentAjaxSeq;
|
||||||
function setContent(ret) {
|
function setContent(ret) {
|
||||||
|
if(ret) {
|
||||||
|
cacheNote.InitSync = false;
|
||||||
|
}
|
||||||
|
ret = ret || {};
|
||||||
|
log(">>")
|
||||||
|
log(ret);
|
||||||
Note.contentAjax = null;
|
Note.contentAjax = null;
|
||||||
if(seq != Note.contentAjaxSeq) {
|
if(seq != Note.contentAjaxSeq) {
|
||||||
return;
|
return;
|
||||||
@@ -641,7 +647,8 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) {
|
|||||||
callback && callback(ret);
|
callback && callback(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(cacheNote.Content) {
|
// 不是刚同步过来的, 且有内容
|
||||||
|
if(!cacheNote.InitSync && cacheNote.Content) {
|
||||||
setContent(cacheNote);
|
setContent(cacheNote);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -654,10 +661,7 @@ Note.changeNote = function(selectNoteId, isShare, needSaveChanged, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.showContentLoading();
|
self.showContentLoading();
|
||||||
if(Note.contentAjax != null) {
|
Service.noteService.getNoteContent(cacheNote.NoteId, setContent); // ajaxGet(url, param, setContent);
|
||||||
Note.contentAjax.abort();
|
|
||||||
}
|
|
||||||
Note.contentAjax = ajaxGet(url, param, setContent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 渲染
|
// 渲染
|
||||||
|
|||||||
Reference in New Issue
Block a user