commit
ac80ef0d4e
@ -0,0 +1,88 @@
|
|||||||
|
/* global Whisper, i18n, _ */
|
||||||
|
|
||||||
|
// eslint-disable-next-line func-names
|
||||||
|
(function() {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
window.Whisper = window.Whisper || {};
|
||||||
|
|
||||||
|
Whisper.AddServerDialogView = Whisper.View.extend({
|
||||||
|
templateName: 'add-server-template',
|
||||||
|
className: 'loki-dialog add-server modal',
|
||||||
|
initialize(options = {}) {
|
||||||
|
this.title = i18n('addServerDialogTitle');
|
||||||
|
this.okText = options.okText || i18n('ok');
|
||||||
|
this.cancelText = options.cancelText || i18n('cancel');
|
||||||
|
this.$('input').focus();
|
||||||
|
this.render();
|
||||||
|
},
|
||||||
|
events: {
|
||||||
|
keyup: 'onKeyup',
|
||||||
|
'click .ok': 'confirm',
|
||||||
|
'click .cancel': 'close',
|
||||||
|
},
|
||||||
|
render_attributes() {
|
||||||
|
return {
|
||||||
|
title: this.title,
|
||||||
|
ok: this.okText,
|
||||||
|
cancel: this.cancelText,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
confirm() {
|
||||||
|
// 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':
|
||||||
|
this.confirm();
|
||||||
|
break;
|
||||||
|
case 'Escape':
|
||||||
|
case 'Esc':
|
||||||
|
this.close();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
})();
|
@ -0,0 +1,83 @@
|
|||||||
|
/* global Whisper, i18n, lokiPublicChatAPI, ConversationController, friends */
|
||||||
|
|
||||||
|
// eslint-disable-next-line func-names
|
||||||
|
(function() {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
window.Whisper = window.Whisper || {};
|
||||||
|
|
||||||
|
Whisper.ConnectingToServerDialogView = Whisper.View.extend({
|
||||||
|
templateName: 'connecting-to-server-template',
|
||||||
|
className: 'loki-dialog connecting-to-server modal',
|
||||||
|
initialize(options = {}) {
|
||||||
|
this.title = i18n('connectingLoad');
|
||||||
|
this.cancelText = options.cancelText || i18n('cancel');
|
||||||
|
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,
|
||||||
|
cancel: this.cancelText,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
close() {
|
||||||
|
this.trigger('connectionResult', { cancelled: true });
|
||||||
|
this.remove();
|
||||||
|
},
|
||||||
|
onKeyup(event) {
|
||||||
|
switch (event.key) {
|
||||||
|
case 'Escape':
|
||||||
|
case 'Esc':
|
||||||
|
this.close();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
})();
|
Loading…
Reference in New Issue