Esempio n. 1
0
	updateNoteForceForConflict: function(note, callback) {
		var me = this;
		note.NoteId = note.ServerNoteId;
		me.updateNoteForce(note, callback);
		return;

		note.IsDirty = false;
		note.InitSync = true;
		note.LocalIsNew = false;
		note.LocalIsDelete = false;
		// 文件操作

		Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
			note['NotebookId'] = localNotebookId;
			db.notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, cnt) {   // Callback is optional
				if(err) {
					console.log(err);
					callback && callback(false);
				} else {
					log('强制更新...');
					callback && callback(note);
				}
			});
		});
	},
Esempio n. 2
0
			(function(notebook) {

				var usn = notebook.Usn;
				var notebookId = notebook.NotebookId; // 服务器端的

				// 1) 服务器端删除了, 本地肯定删除
				if(notebook.IsDeleted) {
					// console.log('delete: ');
					// console.log(notebook);
					Notebook.getNotebookIdByServerNotebookId(notebookId, function(localNotebookId) {
						Notebook.deleteNotebookForce(notebookId, function() { 
							me._syncInfo.notebook.deletes.push(localNotebookId);
							canCall();
						})
					});
					return;
				}
				// 2) 查看本地的, 与本地合并
				Notebook.getNotebookByServerNotebookId(notebookId, function(notebookLocal) {
					// 2.1 本地没有, 表示是新建
					if(!notebookLocal) {
						console.log('add addNotebookForce...')
						// TODO
						Notebook.addNotebookForce(notebook, function(notebook) {
							me._syncInfo.notebook.adds.push(notebook);
							canCall();
						});
					} else {
						// 如果Usn一样, 表示服务器端并没有修改
						if(notebookLocal.Usn === notebook.Usn) {
							console.log('notebookLocal 如果Usn一样, 表示服务器端并没有修改');
							return canCall();
						}
						
						// 2.2 本地是否修改了, 需要合并, 使用服务端的数据
						if(notebook.IsDirty) {
							console.log('冲突....')
						// 2.3 服务器是最新的, 用服务器的
						} else {
						}
						// 这里都是用服务器端的数据, 不处理冲突
						Notebook.updateNotebookForce(notebook, notebookLocal, function(notebook) {
							if(notebook) {
								me._syncInfo.notebook.updates.push(notebook);
							}
							canCall();
						})
					}
				});
			})(notebook);
Esempio n. 3
0
	addNoteForce: function(note, callback) {
		var me = this;
		note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步
		note.IsDirty = false;
		note.ServerNoteId = note.NoteId;
		note.NoteId = Common.objectId();
		note.LocalIsDelete = false;

		console.error('add note' +  note.Title + Common.goNowToDate(note.CreatedTime));
		note.CreatedTime = Common.goNowToDate(note.CreatedTime);
		note.UpdatedTime = Common.goNowToDate(note.UpdatedTime);

		// 附件操作
		var files = note.Files || [];
		var attachs = [];
		for(var i in files) {
			var file = files[i];
			if(file.IsAttach) { // LocalFileId, FileId
				file.ServerFileId = file.FileId;
				file.FileId = file.ServerFileId; // 弄成一样的, 只是没有Path
				attachs.push(file);
			}
		}
		note.Attachs = attachs;
		delete note['Files'];

		Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
			note.NotebookId = localNotebookId;
			Notes.insert(note, function (err, newDoc) {   // Callback is optional
				if(err) {
					console.log(err);
					callback && callback(false);
				} else {
					// console.log("?????????")
					// console.log(note);
					// console.log(note.CreatedTime);
					callback && callback(newDoc);

					// 重新统计
					Notebook.reCountNotebookNumberNotes(note.NotebookId);

					// 下载内容, 图片, 附件
					me.syncContentAndImagesAndAttachs(newDoc, 2000);
				}
			});
		});
	},
Esempio n. 4
0
		me.getNoteByServerNoteId(note.NoteId, function(everNote) {
			if(!everNote) {
				return;
			}
			var everAttachs = everNote.Attachs;
			var everAttachsMap = {};

			// var needAdds = [];

			// 得到要删除的
			var needDeletes = [];
			for(var i in everAttachs) {
				var everAttach = everAttachs[i];
				everAttachsMap[everAttach.ServerFileId] = everAttach;
				if(!attachsMap[everAttach.ServerFileId]) {
					needDeletes.push(everAttach);
				}
			}
			// console.log('everAttachs');
			// console.log(everAttachs);
			// console.log('attachsMap')
			// console.log(attachsMap);
			// 通过FileId删除文件
			me.deleteAttachs(needDeletes);

			// 得到要添加的,所有的
			// 新添加的没有Path
			var allAttachs = [];
			for(var serverFileId in attachsMap) {
				if(!everAttachsMap[serverFileId]) {
					// needAdds.push(attachMap[serverFileId]);
					attachsMap[serverFileId].FileId = serverFileId; // 生成一个Id(一样的), 但是没有Path
					allAttachs.push(attachsMap[serverFileId]);
				} else {
					allAttachs.push(everAttachsMap[serverFileId]);
				}
			}
			note.Attachs = allAttachs;

			note.ServerNoteId = note.NoteId;
			note.NoteId = everNote.NoteId;
			delete note['Files'];
			// console.log('evernote');
			// console.log(everNote);

			// 得到本地笔记本Id
			Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
				note['NotebookId'] = localNotebookId;

				// console.log("updateNoteForce 后的")
				// console.log(note);
				// console.log(note.ServerNoteId + " " + note.IsDirty);
				
				// console.log('ever note');
				// console.log(everNote.NoteId);
				// console.log(everNote);

				// 不要服务器上的
				delete note['UpdatedTime'];
				delete note['CreatedTime'];

				db.notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, cnt) { // Callback is optional
					console.log('re:');
					console.log(err);
					console.log(cnt);
					if(err) {
						console.error(err);
						callback && callback(false);
					} else {
						console.log('强制更新...');
						callback && callback(note);

						/*
						me.getNoteByServerNoteId(note.ServerNoteId, function(t) {
							console.log('强制更新后的...');
							console.log(t);
						});
						*/

						// 重新统计之
						Notebook.reCountNotebookNumberNotes(note.NotebookId);

						// 下载内容, 图片, 附件
						me.syncContentAndImagesAndAttachs(note);
					}
				});
			});
		});
Esempio n. 5
0
	addNoteForce: function(note, callback) {
		var me = this;
		note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步
		note.IsDirty = false;
		note.LocalIsDelete = false;
		// 这里, 悲剧, 一个大BUG, 应该和server端IsTrash一致,
		// 不然同步的时候将IsTrash的笔记同步到非IsTrash, 2015/10/31 fixed 谢谢 3601提供的信息
		// note.IsTrash = false; 
		if (typeof note.IsTrash == 'boolean') {
			note.IsTrash = note.IsTrash;
		}
		else {
			note.IsTrash = false;
		}
		
		note.ServerNoteId = note.NoteId;
		note.NoteId = Common.objectId();
		
		note.CreatedTime = Common.goNowToDate(note.CreatedTime);
		note.UpdatedTime = Common.goNowToDate(note.UpdatedTime);

		// 附件操作
		var files = note.Files || [];
		var attachs = [];
		for(var i in files) {
			var file = files[i];
			if(file.IsAttach) { // LocalFileId, FileId
				file.ServerFileId = file.FileId;
				file.FileId = file.ServerFileId; // 弄成一样的, 只是没有Path
				attachs.push(file);
			}
		}
		note.Attachs = attachs;
		delete note['Files'];

		Notebook.getNotebookIdByServerNotebookId(note.NotebookId, function(localNotebookId) {
			note.NotebookId = localNotebookId;
			db.notes.insert(note, function (err, newDoc) {   // Callback is optional
				if(err) {
					console.log(err);
					callback && callback(false);
				} else {
					// console.log("?????????")
					// console.log(note);
					// console.log(note.CreatedTime);
					callback && callback(newDoc);

					// 添加到历史中
					// 因为没有内容, 所以不用添加
					// me.addNoteHistory(note.NoteId, note.Content);

					// 重新统计
					Notebook.reCountNotebookNumberNotes(note.NotebookId);

					// 下载内容, 图片, 附件
					// 添加时不要, 这个请求由前端发出
					// me.syncContentAndImagesAndAttachs(newDoc, 2000);
				}
			});
		});
	},