2019-06-27 12:35:20 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
2018-12-05 20:52:08 +00:00
|
|
|
import { put, takeLatest, all } from 'redux-saga/effects';
|
2019-03-12 16:23:06 +00:00
|
|
|
import SplashScreen from 'react-native-splash-screen';
|
2019-06-26 19:50:03 +00:00
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2017-08-21 03:00:41 +00:00
|
|
|
import * as actions from '../actions';
|
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';
|
2019-05-21 12:12:15 +00:00
|
|
|
import { toggleMarkdown } from '../actions/markdown';
|
2019-08-23 13:18:47 +00:00
|
|
|
import { toggleCrashReport } 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-03-12 16:23:06 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2019-06-26 19:50:03 +00:00
|
|
|
import {
|
2019-09-23 19:53:30 +00:00
|
|
|
IDENTIFIER, SERVERS, SERVER_ICON, SERVER_NAME, SERVER_URL, TOKEN, USER_ID, ANDROID_PACKAGE_CONTEXT
|
2019-08-22 17:53:57 +00:00
|
|
|
} from '../constants/credentials';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
|
|
|
import protectedFunction from '../lib/methods/helpers/protectedFunction';
|
2019-09-23 19:53:30 +00:00
|
|
|
import { isAndroid } from '../utils/deviceInfo';
|
2017-08-21 03:00:41 +00:00
|
|
|
|
|
|
|
const restore = function* restore() {
|
|
|
|
try {
|
2019-08-29 20:29:06 +00:00
|
|
|
yield RNUserDefaults.setName(IDENTIFIER);
|
2019-09-23 19:53:30 +00:00
|
|
|
if (isAndroid) { yield RNUserDefaults.setPackageContext(ANDROID_PACKAGE_CONTEXT); }
|
2019-08-22 14:32:23 +00:00
|
|
|
const hasMigration = yield AsyncStorage.getItem('hasMigration');
|
2019-06-26 19:50:03 +00:00
|
|
|
|
|
|
|
let { token, server } = yield all({
|
|
|
|
token: RNUserDefaults.get(RocketChat.TOKEN_KEY),
|
|
|
|
server: RNUserDefaults.get('currentServer')
|
2018-12-05 20:52:08 +00:00
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-09-27 18:52:51 +00:00
|
|
|
if (!hasMigration && isIOS) {
|
|
|
|
let servers = yield RNUserDefaults.objectForKey(SERVERS);
|
|
|
|
// if not have current
|
|
|
|
if (servers && servers.length !== 0 && (!token || !server)) {
|
|
|
|
server = servers[0][SERVER_URL];
|
|
|
|
token = servers[0][TOKEN];
|
|
|
|
}
|
2019-09-19 18:55:23 +00:00
|
|
|
|
2019-09-27 18:52:51 +00:00
|
|
|
// get native credentials
|
|
|
|
if (servers) {
|
|
|
|
try {
|
|
|
|
// parse servers
|
|
|
|
servers = yield Promise.all(servers.map(async(s) => {
|
|
|
|
await RNUserDefaults.set(`${ RocketChat.TOKEN_KEY }-${ s[SERVER_URL] }`, s[USER_ID]);
|
|
|
|
return ({ id: s[SERVER_URL], name: s[SERVER_NAME], iconURL: s[SERVER_ICON] });
|
|
|
|
}));
|
|
|
|
const serversDB = database.servers;
|
|
|
|
yield serversDB.action(async() => {
|
|
|
|
const serversCollection = serversDB.collections.get('servers');
|
|
|
|
const allServerRecords = await serversCollection.query().fetch();
|
|
|
|
|
|
|
|
// filter servers
|
|
|
|
let serversToCreate = servers.filter(i1 => !allServerRecords.find(i2 => i1.id === i2.id));
|
|
|
|
|
|
|
|
// Create
|
|
|
|
serversToCreate = serversToCreate.map(record => serversCollection.prepareCreate(protectedFunction((s) => {
|
|
|
|
s._raw = sanitizedRaw({ id: record.id }, serversCollection.schema);
|
|
|
|
Object.assign(s, record);
|
|
|
|
})));
|
|
|
|
|
|
|
|
const allRecords = serversToCreate;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await serversDB.batch(...allRecords);
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
return allRecords.length;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
try {
|
2019-07-02 23:16:35 +00:00
|
|
|
yield AsyncStorage.setItem('hasMigration', '1');
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-06-27 12:35:20 +00:00
|
|
|
}
|
2019-06-26 19:50:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 14:29:20 +00:00
|
|
|
const sortPreferences = yield RocketChat.getSortPreferences();
|
|
|
|
yield put(setAllPreferences(sortPreferences));
|
|
|
|
|
2019-05-21 12:12:15 +00:00
|
|
|
const useMarkdown = yield RocketChat.getUseMarkdown();
|
|
|
|
yield put(toggleMarkdown(useMarkdown));
|
|
|
|
|
2019-08-23 13:18:47 +00:00
|
|
|
const allowCrashReport = yield RocketChat.getAllowCrashReport();
|
|
|
|
yield put(toggleCrashReport(allowCrashReport));
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
if (!token || !server) {
|
|
|
|
yield all([
|
2019-06-26 19:50:03 +00:00
|
|
|
RNUserDefaults.clear(RocketChat.TOKEN_KEY),
|
|
|
|
RNUserDefaults.clear('currentServer')
|
2018-12-05 20:52:08 +00:00
|
|
|
]);
|
|
|
|
yield put(actions.appStart('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');
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-13 13:19:24 +00:00
|
|
|
yield put(actions.appReady({}));
|
2017-08-21 03:00:41 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-09-16 20:26:32 +00:00
|
|
|
yield put(actions.appStart('outside'));
|
2017-08-21 03:00:41 +00:00
|
|
|
}
|
|
|
|
};
|
2017-11-28 11:01:18 +00:00
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
const start = function* start({ root }) {
|
|
|
|
if (root === 'inside') {
|
|
|
|
yield Navigation.navigate('InsideStack');
|
|
|
|
} else if (root === 'setUsername') {
|
|
|
|
yield Navigation.navigate('SetUsernameView');
|
|
|
|
} else if (root === 'outside') {
|
|
|
|
yield Navigation.navigate('OutsideStack');
|
|
|
|
}
|
|
|
|
SplashScreen.hide();
|
|
|
|
};
|
|
|
|
|
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);
|
2017-11-28 11:01:18 +00:00
|
|
|
};
|
|
|
|
export default root;
|