From b4cc9430bef0550f9f1f5e391fe7c06e240b2e46 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Thu, 18 Jul 2019 18:06:43 +1000 Subject: [PATCH] Add loki schema versioning and create new public conversation --- app/sql.js | 123 +++++++++++++++++++++++++++++++++++++ js/models/conversations.js | 2 +- 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/app/sql.js b/app/sql.js index ac5d5ec2c..e9be61f07 100644 --- a/app/sql.js +++ b/app/sql.js @@ -770,6 +770,129 @@ async function updateSchema(instance) { // eslint-disable-next-line no-await-in-loop await runSchemaUpdate(schemaVersion, instance); } + await updateLokiSchema(instance); +} + +const LOKI_SCHEMA_VERSIONS = [ + updateToLokiSchemaVersion1, +]; + +async function updateToLokiSchemaVersion1(currentVersion, instance) { + if (currentVersion >= 1) { + return; + } + console.log('updateToLokiSchemaVersion1: starting...'); + await instance.run('BEGIN TRANSACTION;'); + + const publicChatData = { + id: '06lokiPublicChat', + friendRequestStatus: 4, // Friends + sealedSender: 0, + sessionResetStatus: 0, + swarmNodes: [], + type: 'private', + profileName: 'Loki Public Chat', + unlockTimestamp: null, + unreadCount: 0, + verified: 0, + version: 2, + }; + + const { + id, + type, + name, + friendRequestStatus, + profileName, + } = publicChatData; + + await instance.run( + `INSERT INTO conversations ( + id, + json, + + type, + members, + name, + friendRequestStatus, + profileName + ) values ( + $id, + $json, + + $type, + $members, + $name, + $friendRequestStatus, + $profileName + );`, + { + $id: id, + $json: objectToJSON(publicChatData), + + $type: type, + $members: null, + $name: name, + $friendRequestStatus: friendRequestStatus, + $profileName: profileName, + } + ); + + await instance.run( + `INSERT INTO loki_schema ( + version + ) values ( + 1 + );` + ); + await instance.run('COMMIT TRANSACTION;'); + console.log('updateToLokiSchemaVersion1: success!'); +} + +async function updateLokiSchema(instance) { + const result = await instance.get("SELECT name FROM sqlite_master WHERE type = 'table' AND name='loki_schema'"); + if (!result) { + await createLokiSchemaTable(instance); + } + const lokiSchemaVersion = await getLokiSchemaVersion(instance); + console.log( + 'updateLokiSchema:', + `Current loki schema version: ${lokiSchemaVersion};`, + `Most recent schema version: ${LOKI_SCHEMA_VERSIONS.length};` + ); + for (let index = 0, max = LOKI_SCHEMA_VERSIONS.length; index < max; index += 1) { + const runSchemaUpdate = LOKI_SCHEMA_VERSIONS[index]; + + // Yes, we really want to do this asynchronously, in order + // eslint-disable-next-line no-await-in-loop + await runSchemaUpdate(lokiSchemaVersion, instance); + } +} + +async function getLokiSchemaVersion(instance) { + const result = await instance.get('SELECT version FROM loki_schema WHERE version = (SELECT MAX(version) FROM loki_schema);'); + if (!result.version) { + return 0; + } + return result.version; +} + +async function createLokiSchemaTable(instance) { + await instance.run('BEGIN TRANSACTION;'); + await instance.run( + `CREATE TABLE loki_schema( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + version INTEGER + );` + ); + await instance.run( + `INSERT INTO loki_schema ( + version + ) values ( + 0 + );` + ); + await instance.run('COMMIT TRANSACTION;'); } let db; diff --git a/js/models/conversations.js b/js/models/conversations.js index 891a345d5..15d265021 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -167,7 +167,7 @@ if (this.id === this.ourNumber) { this.set({ friendRequestStatus: FriendRequestStatusEnum.friends }); - } else if (lokiP2pAPI) { + } else if (typeof lokiP2pAPI !== 'undefined') { // Online status handling, only for contacts that aren't us this.set({ isOnline: lokiP2pAPI.isOnline(this.id) }); } else {