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
|
|
|
|
2020-08-19 17:14:22 +00:00
|
|
|
import UserPreferences from '../lib/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';
|
2020-09-11 17:34:11 +00:00
|
|
|
import { toggleCrashReport, toggleAnalyticsEvents } from '../actions/crashReport';
|
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';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { appStart, ROOT_OUTSIDE, appReady } from '../actions/app';
|
2017-08-21 03:00:41 +00:00
|
|
|
|
2019-12-04 16:50:22 +00:00
|
|
|
export const initLocalSettings = function* initLocalSettings() {
|
|
|
|
const sortPreferences = yield RocketChat.getSortPreferences();
|
|
|
|
yield put(setAllPreferences(sortPreferences));
|
|
|
|
|
|
|
|
const allowCrashReport = yield RocketChat.getAllowCrashReport();
|
|
|
|
yield put(toggleCrashReport(allowCrashReport));
|
2020-09-11 17:34:11 +00:00
|
|
|
|
|
|
|
const allowAnalyticsEvents = yield RocketChat.getAllowAnalyticsEvents();
|
|
|
|
yield put(toggleAnalyticsEvents(allowAnalyticsEvents));
|
2019-12-04 16:50:22 +00:00
|
|
|
};
|
|
|
|
|
2017-08-21 03:00:41 +00:00
|
|
|
const restore = function* restore() {
|
|
|
|
try {
|
2021-03-18 13:18:33 +00:00
|
|
|
const server = yield UserPreferences.getStringAsync(RocketChat.CURRENT_SERVER);
|
|
|
|
let userId = yield UserPreferences.getStringAsync(`${ RocketChat.TOKEN_KEY }-${ server }`);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-03-18 13:18:33 +00:00
|
|
|
if (!server) {
|
|
|
|
yield put(appStart({ root: ROOT_OUTSIDE }));
|
|
|
|
} 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;
|
|
|
|
userId = yield UserPreferences.getStringAsync(`${ RocketChat.TOKEN_KEY }-${ newServer }`);
|
|
|
|
if (userId) {
|
|
|
|
return yield put(selectServerRequest(newServer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
yield put(appStart({ root: 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);
|
2020-06-15 14:00:46 +00:00
|
|
|
yield put(appStart({ root: ROOT_OUTSIDE }));
|
2017-08-21 03:00:41 +00:00
|
|
|
}
|
|
|
|
};
|
2017-11-28 11:01:18 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
const start = function start() {
|
2019-12-17 14:08:06 +00:00
|
|
|
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;
|