From 21f5306f7935e025b08f68f748ec239727bc49a4 Mon Sep 17 00:00:00 2001 From: life Date: Fri, 20 Jan 2017 12:45:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0=E5=92=8C=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E5=90=8C=E6=AD=A5BUGs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修复不能添加已删除的tag 2. FullSync时可能导致一些笔记为Dirty 3. 笔记错误同步信息显示 --- node_modules/api.js | 24 +++++------------------- node_modules/note.js | 32 +++++++++++++++++++++----------- node_modules/sync.js | 36 +++++++++++++++++++++++++++--------- node_modules/tag.js | 43 +++++++++++++++++++++++++++++++------------ public/js/app/note.js | 8 +++++++- public/js/app/page.js | 2 +- public/js/app/tag.js | 34 +++++++++++++++++++++++----------- 7 files changed, 115 insertions(+), 64 deletions(-) diff --git a/node_modules/api.js b/node_modules/api.js index 6c869dc0..00906f26 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -571,7 +571,7 @@ var Api = { break; } - + // for test // data.FileDatas = null; @@ -687,12 +687,9 @@ var Api = { return callback && callback(false); } var ret = resp.body; - console.log('add tag ret =========='); - console.log(ret); + console.log(' add tag ret: ', ret); if(Common.isOk(ret)) { - // Tag.setNotDirty(title); - // 更新, 添加usn - Tag.setNotDirtyAndUsn(title, ret.Usn); + callback && callback(ret); } else { callback && callback(false); @@ -708,19 +705,8 @@ var Api = { return callback && callback(false); } var ret = resp.body; - log('delete tag ret ==========='); - log(ret); - if(Common.isOk(ret)) { - // 以后不要再发了 - Tag.setNotDirty(tag.Tag); - callback && callback(ret); - } else { - // 出错了也不要发了, 万一是网络原因呢? - if(ret.Msg == 'conflict') { - Tag.setNotDirty(tag.Tag); - } - callback && callback(false); - } + console.log(' delete tag ret:', ret); + callback && callback(ret); }); }, diff --git a/node_modules/note.js b/node_modules/note.js index 21bc8ca1..edd278fd 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -616,7 +616,7 @@ var Note = { var me = this; // 修复内容, 修改图片, 附件链接为本地链接 content = me.fixNoteContent(content); - db.notes.update({NoteId: noteId}, { $set: {Err: null, Content: content, InitSync: false, IsContentDirty: false} }, {}, function (err, numReplaced) { + db.notes.update({NoteId: noteId}, { $set: {Err: '', Content: content, InitSync: false, IsContentDirty: false} }, {}, function (err, numReplaced) { if(err) { log(err); callback && callback(false); @@ -965,7 +965,7 @@ var Note = { // 不要服务器上的 delete note['UpdatedTime']; delete note['CreatedTime']; - note.Err = null; + note.Err = ''; db.notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional // console.log('re:'); @@ -1056,6 +1056,7 @@ var Note = { delete note['Files']; delete note['UpdatedTime']; delete note['CreatedTime']; + note.Err = ''; // multi: true, 避免有历史的笔记有问题 db.notes.update({NoteId: note.NoteId}, {$set: note}, {multi: true}, function(err, n) { @@ -1663,10 +1664,10 @@ var Note = { var me = this; setTimeout(function() { // 内容 - console.log("syncContentAndImagesAndAttachs..................." + note.NoteId); + console.log(" syncContentAndImagesAndAttachs..... " + note.NoteId); me.getNoteContent(note.NoteId, function(noteAndContent) { if(noteAndContent) { - console.log('sync content ' + note.NoteId + ' ok'); + console.log(' sync content ' + note.NoteId + ' ok'); var content = noteAndContent.Content; Web.contentSynced(note.NoteId, note.Content); // 图片 @@ -1812,21 +1813,30 @@ var Note = { })(title); } }, + + // 这里,有一个大BUG, 导致全量同步时会修改一些笔记的IsDirty + // title==null时传过来 + // tag.js调用 // 删除包含title的笔记 + // 先删除tag, 再删除tag.js updateNoteToDeleteTag: function(title, callback) { + if (!title) { + return callback({}); + } var updates = {}; // noteId => var userId = User.getCurActiveUserId(); - // console.log('updateNoteToDeleteTag--'); + console.log(' updateNoteToDeleteTag', title); db.notes.find({UserId: userId, LocalIsDelete: false , Tags: {$in: [title]}}, function(err, notes) { - console.log(notes); + console.log( 'updateNoteToDeleteTag notes', err, title, notes); if(!err && notes && notes.length > 0) { - for(var i in notes) { + for(var i = 0; i < notes.length; ++i) { var note = notes[i]; var tags = note.Tags; // 删除之 - for(var j in tags) { + for(var j = 0; j < tags.length; ++j) { if(tags[j] == title) { - tags = tags.splice(j, 1); + // tags = tags.splice(j, 1); // 之前是这样, 返回的是删除之后的 + tags.splice(j, 1); break; } } @@ -1834,8 +1844,8 @@ var Note = { note.IsDirty = true; updates[note.NoteId] = note; db.notes.update({_id: note._id}, {$set: {Tags: tags, IsDirty: true}}, function(err) { - console.log("??"); - console.log(err); + // console.log("??"); + // console.log(err); callback(updates); }); } diff --git a/node_modules/sync.js b/node_modules/sync.js index e130c731..fe16ea82 100644 --- a/node_modules/sync.js +++ b/node_modules/sync.js @@ -497,20 +497,19 @@ var Sync = { // 1) 服务器端删除了, 本地肯定删除 if(tag.IsDeleted) { - log('delete tag: '); - log(tag); + console.log(' delete tag: ', tag); Tag.deleteTag(tag.Tag, function() { // me._syncInfo.tag.deletes.push(tag.Tag); me.fixSynced('tag', 'deletes', tag.Tag); canCall(); - }, true); + }, true, me.fullSyncStart); return; } // 2) 查看本地的, 与本地合并 Tag.getTag(tag.Tag, function(tagLocal) { // 2.1 本地没有, 表示是新建 if(!tagLocal) { - log('add tag: ...') + console.log(' add tag: ...') Tag.addOrUpdateTag(tag.Tag, function(tagAdded) { // me._syncInfo.tag.adds.push(tagAdded); me.fixSynced('tag', 'adds', tagAdded); @@ -586,6 +585,7 @@ var Sync = { var me = this; me._stop = false; me._initSyncInfo(); + me.fullSyncStart = true; User.getAllLastSyncState(function(lastUsn, notebookUsn, noteUsn, tagUsn) { // 不可能会有lastUsn吧 @@ -605,7 +605,7 @@ var Sync = { tagUsn = -1; } - // console.log('fullSync------ ' + notebookUsn + ' ' + noteUsn + ' ' + tagUsn); + console.log('fullSync ' + notebookUsn + ' ' + noteUsn + ' ' + tagUsn); // Web.syncNotebookFinish(); // 同步笔记本 @@ -620,6 +620,7 @@ var Sync = { // 同步标签 me.syncTag(tagUsn, function(ok) { if (ok) { + me.fullSyncStart = false; // 更新上次同步时间 me.updateLastSyncState(function() { // send changes @@ -628,16 +629,19 @@ var Sync = { }); } else { + me.fullSyncStart = false; console.error('syncTag error....'); callback && callback(me._syncInfo, false); } }); } else { + me.fullSyncStart = false; console.error('syncNote error.... 跳过tag'); callback && callback(me._syncInfo, false); } }); } else { + me.fullSyncStart = false; console.error('syncNotebook error.... 跳过note,tag'); callback && callback(me._syncInfo, false); } @@ -1015,6 +1019,11 @@ var Sync = { Api.deleteTrash(note, function(ret) { if(Common.isOk(ret)) { me.checkNeedIncSyncAgain(ret.Usn); + + // 本地删除了的, 服务端没有, 直接删除本地的 + } else if (typeof ret == 'object' && ret.Msg == 'notExists') { + console.log( '本地删除了的, 服务端没有, 直接删除本地的'); + Note.deleteLocalNote(note.NoteId); } return cb(); }); @@ -1121,7 +1130,10 @@ var Sync = { if(!Common.isOk(newTag)) { return cb(); } - me._syncInfo.note.changeAdds.push(newTag); + // 更新, 添加usn + Tag.setNotDirtyAndUsn(tag.Tag, newTag.Usn); + + me._syncInfo.tag.changeAdds.push(newTag); // 之前是note.changeAdds // Tag.updateTagForce(newTag); me.checkNeedIncSyncAgain(newTag.Usn); cb(); @@ -1130,10 +1142,16 @@ var Sync = { // 删除, 不管它了 Api.deleteTag(tag, function(ret) { if(Common.isOk(ret)) { + Tag.setNotDirty(tag.Tag); me.checkNeedIncSyncAgain(ret.Usn); - } else { - // 有问题, 可能本地不存在 - Tag.setNotDirty(tag); + // 本地删除了的, 服务端没有, 直接删除本地的 + } else if (typeof ret == 'object') { + if (ret.Msg == 'notExists') { + console.log( 'tag本地删除了的, 服务端没有, 直接删除本地的'); + Tag.deleteLocalTag(tag.Tag); + } else if(ret.Msg == 'conflict') { + Tag.setNotDirty(tag.Tag); + } } return cb(); }); diff --git a/node_modules/tag.js b/node_modules/tag.js index 0a6b8ed8..2d0cf270 100644 --- a/node_modules/tag.js +++ b/node_modules/tag.js @@ -26,13 +26,15 @@ var Tag = { // 已存的, 不更新IsDirty var Note = require('note'); if(!err && tag) { - Note.countNoteByTag(title, function(cnt) { - tag.Count = cnt; - db.tags.update({UserId: userId, Title: title}, {$set: {Count: cnt, UpdatedTime: new Date()}}, function() { - console.log('已存在tag' + title); + // Note.countNoteByTag(title, function(cnt) { + // tag.Count = cnt; + // LocalIsDelete变为false, 可能之前删除了, 后来又重新添加了 + // {Tag: title}, 之前是 {Title: title} + db.tags.update({UserId: userId, Tag: title}, {$set: {UpdatedTime: new Date(), LocalIsDelete: false, IsDirty: true}}, function() { + console.log('已存在tag ' + title); callback(tag); }); - }); + // }); } else { var date = new Date(); db.tags.insert({ @@ -55,27 +57,44 @@ var Tag = { } }); }, + getTags: function(callback) { db.tags.find({UserId: User.getCurActiveUserId(), LocalIsDelete: false}, function(err, tags) { if(err) { callback && callback(false); } else { + // 排序, 用UpdatedTime来排序 + tags.sort(function (a, b) { + return a.UpdatedTime < b.UpdatedTime; + }); callback && callback(tags); } }); }, // 删除标签, 更新为LocaleIsDelete = true - deleteTag: function(title, callback, isForce) { + deleteTag: function(title, callback, isForce, isFullSync) { + title += ''; var me = this; db.tags.update({UserId: User.getCurActiveUserId(), Tag: title}, {$set: {LocalIsDelete: true, IsDirty: !isForce, UpdatedTime: new Date()}}, function() { }); - // - var Note = require('./note'); - // console.log(Note); - Note.updateNoteToDeleteTag(title, function(updates) { - callback && callback(updates); - }); + + // 不是fullSync才更新笔记的标签, 防止fullSync时还有一批IsDirty的 + if (!isFullSync) { + // 删除笔记内的tag + var Note = require('./note'); + Note.updateNoteToDeleteTag(title, function(updates) { + callback && callback(updates); + }); + } else { + console.log(' isFullSync not need updateNoteToDeleteTag'); + callback && callback({}); + } + }, + + deleteLocalTag: function (title) { + console.log(' deleteLocalTag', title) + db.tags.remove({Tag: title}); }, // 更新标签的数量, 在彻底删除笔记时调用 diff --git a/public/js/app/note.js b/public/js/app/note.js index c9e6dddf..bd95bb44 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -450,6 +450,7 @@ Note.setNoteDirty = function (noteId, isDirty) { if (!isDirty) { $leftNoteNav.removeClass('item-err'); } + this.setNoteCache({NoteId: noteId, IsDirty: isDirty}, false); isDirty ? $leftNoteNav.addClass('item-dirty') : $leftNoteNav.removeClass('item-dirty'); }; @@ -1778,11 +1779,12 @@ Note.deleteNoteTag = function(item, tag) { if(!item) { return; } + // noteId => note for(var noteId in item) { var note = Note.getNote(noteId); if(note) { note.Tags = note.Tags || []; - for(var i in note.Tags) { + for(var i = 0; i < note.Tags.length; ++i) { if(note.Tags[i] == tag) { note.Tags.splice(i, 1); continue; @@ -1792,6 +1794,8 @@ Note.deleteNoteTag = function(item, tag) { if(noteId == Note.curNoteId) { Tag.input.setTags(note.Tags); } + + Note.setNoteDirty(noteId, true); } } }; @@ -3511,6 +3515,8 @@ Note.updateSync = function(notes) { // 先删除, 不然changeToNext()之前会先保存现在的, 导致僵尸note Note.deleteCache(note.NoteId); + var target = $(tt('#noteItemList [noteId="?"]', note.NoteId)); + // 当前笔记要删除了, 如果有多个笔记要删除, 这就有问题了 // 刚一切换到下一个, 就被删除了, 导致没有被选中 if(target.length) { diff --git a/public/js/app/page.js b/public/js/app/page.js index 33cc8663..60254f50 100644 --- a/public/js/app/page.js +++ b/public/js/app/page.js @@ -1489,7 +1489,7 @@ function initPage(initedCallback) { // 标签 TagService.getTags(function(tags) { - Tag.nav.setTags(tags); + Tag.nav.setTags(tags); ok(); }); diff --git a/public/js/app/tag.js b/public/js/app/tag.js index b7aef70e..75e88044 100644 --- a/public/js/app/tag.js +++ b/public/js/app/tag.js @@ -6,17 +6,26 @@ var TagNav = function() { this.tags = []; this.curTag = null; this.$element = $('#tagNav'); + + // 搜索 this.$element.on('click', 'li .label', function() { var tagValue = $(this).closest('li').data('tag').trim(); me.searchByTag(tagValue); }); + + // 删除 this.$element.on('click', 'li .tag-delete', function () { var $li = $(this).closest('li'); var tag = $.trim($li.data('tag')); if(confirm(getMsg('Are you sure ?'))) { TagService.deleteTag(tag, function(re) { + console.log(' delete tag', re); if(typeof re == 'object' && re.Ok !== false) { + // re == {noteId => note} + // 笔记删除标签 Note.deleteNoteTag(re, tag); + + // 导航删除 $li.remove(); me._deleteTag(tag); } @@ -175,7 +184,7 @@ var TagInput = function() { } }, 200); }); - + me.$tags.on('click', 'i', function() { me._removeTag($(this).parent()); }); @@ -244,18 +253,21 @@ TagInput.prototype = { } }, - // 保存tag + // 保存tag, 到数据库 _saveTag(text) { - if(text) - { - Note.curChangedSaveIt(true, function() { - TagService.addOrUpdateTag(text, function(ret) { - if(typeof ret == 'object' && ret.Ok !== false) { - Tag.nav.addTags([ret]); - } - }); - }); + if (!text) { + return; } + console.log('save tag to db', text); + Note.curChangedSaveIt(true, function() { + console.log('save tag to db yes!!'); + TagService.addOrUpdateTag(text, function(ret) { + console.log(ret); + if(typeof ret == 'object' && ret.Ok !== false) { + Tag.nav.addTags([ret]); + } + }); + }); }, // 删除tag