use attempt from window object to reduce code duplication

pull/787/head
Ryan Tharp 5 years ago
parent 83e2404d89
commit eecf2252d7

@ -1,4 +1,4 @@
/* global Whisper, i18n, ConversationController, friends */ /* global Whisper, i18n, ConversationController, friends, lokiPublicChatAPI */
// eslint-disable-next-line func-names // eslint-disable-next-line func-names
(function() { (function() {
@ -24,31 +24,12 @@
'click .cancel': 'close', 'click .cancel': 'close',
}, },
async attemptConnection(serverUrl, channelId) { async attemptConnection(serverUrl, channelId) {
const rawServerUrl = serverUrl try {
.replace(/^https?:\/\//i, '') const conversion = await window.attemptConnection(serverUrl, channelId);
.replace(/[/\\]+$/i, ''); } catch(e) {
const sslServerUrl = `https://${rawServerUrl}`; log.error('can not connect', e.message, e.code);
const conversationId = `publicChat:${channelId}@${rawServerUrl}`; return this.resolveWith({ errorCode: e.message });
const conversationExists = ConversationController.get(conversationId);
if (conversationExists) {
// We are already a member of this public chat
return this.resolveWith({ errorCode: i18n('publicChatExists') });
} }
// create conversation
const conversation = await ConversationController.getOrCreateAndWait(
conversationId,
'group'
);
// convert conversation to a public one
await conversation.setPublicSource(sslServerUrl, channelId);
// set friend and appropriate SYNC messages for multidevice
await conversation.setFriendRequestStatus(
friends.friendRequestStatusEnum.friends
);
// and finally activate it
conversation.getPublicSendData(); // may want "await" if you want to use the API
return this.resolveWith({ conversation }); return this.resolveWith({ conversation });
}, },
resolveWith(result) { resolveWith(result) {

Loading…
Cancel
Save