From cd37a9b4ecbd005da1ce87e2162035f7b01e05fd Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Wed, 4 Dec 2019 13:41:37 -0300 Subject: [PATCH] [FIX] Share extension doesn't reconnect to previous selected server on Android (#1429) --- app/lib/database/index.js | 33 ++++++++++++++++++++++++++++++++- app/lib/methods/sendMessage.js | 3 ++- app/lib/rocketchat.js | 22 +++++++++++++++------- app/share.js | 1 + 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/app/lib/database/index.js b/app/lib/database/index.js index b385a9b63..1508d11fc 100644 --- a/app/lib/database/index.js +++ b/app/lib/database/index.js @@ -44,13 +44,44 @@ class DB { } get active() { - return this.databases.activeDB; + return this.databases.shareDB || this.databases.activeDB; + } + + get share() { + return this.databases.shareDB; + } + + set share(db) { + this.databases.shareDB = db; } get servers() { return this.databases.serversDB; } + setShareDB(database = '') { + const path = database.replace(/(^\w+:|^)\/\//, '').replace(/\//, '.'); + const dbName = `${ appGroupPath }${ path }.db`; + + const adapter = new SQLiteAdapter({ + dbName, + schema: appSchema, + migrations + }); + + this.databases.shareDB = new Database({ + adapter, + modelClasses: [ + Subscription, + Message, + Thread, + ThreadMessage, + Upload + ], + actionsEnabled: true + }); + } + setActiveDB(database = '') { const path = database.replace(/(^\w+:|^)\/\//, '').replace(/\//, '.'); const dbName = `${ appGroupPath }${ path }.db`; diff --git a/app/lib/methods/sendMessage.js b/app/lib/methods/sendMessage.js index bd383bd5d..7b9e341ec 100644 --- a/app/lib/methods/sendMessage.js +++ b/app/lib/methods/sendMessage.js @@ -10,8 +10,9 @@ export async function sendMessageCall(message) { id: _id, subscription: { id: rid }, msg, tmid } = message; try { + const sdk = this.shareSDK || this.sdk; // RC 0.60.0 - await this.sdk.post('chat.sendMessage', { + await sdk.post('chat.sendMessage', { message: { _id, rid, msg, tmid } diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index e7d2061fc..fb79d6b45 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -259,17 +259,17 @@ const RocketChat = { }, async shareExtensionInit(server) { - database.setActiveDB(server); + database.setShareDB(server); - if (this.sdk) { - this.sdk.disconnect(); - this.sdk = null; + if (this.shareSDK) { + this.shareSDK.disconnect(); + this.shareSDK = null; } // Use useSsl: false only if server url starts with http:// const useSsl = !/http:\/\//.test(server); - this.sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl }); + this.shareSDK = new RocketchatClient({ host: server, protocol: 'ddp', useSsl }); // set Server const serversDB = database.servers; @@ -298,6 +298,13 @@ const RocketChat = { log(e); } }, + closeShareExtension() { + if (this.shareSDK) { + this.shareSDK.disconnect(); + this.shareSDK = null; + } + database.share = null; + }, updateJitsiTimeout(rid) { return this.sdk.methodCall('jitsi:updateTimeout', rid); @@ -363,9 +370,10 @@ const RocketChat = { async login(params) { try { + const sdk = this.shareSDK || this.sdk; // RC 0.64.0 - await this.sdk.login(params); - const { result } = this.sdk.currentLogin; + await sdk.login(params); + const { result } = sdk.currentLogin; const user = { id: result.userId, token: result.authToken, diff --git a/app/share.js b/app/share.js index 0266ba16a..3d2484a7d 100644 --- a/app/share.js +++ b/app/share.js @@ -72,6 +72,7 @@ class Root extends React.Component { } componentWillUnmount() { + RocketChat.closeShareExtension(); unsubscribeTheme(); }