2018-12-05 20:52:08 +00:00
|
|
|
import { put, takeLatest, all } 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 {
|
2020-08-19 17:14:22 +00:00
|
|
|
const { token, server } = yield all({
|
|
|
|
token: UserPreferences.getStringAsync(RocketChat.TOKEN_KEY),
|
|
|
|
server: UserPreferences.getStringAsync(RocketChat.CURRENT_SERVER)
|
2018-12-05 20:52:08 +00:00
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
if (!token || !server) {
|
|
|
|
yield all([
|
2020-08-19 17:14:22 +00:00
|
|
|
UserPreferences.removeItem(RocketChat.TOKEN_KEY),
|
|
|
|
UserPreferences.removeItem(RocketChat.CURRENT_SERVER)
|
2018-12-05 20:52:08 +00:00
|
|
|
]);
|
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;
|
|
|
|
const serverCollections = serversDB.collections.get('servers');
|
2020-05-08 17:04:37 +00:00
|
|
|
|
|
|
|
yield localAuthenticate(server);
|
2019-09-16 20:26:32 +00:00
|
|
|
const serverObj = yield serverCollections.find(server);
|
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;
|