Hook up UI to all the server creation logic etc

pull/557/head
Beaudan Brown 6 years ago
parent 4e70b66131
commit 363cd81ccc

@ -37,7 +37,7 @@ class LokiAppDotNetAPI extends EventEmitter {
thisServer = new LokiAppDotNetServerAPI(this, serverUrl); thisServer = new LokiAppDotNetServerAPI(this, serverUrl);
const gotToken = await thisServer.getOrRefreshServerToken(); const gotToken = await thisServer.getOrRefreshServerToken();
if (!gotToken) { if (!gotToken) {
log.error(`Invalid server ${serverUrl}`); log.warn(`Invalid server ${serverUrl}`);
return null; return null;
} }
log.info(`set token ${thisServer.token}`); log.info(`set token ${thisServer.token}`);

@ -1,8 +1,9 @@
/* global log */
const LokiAppDotNetAPI = require('./loki_app_dot_net_api'); const LokiAppDotNetAPI = require('./loki_app_dot_net_api');
const DEVICE_MAPPING_ANNOTATION_KEY = 'network.loki.messenger.devicemapping'; const DEVICE_MAPPING_ANNOTATION_KEY = 'network.loki.messenger.devicemapping';
class LokiFileServerAPI { class LokiFileServerAPI {
constructor(ourKey) { constructor(ourKey) {
this.ourKey = ourKey; this.ourKey = ourKey;
@ -13,7 +14,7 @@ class LokiFileServerAPI {
this._server = await this._adnApi.findOrCreateServer(serverUrl); this._server = await this._adnApi.findOrCreateServer(serverUrl);
// TODO: Handle this failure gracefully // TODO: Handle this failure gracefully
if (!this._server) { if (!this._server) {
// console.error('Failed to establish connection to file server'); log.error('Failed to establish connection to file server');
} }
} }

@ -29,16 +29,48 @@
}; };
}, },
confirm() { confirm() {
const serverUrl = this.$('#server-url').val(); // Remove error if there is one
console.log(`You confirmed the adding of a new server: ${serverUrl}`); this.showError(null);
const dialog = new Whisper.ConnectingToServerDialogView({ serverUrl }); 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); this.el.append(dialog.el);
}, }, 200);
async validateServer() { 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() { close() {
this.remove(); 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) { onKeyup(event) {
switch (event.key) { switch (event.key) {
case 'Enter': case 'Enter':

@ -10,18 +10,55 @@
templateName: 'connecting-to-server-template', templateName: 'connecting-to-server-template',
className: 'loki-dialog connecting-to-server modal', className: 'loki-dialog connecting-to-server modal',
initialize(options = {}) { initialize(options = {}) {
console.log(`Add server init: ${options}`); this.title = i18n('connectingLoad');
this.title = i18n('loading');
this.cancelText = options.cancelText || i18n('cancel'); this.cancelText = options.cancelText || i18n('cancel');
this.render(); this.serverUrl = options.serverUrl;
this.$('.connecting-to-server').bind('keyup', event => this.onKeyup(event)); this.channelId = options.channelId;
const serverAPI = lokiPublicChatAPI.findOrCreateServer( this.once('attemptConnection', () =>
options.serverUrl this.attemptConnection(options.serverUrl, options.channelId)
); );
this.render();
}, },
events: { events: {
keyup: 'onKeyup',
'click .cancel': 'close', '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() { render_attributes() {
return { return {
title: this.title, title: this.title,
@ -29,6 +66,7 @@
}; };
}, },
close() { close() {
this.trigger('connectionResult', { cancelled: true });
this.remove(); this.remove();
}, },
onKeyup(event) { onKeyup(event) {

Loading…
Cancel
Save