From 143bf1bdc1df62b7d8b8ccb8ea843b0df0c8a7db Mon Sep 17 00:00:00 2001 From: Beaudan Date: Tue, 23 Jul 2019 16:42:51 +1000 Subject: [PATCH] Fill out unregister channel stub, trigger unregister channel on conversation deletion --- js/background.js | 2 +- js/conversation_controller.js | 6 ++- js/models/conversations.js | 2 +- js/modules/loki_public_chat_api.js | 67 ++++++++++++++---------------- 4 files changed, 39 insertions(+), 38 deletions(-) diff --git a/js/background.js b/js/background.js index 544deb735..c8674a4cd 100644 --- a/js/background.js +++ b/js/background.js @@ -221,7 +221,7 @@ ); if (publicChatServer) { publicChatServer.findOrCreateChannel( - settings.channel_id, + settings.channelId, conversation.id ); } else { diff --git a/js/conversation_controller.js b/js/conversation_controller.js index fe2a45074..50b3aae33 100644 --- a/js/conversation_controller.js +++ b/js/conversation_controller.js @@ -1,4 +1,4 @@ -/* global _, Whisper, Backbone, storage, textsecure, libsignal */ +/* global _, Whisper, Backbone, storage, textsecure, libsignal, lokiPublicChatAPI */ /* eslint-disable more/no-then */ @@ -159,6 +159,10 @@ if (!conversation) { return; } + if (conversation.isPublic()) { + const server = conversation.getPublicSource(); + lokiPublicChatAPI.unregisterChannel(server.server, server.channelId); + } await conversation.destroyMessages(); const deviceIds = await textsecure.storage.protocol.getDeviceIds(id); await Promise.all( diff --git a/js/models/conversations.js b/js/models/conversations.js index b19674da6..c8c124b4d 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -2033,7 +2033,7 @@ } return { server: this.get('server'), - channel_id: this.get('channelId'), + channelId: this.get('channelId'), }; }, // FIXME: remove or add public and/or "sending" hint to name... diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index b2b55c4e2..f563c36e6 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -14,22 +14,28 @@ class LokiPublicChatAPI extends EventEmitter { this.servers = []; } findOrCreateServer(hostport) { - let thisServer = null; log.info(`LokiPublicChatAPI looking for ${hostport}`); - this.servers.some(server => { - // if we already have this hostport registered - if (server.server === hostport) { - thisServer = server; - return true; - } - return false; - }); - if (thisServer === null) { + let thisServer = this.servers.find(server => server.server === hostport); + if (!thisServer) { thisServer = new LokiPublicServerAPI(this, hostport); this.servers.push(thisServer); } return thisServer; } + unregisterChannel(hostport, channelId) { + const thisServer = this.servers.find(server => server.server === hostport); + if (!thisServer) { + log.warn(`Tried to unregister from nonexistent server ${hostport}`); + return; + } + thisServer.unregisterChannel(channelId); + if (thisServer.channels.length === 0) { + const index = this.servers.indexOf(thisServer); + if (index > -1) { + this.servers.splice(index, 1); + } + } + } } class LokiPublicServerAPI { @@ -39,25 +45,23 @@ class LokiPublicServerAPI { this.channels = []; } findOrCreateChannel(channelId, conversationId) { - let thisChannel = null; - this.channels.forEach(channel => { - if ( - channel.channelId === channelId && - channel.conversationId === conversationId - ) { - thisChannel = channel; - } - }); - if (thisChannel === null) { + let thisChannel = this.channels.find(channel => channel.channelId === channelId); + if (!thisChannel) { thisChannel = new LokiPublicChannelAPI(this, channelId, conversationId); this.channels.push(thisChannel); } return thisChannel; } unregisterChannel(channelId) { - // find it, remove it - // if no channels left, request we deregister server - return channelId || this; // this is just to make eslint happy + const thisChannel = this.channels.find(channel => channel.channelId === channelId); + if (!thisChannel) { + return; + } + thisChannel.stopPolling = true; + const index = this.channels.indexOf(thisChannel); + if (index > -1) { + this.channels.splice(index, 1); + } } } @@ -69,6 +73,7 @@ class LokiPublicChannelAPI { this.groupName = 'unknown'; this.conversationId = conversationId; this.lastGot = 0; + this.stopPolling = false; log.info(`registered LokiPublicChannel ${channelId}`); // start polling this.pollForMessages(); @@ -77,11 +82,6 @@ class LokiPublicChannelAPI { async pollForChannel(source, endpoint) { // groupName will be loaded from server const url = new URL(this.baseChannelUrl); - /* - const params = { - include_annotations: 1, - }; - */ let res; let success = true; try { @@ -99,16 +99,10 @@ class LokiPublicChannelAPI { } async pollForDeletions() { - // let id = 0; // read all messages from 0 to current // delete local copies if server state has changed to delete // run every minute const url = new URL(this.baseChannelUrl); - /* - const params = { - include_annotations: 1, - }; - */ let res; let success = true; try { @@ -144,6 +138,10 @@ class LokiPublicChannelAPI { } const response = await res.json(); + if (this.stopPolling) { + // Stop after latest await possible + return; + } if (response.meta.code !== 200) { success = false; } @@ -151,7 +149,6 @@ class LokiPublicChannelAPI { if (success) { let receivedAt = new Date().getTime(); response.data.forEach(adnMessage => { - // FIXME: create proper message for this message.DataMessage.body let timestamp = new Date(adnMessage.created_at).getTime(); let from = adnMessage.user.username; let source;