1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

All: Improved synchronisation process and saving of models so that reducer can deal with full objects

This commit is contained in:
Laurent Cozic
2017-12-04 22:58:42 +00:00
parent 73e81a54b4
commit a6d6201ecb
3 changed files with 48 additions and 24 deletions

View File

@ -410,14 +410,18 @@ class Synchronizer {
let action = null;
let reason = '';
let local = await BaseItem.loadItemByPath(path);
let ItemClass = null;
let content = null;
if (!local) {
if (remote.isDeleted !== true) {
action = 'createLocal';
reason = 'remote exists but local does not';
content = await loadContent();
ItemClass = content ? BaseItem.itemClass(content) : null;
}
} else {
ItemClass = BaseItem.itemClass(local);
local = ItemClass.filter(local);
if (remote.isDeleted) {
action = 'deleteLocal';
reason = 'remote has been deleted';
@ -440,7 +444,6 @@ class Synchronizer {
this.logger().warn('Remote has been deleted between now and the list() call? In that case it will be handled during the next sync: ' + path);
continue;
}
let ItemClass = BaseItem.itemClass(content);
content = ItemClass.filter(content);
// 2017-12-03: This was added because the new user_updated_time and user_created_time properties were added
@ -451,34 +454,20 @@ class Synchronizer {
if (!content.user_updated_time) content.user_updated_time = content.updated_time;
if (!content.user_created_time) content.user_created_time = content.created_time;
let newContent = null;
if (action === 'createLocal') {
newContent = Object.assign({}, content);
} else if (action === 'updateLocal') {
newContent = BaseModel.diffObjects(local, content);
newContent.type_ = content.type_;
newContent.id = content.id;
} else {
throw new Error('Unknown action: ' + action);
}
let options = {
autoTimestamp: false,
nextQueries: BaseItem.updateSyncTimeQueries(syncTargetId, newContent, time.unixMs()),
nextQueries: BaseItem.updateSyncTimeQueries(syncTargetId, content, time.unixMs()),
};
if (action == 'createLocal') options.isNew = true;
if (action == 'updateLocal') options.oldItem = local;
if (newContent.type_ == BaseModel.TYPE_RESOURCE && action == 'createLocal') {
let localResourceContentPath = Resource.fullPath(newContent);
let remoteResourceContentPath = this.resourceDirName_ + '/' + newContent.id;
if (content.type_ == BaseModel.TYPE_RESOURCE && action == 'createLocal') {
let localResourceContentPath = Resource.fullPath(content);
let remoteResourceContentPath = this.resourceDirName_ + '/' + content.id;
await this.api().get(remoteResourceContentPath, { path: localResourceContentPath, target: 'file' });
}
// if (!newContent.user_updated_time) newContent.user_updated_time = newContent.updated_time;
// if (!newContent.user_created_time) newContent.user_created_time = newContent.created_time;
await ItemClass.save(newContent, options);
await ItemClass.save(content, options);
} else if (action == 'deleteLocal') {