From 2c1375e42a93569325aaaaf82faea35d2fd4a91c Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Wed, 13 Nov 2019 15:52:17 +1100 Subject: [PATCH] Fix recurring memory leak in mentions --- app/sql.js | 14 ++++++++++++++ js/modules/data.js | 14 ++++++++++++-- js/modules/loki_app_dot_net_api.js | 5 +++-- js/views/conversation_view.js | 19 +++++++++++-------- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/app/sql.js b/app/sql.js index 491ca5403..5d3410b7e 100644 --- a/app/sql.js +++ b/app/sql.js @@ -103,6 +103,7 @@ module.exports = { getAllRssFeedConversations, getAllPublicConversations, getPublicConversationsByServer, + getPubkeysInPublicConversation, getPubKeysWithFriendStatus, getAllConversationIds, getAllPrivateConversations, @@ -1740,6 +1741,19 @@ async function getPublicConversationsByServer(server) { return map(rows, row => jsonToObject(row.json)); } +async function getPubkeysInPublicConversation(id) { + const rows = await db.all( + `SELECT DISTINCT source FROM messages WHERE + conversationId = $conversationId + ORDER BY id ASC;`, + { + $conversationId: id, + } + ); + + return map(rows, row => row.source); +} + async function getAllGroupsInvolvingId(id) { const rows = await db.all( `SELECT json FROM conversations WHERE diff --git a/js/modules/data.js b/js/modules/data.js index e5d914cba..f304d9ed8 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -1,4 +1,4 @@ -/* global window, setTimeout, IDBKeyRange */ +/* global window, setTimeout, clearTimeout, IDBKeyRange */ const electron = require('electron'); @@ -124,6 +124,7 @@ module.exports = { getAllRssFeedConversations, getAllPublicConversations, getPublicConversationsByServer, + getPubkeysInPublicConversation, savePublicServerToken, getPublicServerTokenByServerUrl, getAllGroupsInvolvingId, @@ -312,6 +313,11 @@ function _removeJob(id) { return; } + if (_jobs[id].timer) { + clearTimeout(_jobs[id].timer); + _jobs[id].timer = null; + } + delete _jobs[id]; if (_shutdownCallback) { @@ -363,7 +369,7 @@ function makeChannel(fnName) { args: _DEBUG ? args : null, }); - setTimeout( + _jobs[jobId].timer = setTimeout( () => reject(new Error(`SQL channel job ${jobId} (${fnName}) timed out`)), DATABASE_UPDATE_TIMEOUT @@ -764,6 +770,10 @@ async function getAllPrivateConversations({ ConversationCollection }) { return collection; } +async function getPubkeysInPublicConversation(id) { + return channels.getPubkeysInPublicConversation(id); +} + async function savePublicServerToken(data) { await channels.savePublicServerToken(data); } diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 8398ef65c..66c4056a8 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -22,6 +22,7 @@ class LokiAppDotNetAPI extends EventEmitter { this.ourKey = ourKey; this.servers = []; this.myPrivateKey = false; + this.allMembers = []; } async getPrivateKey() { @@ -785,8 +786,8 @@ class LokiPublicChannelAPI { log.warn(`Error while polling for public chat messages: ${e}`); } if (this.running) { - setTimeout(() => { - this.timers.message = this.pollForMessages(); + this.timers.message = setTimeout(() => { + this.pollForMessages(); }, PUBLICCHAT_MSG_POLL_EVERY); } } diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index 8c97a9389..b9fbcd2fb 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -341,16 +341,19 @@ this.selectMember = this.selectMember.bind(this); const updateMemberList = async () => { - const maxToFetch = 1000; - const allMessages = await window.Signal.Data.getMessagesByConversation( - this.model.id, - { - limit: maxToFetch, - MessageCollection: Whisper.MessageCollection, - } + const allPubKeys = await window.Signal.Data.getPubkeysInPublicConversation( + this.model.id ); - const allMembers = allMessages.models.map(d => d.propsForMessage); + const allMembers = await Promise.all( + allPubKeys.map(async pubKey => ({ + id: pubKey, + authorPhoneNumber: pubKey, + authorProfileName: await ConversationController.get( + pubKey + ).getProfileName(), + })) + ); window.lokiPublicChatAPI.setListOfMembers(allMembers); };