mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-16 08:01:53 +00:00
send changes note.update [ok]
This commit is contained in:
28
node_modules/api.js
generated
vendored
28
node_modules/api.js
generated
vendored
@@ -322,18 +322,34 @@ var Api = {
|
|||||||
//--------
|
//--------
|
||||||
|
|
||||||
// 获取笔记
|
// 获取笔记
|
||||||
|
// noteId是serverNoteId
|
||||||
getNote: function(noteId, callback) {
|
getNote: function(noteId, callback) {
|
||||||
|
needle.get(this.getUrl('note/getNote', {NoteId: noteId}), function(error, response) {
|
||||||
|
if(error) {
|
||||||
|
return callback && callback(false);
|
||||||
|
}
|
||||||
|
var ret = response.body;
|
||||||
|
if(Common.isOk(ret)) {
|
||||||
|
callback && callback(ret);
|
||||||
|
} else {
|
||||||
|
log(error);
|
||||||
|
callback && callback(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
// 添加
|
// 添加
|
||||||
addNote: function(note, callback) {
|
addNote: function(note, callback) {
|
||||||
var me = this;
|
var me = this;
|
||||||
// note.NotebookId是本的, 要得到远程的
|
// note.NotebookId是本的, 要得到远程的
|
||||||
Notebook.getServerNotebookIdByNotebookId(notebook.NotebookId, function(serverNotebookId) {
|
Notebook.getServerNotebookIdByNotebookId(note.NotebookId, function(serverNotebookId) {
|
||||||
var data = {
|
var data = {
|
||||||
title: note.Title,
|
title: note.Title,
|
||||||
notebookId: serverNotebookId
|
notebookId: serverNotebookId,
|
||||||
|
content: note.Content,
|
||||||
|
isMarkdown: note.isMarkdown,
|
||||||
|
Tags: note.Tags,
|
||||||
|
IsBlog: note.IsBlog
|
||||||
}
|
}
|
||||||
log('add note');
|
log('add note');
|
||||||
log(data);
|
log(data);
|
||||||
@@ -342,6 +358,7 @@ var Api = {
|
|||||||
return callback(false);
|
return callback(false);
|
||||||
}
|
}
|
||||||
var ret = resp.body;
|
var ret = resp.body;
|
||||||
|
log('add note ret');
|
||||||
log(ret);
|
log(ret);
|
||||||
if(Common.isOk(ret)) {
|
if(Common.isOk(ret)) {
|
||||||
callback(ret);
|
callback(ret);
|
||||||
@@ -354,13 +371,14 @@ var Api = {
|
|||||||
// 更新
|
// 更新
|
||||||
updateNote: function(note, callback) {
|
updateNote: function(note, callback) {
|
||||||
var me = this;
|
var me = this;
|
||||||
Notebook.getServerNotebookIdByNotebookId(notebook.NotebookId, function(serverNotebookId) {
|
Notebook.getServerNotebookIdByNotebookId(note.NotebookId, function(serverNotebookId) {
|
||||||
var data = {
|
var data = {
|
||||||
noteId: note.ServerNoteId,
|
noteId: note.ServerNoteId,
|
||||||
notebookId: serverNotebookId || "",
|
notebookId: serverNotebookId || "",
|
||||||
title: note.Title,
|
title: note.Title,
|
||||||
usn: note.Usn,
|
usn: note.Usn,
|
||||||
isTrash: note.IsTrash
|
isTrash: note.IsTrash,
|
||||||
|
content: note.Content
|
||||||
}
|
}
|
||||||
log('update note');
|
log('update note');
|
||||||
log(data);
|
log(data);
|
||||||
|
47
node_modules/note.js
generated
vendored
47
node_modules/note.js
generated
vendored
@@ -329,6 +329,23 @@ var Note = {
|
|||||||
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 添加修改ServerNoteId; 更新修改usn
|
||||||
|
updateNoteForceForSendChange: function(note, callback) {
|
||||||
|
var me = this;
|
||||||
|
note.IsDirty = false;
|
||||||
|
note.InitSync = false;
|
||||||
|
note.LocalIsNew = false;
|
||||||
|
Notes.update({NoteId: note.NoteId}, {$set: note}, function(err, n) {
|
||||||
|
if(err || !n) {
|
||||||
|
log('updateNoteForceForSendChange err');
|
||||||
|
log(err);
|
||||||
|
return callback && callback(false);
|
||||||
|
}
|
||||||
|
return callback && callback(true);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
// 服务器上的数据
|
// 服务器上的数据
|
||||||
// 为冲突更新, note已有有NoteId, ServerNoteId, 但NotebookId是服务器端的
|
// 为冲突更新, note已有有NoteId, ServerNoteId, 但NotebookId是服务器端的
|
||||||
updateNoteForceForConflict: function(note, callback) {
|
updateNoteForceForConflict: function(note, callback) {
|
||||||
@@ -338,7 +355,7 @@ var Note = {
|
|||||||
note.LocalIsNew = false;
|
note.LocalIsNew = false;
|
||||||
Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
|
Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
|
||||||
note['NotebookId'] = localNotebookId;
|
note['NotebookId'] = localNotebookId;
|
||||||
Notes.update({NoteId: localNoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional
|
Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional
|
||||||
if(err) {
|
if(err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
callback && callback(false);
|
callback && callback(false);
|
||||||
@@ -392,20 +409,21 @@ var Note = {
|
|||||||
|
|
||||||
var conflictNotes = noteSyncInfo.conflicts;
|
var conflictNotes = noteSyncInfo.conflicts;
|
||||||
log('fix note conflicts');
|
log('fix note conflicts');
|
||||||
|
log(conflictNotes);
|
||||||
// 这里为什么要同步? 因为fixConflicts后要进行send changes, 这些有冲突的不能发送changes
|
// 这里为什么要同步? 因为fixConflicts后要进行send changes, 这些有冲突的不能发送changes
|
||||||
if(conflictNotes) {
|
if(conflictNotes) {
|
||||||
async.eachSeries(conflictNotes, function(note, cb) {
|
async.eachSeries(conflictNotes, function(note, cb) { // note是服务器上最新的
|
||||||
var noteId = note.NoteId;
|
var noteId = note.NoteId; // 本地noteId
|
||||||
// 复制一份
|
// 复制一份
|
||||||
me.copyNoteForConfict(noteId, function(newNote) {
|
me.copyNoteForConfict(noteId, function(newNote) {
|
||||||
if(newNote) {
|
if(newNote) {
|
||||||
// 更新之前的
|
// 更新之前的
|
||||||
me.updateNoteForceForConflict(note, function(err, note2) {
|
me.updateNoteForceForConflict(note, function(note2) {
|
||||||
cb();
|
if(note2) {
|
||||||
if(!err) {
|
|
||||||
// 前端来处理, 全量sync时不用前端一个个处理
|
// 前端来处理, 全量sync时不用前端一个个处理
|
||||||
noteWeb && noteWeb.fixSyncConflict(note2, newNote);
|
noteWeb && noteWeb.fixSyncConflict(note2, newNote);
|
||||||
}
|
}
|
||||||
|
cb();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
cb();
|
cb();
|
||||||
@@ -473,13 +491,26 @@ var Note = {
|
|||||||
// 处理删除的
|
// 处理删除的
|
||||||
noteWeb.deleteSync(noteSyncInfo.deletes);
|
noteWeb.deleteSync(noteSyncInfo.deletes);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 获得用户修改的笔记
|
||||||
|
getDirtyNotes: function(callback) {
|
||||||
|
Notes.find({UserId: User.getCurActiveUserId(), IsDirty: true}, function(err, notes) {
|
||||||
|
if(err) {
|
||||||
|
log(err);
|
||||||
|
return callback && callback(false);
|
||||||
|
} else {
|
||||||
|
callback(notes);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
// 在send delete笔记时成功
|
// 在send delete笔记时成功
|
||||||
setNotDirty: function(noteId) {
|
setNotDirty: function(noteId) {
|
||||||
NB.update({NoteId: noteId}, {$set:{IsDirty: false}})
|
Notes.update({NoteId: noteId}, {$set:{IsDirty: false}})
|
||||||
},
|
},
|
||||||
// 在send delete笔记时有冲突, 设为不删除
|
// 在send delete笔记时有冲突, 设为不删除
|
||||||
setNotDirtyNotDelete: function(noteId) {
|
setNotDirtyNotDelete: function(noteId) {
|
||||||
NB.update({NoteId: noteId}, {$set:{IsDirty: false, LocalIsDelete: false}})
|
Notes.update({NoteId: noteId}, {$set:{IsDirty: false, LocalIsDelete: false}})
|
||||||
},
|
},
|
||||||
setIsNew: function(noteId) {
|
setIsNew: function(noteId) {
|
||||||
Notes.update({NoteId: noteId}, {$set:{LocalIsNew: true, IsDirty: true}})
|
Notes.update({NoteId: noteId}, {$set:{LocalIsNew: true, IsDirty: true}})
|
||||||
|
4
node_modules/notebook.js
generated
vendored
4
node_modules/notebook.js
generated
vendored
@@ -280,13 +280,13 @@ var Notebook = {
|
|||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
// 更新笔记本, NoteId可能也要更改
|
// 更新笔记本, NotebookId可能也要更改
|
||||||
// notebook是服务器传过来的
|
// notebook是服务器传过来的
|
||||||
updateNotebookForceForSendChange: function(notebookId, notebook, callback) {
|
updateNotebookForceForSendChange: function(notebookId, notebook, callback) {
|
||||||
var me = this;
|
var me = this;
|
||||||
notebook.IsDirty = false;
|
notebook.IsDirty = false;
|
||||||
notebook.LocalIsNew = false;
|
notebook.LocalIsNew = false;
|
||||||
notebook.ServerNotebookId = notebook.NotebookId;
|
notebook.ServerNotebookId = notebook.NotebookId; // ? 怎么可能要改呢? 因为这可能是添加后的笔记本
|
||||||
me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) {
|
me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) {
|
||||||
notebook.ParentNotebookId = parentNotebookId;
|
notebook.ParentNotebookId = parentNotebookId;
|
||||||
NB.update({NotebookId: notebookId}, {$set: notebook}, {}, function (err, n) {
|
NB.update({NotebookId: notebookId}, {$set: notebook}, {}, function (err, n) {
|
||||||
|
59
node_modules/sync.js
generated
vendored
59
node_modules/sync.js
generated
vendored
@@ -268,7 +268,7 @@ var Sync = {
|
|||||||
// 2.2 本地是否修改了, 冲突, 报告给前端, 前端处理
|
// 2.2 本地是否修改了, 冲突, 报告给前端, 前端处理
|
||||||
// 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的
|
// 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的
|
||||||
if(noteLocal.IsDirty) {
|
if(noteLocal.IsDirty) {
|
||||||
log('冲突....')
|
log('note 冲突....')
|
||||||
note.ServerNoteId = note.NoteId;
|
note.ServerNoteId = note.NoteId;
|
||||||
note.NoteId = noteLocal.NoteId;
|
note.NoteId = noteLocal.NoteId;
|
||||||
me._syncInfo.note.conflicts.push(note);
|
me._syncInfo.note.conflicts.push(note);
|
||||||
@@ -477,6 +477,7 @@ var Sync = {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// 更新
|
// 更新
|
||||||
|
// TODO 后端updateNotebook只要传Usn回来即可
|
||||||
Notebook.updateNotebookForceForSendChange(notebook.NotebookId, newNotebook);
|
Notebook.updateNotebookForceForSendChange(notebook.NotebookId, newNotebook);
|
||||||
|
|
||||||
if(notebook.LocalIsNew) {
|
if(notebook.LocalIsNew) {
|
||||||
@@ -527,14 +528,67 @@ var Sync = {
|
|||||||
// 调api, 所有执行后再callback();
|
// 调api, 所有执行后再callback();
|
||||||
// 一个一个同步执行, 因为要有
|
// 一个一个同步执行, 因为要有
|
||||||
async.eachSeries(notes, function(note, cb) {
|
async.eachSeries(notes, function(note, cb) {
|
||||||
|
/*
|
||||||
var api = Api.updateNote;
|
var api = Api.updateNote;
|
||||||
if(note.LocalIsNew) {
|
if(note.LocalIsNew) {
|
||||||
api = Api.addNote;
|
api = Api.addNote;
|
||||||
} else if(note.LocalIsDelete) {
|
} else if(note.LocalIsDelete) {
|
||||||
api = Api.deleteNote;
|
api = Api.deleteNote;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
api.call(Api, note, function(newNote) {
|
if(note.LocalIsNew) {
|
||||||
|
// 添加
|
||||||
|
Api.addNote(note, function(newNote) {
|
||||||
|
if(!Common.isOk(newNote)) {
|
||||||
|
return cb();
|
||||||
|
}
|
||||||
|
|
||||||
|
newNote.ServerNoteId = newNote.NoteId;
|
||||||
|
newNote.NoteId = note.NoteId;
|
||||||
|
me._syncInfo.note.changeAdds.push(newNote);
|
||||||
|
|
||||||
|
Note.updateNoteForceForSendChange(newNote);
|
||||||
|
|
||||||
|
me.checkNeedIncSyncAgain(newNote.Usn);
|
||||||
|
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
} else if(note.LocalIsDelete) {
|
||||||
|
// 删除, 不管它了
|
||||||
|
// TODO
|
||||||
|
Api.deleteTrash(note, function(ret) {
|
||||||
|
if(!Common.isOk(ret)) {
|
||||||
|
return cb();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// 更新
|
||||||
|
Api.updateNote(note, function(ret) {
|
||||||
|
if(!Common.isOk(ret)) {
|
||||||
|
if(typeof ret == 'object') {
|
||||||
|
if(ret.msg == 'conflict') {
|
||||||
|
me._syncInfo.note.changeConflicts.push(note);
|
||||||
|
}
|
||||||
|
else if(newNote.Msg == 'notExists') {
|
||||||
|
// 可能服务器上已删除, 此时应该要作为添加而不是更新
|
||||||
|
me._syncInfo.note.changeNeedAdds.push(note);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cb();
|
||||||
|
}
|
||||||
|
// 更新成功
|
||||||
|
Note.updateNoteForceForSendChange({NoteId: note.NoteId, Usn: ret.Usn});
|
||||||
|
|
||||||
|
me.checkNeedIncSyncAgain(ret.Usn);
|
||||||
|
|
||||||
|
return cb();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//...... 过时
|
||||||
|
/*
|
||||||
|
api.call(Api, note, function(ret) {
|
||||||
// 更新失败
|
// 更新失败
|
||||||
if(!newNote) {
|
if(!newNote) {
|
||||||
return cb();
|
return cb();
|
||||||
@@ -573,6 +627,7 @@ var Sync = {
|
|||||||
}
|
}
|
||||||
cb();
|
cb();
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
}, function() {
|
}, function() {
|
||||||
callback && callback();
|
callback && callback();
|
||||||
});
|
});
|
||||||
|
13
test.js
13
test.js
@@ -3,6 +3,7 @@
|
|||||||
var Notebook = require('notebook');
|
var Notebook = require('notebook');
|
||||||
var Note = require('note');
|
var Note = require('note');
|
||||||
var Api = require('api');
|
var Api = require('api');
|
||||||
|
var User = require('user');
|
||||||
var Server = require('server');
|
var Server = require('server');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -10,11 +11,21 @@ Notebook.addNotebook("1", "life");
|
|||||||
Notebook.addNotebook("2", "生活");
|
Notebook.addNotebook("2", "生活");
|
||||||
Notebook.addNotebook("3", "工作");
|
Notebook.addNotebook("3", "工作");
|
||||||
Notebook.addNotebook("4", "life2", "1");
|
Notebook.addNotebook("4", "life2", "1");
|
||||||
*/
|
|
||||||
|
|
||||||
Api.addNotebook({
|
Api.addNotebook({
|
||||||
Title: "哈哈"
|
Title: "哈哈"
|
||||||
}, function() {});
|
}, function() {});
|
||||||
|
*/
|
||||||
|
User.userId = '54bdc65599c37b0da9000002';
|
||||||
|
console.log(User.getCurActiveUserId());
|
||||||
|
Note.getDirtyNotes(function(ret) {
|
||||||
|
console.log(ret);
|
||||||
|
});
|
||||||
|
/*
|
||||||
|
Note.getNoteByServerNoteId("54c6313799c37bdeec000008", function(ret){
|
||||||
|
console.log(ret);
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
// Api.auth('c@a.com', 'abc123');
|
// Api.auth('c@a.com', 'abc123');
|
||||||
|
Reference in New Issue
Block a user