2018-12-05 20:52:08 +00:00
|
|
|
import { put, takeLatest } from 'redux-saga/effects';
|
2018-12-21 10:55:35 +00:00
|
|
|
import { AsyncStorage, Alert } from 'react-native';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2019-01-31 16:08:38 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { SERVER } from '../actions/actionsTypes';
|
2017-12-27 15:22:06 +00:00
|
|
|
import * as actions from '../actions';
|
2018-08-10 17:26:36 +00:00
|
|
|
import { serverFailure, selectServerRequest, selectServerSuccess } from '../actions/server';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { setRoles } from '../actions/roles';
|
2018-12-05 20:52:08 +00:00
|
|
|
import { setUser } from '../actions/login';
|
2017-09-01 19:42:50 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../lib/realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../utils/log';
|
2018-12-21 10:55:35 +00:00
|
|
|
import I18n from '../i18n';
|
2018-09-26 13:56:36 +00:00
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
const getServerInfo = function* getServerInfo({ server }) {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2019-04-17 17:01:03 +00:00
|
|
|
const serverInfo = yield RocketChat.getServerInfo(server);
|
|
|
|
if (!serverInfo.success) {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t(serverInfo.message, serverInfo.messageOptions));
|
|
|
|
yield put(serverFailure());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
database.databases.serversDB.write(() => {
|
|
|
|
database.databases.serversDB.create('servers', { id: server, version: serverInfo.version }, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
return serverInfo;
|
|
|
|
} catch (e) {
|
|
|
|
log('getServerInfo', e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSelectServer = function* handleSelectServer({ server, version, fetchVersion }) {
|
|
|
|
try {
|
|
|
|
let serverInfo;
|
|
|
|
if (fetchVersion) {
|
|
|
|
serverInfo = yield getServerInfo({ server });
|
|
|
|
}
|
2018-12-05 20:52:08 +00:00
|
|
|
yield AsyncStorage.setItem('currentServer', server);
|
|
|
|
const userStringified = yield AsyncStorage.getItem(`${ RocketChat.TOKEN_KEY }-${ server }`);
|
|
|
|
|
|
|
|
if (userStringified) {
|
|
|
|
const user = JSON.parse(userStringified);
|
2018-12-07 17:47:50 +00:00
|
|
|
RocketChat.connect({ server, user });
|
2018-12-05 20:52:08 +00:00
|
|
|
yield put(setUser(user));
|
2018-08-01 19:35:06 +00:00
|
|
|
yield put(actions.appStart('inside'));
|
2018-12-05 20:52:08 +00:00
|
|
|
} else {
|
|
|
|
RocketChat.connect({ server });
|
2019-03-18 18:52:38 +00:00
|
|
|
yield put(actions.appStart('outside'));
|
2018-08-01 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const settings = database.objects('settings');
|
2018-12-05 20:52:08 +00:00
|
|
|
yield put(actions.setAllSettings(RocketChat.parseSettings(settings.slice(0, settings.length))));
|
2018-04-24 19:34:03 +00:00
|
|
|
const emojis = database.objects('customEmojis');
|
|
|
|
yield put(actions.setCustomEmojis(RocketChat.parseEmojis(emojis.slice(0, emojis.length))));
|
|
|
|
const roles = database.objects('roles');
|
|
|
|
yield put(setRoles(roles.reduce((result, role) => {
|
|
|
|
result[role._id] = role.description;
|
|
|
|
return result;
|
|
|
|
}, {})));
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
yield put(selectServerSuccess(server, fetchVersion ? serverInfo && serverInfo.version : version));
|
2018-04-24 19:34:03 +00:00
|
|
|
} catch (e) {
|
2018-07-10 13:40:32 +00:00
|
|
|
log('handleSelectServer', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2017-09-01 19:42:50 +00:00
|
|
|
};
|
|
|
|
|
2018-08-10 17:26:36 +00:00
|
|
|
const handleServerRequest = function* handleServerRequest({ server }) {
|
2017-09-01 19:42:50 +00:00
|
|
|
try {
|
2019-04-17 17:01:03 +00:00
|
|
|
const serverInfo = yield getServerInfo({ server });
|
2018-12-21 10:55:35 +00:00
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
// TODO: cai aqui O.o
|
2018-11-14 21:42:03 +00:00
|
|
|
const loginServicesLength = yield RocketChat.getLoginServices(server);
|
|
|
|
if (loginServicesLength === 0) {
|
2019-03-12 16:23:06 +00:00
|
|
|
Navigation.navigate('LoginView');
|
2018-11-14 21:42:03 +00:00
|
|
|
} else {
|
2019-03-12 16:23:06 +00:00
|
|
|
Navigation.navigate('LoginSignupView');
|
2018-11-14 21:42:03 +00:00
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
yield put(selectServerRequest(server, serverInfo.version, false));
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2018-08-10 17:26:36 +00:00
|
|
|
yield put(serverFailure());
|
|
|
|
log('handleServerRequest', e);
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2017-11-13 13:53:45 +00:00
|
|
|
};
|
2017-09-01 19:42:50 +00:00
|
|
|
|
2017-08-18 21:30:16 +00:00
|
|
|
const root = function* root() {
|
2018-08-01 19:35:06 +00:00
|
|
|
yield takeLatest(SERVER.SELECT_REQUEST, handleSelectServer);
|
2018-08-10 17:26:36 +00:00
|
|
|
yield takeLatest(SERVER.REQUEST, handleServerRequest);
|
2017-08-18 21:30:16 +00:00
|
|
|
};
|
|
|
|
export default root;
|