[FIX] Change server issue (#960)

* [FIX] Lazy fetch server info

* [FIX] Multiple servers issues
This commit is contained in:
Diego Mello 2019-06-05 16:11:29 -03:00 committed by GitHub
parent 109a247c8d
commit 3cd84a10f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 61 deletions

View File

@ -174,71 +174,75 @@ const RocketChat = {
this.getUserPresence();
},
connect({ server, user }) {
database.setActiveDB(server);
reduxStore.dispatch(connectRequest());
return new Promise((resolve) => {
database.setActiveDB(server);
reduxStore.dispatch(connectRequest());
if (this.connectTimeout) {
clearTimeout(this.connectTimeout);
}
if (this.connectTimeout) {
clearTimeout(this.connectTimeout);
}
if (this.sdk) {
this.sdk.disconnect();
this.sdk = null;
}
if (this.sdk) {
this.sdk.disconnect();
this.sdk = null;
}
// Use useSsl: false only if server url starts with http://
const useSsl = !/http:\/\//.test(server);
// Use useSsl: false only if server url starts with http://
const useSsl = !/http:\/\//.test(server);
this.sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl });
this.getSettings();
this.sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl });
this.getSettings();
this.sdk.connect()
.then(() => {
if (user && user.token) {
reduxStore.dispatch(loginRequest({ resume: user.token }));
this.sdk.connect()
.then(() => {
if (user && user.token) {
reduxStore.dispatch(loginRequest({ resume: user.token }));
}
})
.catch((err) => {
console.log('connect error', err);
// when `connect` raises an error, we try again in 10 seconds
this.connectTimeout = setTimeout(() => {
this.connect({ server, user });
}, 10000);
});
this.sdk.onStreamData('connected', () => {
reduxStore.dispatch(connectSuccess());
const { isAuthenticated } = reduxStore.getState().login;
if (isAuthenticated) {
this.getUserPresence();
}
})
.catch((err) => {
console.log('connect error', err);
// when `connect` raises an error, we try again in 10 seconds
this.connectTimeout = setTimeout(() => {
this.connect({ server, user });
}, 10000);
});
this.sdk.onStreamData('connected', () => {
reduxStore.dispatch(connectSuccess());
const { isAuthenticated } = reduxStore.getState().login;
if (isAuthenticated) {
this.getUserPresence();
}
});
this.sdk.onStreamData('close', () => {
reduxStore.dispatch(disconnect());
});
this.sdk.onStreamData('close', () => {
reduxStore.dispatch(disconnect());
});
this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage)));
this.sdk.onStreamData('users', protectedFunction(ddpMessage => RocketChat._setUser(ddpMessage)));
this.sdk.onStreamData('stream-notify-logged', protectedFunction((ddpMessage) => {
const { eventName } = ddpMessage.fields;
if (eventName === 'user-status') {
const userStatus = ddpMessage.fields.args[0];
const [id, username, status] = userStatus;
if (username) {
database.memoryDatabase.write(() => {
try {
database.memoryDatabase.create('activeUsers', {
id, username, status: STATUSES[status]
}, true);
} catch (error) {
console.log(error);
}
});
this.sdk.onStreamData('stream-notify-logged', protectedFunction((ddpMessage) => {
const { eventName } = ddpMessage.fields;
if (eventName === 'user-status') {
const userStatus = ddpMessage.fields.args[0];
const [id, username, status] = userStatus;
if (username) {
database.memoryDatabase.write(() => {
try {
database.memoryDatabase.create('activeUsers', {
id, username, status: STATUSES[status]
}, true);
} catch (error) {
console.log(error);
}
});
}
}
}
}));
}));
resolve();
});
},
register(credentials) {

View File

@ -39,11 +39,11 @@ const handleSelectServer = function* handleSelectServer({ server, version, fetch
if (userStringified) {
const user = JSON.parse(userStringified);
RocketChat.connect({ server, user });
yield RocketChat.connect({ server, user });
yield put(setUser(user));
yield put(actions.appStart('inside'));
} else {
RocketChat.connect({ server });
yield RocketChat.connect({ server });
yield put(actions.appStart('outside'));
}

View File

@ -60,7 +60,11 @@ const Header = React.memo(({
}
return (
<View style={styles.container}>
<TouchableOpacity onPress={onPress} testID='rooms-list-header-server-dropdown-button'>
<TouchableOpacity
onPress={onPress}
testID='rooms-list-header-server-dropdown-button'
disabled={connecting || isFetching}
>
{connecting ? <Text style={styles.updating}>{I18n.t('Connecting')}</Text> : null}
{isFetching ? <Text style={styles.updating}>{I18n.t('Updating')}</Text> : null}
<View style={styles.button}>

View File

@ -40,13 +40,14 @@ const styles = StyleSheet.create({
});
const HeaderTitle = React.memo(({ connecting, isFetching }) => {
let title = I18n.t('Messages');
if (connecting) {
return <Text style={styles.title}>{I18n.t('Connecting')}</Text>;
title = I18n.t('Connecting');
}
if (isFetching) {
return <Text style={styles.title}>{I18n.t('Updating')}</Text>;
title = I18n.t('Updating');
}
return <Text style={styles.title}>{I18n.t('Messages')}</Text>;
return <Text style={styles.title}>{title}</Text>;
});
const Header = React.memo(({
@ -57,6 +58,7 @@ const Header = React.memo(({
onPress={onPress}
testID='rooms-list-header-server-dropdown-button'
style={styles.container}
disabled={connecting || isFetching}
>
<HeaderTitle connecting={connecting} isFetching={isFetching} />
<View style={styles.button}>

View File

@ -11,7 +11,7 @@ import Header from './Header';
showServerDropdown: state.rooms.showServerDropdown,
showSortDropdown: state.rooms.showSortDropdown,
showSearchHeader: state.rooms.showSearchHeader,
connecting: state.meteor.connecting,
connecting: state.meteor.connecting || state.server.loading,
isFetching: state.rooms.isFetching,
serverName: state.settings.Site_Name
}), dispatch => ({