2017-08-03 18:23:43 +00:00
|
|
|
import React from 'react';
|
2017-08-05 18:16:32 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2020-11-30 17:00:06 +00:00
|
|
|
Text, Keyboard, StyleSheet, View, BackHandler
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { connect } from 'react-redux';
|
2020-07-20 16:35:17 +00:00
|
|
|
import { Base64 } from 'js-base64';
|
2020-02-20 12:58:13 +00:00
|
|
|
import parse from 'url-parse';
|
2020-09-11 17:10:16 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
|
|
|
|
import { TouchableOpacity } from 'react-native-gesture-handler';
|
|
|
|
import UserPreferences from '../../lib/userPreferences';
|
|
|
|
import EventEmitter from '../../utils/events';
|
|
|
|
import { selectServerRequest, serverRequest } from '../../actions/server';
|
|
|
|
import { inviteLinksClear as inviteLinksClearAction } from '../../actions/inviteLinks';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import Button from '../../containers/Button';
|
|
|
|
import OrSeparator from '../../containers/OrSeparator';
|
|
|
|
import FormContainer, { FormContainerInner } from '../../containers/FormContainer';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { themes } from '../../constants/colors';
|
2020-11-30 17:00:06 +00:00
|
|
|
import { logEvent, events } from '../../utils/log';
|
2020-09-11 17:10:16 +00:00
|
|
|
import { animateNextTransition } from '../../utils/layoutAnimation';
|
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { setBasicAuth, BASIC_AUTH_KEY } from '../../utils/fetch';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
2020-09-11 17:10:16 +00:00
|
|
|
import { showConfirmationAlert } from '../../utils/info';
|
|
|
|
import database from '../../lib/database';
|
|
|
|
import ServerInput from './ServerInput';
|
2020-09-15 13:01:43 +00:00
|
|
|
import { sanitizeLikeString } from '../../lib/database/utils';
|
2020-11-30 17:00:06 +00:00
|
|
|
import SSLPinning from '../../utils/sslPinning';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2018-08-10 17:26:36 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
title: {
|
2018-11-14 21:42:03 +00:00
|
|
|
...sharedStyles.textBold,
|
2020-03-30 19:20:50 +00:00
|
|
|
fontSize: 22
|
2018-08-10 17:26:36 +00:00
|
|
|
},
|
2019-09-02 16:19:05 +00:00
|
|
|
certificatePicker: {
|
2020-03-30 19:20:50 +00:00
|
|
|
marginBottom: 32,
|
2019-09-02 16:19:05 +00:00
|
|
|
alignItems: 'center',
|
2020-03-30 19:20:50 +00:00
|
|
|
justifyContent: 'flex-end'
|
2019-09-02 16:19:05 +00:00
|
|
|
},
|
|
|
|
chooseCertificateTitle: {
|
2020-03-30 19:20:50 +00:00
|
|
|
fontSize: 13,
|
2019-12-04 16:39:53 +00:00
|
|
|
...sharedStyles.textRegular
|
2019-09-02 16:19:05 +00:00
|
|
|
},
|
|
|
|
chooseCertificate: {
|
2020-03-30 19:20:50 +00:00
|
|
|
fontSize: 13,
|
2019-12-04 16:39:53 +00:00
|
|
|
...sharedStyles.textSemibold
|
2020-03-30 19:20:50 +00:00
|
|
|
},
|
|
|
|
description: {
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
fontSize: 14,
|
|
|
|
textAlign: 'left',
|
|
|
|
marginBottom: 24
|
|
|
|
},
|
|
|
|
connectButton: {
|
|
|
|
marginBottom: 0
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class NewServerView extends React.Component {
|
2020-07-31 18:30:36 +00:00
|
|
|
static navigationOptions = () => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Workspaces')
|
2020-07-31 18:30:36 +00:00
|
|
|
})
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2017-08-05 18:16:32 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2018-08-10 17:26:36 +00:00
|
|
|
connecting: PropTypes.bool.isRequired,
|
2020-03-30 19:20:50 +00:00
|
|
|
connectServer: PropTypes.func.isRequired,
|
|
|
|
selectServer: PropTypes.func.isRequired,
|
2020-06-15 14:00:46 +00:00
|
|
|
adding: PropTypes.bool,
|
2020-07-24 15:41:59 +00:00
|
|
|
previousServer: PropTypes.string,
|
|
|
|
inviteLinksClear: PropTypes.func
|
2017-08-05 18:16:32 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:20:16 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-07-24 15:41:59 +00:00
|
|
|
this.setHeader();
|
2019-09-02 16:19:05 +00:00
|
|
|
|
2019-08-07 19:20:16 +00:00
|
|
|
this.state = {
|
2020-03-30 19:20:50 +00:00
|
|
|
text: '',
|
|
|
|
connectingOpen: false,
|
2020-09-11 17:10:16 +00:00
|
|
|
certificate: null,
|
|
|
|
serversHistory: []
|
2019-08-07 19:20:16 +00:00
|
|
|
};
|
2020-03-30 19:20:50 +00:00
|
|
|
EventEmitter.addEventListener('NewServer', this.handleNewServerEvent);
|
2020-04-30 16:06:58 +00:00
|
|
|
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:10:16 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.queryServerHistory();
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:41:59 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const { adding } = this.props;
|
|
|
|
if (prevProps.adding !== adding) {
|
|
|
|
this.setHeader();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:20:50 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
EventEmitter.removeListener('NewServer', this.handleNewServerEvent);
|
2020-04-30 16:06:58 +00:00
|
|
|
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:41:59 +00:00
|
|
|
setHeader = () => {
|
|
|
|
const { adding, navigation } = this.props;
|
|
|
|
if (adding) {
|
|
|
|
navigation.setOptions({
|
2020-10-30 16:15:58 +00:00
|
|
|
headerLeft: () => <HeaderButton.CloseModal navigation={navigation} onPress={this.close} testID='new-server-view-close' />
|
2020-07-24 15:41:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 16:06:58 +00:00
|
|
|
handleBackPress = () => {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { navigation, previousServer } = this.props;
|
|
|
|
if (navigation.isFocused() && previousServer) {
|
2020-04-30 16:06:58 +00:00
|
|
|
this.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 15:47:18 +00:00
|
|
|
onChangeText = (text) => {
|
|
|
|
this.setState({ text });
|
2020-09-11 17:10:16 +00:00
|
|
|
this.queryServerHistory(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
queryServerHistory = async(text) => {
|
|
|
|
const db = database.servers;
|
|
|
|
try {
|
2021-03-15 20:16:34 +00:00
|
|
|
const serversHistoryCollection = db.get('servers_history');
|
2020-09-11 17:10:16 +00:00
|
|
|
let whereClause = [
|
|
|
|
Q.where('username', Q.notEq(null)),
|
|
|
|
Q.experimentalSortBy('updated_at', Q.desc),
|
|
|
|
Q.experimentalTake(3)
|
|
|
|
];
|
2020-09-15 13:01:43 +00:00
|
|
|
const likeString = sanitizeLikeString(text);
|
2020-09-11 17:10:16 +00:00
|
|
|
if (text) {
|
|
|
|
whereClause = [
|
|
|
|
...whereClause,
|
2020-09-15 13:01:43 +00:00
|
|
|
Q.where('url', Q.like(`%${ likeString }%`))
|
2020-09-11 17:10:16 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
const serversHistory = await serversHistoryCollection.query(...whereClause).fetch();
|
|
|
|
this.setState({ serversHistory });
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2017-08-11 15:47:18 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2020-03-30 19:20:50 +00:00
|
|
|
close = () => {
|
2020-07-24 15:41:59 +00:00
|
|
|
const { selectServer, previousServer, inviteLinksClear } = this.props;
|
|
|
|
inviteLinksClear();
|
2020-06-15 14:00:46 +00:00
|
|
|
selectServer(previousServer);
|
2020-03-30 19:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleNewServerEvent = (event) => {
|
|
|
|
let { server } = event;
|
2020-11-12 17:09:24 +00:00
|
|
|
if (!server) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-30 19:20:50 +00:00
|
|
|
const { connectServer } = this.props;
|
|
|
|
this.setState({ text: server });
|
|
|
|
server = this.completeUrl(server);
|
|
|
|
connectServer(server);
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:10:16 +00:00
|
|
|
onPressServerHistory = (serverHistory) => {
|
|
|
|
this.setState({ text: serverHistory?.url }, () => this.submit({ fromServerHistory: true, username: serverHistory?.username }));
|
|
|
|
}
|
|
|
|
|
|
|
|
submit = async({ fromServerHistory = false, username }) => {
|
2021-03-15 20:16:34 +00:00
|
|
|
logEvent(events.NS_CONNECT_TO_WORKSPACE);
|
2019-09-02 16:19:05 +00:00
|
|
|
const { text, certificate } = this.state;
|
2018-09-25 19:28:42 +00:00
|
|
|
const { connectServer } = this.props;
|
2019-09-02 16:19:05 +00:00
|
|
|
|
2020-03-30 19:20:50 +00:00
|
|
|
this.setState({ connectingOpen: false });
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
if (text) {
|
2018-05-30 16:51:30 +00:00
|
|
|
Keyboard.dismiss();
|
2020-02-20 12:58:13 +00:00
|
|
|
const server = this.completeUrl(text);
|
2020-11-30 17:00:06 +00:00
|
|
|
|
|
|
|
// Save info - SSL Pinning
|
|
|
|
await UserPreferences.setStringAsync(`${ RocketChat.CERTIFICATE_KEY }-${ server }`, certificate);
|
|
|
|
|
|
|
|
// Save info - HTTP Basic Authentication
|
2020-02-20 12:58:13 +00:00
|
|
|
await this.basicAuth(server, text);
|
2020-11-30 17:00:06 +00:00
|
|
|
|
2020-09-11 17:10:16 +00:00
|
|
|
if (fromServerHistory) {
|
2020-11-30 17:00:06 +00:00
|
|
|
connectServer(server, username, true);
|
2020-09-11 17:10:16 +00:00
|
|
|
} else {
|
2020-11-30 17:00:06 +00:00
|
|
|
connectServer(server);
|
2020-09-11 17:10:16 +00:00
|
|
|
}
|
2020-02-20 12:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:20:50 +00:00
|
|
|
connectOpen = () => {
|
2021-03-15 20:16:34 +00:00
|
|
|
logEvent(events.NS_JOIN_OPEN_WORKSPACE);
|
2020-03-30 19:20:50 +00:00
|
|
|
this.setState({ connectingOpen: true });
|
|
|
|
const { connectServer } = this.props;
|
|
|
|
connectServer('https://open.rocket.chat');
|
|
|
|
}
|
|
|
|
|
2020-02-20 12:58:13 +00:00
|
|
|
basicAuth = async(server, text) => {
|
|
|
|
try {
|
|
|
|
const parsedUrl = parse(text, true);
|
|
|
|
if (parsedUrl.auth.length) {
|
2020-07-20 16:35:17 +00:00
|
|
|
const credentials = Base64.encode(parsedUrl.auth);
|
2020-08-19 17:14:22 +00:00
|
|
|
await UserPreferences.setStringAsync(`${ BASIC_AUTH_KEY }-${ server }`, credentials);
|
2020-02-20 12:58:13 +00:00
|
|
|
setBasicAuth(credentials);
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
2019-09-02 16:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chooseCertificate = async() => {
|
|
|
|
try {
|
2020-11-30 17:00:06 +00:00
|
|
|
const certificate = await SSLPinning.pickCertificate();
|
|
|
|
this.setState({ certificate });
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
2018-05-30 16:51:30 +00:00
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 15:47:18 +00:00
|
|
|
completeUrl = (url) => {
|
2020-02-20 12:58:13 +00:00
|
|
|
const parsedUrl = parse(url, true);
|
|
|
|
if (parsedUrl.auth.length) {
|
2020-03-26 13:20:41 +00:00
|
|
|
url = parsedUrl.origin;
|
2020-02-20 12:58:13 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 19:58:24 +00:00
|
|
|
url = url && url.replace(/\s/g, '');
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
if (/^(\w|[0-9-_]){3,}$/.test(url)
|
|
|
|
&& /^(htt(ps?)?)|(loca((l)?|(lh)?|(lho)?|(lhos)?|(lhost:?\d*)?)$)/.test(url) === false) {
|
2017-08-11 15:47:18 +00:00
|
|
|
url = `${ url }.rocket.chat`;
|
|
|
|
}
|
|
|
|
|
2019-09-16 21:04:20 +00:00
|
|
|
if (/^(https?:\/\/)?(((\w|[0-9-_])+(\.(\w|[0-9-_])+)+)|localhost)(:\d+)?$/.test(url)) {
|
2017-08-11 15:47:18 +00:00
|
|
|
if (/^localhost(:\d+)?/.test(url)) {
|
|
|
|
url = `http://${ url }`;
|
|
|
|
} else if (/^https?:\/\//.test(url) === false) {
|
2017-08-07 00:34:35 +00:00
|
|
|
url = `https://${ url }`;
|
|
|
|
}
|
2017-08-11 15:47:18 +00:00
|
|
|
}
|
2017-08-03 18:23:43 +00:00
|
|
|
|
2019-10-23 19:28:24 +00:00
|
|
|
return url.replace(/\/+$/, '').replace(/\\/g, '/');
|
2017-08-11 15:47:18 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 16:19:05 +00:00
|
|
|
uriToPath = uri => uri.replace('file://', '');
|
|
|
|
|
|
|
|
saveCertificate = (certificate) => {
|
2019-09-19 13:32:24 +00:00
|
|
|
animateNextTransition();
|
2019-09-02 16:19:05 +00:00
|
|
|
this.setState({ certificate });
|
|
|
|
}
|
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
handleRemove = () => {
|
|
|
|
showConfirmationAlert({
|
|
|
|
message: I18n.t('You_will_unset_a_certificate_for_this_server'),
|
2020-08-25 20:04:18 +00:00
|
|
|
confirmationText: I18n.t('Remove'),
|
2020-06-15 19:35:45 +00:00
|
|
|
onPress: this.setState({ certificate: null }) // We not need delete file from DocumentPicker because it is a temp file
|
2019-09-02 16:19:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:10:16 +00:00
|
|
|
deleteServerHistory = async(item) => {
|
|
|
|
const { serversHistory } = this.state;
|
|
|
|
const db = database.servers;
|
|
|
|
try {
|
|
|
|
await db.action(async() => {
|
|
|
|
await item.destroyPermanently();
|
|
|
|
});
|
|
|
|
this.setState({ serversHistory: serversHistory.filter(server => server.id !== item.id) });
|
|
|
|
} catch {
|
|
|
|
// Nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 16:19:05 +00:00
|
|
|
renderCertificatePicker = () => {
|
|
|
|
const { certificate } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2019-09-02 16:19:05 +00:00
|
|
|
return (
|
|
|
|
<View style={styles.certificatePicker}>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.chooseCertificateTitle,
|
|
|
|
{ color: themes[theme].auxiliaryText }
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{certificate ? I18n.t('Your_certificate') : I18n.t('Do_you_have_a_certificate')}
|
|
|
|
</Text>
|
|
|
|
<TouchableOpacity
|
2020-06-15 19:35:45 +00:00
|
|
|
onPress={certificate ? this.handleRemove : this.chooseCertificate}
|
2019-12-04 16:39:53 +00:00
|
|
|
testID='new-server-choose-certificate'
|
|
|
|
>
|
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.chooseCertificate,
|
|
|
|
{ color: themes[theme].tintColor }
|
|
|
|
]}
|
|
|
|
>
|
2020-11-30 17:00:06 +00:00
|
|
|
{certificate ?? I18n.t('Apply_Your_Certificate')}
|
2019-12-04 16:39:53 +00:00
|
|
|
</Text>
|
2019-09-02 16:19:05 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-03 18:23:43 +00:00
|
|
|
render() {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { connecting, theme } = this.props;
|
2020-09-11 17:10:16 +00:00
|
|
|
const {
|
|
|
|
text, connectingOpen, serversHistory
|
|
|
|
} = this.state;
|
2017-08-03 18:23:43 +00:00
|
|
|
return (
|
2020-09-11 17:10:16 +00:00
|
|
|
<FormContainer
|
|
|
|
theme={theme}
|
|
|
|
testID='new-server-view'
|
|
|
|
keyboardShouldPersistTaps='never'
|
|
|
|
>
|
2020-03-30 19:20:50 +00:00
|
|
|
<FormContainerInner>
|
|
|
|
<Text style={[styles.title, { color: themes[theme].titleText }]}>{I18n.t('Join_your_workspace')}</Text>
|
2020-09-11 17:10:16 +00:00
|
|
|
<ServerInput
|
|
|
|
text={text}
|
2020-03-30 19:20:50 +00:00
|
|
|
theme={theme}
|
2020-09-11 17:10:16 +00:00
|
|
|
serversHistory={serversHistory}
|
|
|
|
onChangeText={this.onChangeText}
|
|
|
|
onSubmit={this.submit}
|
|
|
|
onDelete={this.deleteServerHistory}
|
|
|
|
onPressServerHistory={this.onPressServerHistory}
|
2020-03-30 19:20:50 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Connect')}
|
|
|
|
type='primary'
|
|
|
|
onPress={this.submit}
|
|
|
|
disabled={!text || connecting}
|
|
|
|
loading={!connectingOpen && connecting}
|
|
|
|
style={styles.connectButton}
|
|
|
|
theme={theme}
|
2020-05-20 16:33:40 +00:00
|
|
|
testID='new-server-view-button'
|
2020-03-30 19:20:50 +00:00
|
|
|
/>
|
2020-05-08 17:36:10 +00:00
|
|
|
<OrSeparator theme={theme} />
|
2020-03-30 19:20:50 +00:00
|
|
|
<Text style={[styles.description, { color: themes[theme].auxiliaryText }]}>{I18n.t('Onboarding_join_open_description')}</Text>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Join_our_open_workspace')}
|
|
|
|
type='secondary'
|
|
|
|
backgroundColor={themes[theme].chatComponentBackground}
|
|
|
|
onPress={this.connectOpen}
|
|
|
|
disabled={connecting}
|
|
|
|
loading={connectingOpen && connecting}
|
|
|
|
theme={theme}
|
2020-05-20 16:33:40 +00:00
|
|
|
testID='new-server-view-open'
|
2020-03-30 19:20:50 +00:00
|
|
|
/>
|
|
|
|
</FormContainerInner>
|
2020-11-30 17:00:06 +00:00
|
|
|
{this.renderCertificatePicker()}
|
2020-03-30 19:20:50 +00:00
|
|
|
</FormContainer>
|
2017-08-03 18:23:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
connecting: state.server.connecting,
|
|
|
|
adding: state.server.adding,
|
|
|
|
previousServer: state.server.previousServer
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
2020-11-30 17:00:06 +00:00
|
|
|
connectServer: (...params) => dispatch(serverRequest(...params)),
|
2020-07-24 15:41:59 +00:00
|
|
|
selectServer: server => dispatch(selectServerRequest(server)),
|
|
|
|
inviteLinksClear: () => dispatch(inviteLinksClearAction())
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(NewServerView));
|