2021-03-18 13:18:33 +00:00
|
|
|
import { put, takeLatest } from 'redux-saga/effects';
|
2019-12-17 14:08:06 +00:00
|
|
|
import RNBootSplash from 'react-native-bootsplash';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2022-04-07 14:10:03 +00:00
|
|
|
import { BIOMETRY_ENABLED_KEY } from '../lib/constants';
|
2022-04-07 13:13:19 +00:00
|
|
|
import UserPreferences from '../lib/methods/userPreferences';
|
2018-08-01 19:35:06 +00:00
|
|
|
import { selectServerRequest } from '../actions/server';
|
2018-09-04 14:29:20 +00:00
|
|
|
import { setAllPreferences } from '../actions/sortPreferences';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { APP } from '../actions/actionsTypes';
|
2017-11-19 02:44:55 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../utils/log';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2020-05-08 17:04:37 +00:00
|
|
|
import { localAuthenticate } from '../utils/localAuthentication';
|
2022-02-02 18:27:10 +00:00
|
|
|
import { appReady, appStart } from '../actions/app';
|
|
|
|
import { RootEnum } from '../definitions';
|
2017-08-21 03:00:41 +00:00
|
|
|
|
2019-12-04 16:50:22 +00:00
|
|
|
export const initLocalSettings = function* initLocalSettings() {
|
2022-03-09 19:41:26 +00:00
|
|
|
const sortPreferences = RocketChat.getSortPreferences();
|
2019-12-04 16:50:22 +00:00
|
|
|
yield put(setAllPreferences(sortPreferences));
|
|
|
|
};
|
|
|
|
|
2022-02-08 16:44:34 +00:00
|
|
|
const BIOMETRY_MIGRATION_KEY = 'kBiometryMigration';
|
|
|
|
|
2017-08-21 03:00:41 +00:00
|
|
|
const restore = function* restore() {
|
|
|
|
try {
|
2022-03-09 19:41:26 +00:00
|
|
|
const server = UserPreferences.getString(RocketChat.CURRENT_SERVER);
|
|
|
|
let userId = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${server}`);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-02-08 16:44:34 +00:00
|
|
|
// Migration biometry setting from WatermelonDB to MMKV
|
|
|
|
// TODO: remove it after a few versions
|
2022-03-09 19:41:26 +00:00
|
|
|
const hasMigratedBiometry = UserPreferences.getBool(BIOMETRY_MIGRATION_KEY);
|
2022-02-08 16:44:34 +00:00
|
|
|
if (!hasMigratedBiometry) {
|
|
|
|
const serversDB = database.servers;
|
|
|
|
const serversCollection = serversDB.get('servers');
|
|
|
|
const servers = yield serversCollection.query().fetch();
|
|
|
|
const isBiometryEnabled = servers.some(server => !!server.biometry);
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.setBool(BIOMETRY_ENABLED_KEY, isBiometryEnabled);
|
|
|
|
UserPreferences.setBool(BIOMETRY_MIGRATION_KEY, true);
|
2022-02-08 16:44:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 13:18:33 +00:00
|
|
|
if (!server) {
|
2022-02-02 18:27:10 +00:00
|
|
|
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
|
2021-03-18 13:18:33 +00:00
|
|
|
} else if (!userId) {
|
|
|
|
const serversDB = database.servers;
|
|
|
|
const serversCollection = serversDB.get('servers');
|
|
|
|
const servers = yield serversCollection.query().fetch();
|
|
|
|
|
|
|
|
// Check if there're other logged in servers and picks first one
|
|
|
|
if (servers.length > 0) {
|
|
|
|
for (let i = 0; i < servers.length; i += 1) {
|
|
|
|
const newServer = servers[i].id;
|
2022-03-09 19:41:26 +00:00
|
|
|
userId = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${newServer}`);
|
2021-03-18 13:18:33 +00:00
|
|
|
if (userId) {
|
|
|
|
return yield put(selectServerRequest(newServer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-02 18:27:10 +00:00
|
|
|
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
|
2019-09-27 18:52:51 +00:00
|
|
|
} else {
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serverCollections = serversDB.get('servers');
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2020-09-23 17:16:04 +00:00
|
|
|
let serverObj;
|
|
|
|
try {
|
|
|
|
yield localAuthenticate(server);
|
|
|
|
serverObj = yield serverCollections.find(server);
|
|
|
|
} catch {
|
|
|
|
// Server not found
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
yield put(selectServerRequest(server, serverObj && serverObj.version));
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
yield put(appReady({}));
|
2017-08-21 03:00:41 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2022-02-02 18:27:10 +00:00
|
|
|
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
|
2017-08-21 03:00:41 +00:00
|
|
|
}
|
|
|
|
};
|
2017-11-28 11:01:18 +00:00
|
|
|
|
2021-07-20 13:23:29 +00:00
|
|
|
const start = function* start() {
|
|
|
|
yield RNBootSplash.hide();
|
2019-03-12 16:23:06 +00:00
|
|
|
};
|
|
|
|
|
2017-11-28 11:01:18 +00:00
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(APP.INIT, restore);
|
2019-03-12 16:23:06 +00:00
|
|
|
yield takeLatest(APP.START, start);
|
2019-12-04 16:50:22 +00:00
|
|
|
yield takeLatest(APP.INIT_LOCAL_SETTINGS, initLocalSettings);
|
2017-11-28 11:01:18 +00:00
|
|
|
};
|
|
|
|
export default root;
|