From 8ae489074e9a699908b95bc5b314bce7e2a2b1ae Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Wed, 23 Sep 2020 14:16:04 -0300 Subject: [PATCH] [CHORE] Change database location to Experimental Apps (#2483) * change database location of experimental apps * fix migration from older versions --- app/lib/database/index.js | 6 +++--- app/lib/encryption/encryption.js | 2 +- app/lib/methods/enterpriseModules.js | 9 +++++++-- app/lib/methods/getSettings.js | 6 +++++- app/sagas/encryption.js | 11 +++++++++-- app/sagas/init.js | 9 +++++++-- app/sagas/rooms.js | 22 +++++++++++++++------- app/sagas/selectServer.js | 7 ++++++- 8 files changed, 53 insertions(+), 19 deletions(-) diff --git a/app/lib/database/index.js b/app/lib/database/index.js index d0de06f97..fbebde6c1 100644 --- a/app/lib/database/index.js +++ b/app/lib/database/index.js @@ -36,7 +36,7 @@ if (__DEV__ && isIOS) { export const getDatabase = (database = '') => { const path = database.replace(/(^\w+:|^)\/\//, '').replace(/\//g, '.'); - const dbName = `${ appGroupPath }${ path }.db`; + const dbName = `${ appGroupPath }${ path }-experimental.db`; const adapter = new SQLiteAdapter({ dbName, @@ -68,7 +68,7 @@ class DB { databases = { serversDB: new Database({ adapter: new SQLiteAdapter({ - dbName: `${ appGroupPath }default.db`, + dbName: `${ appGroupPath }default-experimental.db`, schema: serversSchema, migrations: serversMigrations }), @@ -95,7 +95,7 @@ class DB { setShareDB(database = '') { const path = database.replace(/(^\w+:|^)\/\//, '').replace(/\//g, '.'); - const dbName = `${ appGroupPath }${ path }.db`; + const dbName = `${ appGroupPath }${ path }-experimental.db`; const adapter = new SQLiteAdapter({ dbName, diff --git a/app/lib/encryption/encryption.js b/app/lib/encryption/encryption.js index 34c9b8a14..e7b077b5f 100644 --- a/app/lib/encryption/encryption.js +++ b/app/lib/encryption/encryption.js @@ -58,7 +58,7 @@ class Encryption { get establishing() { const { banner } = store.getState().encryption; // If the password was not inserted yet - if (banner === E2E_BANNER_TYPE.REQUEST_PASSWORD) { + if (!banner || banner === E2E_BANNER_TYPE.REQUEST_PASSWORD) { // We can't decrypt/encrypt, so, reject this try return Promise.reject(); } diff --git a/app/lib/methods/enterpriseModules.js b/app/lib/methods/enterpriseModules.js index 60600afae..4a0d1b38d 100644 --- a/app/lib/methods/enterpriseModules.js +++ b/app/lib/methods/enterpriseModules.js @@ -13,8 +13,13 @@ export async function setEnterpriseModules() { const { server: serverId } = reduxStore.getState().server; const serversDB = database.servers; const serversCollection = serversDB.collections.get('servers'); - const server = await serversCollection.find(serverId); - if (server.enterpriseModules) { + let server; + try { + server = await serversCollection.find(serverId); + } catch { + // Server not found + } + if (server?.enterpriseModules) { reduxStore.dispatch(setEnterpriseModulesAction(server.enterpriseModules.split(','))); return; } diff --git a/app/lib/methods/getSettings.js b/app/lib/methods/getSettings.js index 2cec925b5..f433f4a9a 100644 --- a/app/lib/methods/getSettings.js +++ b/app/lib/methods/getSettings.js @@ -150,7 +150,11 @@ export default async function() { // filter server info const serverInfo = filteredSettings.filter(i1 => serverInfoKeys.includes(i1._id)); const iconSetting = data.find(item => item._id === 'Assets_favicon_512'); - await serverInfoUpdate(serverInfo, iconSetting); + try { + await serverInfoUpdate(serverInfo, iconSetting); + } catch { + // Server not found + } await db.action(async() => { const settingsCollection = db.collections.get('settings'); diff --git a/app/sagas/encryption.js b/app/sagas/encryption.js index 4201e6bcd..0250a2c19 100644 --- a/app/sagas/encryption.js +++ b/app/sagas/encryption.js @@ -20,19 +20,26 @@ import I18n from '../i18n'; import log from '../utils/log'; const getServer = state => state.share.server || state.server.server; +const getE2eEnable = state => state.settings.E2E_Enable; const handleEncryptionInit = function* handleEncryptionInit() { try { const server = yield select(getServer); const user = yield select(getUserSelector); + const E2E_Enable = yield select(getE2eEnable); // Fetch server info to check E2E enable const serversDB = database.servers; const serversCollection = serversDB.collections.get('servers'); - const serverInfo = yield serversCollection.find(server); + let serverInfo; + try { + serverInfo = yield serversCollection.find(server); + } catch { + // Server not found + } // If E2E is disabled on server, skip - if (!serverInfo?.E2E_Enable) { + if (!serverInfo?.E2E_Enable && !E2E_Enable) { return; } diff --git a/app/sagas/init.js b/app/sagas/init.js index a39183348..5c0cb7286 100644 --- a/app/sagas/init.js +++ b/app/sagas/init.js @@ -40,8 +40,13 @@ const restore = function* restore() { const serversDB = database.servers; const serverCollections = serversDB.collections.get('servers'); - yield localAuthenticate(server); - const serverObj = yield serverCollections.find(server); + let serverObj; + try { + yield localAuthenticate(server); + serverObj = yield serverCollections.find(server); + } catch { + // Server not found + } yield put(selectServerRequest(server, serverObj && serverObj.version)); } diff --git a/app/sagas/rooms.js b/app/sagas/rooms.js index 7204a33a9..3f240f729 100644 --- a/app/sagas/rooms.js +++ b/app/sagas/rooms.js @@ -16,13 +16,17 @@ import protectedFunction from '../lib/methods/helpers/protectedFunction'; const updateRooms = function* updateRooms({ server, newRoomsUpdatedAt }) { const serversDB = database.servers; const serversCollection = serversDB.collections.get('servers'); - const serverRecord = yield serversCollection.find(server); + try { + const serverRecord = yield serversCollection.find(server); - return serversDB.action(async() => { - await serverRecord.update((record) => { - record.roomsUpdatedAt = newRoomsUpdatedAt; + return serversDB.action(async() => { + await serverRecord.update((record) => { + record.roomsUpdatedAt = newRoomsUpdatedAt; + }); }); - }); + } catch { + // Server not found + } }; const handleRoomsRequest = function* handleRoomsRequest({ params }) { @@ -36,8 +40,12 @@ const handleRoomsRequest = function* handleRoomsRequest({ params }) { yield put(roomsRefresh()); } else { const serversCollection = serversDB.collections.get('servers'); - const serverRecord = yield serversCollection.find(server); - ({ roomsUpdatedAt } = serverRecord); + try { + const serverRecord = yield serversCollection.find(server); + ({ roomsUpdatedAt } = serverRecord); + } catch { + // Server not found + } } const [subscriptionsResult, roomsResult] = yield RocketChat.getRooms(roomsUpdatedAt); const { subscriptions } = yield mergeSubscriptionsRooms(subscriptionsResult, roomsResult); diff --git a/app/sagas/selectServer.js b/app/sagas/selectServer.js index 61b947508..6afb8fd1d 100644 --- a/app/sagas/selectServer.js +++ b/app/sagas/selectServer.js @@ -77,6 +77,7 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch let user = null; if (userId) { try { + // search credentials on database const userRecord = yield userCollections.find(userId); user = { id: userRecord.id, @@ -89,7 +90,11 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch roles: userRecord.roles }; } catch { - // Do nothing + // search credentials on shared credentials (Experimental/Official) + const token = yield UserPreferences.getStringAsync(`${ RocketChat.TOKEN_KEY }-${ userId }`); + if (token) { + user = { token }; + } } }