diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index b7ccf6c03..9b7e16e2c 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -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) { diff --git a/app/sagas/selectServer.js b/app/sagas/selectServer.js index 10aad2fad..859103901 100644 --- a/app/sagas/selectServer.js +++ b/app/sagas/selectServer.js @@ -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')); } diff --git a/app/views/RoomsListView/Header/Header.android.js b/app/views/RoomsListView/Header/Header.android.js index a2393caf0..333677fc7 100644 --- a/app/views/RoomsListView/Header/Header.android.js +++ b/app/views/RoomsListView/Header/Header.android.js @@ -60,7 +60,11 @@ const Header = React.memo(({ } return ( - + {connecting ? {I18n.t('Connecting')} : null} {isFetching ? {I18n.t('Updating')} : null} diff --git a/app/views/RoomsListView/Header/Header.ios.js b/app/views/RoomsListView/Header/Header.ios.js index 20d6f9b13..09adacfec 100644 --- a/app/views/RoomsListView/Header/Header.ios.js +++ b/app/views/RoomsListView/Header/Header.ios.js @@ -40,13 +40,14 @@ const styles = StyleSheet.create({ }); const HeaderTitle = React.memo(({ connecting, isFetching }) => { + let title = I18n.t('Messages'); if (connecting) { - return {I18n.t('Connecting')}; + title = I18n.t('Connecting'); } if (isFetching) { - return {I18n.t('Updating')}; + title = I18n.t('Updating'); } - return {I18n.t('Messages')}; + return {title}; }); 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} > diff --git a/app/views/RoomsListView/Header/index.js b/app/views/RoomsListView/Header/index.js index 853602b5a..74c679b58 100644 --- a/app/views/RoomsListView/Header/index.js +++ b/app/views/RoomsListView/Header/index.js @@ -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 => ({