2020-05-05 13:11:28 +00:00
|
|
|
import { put, takeLatest } from 'redux-saga/effects';
|
2019-06-26 19:50:03 +00:00
|
|
|
import { Alert } from 'react-native';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2020-09-11 17:10:16 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2021-02-01 17:18:55 +00:00
|
|
|
import valid from 'semver/functions/valid';
|
|
|
|
import coerce from 'semver/functions/coerce';
|
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';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { selectServerFailure, selectServerRequest, selectServerSuccess, serverFailure } from '../actions/server';
|
2020-04-01 19:24:42 +00:00
|
|
|
import { clearSettings } from '../actions/settings';
|
2022-03-11 14:12:25 +00:00
|
|
|
import { clearUser, setUser } from '../actions/login';
|
2022-02-09 20:44:53 +00:00
|
|
|
import { clearActiveUsers } from '../actions/activeUsers';
|
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';
|
2018-12-21 10:55:35 +00:00
|
|
|
import I18n from '../i18n';
|
2020-02-20 12:58:13 +00:00
|
|
|
import { BASIC_AUTH_KEY, setBasicAuth } from '../utils/fetch';
|
2022-02-02 18:27:10 +00:00
|
|
|
import { appStart } from '../actions/app';
|
2020-08-19 17:14:22 +00:00
|
|
|
import UserPreferences from '../lib/userPreferences';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { encryptionStop } from '../actions/encryption';
|
2020-11-30 17:00:06 +00:00
|
|
|
import SSLPinning from '../utils/sslPinning';
|
2020-08-28 19:41:08 +00:00
|
|
|
import { inquiryReset } from '../ee/omnichannel/actions/inquiry';
|
2022-02-02 18:27:10 +00:00
|
|
|
import { RootEnum } from '../definitions';
|
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;
|
2020-09-11 16:30:28 +00:00
|
|
|
Alert.alert(I18n.t('Oops'), info.message);
|
2019-06-05 13:38:41 +00:00
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
yield put(serverFailure());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:18:55 +00:00
|
|
|
let serverVersion = valid(serverInfo.version);
|
2020-04-03 18:03:53 +00:00
|
|
|
if (!serverVersion) {
|
2021-02-01 17:18:55 +00:00
|
|
|
({ version: serverVersion } = coerce(serverInfo.version));
|
2020-04-03 18:03:53 +00:00
|
|
|
}
|
2020-01-07 18:54:02 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serversCollection = serversDB.get('servers');
|
2021-09-13 20:41:05 +00:00
|
|
|
yield serversDB.action(async () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const serverRecord = await serversCollection.find(server);
|
2021-09-13 20:41:05 +00:00
|
|
|
await serverRecord.update(record => {
|
2020-04-03 18:03:53 +00:00
|
|
|
record.version = serverVersion;
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
2021-09-13 20:41:05 +00:00
|
|
|
await serversCollection.create(record => {
|
2019-09-16 20:26:32 +00:00
|
|
|
record._raw = sanitizedRaw({ id: server }, serversCollection.schema);
|
2020-04-03 18:03:53 +00:00
|
|
|
record.version = serverVersion;
|
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 {
|
2020-11-30 17:00:06 +00:00
|
|
|
// SSL Pinning - Read certificate alias and set it to be used by network requests
|
2022-03-09 19:41:26 +00:00
|
|
|
const certificate = UserPreferences.getString(`${RocketChat.CERTIFICATE_KEY}-${server}`);
|
|
|
|
SSLPinning.setCertificate(certificate, server);
|
2020-07-31 18:22:30 +00:00
|
|
|
yield put(inquiryReset());
|
2020-09-11 14:31:38 +00:00
|
|
|
yield put(encryptionStop());
|
2022-02-09 20:44:53 +00:00
|
|
|
yield put(clearActiveUsers());
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.setString(RocketChat.CURRENT_SERVER, server);
|
|
|
|
const userId = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${server}`);
|
2021-02-26 16:25:51 +00:00
|
|
|
const userCollections = serversDB.get('users');
|
2019-09-16 20:26:32 +00:00
|
|
|
let user = null;
|
|
|
|
if (userId) {
|
|
|
|
try {
|
2020-09-23 17:16:04 +00:00
|
|
|
// search credentials on database
|
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,
|
2020-03-30 20:19:01 +00:00
|
|
|
statusText: userRecord.statusText,
|
2020-10-30 13:12:02 +00:00
|
|
|
roles: userRecord.roles,
|
|
|
|
avatarETag: userRecord.avatarETag
|
2019-09-16 20:26:32 +00:00
|
|
|
};
|
2020-08-19 17:14:22 +00:00
|
|
|
} catch {
|
2020-09-23 17:16:04 +00:00
|
|
|
// search credentials on shared credentials (Experimental/Official)
|
2022-03-09 19:41:26 +00:00
|
|
|
const token = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${userId}`);
|
2020-09-23 17:16:04 +00:00
|
|
|
if (token) {
|
|
|
|
user = { token };
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-26 19:50:03 +00:00
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
const basicAuth = UserPreferences.getString(`${BASIC_AUTH_KEY}-${server}`);
|
2020-02-20 12:58:13 +00:00
|
|
|
setBasicAuth(basicAuth);
|
|
|
|
|
2020-05-05 13:11:28 +00:00
|
|
|
// Check for running requests and abort them before connecting to the server
|
|
|
|
RocketChat.abort();
|
|
|
|
|
2019-10-02 12:55:25 +00:00
|
|
|
if (user) {
|
2020-04-09 05:26:28 +00:00
|
|
|
yield put(clearSettings());
|
2019-10-02 12:55:25 +00:00
|
|
|
yield put(setUser(user));
|
2022-03-11 14:12:25 +00:00
|
|
|
yield RocketChat.connect({ server, logoutOnError: true });
|
2022-02-02 18:27:10 +00:00
|
|
|
yield put(appStart({ root: RootEnum.ROOT_INSIDE }));
|
2018-12-05 20:52:08 +00:00
|
|
|
} else {
|
2022-03-11 14:12:25 +00:00
|
|
|
yield put(clearUser());
|
2019-06-05 19:11:29 +00:00
|
|
|
yield RocketChat.connect({ server });
|
2022-02-02 18:27:10 +00:00
|
|
|
yield put(appStart({ root: RootEnum.ROOT_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();
|
2021-02-25 16:41:44 +00:00
|
|
|
RocketChat.setPermissions();
|
2021-07-01 16:51:19 +00:00
|
|
|
RocketChat.setRoles();
|
2020-08-21 13:38:50 +00:00
|
|
|
RocketChat.setEnterpriseModules();
|
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
|
|
|
};
|
|
|
|
|
2020-11-30 17:00:06 +00:00
|
|
|
const handleServerRequest = function* handleServerRequest({ server, username, fromServerHistory }) {
|
2017-09-01 19:42:50 +00:00
|
|
|
try {
|
2020-11-30 17:00:06 +00:00
|
|
|
// SSL Pinning - Read certificate alias and set it to be used by network requests
|
2022-03-09 19:41:26 +00:00
|
|
|
const certificate = UserPreferences.getString(`${RocketChat.CERTIFICATE_KEY}-${server}`);
|
|
|
|
SSLPinning.setCertificate(certificate, server);
|
2019-09-02 16:19:05 +00:00
|
|
|
|
2019-04-17 17:01:03 +00:00
|
|
|
const serverInfo = yield getServerInfo({ server });
|
2020-09-11 17:10:16 +00:00
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serversHistoryCollection = serversDB.get('servers_history');
|
2018-12-21 10:55:35 +00:00
|
|
|
|
2019-09-02 16:59:41 +00:00
|
|
|
if (serverInfo) {
|
2020-03-30 19:20:50 +00:00
|
|
|
yield RocketChat.getLoginServices(server);
|
2020-03-03 20:53:48 +00:00
|
|
|
yield RocketChat.getLoginSettings({ server });
|
2020-03-30 19:20:50 +00:00
|
|
|
Navigation.navigate('WorkspaceView');
|
2020-09-11 17:10:16 +00:00
|
|
|
|
|
|
|
if (fromServerHistory) {
|
|
|
|
Navigation.navigate('LoginView', { username });
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
yield serversDB.action(async () => {
|
2020-09-11 17:10:16 +00:00
|
|
|
try {
|
|
|
|
const serversHistory = await serversHistoryCollection.query(Q.where('url', server)).fetch();
|
|
|
|
if (!serversHistory?.length) {
|
2021-09-13 20:41:05 +00:00
|
|
|
await serversHistoryCollection.create(s => {
|
2020-09-11 17:10:16 +00:00
|
|
|
s.url = server;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
});
|
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);
|
2020-05-05 13:11:28 +00:00
|
|
|
yield takeLatest(SERVER.SELECT_REQUEST, handleSelectServer);
|
2017-08-18 21:30:16 +00:00
|
|
|
};
|
|
|
|
export default root;
|