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-04-30 16:06:58 +00:00
|
|
|
Text, Keyboard, StyleSheet, TouchableOpacity, View, Alert, 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';
|
2019-09-02 16:19:05 +00:00
|
|
|
import * as FileSystem from 'expo-file-system';
|
|
|
|
import DocumentPicker from 'react-native-document-picker';
|
|
|
|
import ActionSheet from 'react-native-action-sheet';
|
2020-02-20 12:58:13 +00:00
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
|
|
|
import { encode } from 'base-64';
|
|
|
|
import parse from 'url-parse';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2020-03-30 19:20:50 +00:00
|
|
|
import EventEmitter from '../utils/events';
|
|
|
|
import {
|
|
|
|
selectServerRequest, serverRequest, serverInitAdd, serverFinishAdd
|
|
|
|
} from '../actions/server';
|
|
|
|
import { appStart as appStartAction } from '../actions';
|
2018-08-10 17:26:36 +00:00
|
|
|
import sharedStyles from './Styles';
|
2018-04-24 19:34:03 +00:00
|
|
|
import Button from '../containers/Button';
|
|
|
|
import TextInput from '../containers/TextInput';
|
2020-05-08 17:36:10 +00:00
|
|
|
import OrSeparator from '../containers/OrSeparator';
|
2020-03-30 19:20:50 +00:00
|
|
|
import FormContainer, { FormContainerInner } from '../containers/FormContainer';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2020-03-30 19:20:50 +00:00
|
|
|
import { isIOS } from '../utils/deviceInfo';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../constants/colors';
|
2019-09-02 16:19:05 +00:00
|
|
|
import log from '../utils/log';
|
2019-09-19 13:32:24 +00:00
|
|
|
import { animateNextTransition } from '../utils/layoutAnimation';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../theme';
|
2020-02-20 12:58:13 +00:00
|
|
|
import { setBasicAuth, BASIC_AUTH_KEY } from '../utils/fetch';
|
2020-03-30 19:20:50 +00:00
|
|
|
import { themedHeader } from '../utils/navigation';
|
|
|
|
import { CloseModalButton } from '../containers/HeaderButton';
|
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
|
|
|
},
|
|
|
|
inputContainer: {
|
2020-03-30 19:20:50 +00:00
|
|
|
marginTop: 24,
|
|
|
|
marginBottom: 32
|
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-03-30 19:20:50 +00:00
|
|
|
static navigationOptions = ({ screenProps, navigation }) => {
|
|
|
|
const previousServer = navigation.getParam('previousServer', null);
|
|
|
|
const close = navigation.getParam('close', () => {});
|
|
|
|
return {
|
|
|
|
headerLeft: previousServer ? <CloseModalButton navigation={navigation} onPress={close} /> : undefined,
|
|
|
|
title: I18n.t('Workspaces'),
|
|
|
|
...themedHeader(screenProps.theme)
|
|
|
|
};
|
|
|
|
}
|
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,
|
|
|
|
currentServer: PropTypes.string,
|
|
|
|
initAdd: PropTypes.func,
|
|
|
|
finishAdd: PropTypes.func
|
2017-08-05 18:16:32 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:20:16 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-03-30 19:20:50 +00:00
|
|
|
this.previousServer = props.navigation.getParam('previousServer');
|
|
|
|
props.navigation.setParams({ close: this.close, previousServer: this.previousServer });
|
2019-09-02 16:19:05 +00:00
|
|
|
|
|
|
|
// Cancel
|
|
|
|
this.options = [I18n.t('Cancel')];
|
|
|
|
this.CANCEL_INDEX = 0;
|
|
|
|
|
|
|
|
// Delete
|
|
|
|
this.options.push(I18n.t('Delete'));
|
|
|
|
this.DELETE_INDEX = 1;
|
|
|
|
|
2019-08-07 19:20:16 +00:00
|
|
|
this.state = {
|
2020-03-30 19:20:50 +00:00
|
|
|
text: '',
|
|
|
|
connectingOpen: false,
|
2019-09-02 16:19:05 +00:00
|
|
|
certificate: null
|
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
|
|
|
}
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
componentDidMount() {
|
2020-03-30 19:20:50 +00:00
|
|
|
const { initAdd } = this.props;
|
|
|
|
if (this.previousServer) {
|
|
|
|
initAdd();
|
2018-08-10 17:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBackPress = () => {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
if (navigation.isFocused() && this.previousServer) {
|
|
|
|
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 });
|
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2020-03-30 19:20:50 +00:00
|
|
|
close = () => {
|
|
|
|
const { selectServer, currentServer, finishAdd } = this.props;
|
|
|
|
if (this.previousServer !== currentServer) {
|
|
|
|
selectServer(this.previousServer);
|
|
|
|
}
|
|
|
|
finishAdd();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleNewServerEvent = (event) => {
|
|
|
|
let { server } = event;
|
|
|
|
const { connectServer } = this.props;
|
|
|
|
this.setState({ text: server });
|
|
|
|
server = this.completeUrl(server);
|
|
|
|
connectServer(server);
|
|
|
|
}
|
|
|
|
|
2019-09-02 16:19:05 +00:00
|
|
|
submit = async() => {
|
|
|
|
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
|
|
|
let cert = null;
|
|
|
|
|
2020-03-30 19:20:50 +00:00
|
|
|
this.setState({ connectingOpen: false });
|
|
|
|
|
2019-09-02 16:19:05 +00:00
|
|
|
if (certificate) {
|
|
|
|
const certificatePath = `${ FileSystem.documentDirectory }/${ certificate.name }`;
|
|
|
|
try {
|
|
|
|
await FileSystem.copyAsync({ from: certificate.path, to: certificatePath });
|
2019-09-02 16:59:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-09-02 16:19:05 +00:00
|
|
|
}
|
|
|
|
cert = {
|
|
|
|
path: this.uriToPath(certificatePath), // file:// isn't allowed by obj-C
|
|
|
|
password: certificate.password
|
|
|
|
};
|
|
|
|
}
|
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);
|
|
|
|
await this.basicAuth(server, text);
|
|
|
|
connectServer(server, cert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:20:50 +00:00
|
|
|
connectOpen = () => {
|
|
|
|
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) {
|
|
|
|
const credentials = encode(parsedUrl.auth);
|
|
|
|
await RNUserDefaults.set(`${ BASIC_AUTH_KEY }-${ server }`, credentials);
|
|
|
|
setBasicAuth(credentials);
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
2019-09-02 16:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chooseCertificate = async() => {
|
|
|
|
try {
|
|
|
|
const res = await DocumentPicker.pick({
|
|
|
|
type: ['com.rsa.pkcs-12']
|
|
|
|
});
|
|
|
|
const { uri: path, name } = res;
|
|
|
|
Alert.prompt(
|
|
|
|
I18n.t('Certificate_password'),
|
|
|
|
I18n.t('Whats_the_password_for_your_certificate'),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
text: 'OK',
|
|
|
|
onPress: password => this.saveCertificate({ path, name, password })
|
|
|
|
}
|
|
|
|
],
|
2019-10-31 16:21:59 +00:00
|
|
|
'secure-text'
|
2019-09-02 16:19:05 +00:00
|
|
|
);
|
2019-09-02 16:59:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (!DocumentPicker.isCancel(e)) {
|
|
|
|
log(e);
|
2019-09-02 16:19:05 +00:00
|
|
|
}
|
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 });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDelete = () => this.setState({ certificate: null }); // We not need delete file from DocumentPicker because it is a temp file
|
|
|
|
|
|
|
|
showActionSheet = () => {
|
|
|
|
ActionSheet.showActionSheetWithOptions({
|
|
|
|
options: this.options,
|
|
|
|
cancelButtonIndex: this.CANCEL_INDEX,
|
|
|
|
destructiveButtonIndex: this.DELETE_INDEX
|
|
|
|
}, (actionIndex) => {
|
|
|
|
if (actionIndex === this.DELETE_INDEX) { this.handleDelete(); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
onPress={certificate ? this.showActionSheet : this.chooseCertificate}
|
|
|
|
testID='new-server-choose-certificate'
|
|
|
|
>
|
|
|
|
<Text
|
|
|
|
style={[
|
|
|
|
styles.chooseCertificate,
|
|
|
|
{ color: themes[theme].tintColor }
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{certificate ? certificate.name : I18n.t('Apply_Your_Certificate')}
|
|
|
|
</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-03-30 19:20:50 +00:00
|
|
|
const { text, connectingOpen } = this.state;
|
2017-08-03 18:23:43 +00:00
|
|
|
return (
|
2020-03-30 19:20:50 +00:00
|
|
|
<FormContainer theme={theme}>
|
|
|
|
<FormContainerInner>
|
|
|
|
<Text style={[styles.title, { color: themes[theme].titleText }]}>{I18n.t('Join_your_workspace')}</Text>
|
|
|
|
<TextInput
|
|
|
|
label='Enter workspace URL'
|
|
|
|
placeholder='Ex. your-company.rocket.chat'
|
|
|
|
containerStyle={styles.inputContainer}
|
|
|
|
value={text}
|
|
|
|
returnKeyType='send'
|
|
|
|
onChangeText={this.onChangeText}
|
|
|
|
testID='new-server-view-input'
|
|
|
|
onSubmitEditing={this.submit}
|
|
|
|
clearButtonMode='while-editing'
|
|
|
|
keyboardType='url'
|
|
|
|
textContentType='URL'
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Connect')}
|
|
|
|
type='primary'
|
|
|
|
onPress={this.submit}
|
|
|
|
disabled={!text || connecting}
|
|
|
|
loading={!connectingOpen && connecting}
|
|
|
|
style={styles.connectButton}
|
|
|
|
testID='new-server-view-button'
|
|
|
|
theme={theme}
|
|
|
|
/>
|
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}
|
|
|
|
/>
|
|
|
|
</FormContainerInner>
|
|
|
|
{ isIOS ? this.renderCertificatePicker() : null }
|
|
|
|
</FormContainer>
|
2017-08-03 18:23:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
connecting: state.server.connecting
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
2020-03-30 19:20:50 +00:00
|
|
|
connectServer: (server, certificate) => dispatch(serverRequest(server, certificate)),
|
|
|
|
initAdd: () => dispatch(serverInitAdd()),
|
|
|
|
finishAdd: () => dispatch(serverFinishAdd()),
|
|
|
|
selectServer: server => dispatch(selectServerRequest(server)),
|
|
|
|
appStart: root => dispatch(appStartAction(root))
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(NewServerView));
|