From 124a959c8d9fbe4976934cad736ec992f98672a9 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sun, 7 Oct 2018 20:11:33 +0100 Subject: [PATCH] All: Simplifying serialisation of base items --- ReactNativeClient/lib/joplin-database.js | 7 ++++++- ReactNativeClient/lib/models/BaseItem.js | 7 ++++++- ReactNativeClient/lib/models/Folder.js | 7 ------- ReactNativeClient/lib/models/MasterKey.js | 6 ------ ReactNativeClient/lib/models/Note.js | 12 +++--------- ReactNativeClient/lib/models/NoteTag.js | 6 ------ ReactNativeClient/lib/models/Resource.js | 12 +++++------- ReactNativeClient/lib/models/Tag.js | 6 ------ 8 files changed, 20 insertions(+), 43 deletions(-) diff --git a/ReactNativeClient/lib/joplin-database.js b/ReactNativeClient/lib/joplin-database.js index f4af95e0a..886cc06ec 100644 --- a/ReactNativeClient/lib/joplin-database.js +++ b/ReactNativeClient/lib/joplin-database.js @@ -249,7 +249,7 @@ class JoplinDatabase extends Database { // default value and thus might cause problems. In that case, the default value // must be set in the synchronizer too. - const existingDatabaseVersions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + const existingDatabaseVersions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; let currentVersionIndex = existingDatabaseVersions.indexOf(fromVersion); @@ -395,6 +395,11 @@ class JoplinDatabase extends Database { queries.push('ALTER TABLE folders ADD COLUMN parent_id TEXT NOT NULL DEFAULT ""'); } + if (targetVersion == 13) { + queries.push('ALTER TABLE resources ADD COLUMN fetch_status INT NOT NULL DEFAULT "0"'); + queries.push('ALTER TABLE resources ADD COLUMN fetch_error TEXT NOT NULL DEFAULT ""'); + } + queries.push({ sql: 'UPDATE version SET version = ?', params: [targetVersion] }); await this.transactionExecBatch(queries); diff --git a/ReactNativeClient/lib/models/BaseItem.js b/ReactNativeClient/lib/models/BaseItem.js index 09709173f..a6d62d16b 100644 --- a/ReactNativeClient/lib/models/BaseItem.js +++ b/ReactNativeClient/lib/models/BaseItem.js @@ -236,7 +236,12 @@ class BaseItem extends BaseModel { return propValue; } - static async serialize(item, type = null, shownKeys = null) { + static async serialize(item, shownKeys = null) { + if (shownKeys === null) { + shownKeys = this.itemClass(item).fieldNames(); + shownKeys.push('type_'); + } + item = this.filter(item); let output = {}; diff --git a/ReactNativeClient/lib/models/Folder.js b/ReactNativeClient/lib/models/Folder.js index 63fdbe482..36ebd690b 100644 --- a/ReactNativeClient/lib/models/Folder.js +++ b/ReactNativeClient/lib/models/Folder.js @@ -15,13 +15,6 @@ class Folder extends BaseItem { return 'folders'; } - static async serialize(folder) { - let fieldNames = this.fieldNames(); - fieldNames.push('type_'); - //lodash.pull(fieldNames, 'parent_id'); - return super.serialize(folder, 'folder', fieldNames); - } - static modelType() { return BaseModel.TYPE_FOLDER; } diff --git a/ReactNativeClient/lib/models/MasterKey.js b/ReactNativeClient/lib/models/MasterKey.js index a3e5322e6..8b5aed2dd 100644 --- a/ReactNativeClient/lib/models/MasterKey.js +++ b/ReactNativeClient/lib/models/MasterKey.js @@ -19,12 +19,6 @@ class MasterKey extends BaseItem { return this.modelSelectOne('SELECT * FROM master_keys WHERE created_time >= (SELECT max(created_time) FROM master_keys)'); } - static async serialize(item, type = null, shownKeys = null) { - let fieldNames = this.fieldNames(); - fieldNames.push('type_'); - return super.serialize(item, 'master_key', fieldNames); - } - static async save(o, options = null) { return super.save(o, options).then((item) => { this.dispatch({ diff --git a/ReactNativeClient/lib/models/Note.js b/ReactNativeClient/lib/models/Note.js index 31575fa16..7fd17757c 100644 --- a/ReactNativeClient/lib/models/Note.js +++ b/ReactNativeClient/lib/models/Note.js @@ -25,14 +25,8 @@ class Note extends BaseItem { return field in fieldsToLabels ? fieldsToLabels[field] : field; } - static async serialize(note, type = null, shownKeys = null) { - let fieldNames = this.fieldNames(); - fieldNames.push('type_'); - return super.serialize(note, 'note', fieldNames); - } - static async serializeForEdit(note) { - return super.serialize(note, 'note', ['title', 'body']); + return super.serialize(note, ['title', 'body']); } static async unserializeForEdit(content) { @@ -47,7 +41,7 @@ class Note extends BaseItem { let fieldNames = this.fieldNames(); fieldNames.push('type_'); lodash.pull(fieldNames, 'title', 'body'); - return super.serialize(note, 'note', fieldNames); + return super.serialize(note, fieldNames); } static minimalSerializeForDisplay(note) { @@ -75,7 +69,7 @@ class Note extends BaseItem { lodash.pull(fieldNames, 'updated_time'); lodash.pull(fieldNames, 'order'); - return super.serialize(n, 'note', fieldNames); + return super.serialize(n, fieldNames); } static defaultTitle(note) { diff --git a/ReactNativeClient/lib/models/NoteTag.js b/ReactNativeClient/lib/models/NoteTag.js index b2de61265..550c1b15f 100644 --- a/ReactNativeClient/lib/models/NoteTag.js +++ b/ReactNativeClient/lib/models/NoteTag.js @@ -11,12 +11,6 @@ class NoteTag extends BaseItem { return BaseModel.TYPE_NOTE_TAG; } - static async serialize(item, type = null, shownKeys = null) { - let fieldNames = this.fieldNames(); - fieldNames.push('type_'); - return super.serialize(item, 'note_tag', fieldNames); - } - static async byNoteIds(noteIds) { if (!noteIds.length) return []; return this.modelSelectAll('SELECT * FROM note_tags WHERE note_id IN ("' + noteIds.join('","') + '")'); diff --git a/ReactNativeClient/lib/models/Resource.js b/ReactNativeClient/lib/models/Resource.js index de8391323..29a2aed2f 100644 --- a/ReactNativeClient/lib/models/Resource.js +++ b/ReactNativeClient/lib/models/Resource.js @@ -35,13 +35,6 @@ class Resource extends BaseItem { return Resource.fsDriver_; } - static async serialize(item, type = null, shownKeys = null) { - let fieldNames = this.fieldNames(); - fieldNames.push('type_'); - //fieldNames = ArrayUtils.removeElement(fieldNames, 'encryption_blob_encrypted'); - return super.serialize(item, 'resource', fieldNames); - } - static filename(resource, encryptedBlob = false) { let extension = encryptedBlob ? 'crypted' : resource.file_extension; if (!extension) extension = resource.mime ? mime.toFileExtension(resource.mime) : ''; @@ -199,4 +192,9 @@ class Resource extends BaseItem { Resource.IMAGE_MAX_DIMENSION = 1920; +Resource.FETCH_STATUS_IDLE = 0; +Resource.FETCH_STATUS_STARTED = 1; +Resource.FETCH_STATUS_DONE = 2; +Resource.FETCH_STATUS_ERROR = 3; + module.exports = Resource; \ No newline at end of file diff --git a/ReactNativeClient/lib/models/Tag.js b/ReactNativeClient/lib/models/Tag.js index 9d5cf3b75..27b8d99d5 100644 --- a/ReactNativeClient/lib/models/Tag.js +++ b/ReactNativeClient/lib/models/Tag.js @@ -15,12 +15,6 @@ class Tag extends BaseItem { return BaseModel.TYPE_TAG; } - static async serialize(item, type = null, shownKeys = null) { - let fieldNames = this.fieldNames(); - fieldNames.push('type_'); - return super.serialize(item, 'tag', fieldNames); - } - static async noteIds(tagId) { let rows = await this.db().selectAll('SELECT note_id FROM note_tags WHERE tag_id = ?', [tagId]); let output = [];