diff --git a/node_modules/api.js b/node_modules/api.js index 156ad4c9..d2999bcb 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -209,12 +209,8 @@ var Api = { return callback && callback(false); } 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); } diff --git a/node_modules/note.js b/node_modules/note.js index 85b0c7d8..f98b47d9 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -408,7 +408,7 @@ var Note = { var me = this; if(note) { // 如果是本地用户, 则直接删除 - if(User.isLocal) { + if(User.isLocal()) { db.notes.remove({_id: note._id}, function(err, n) { if(n) { me.deleteNoteAllOthers(note); @@ -574,9 +574,8 @@ var Note = { // 远程修改本地内容 updateNoteContentForce: function(noteId, content, callback) { var me = this; - + // 修复内容, 修改图片, 附件链接为本地链接 content = me.fixNoteContent(content); - db.notes.update({NoteId: noteId}, { $set: {Content: content, InitSync: false, IsContentDirty: false} }, {}, function (err, numReplaced) { if(err) { log(err); @@ -587,21 +586,20 @@ var Note = { }); }, - /* - // 同步内容 - updateNoteContentForce: function(noteId, content, callback) { - // 将笔记内容中 - - db.notes.update({NoteId: noteId}, { $set: {Content: content, InitSync: false} }, {}, function (err, numReplaced) { - if(err) { - log(err); - callback && callback(false); + // sync调用, 用于判断是否真的有冲突 + // 从服务器上获取内容 + getNoteContentFromServer: function (serverNoteId, callback) { + var me = this; + Api.getNoteContent(serverNoteId, function(noteContent) { + // 同步到本地 + if(Common.isOk(noteContent)) { + var content = me.fixNoteContent(noteContent.Content); + callback(content); } else { - callback && callback(content); + callback(false); } }); }, - */ // 得到笔记内容 // noteId是本地Id @@ -654,6 +652,7 @@ var Note = { // 同步到本地 if(Common.isOk(noteContent)) { + me.updateNoteContentForce(noteId, noteContent.Content, function(content) { noteContent.Content = content; noteContent.NoteId = noteId; @@ -809,6 +808,11 @@ var Note = { }); }, + // send change后, 发现内容是一样的, 此时修改本地Usn和server的一样, 下次再push + updateNoteUsn: function (noteId, usn) { + db.notes.update({NoteId: note.NoteId}, {$set: {Usn: usn, IsDirty: true}}); + }, + // sync <- 时 // 更新笔记, 合并之, 内容要重新获取 // note是服务器传过来的, 需要处理下fix @@ -1392,6 +1396,9 @@ var Note = { } else { // 每一个笔记得到图片, 附件信息和数据 async.eachSeries(notes, function(note, cb) { + // LocalContent 留作副本, 用于冲突判断 + note.LocalContent = note.Content; + note.Content = me.fixContentUrl(note.Content); me.getNoteFiles(note, function(files) { note.Content = me.fixNoteContentForSend(note.Content); diff --git a/node_modules/sync.js b/node_modules/sync.js index c215be4d..e74b946f 100644 --- a/node_modules/sync.js +++ b/node_modules/sync.js @@ -141,9 +141,9 @@ var Sync = { function canCall() { // 是最后一块, 且 me._addSyncNotebookNum(); - console.log(me._syncNotebookIsLastChunk); - console.log(me._totalHasSyncNotebookNum + ' ' + me._totalSyncNotebookNum); - console.log(me._syncInfo.notebook.ok); + // console.log(me._syncNotebookIsLastChunk); + // console.log(me._totalHasSyncNotebookNum + ' ' + me._totalSyncNotebookNum); + // console.log(me._syncInfo.notebook.ok); if(me._syncNotebookIsLastChunk && me._totalHasSyncNotebookNum >= me._totalSyncNotebookNum) { // 防止多次callback @@ -171,8 +171,8 @@ var Sync = { // 1) 服务器端删除了, 本地肯定删除 if(notebook.IsDeleted) { - console.log('delete: '); - console.log(notebook); + // console.log('delete: '); + // console.log(notebook); Notebook.getNotebookIdByServerNotebookId(notebookId, function(localNotebookId) { Notebook.deleteNotebookForce(notebookId, function() { me._syncInfo.notebook.deletes.push(localNotebookId); @@ -332,6 +332,7 @@ var Sync = { // 2.2 本地是否修改了, 冲突, 报告给前端, 前端处理 // 冲突, 将本地修改的笔记复制一份(设置冲突字段, ConflictNoteId), 远程的覆盖本地的 + // 新方法: 冲突后, 得到最新内容, 看是否与本地内容一致, 如果一致, 则不冲突, 其它数据用服务器上的 if(noteLocal.IsDirty) { console.log('note 冲突.... serverNoteId: ' + noteId); // console.log(noteLocal.NoteId); @@ -339,7 +340,28 @@ var Sync = { // console.log(noteLocal); // note.ServerNoteId = note.NoteId; // note.NoteId = noteLocal.NoteId; - me._syncInfo.note.conflicts.push({server: note, local: noteLocal}); + + Note.getNoteContentFromServer(noteId, function (content) { + // 表示没有获取到content, 则只能标志为冲突了 + // 内容不一样, 也标为冲突 + if (content === false || content != noteLocal.Content) { + me._syncInfo.note.conflicts.push({server: note, local: noteLocal}); + } + // 否则, 内容一样, 标为不冲突, 需要更新 + else { + + // 2.3 服务器是最新的, 用服务器的 + // 服务器是最新的, 本地没动过, 则覆盖之 + Note.updateNoteForce(note, function(note) { + if(note) { + me._syncInfo.note.updates.push(note); + } + canCall(); + }, false); + + } + }); + return canCall(); // 2.3 服务器是最新的, 用服务器的 } else { @@ -364,7 +386,7 @@ var Sync = { } Api.getSyncNotes(afterUsn, me._noteMaxEntry, function(notes) { - console.log('syncNote---') + console.log('syncNote---'); console.log(notes); if(Common.isOk(notes)) { me._totalSyncNoteNum += notes.length; @@ -874,70 +896,55 @@ var Sync = { if(typeof ret == 'object') { if(ret.Msg == 'conflict') { console.error('updateNote 冲突-----------'); - me._syncInfo.note.changeConflicts.push(note); + + // 这种情况有很少见, 原因是先pull, 肯定会pull过来 + // 处理方法和pull一样, 看内容是否一样 + // 如果一样, 则不标志为冲突, 修改本Usn为serverUsn, 等下次再send changes + Note.getNoteContentFromServer(note.ServerNoteId, function (content) { + if (content === false || content != note.LocalContent) { + me._syncInfo.note.changeConflicts.push(note); + cb(); + } + else { + // 不冲突, 修改之Usn + Api.getNote(note.ServerNoteId, function (serverNote) { + // 取不到, 当作冲突 + if (!serverNote) { + me._syncInfo.note.changeConflicts.push(note); + cb(); + } + else { + Note.updateNoteUsn(note.NoteId, serverNote.Usn); + cb(); + } + }); + } + }); } else if(ret.Msg == 'notExists') { // 可能服务器上已删除, 此时应该要作为添加而不是更新 me._syncInfo.note.changeNeedAdds.push(note); + cb(); } } - return cb(); + // 更新失败了, 服务器返回异常 + else { + cb(); + } } // 更新成功 - // TODO 返回是真note - ret.ServerNoteId = ret.NoteId; - ret.NoteId = note.NoteId; - Note.updateNoteForceForSendChange(ret, false); - me._syncInfo.note.changeUpdates.push(note); + else { + ret.ServerNoteId = ret.NoteId; + ret.NoteId = note.NoteId; + Note.updateNoteForceForSendChange(ret, false); + me._syncInfo.note.changeUpdates.push(note); - me.checkNeedIncSyncAgain(ret.Usn); + me.checkNeedIncSyncAgain(ret.Usn); - return cb(); + return cb(); + } }); } - - //...... 过时 - /* - api.call(Api, note, function(ret) { - // 更新失败 - if(!newNote) { - return cb(); - } - - // 删除操作 - if(note.LocalIsDelete) { - return cb(); - } - - // 更新成功, 是否有冲突? - // newNote是服务器上的笔记 - // 没有更新成功, 那么要处理冲突 - if(!newNote.NoteId) { - if(newNote.Msg == 'conflict') { - me._syncInfo.note.changeConflicts.push(note); - } else if(newNote.Msg == 'notExists') { - // 可能服务器上已删除, 此时应该要作为添加而不是更新 - me._syncInfo.note.changeNeedAdds.push(note); - } - } - // 更新成功, 修改到本地 - else { - // 更新 - Note.updateNoteForceForSendChange(note.NoteId, newNote); - - if(note.LocalIsNew) { - newNote.ServerNoteId = newNote.NoteId; - newNote.NoteId = note.NoteId; - me._syncInfo.note.changeAdds.push(newNote); - } else { - me._syncInfo.note.updates.push(newNote); - } - - me.checkNeedIncSyncAgain(newNote.Usn); - } - cb(); - }); - */ }, function() { callback && callback(); });