From 1b2833107fd64a5d24edc58a926fca954c7aa1af Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Wed, 13 Nov 2019 16:52:00 -0300 Subject: [PATCH] [IMPROVEMENT] Error messages on connect (#1379) --- app/i18n/locales/en.js | 5 +++- app/i18n/locales/pt-BR.js | 5 +++- app/lib/rocketchat.js | 56 ++++++++++++++++++++++++++++++++++++--- app/sagas/selectServer.js | 9 +++++-- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/app/i18n/locales/en.js b/app/i18n/locales/en.js index 345544195..614830188 100644 --- a/app/i18n/locales/en.js +++ b/app/i18n/locales/en.js @@ -136,6 +136,7 @@ export default { connecting_server: 'connecting to server', Connecting: 'Connecting...', Contact_us: 'Contact us', + Contact_your_server_admin: 'Contact your server admin.', Continue_with: 'Continue with', Copied_to_clipboard: 'Copied to clipboard!', Copy: 'Copy', @@ -257,6 +258,7 @@ export default { No_Reactions: 'No Reactions', No_Read_Receipts: 'No Read Receipts', Not_logged: 'Not logged', + Not_RC_Server: 'This is not a Rocket.Chat server.\n{{contact}}', Nothing: 'Nothing', Nothing_to_save: 'Nothing to save!', Notify_active_in_this_room: 'Notify active users in this room', @@ -375,7 +377,7 @@ export default { Tap_to_view_servers_list: 'Tap to view servers list', Terms_of_Service: ' Terms of Service ', Theme: 'Theme', - The_URL_is_invalid: 'The URL you entered is invalid. Check it and try again, please!', + The_URL_is_invalid: 'Invalid URL or unable to establish a secure connection.\n{{contact}}', There_was_an_error_while_action: 'There was an error while {{action}}!', This_room_is_blocked: 'This room is blocked', This_room_is_read_only: 'This room is read only', @@ -420,6 +422,7 @@ export default { Video_call: 'Video call', View_Original: 'View Original', Voice_call: 'Voice call', + Websocket_disabled: 'Websocket is disabled for this server.\n{{contact}}', Welcome: 'Welcome', Welcome_to_RocketChat: 'Welcome to Rocket.Chat', Whats_your_2fa: 'What\'s your 2FA code?', diff --git a/app/i18n/locales/pt-BR.js b/app/i18n/locales/pt-BR.js index dbc66a0c4..fed614640 100644 --- a/app/i18n/locales/pt-BR.js +++ b/app/i18n/locales/pt-BR.js @@ -138,6 +138,7 @@ export default { connecting_server: 'conectando no servidor', Connecting: 'Conectando...', Continue_with: 'Entrar com', + Contact_your_server_admin: 'Contate o administrador do servidor.', Copied_to_clipboard: 'Copiado para a área de transferência!', Copy: 'Copiar', Permalink: 'Link-Permanente', @@ -242,6 +243,7 @@ export default { Nothing_to_save: 'Nada para salvar!', Notify_active_in_this_room: 'Notificar usuários ativos nesta sala', Notify_all_in_this_room: 'Notificar todos nesta sala', + Not_RC_Server: 'Este não é um servidor Rocket.Chat.\n{{contact}}', Offline: 'Offline', Oops: 'Ops!', Online: 'Online', @@ -334,7 +336,7 @@ export default { Take_a_photo: 'Tirar uma foto', Take_a_video: 'Gravar um vídeo', Terms_of_Service: ' Termos de Serviço ', - The_URL_is_invalid: 'A URL fornecida é inválida ou não acessível. Por favor tente novamente, mas com uma url diferente.', + The_URL_is_invalid: 'A URL fornecida é inválida ou incapaz de estabelecer uma conexão segura.\n{{contact}}', There_was_an_error_while_action: 'Aconteceu um erro {{action}}!', This_room_is_blocked: 'Este quarto está bloqueado', This_room_is_read_only: 'Este quarto é apenas de leitura', @@ -375,6 +377,7 @@ export default { Username_or_email: 'Usuário ou email', Video_call: 'Chamada de vídeo', Voice_call: 'Chamada de voz', + Websocket_disabled: 'Websocket está desativado para esse servidor.\n{{contact}}', Welcome: 'Bem vindo', Welcome_to_RocketChat: 'Bem vindo ao Rocket.Chat', Whats_your_2fa: 'Qual seu código de autenticação?', diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 7f2e8a046..d253c8204 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -48,6 +48,7 @@ import callJitsi from './methods/callJitsi'; import { getDeviceToken } from '../notifications/push'; import { SERVERS, SERVER_URL } from '../constants/userDefaults'; import { setActiveUsers } from '../actions/activeUsers'; +import I18n from '../i18n'; const TOKEN_KEY = 'reactnativemeteor_usertoken'; const SORT_PREFS_KEY = 'RC_SORT_PREFS_KEY'; @@ -88,9 +89,53 @@ const RocketChat = { console.warn(`RNUserDefaults error: ${ error.message }`); } }, - async getServerInfo(server) { + async getWebsocketInfo({ server }) { + // Use useSsl: false only if server url starts with http:// + const useSsl = !/http:\/\//.test(server); + + const sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl }); + try { - const result = await fetch(`${ server }/api/info`).then(response => response.json()); + await sdk.connect(); + } catch (err) { + if (err.message && err.message.includes('400')) { + return { + success: false, + message: 'Websocket_disabled', + messageOptions: { + contact: I18n.t('Contact_your_server_admin') + } + }; + } + } + + sdk.disconnect(); + + return { + success: true + }; + }, + async getServerInfo(server) { + const notRCServer = { + success: false, + message: 'Not_RC_Server', + messageOptions: { + contact: I18n.t('Contact_your_server_admin') + } + }; + try { + const result = await fetch(`${ server }/api/info`).then(async(response) => { + let res = notRCServer; + try { + res = await response.json(); + if (!(res && res.success)) { + return notRCServer; + } + } catch (e) { + // do nothing + } + return res; + }); if (result.success) { if (semver.lt(result.version, MIN_ROCKETCHAT_VERSION)) { return { @@ -102,14 +147,17 @@ const RocketChat = { } }; } - return result; } + return result; } catch (e) { log(e); } return { success: false, - message: 'The_URL_is_invalid' + message: 'The_URL_is_invalid', + messageOptions: { + contact: I18n.t('Contact_your_server_admin') + } }; }, stopListener(listener) { diff --git a/app/sagas/selectServer.js b/app/sagas/selectServer.js index b997845e7..64795ede2 100644 --- a/app/sagas/selectServer.js +++ b/app/sagas/selectServer.js @@ -22,9 +22,14 @@ import { SERVERS, TOKEN, SERVER_URL } from '../constants/userDefaults'; const getServerInfo = function* getServerInfo({ server, raiseError = true }) { try { const serverInfo = yield RocketChat.getServerInfo(server); - if (!serverInfo.success) { + let websocketInfo = true; + if (raiseError) { + websocketInfo = yield RocketChat.getWebsocketInfo({ server }); + } + if (!serverInfo.success || !websocketInfo.success) { if (raiseError) { - Alert.alert(I18n.t('Oops'), I18n.t(serverInfo.message, serverInfo.messageOptions)); + const info = serverInfo.success ? websocketInfo : serverInfo; + Alert.alert(I18n.t('Oops'), I18n.t(info.message, info.messageOptions)); } yield put(serverFailure()); return;