2019-06-17 13:57:07 +00:00
|
|
|
import {
|
2020-03-03 20:53:48 +00:00
|
|
|
put, take, takeLatest, fork, cancel, race, select
|
2019-06-17 13:57:07 +00:00
|
|
|
} from 'redux-saga/effects';
|
2019-06-26 19:50:03 +00:00
|
|
|
import { Alert } from 'react-native';
|
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2020-01-07 18:54:02 +00:00
|
|
|
import semver from 'semver';
|
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';
|
2019-06-17 13:57:07 +00:00
|
|
|
import {
|
|
|
|
serverFailure, selectServerRequest, selectServerSuccess, selectServerFailure
|
|
|
|
} from '../actions/server';
|
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';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2020-02-28 19:51:13 +00:00
|
|
|
import log, { logServerVersion } from '../utils/log';
|
2019-09-02 16:19:05 +00:00
|
|
|
import { extractHostname } from '../utils/server';
|
2018-12-21 10:55:35 +00:00
|
|
|
import I18n from '../i18n';
|
2019-06-26 19:50:03 +00:00
|
|
|
import { SERVERS, TOKEN, SERVER_URL } from '../constants/userDefaults';
|
2020-02-20 12:58:13 +00:00
|
|
|
import { BASIC_AUTH_KEY, setBasicAuth } from '../utils/fetch';
|
2018-09-26 13:56:36 +00:00
|
|
|
|
2019-06-05 13:38:41 +00:00
|
|
|
const getServerInfo = function* getServerInfo({ server, raiseError = true }) {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2019-04-17 17:01:03 +00:00
|
|
|
const serverInfo = yield RocketChat.getServerInfo(server);
|
2019-11-18 17:50:00 +00:00
|
|
|
let websocketInfo = { success: true };
|
2019-11-13 19:52:00 +00:00
|
|
|
if (raiseError) {
|
|
|
|
websocketInfo = yield RocketChat.getWebsocketInfo({ server });
|
|
|
|
}
|
|
|
|
if (!serverInfo.success || !websocketInfo.success) {
|
2019-06-05 13:38:41 +00:00
|
|
|
if (raiseError) {
|
2019-11-13 19:52:00 +00:00
|
|
|
const info = serverInfo.success ? websocketInfo : serverInfo;
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t(info.message, info.messageOptions));
|
2019-06-05 13:38:41 +00:00
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
yield put(serverFailure());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:54:02 +00:00
|
|
|
const validVersion = semver.coerce(serverInfo.version);
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
|
|
|
const serversCollection = serversDB.collections.get('servers');
|
|
|
|
yield serversDB.action(async() => {
|
|
|
|
try {
|
|
|
|
const serverRecord = await serversCollection.find(server);
|
|
|
|
await serverRecord.update((record) => {
|
2020-01-07 18:54:02 +00:00
|
|
|
record.version = validVersion;
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
await serversCollection.create((record) => {
|
|
|
|
record._raw = sanitizedRaw({ id: server }, serversCollection.schema);
|
2020-01-07 18:54:02 +00:00
|
|
|
record.version = validVersion;
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return serverInfo;
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSelectServer = function* handleSelectServer({ server, version, fetchVersion }) {
|
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
2019-06-26 19:50:03 +00:00
|
|
|
yield RNUserDefaults.set('currentServer', server);
|
|
|
|
const userId = yield RNUserDefaults.get(`${ RocketChat.TOKEN_KEY }-${ server }`);
|
2019-09-16 20:26:32 +00:00
|
|
|
const userCollections = serversDB.collections.get('users');
|
|
|
|
let user = null;
|
|
|
|
if (userId) {
|
|
|
|
try {
|
2019-10-02 12:55:25 +00:00
|
|
|
const userRecord = yield userCollections.find(userId);
|
2019-09-16 20:26:32 +00:00
|
|
|
user = {
|
2019-10-02 12:55:25 +00:00
|
|
|
id: userRecord.id,
|
|
|
|
token: userRecord.token,
|
|
|
|
username: userRecord.username,
|
|
|
|
name: userRecord.name,
|
|
|
|
language: userRecord.language,
|
|
|
|
status: userRecord.status,
|
|
|
|
roles: userRecord.roles
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
|
|
|
} catch (e) {
|
2019-10-02 12:55:25 +00:00
|
|
|
// We only run it if not has user on DB
|
|
|
|
const servers = yield RNUserDefaults.objectForKey(SERVERS);
|
|
|
|
const userCredentials = servers && servers.find(srv => srv[SERVER_URL] === server);
|
|
|
|
user = userCredentials && {
|
|
|
|
token: userCredentials[TOKEN]
|
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-26 19:50:03 +00:00
|
|
|
|
2020-02-20 12:58:13 +00:00
|
|
|
const basicAuth = yield RNUserDefaults.get(`${ BASIC_AUTH_KEY }-${ server }`);
|
|
|
|
setBasicAuth(basicAuth);
|
|
|
|
|
2019-10-02 12:55:25 +00:00
|
|
|
if (user) {
|
2019-11-27 20:52:49 +00:00
|
|
|
yield RocketChat.connect({ server, user, logoutOnError: true });
|
2019-10-02 12:55:25 +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 {
|
2019-06-05 19:11:29 +00:00
|
|
|
yield RocketChat.connect({ server });
|
2019-03-18 18:52:38 +00:00
|
|
|
yield put(actions.appStart('outside'));
|
2018-08-01 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 13:37:13 +00:00
|
|
|
// We can't use yield here because fetch of Settings & Custom Emojis is slower
|
|
|
|
// and block the selectServerSuccess raising multiples errors
|
|
|
|
RocketChat.setSettings();
|
|
|
|
RocketChat.setCustomEmojis();
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2019-06-05 13:38:41 +00:00
|
|
|
let serverInfo;
|
|
|
|
if (fetchVersion) {
|
|
|
|
serverInfo = yield getServerInfo({ server, raiseError: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return server version even when offline
|
2020-02-28 19:51:13 +00:00
|
|
|
const serverVersion = (serverInfo && serverInfo.version) || version;
|
|
|
|
|
|
|
|
// we'll set serverVersion as metadata for bugsnag
|
|
|
|
logServerVersion(serverVersion);
|
|
|
|
yield put(selectServerSuccess(server, serverVersion));
|
2018-04-24 19:34:03 +00:00
|
|
|
} catch (e) {
|
2019-06-17 13:57:07 +00:00
|
|
|
yield put(selectServerFailure());
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2017-09-01 19:42:50 +00:00
|
|
|
};
|
|
|
|
|
2019-09-02 16:19:05 +00:00
|
|
|
const handleServerRequest = function* handleServerRequest({ server, certificate }) {
|
2017-09-01 19:42:50 +00:00
|
|
|
try {
|
2019-09-02 16:19:05 +00:00
|
|
|
if (certificate) {
|
|
|
|
yield RNUserDefaults.setObjectForKey(extractHostname(server), certificate);
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
const serverInfo = yield getServerInfo({ server });
|
2018-12-21 10:55:35 +00:00
|
|
|
|
2019-09-02 16:59:41 +00:00
|
|
|
if (serverInfo) {
|
|
|
|
const loginServicesLength = yield RocketChat.getLoginServices(server);
|
2020-03-03 20:53:48 +00:00
|
|
|
yield RocketChat.getLoginSettings({ server });
|
|
|
|
|
|
|
|
const showFormLogin = yield select(state => state.settings.Accounts_ShowFormLogin);
|
|
|
|
|
2020-03-26 22:38:36 +00:00
|
|
|
// if (!loginServicesLength && showFormLogin) {
|
|
|
|
// Navigation.navigate('LoginView');
|
|
|
|
// } else {
|
|
|
|
// Navigation.navigate('LoginSignupView');
|
|
|
|
// }
|
|
|
|
Navigation.navigate('WorkspaceView');
|
2019-09-02 16:59:41 +00:00
|
|
|
yield put(selectServerRequest(server, serverInfo.version, false));
|
2018-11-14 21:42:03 +00:00
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2018-08-10 17:26:36 +00:00
|
|
|
yield put(serverFailure());
|
2019-08-23 13:18:47 +00:00
|
|
|
log(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-10 17:26:36 +00:00
|
|
|
yield takeLatest(SERVER.REQUEST, handleServerRequest);
|
2019-06-17 13:57:07 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const params = yield take(SERVER.SELECT_REQUEST);
|
|
|
|
const selectServerTask = yield fork(handleSelectServer, params);
|
|
|
|
yield race({
|
|
|
|
request: take(SERVER.SELECT_REQUEST),
|
|
|
|
success: take(SERVER.SELECT_SUCCESS),
|
|
|
|
failure: take(SERVER.SELECT_FAILURE)
|
|
|
|
});
|
|
|
|
yield cancel(selectServerTask);
|
|
|
|
}
|
2017-08-18 21:30:16 +00:00
|
|
|
};
|
|
|
|
export default root;
|