From 190d5978146a23155dbc28e1020ea54c784ddf28 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Tue, 10 Nov 2020 17:33:44 +1100 Subject: [PATCH] only trigger new msg on open group if we are still running --- js/modules/loki_app_dot_net_api.js | 41 +++++++++++-------- .../session/LeftPaneMessageSection.tsx | 27 ++++++------ ts/session/types/OpenGroup.ts | 10 ++--- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 02e63ca9d..cd791ce71 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -1034,7 +1034,7 @@ class LokiPublicChannelAPI { this.channelId = channelId; this.baseChannelUrl = `channels/${this.channelId}`; this.conversationId = conversationId; - this.conversation = ConversationController.get(conversationId); + this.conversation = ConversationController.getOrThrow(conversationId); this.lastGot = null; this.modStatus = false; this.deleteLastId = 1; @@ -1185,7 +1185,9 @@ class LokiPublicChannelAPI { moderators.includes(ourNumberDevice); } - await this.conversation.setModerators(moderators || []); + if(this.running) { + await this.conversation.setModerators(moderators || []); + } } async setChannelSettings(settings) { @@ -1335,6 +1337,9 @@ class LokiPublicChannelAPI { } return; } + if (!this.running) { + return; + } const { data } = res.response; @@ -1474,7 +1479,7 @@ class LokiPublicChannelAPI { // update where we last checked this.deleteLastId = res.response.meta.max_id; - more = res.response.meta.more && res.response.data.length >= params.count; + more = res.response.meta.more && res.response.data.length >= params.count && this.running; } } @@ -1818,13 +1823,14 @@ class LokiPublicChannelAPI { }) ); - // do we really need this? - if (!pendingMessages.length) { + // return early if we should stop processing + if (!pendingMessages.length || !this.running) { this.conversation.setLastRetrievedMessage(this.lastGot); this.messagesPollLock = false; return; } + // slave to primary map for this group of messages let slavePrimaryMap = {}; @@ -1883,6 +1889,7 @@ class LokiPublicChannelAPI { message, }); }); + sendNow = false; } primaryMessages = false; // free memory @@ -1955,17 +1962,19 @@ class LokiPublicChannelAPI { return; } } - log.info( - 'emitting pending message', - message.serverId, - 'on', - this.channelId, - 'at', - this.serverAPI.baseServerUrl - ); - this.chatAPI.emit('publicMessage', { - message, - }); + if (this.running) { + log.info( + 'emitting pending message', + message.serverId, + 'on', + this.channelId, + 'at', + this.serverAPI.baseServerUrl + ); + this.chatAPI.emit('publicMessage', { + message, + }); + } }); /* eslint-enable no-param-reassign */ diff --git a/ts/components/session/LeftPaneMessageSection.tsx b/ts/components/session/LeftPaneMessageSection.tsx index 05676f964..22502921e 100644 --- a/ts/components/session/LeftPaneMessageSection.tsx +++ b/ts/components/session/LeftPaneMessageSection.tsx @@ -432,21 +432,22 @@ export class LeftPaneMessageSection extends React.Component { // Connect to server try { - ToastUtils.pushToastSuccess( + ToastUtils.pushToastInfo( 'connectingToServer', window.i18n('connectingToServer') ); this.setState({ loading: true }); - await OpenGroup.join(serverUrl, async () => { - if (await OpenGroup.serverExists(serverUrl)) { - ToastUtils.pushToastSuccess( - 'connectToServerSuccess', - window.i18n('connectToServerSuccess') - ); - } - this.setState({ loading: false }); - }); + await OpenGroup.join(serverUrl); + if (await OpenGroup.serverExists(serverUrl)) { + ToastUtils.pushToastSuccess( + 'connectToServerSuccess', + window.i18n('connectToServerSuccess') + ); + } else { + throw new Error('Open group joined but the corresponding server does not exist'); + } + this.setState({ loading: false }); const openGroupConversation = await OpenGroup.getConversation(serverUrl); if (openGroupConversation) { @@ -460,6 +461,7 @@ export class LeftPaneMessageSection extends React.Component { 'Joined an opengroup but did not find ther corresponding conversation' ); } + this.handleToggleOverlay(undefined); } catch (e) { window.log.error('Failed to connect to server:', e); ToastUtils.pushToastError( @@ -467,11 +469,6 @@ export class LeftPaneMessageSection extends React.Component { window.i18n('connectToServerFail') ); this.setState({ loading: false }); - } finally { - this.setState({ - loading: false, - }); - this.handleToggleOverlay(undefined); } } diff --git a/ts/session/types/OpenGroup.ts b/ts/session/types/OpenGroup.ts index e8fd0f2e2..38a611a8a 100644 --- a/ts/session/types/OpenGroup.ts +++ b/ts/session/types/OpenGroup.ts @@ -95,8 +95,7 @@ export class OpenGroup { * @returns `OpenGroup` if connection success or if already connected */ public static async join( - server: string, - onLoading?: any + server: string ): Promise { const prefixedServer = OpenGroup.prefixify(server); if (!OpenGroup.validate(server)) { @@ -123,14 +122,11 @@ export class OpenGroup { // Try to connect to server try { - if (onLoading) { - onLoading(); - } - conversation = await PromiseUtils.timeout( window.attemptConnection(prefixedServer, channel), - 15000 + 20000 ); + if (!conversation) { throw new Error(window.i18n('connectToServerFail')); }