diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index da8f86de8..ca0d4d9d6 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -37,7 +37,7 @@ class LokiAppDotNetAPI extends EventEmitter { thisServer = new LokiAppDotNetServerAPI(this, serverUrl); const gotToken = await thisServer.getOrRefreshServerToken(); if (!gotToken) { - log.error(`Invalid server ${serverUrl}`); + log.warn(`Invalid server ${serverUrl}`); return null; } log.info(`set token ${thisServer.token}`); diff --git a/js/modules/loki_file_server_api.js b/js/modules/loki_file_server_api.js index c26533e93..82d4a9e0a 100644 --- a/js/modules/loki_file_server_api.js +++ b/js/modules/loki_file_server_api.js @@ -1,8 +1,9 @@ +/* global log */ + const LokiAppDotNetAPI = require('./loki_app_dot_net_api'); const DEVICE_MAPPING_ANNOTATION_KEY = 'network.loki.messenger.devicemapping'; - class LokiFileServerAPI { constructor(ourKey) { this.ourKey = ourKey; @@ -13,7 +14,7 @@ class LokiFileServerAPI { this._server = await this._adnApi.findOrCreateServer(serverUrl); // TODO: Handle this failure gracefully if (!this._server) { - // console.error('Failed to establish connection to file server'); + log.error('Failed to establish connection to file server'); } } diff --git a/js/views/add_server_dialog_view.js b/js/views/add_server_dialog_view.js index 0b8efbbb7..270912825 100644 --- a/js/views/add_server_dialog_view.js +++ b/js/views/add_server_dialog_view.js @@ -29,16 +29,48 @@ }; }, confirm() { - const serverUrl = this.$('#server-url').val(); - console.log(`You confirmed the adding of a new server: ${serverUrl}`); - const dialog = new Whisper.ConnectingToServerDialogView({ serverUrl }); - this.el.append(dialog.el); - }, - async validateServer() { + // Remove error if there is one + this.showError(null); + const serverUrl = this.$('#server-url').val().toLowerCase(); + // TODO: Make this not hard coded + const channelId = 1; + const dialog = new Whisper.ConnectingToServerDialogView({ + serverUrl, + channelId, + }); + const dialogDelayTimer = setTimeout(() => { + this.el.append(dialog.el); + }, 200); + dialog.once('connectionResult', result => { + clearTimeout(dialogDelayTimer); + if (result.cancelled) { + this.showError(null); + return; + } + if (result.errorCode) { + this.showError(result.errorCode); + return; + } + window.Whisper.events.trigger('showToast', { + message: i18n('connectToServerSuccess'), + }); + this.close(); + }); + dialog.trigger('attemptConnection'); }, close() { this.remove(); }, + showError(message) { + if (_.isEmpty(message)) { + this.$('.error').text(''); + this.$('.error').hide(); + } else { + this.$('.error').text(`Error: ${message}`); + this.$('.error').show(); + } + this.$('input').focus(); + }, onKeyup(event) { switch (event.key) { case 'Enter': diff --git a/js/views/connecting_to_server_dialog_view.js b/js/views/connecting_to_server_dialog_view.js index a9d22febb..5c45a1ef1 100644 --- a/js/views/connecting_to_server_dialog_view.js +++ b/js/views/connecting_to_server_dialog_view.js @@ -10,18 +10,55 @@ templateName: 'connecting-to-server-template', className: 'loki-dialog connecting-to-server modal', initialize(options = {}) { - console.log(`Add server init: ${options}`); - this.title = i18n('loading'); + this.title = i18n('connectingLoad'); this.cancelText = options.cancelText || i18n('cancel'); - this.render(); - this.$('.connecting-to-server').bind('keyup', event => this.onKeyup(event)); - const serverAPI = lokiPublicChatAPI.findOrCreateServer( - options.serverUrl + this.serverUrl = options.serverUrl; + this.channelId = options.channelId; + this.once('attemptConnection', () => + this.attemptConnection(options.serverUrl, options.channelId) ); + this.render(); }, events: { + keyup: 'onKeyup', 'click .cancel': 'close', }, + async attemptConnection(serverUrl, channelId) { + const rawServerUrl = serverUrl + .replace(/^https?:\/\//i, '') + .replace(/[/\\]+$/i, ''); + const sslServerUrl = `https://${rawServerUrl}`; + const conversationId = `publicChat:${channelId}@${rawServerUrl}`; + + const conversationExists = ConversationController.get(conversationId); + if (conversationExists) { + // We are already a member of this public chat + return this.resolveWith({ errorCode: i18n('publicChatExists') }); + } + + const serverAPI = await lokiPublicChatAPI.findOrCreateServer( + sslServerUrl + ); + if (!serverAPI) { + // Url incorrect or server not compatible + return this.resolveWith({ errorCode: i18n('connectToServerFail') }); + } + + const conversation = await ConversationController.getOrCreateAndWait( + conversationId, + 'group' + ); + serverAPI.findOrCreateChannel(channelId, conversationId); + await conversation.setPublicSource(sslServerUrl, channelId); + await conversation.setFriendRequestStatus( + friends.friendRequestStatusEnum.friends + ); + return this.resolveWith({ conversation }); + }, + resolveWith(result) { + this.trigger('connectionResult', result); + this.remove(); + }, render_attributes() { return { title: this.title, @@ -29,6 +66,7 @@ }; }, close() { + this.trigger('connectionResult', { cancelled: true }); this.remove(); }, onKeyup(event) {