From c9f433fbbaca7dbb90f27799371a12ddb6e4e468 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Fri, 9 Aug 2019 13:46:05 +1000 Subject: [PATCH] Save message id when sending/receiving to/from public server and lint --- app/sql.js | 11 +++++++---- js/background.js | 17 ++++++++++------- js/models/conversations.js | 9 +++++++-- js/modules/loki_message_api.js | 4 +++- js/modules/loki_public_chat_api.js | 1 + 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/sql.js b/app/sql.js index d64428152..64fa1848a 100644 --- a/app/sql.js +++ b/app/sql.js @@ -1945,10 +1945,13 @@ async function removeMessage(id) { ); } -async function getMessageByServerId(id) { - const row = await db.get('SELECT * FROM messages WHERE ServerId = $ServerId;', { - $ServerId: ServerId, - }); +async function getMessageByServerId(serverId) { + const row = await db.get( + 'SELECT * FROM messages WHERE serverId = $serverId;', + { + $serverId: serverId, + } + ); if (!row) { return null; diff --git a/js/background.js b/js/background.js index c8674a4cd..1b2317c80 100644 --- a/js/background.js +++ b/js/background.js @@ -750,14 +750,17 @@ } }); - Whisper.events.on('publicMessageSent', ({ pubKey, timestamp }) => { - try { - const conversation = ConversationController.get(pubKey); - conversation.onPublicMessageSent(pubKey, timestamp); - } catch (e) { - window.log.error('Error setting public on message'); + Whisper.events.on( + 'publicMessageSent', + ({ pubKey, timestamp, serverId }) => { + try { + const conversation = ConversationController.get(pubKey); + conversation.onPublicMessageSent(pubKey, timestamp, serverId); + } catch (e) { + window.log.error('Error setting public on message'); + } } - }); + ); Whisper.events.on('password-updated', () => { if (appView && appView.inboxView) { diff --git a/js/models/conversations.js b/js/models/conversations.js index fd887826b..26a27e8a8 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -368,9 +368,14 @@ await Promise.all(messages.map(m => m.setIsP2p(true))); }, - async onPublicMessageSent(pubKey, timestamp) { + async onPublicMessageSent(pubKey, timestamp, serverId) { const messages = this._getMessagesWithTimestamp(pubKey, timestamp); - await Promise.all(messages.map(m => m.setIsPublic(true))); + await Promise.all( + messages.map(message => [ + message.setIsPublic(true), + message.setServerId(serverId), + ]) + ); }, async onNewMessage(message) { diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 87c3ec0d3..672e6c4f6 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -110,7 +110,7 @@ class LokiMessageAPI { ], }; try { - await nodeFetch(publicEndpoint, { + const result = await nodeFetch(publicEndpoint, { method: 'post', headers: { 'Content-Type': 'application/json', @@ -118,6 +118,8 @@ class LokiMessageAPI { }, body: JSON.stringify(payload), }); + const body = await result.json(); + messageEventData.serverId = body.data.id; window.Whisper.events.trigger('publicMessageSent', messageEventData); return; } catch (e) { diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index d9ebde10f..1acc1e884 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -167,6 +167,7 @@ class LokiPublicChannelAPI { } const messageData = { + serverId: adnMessage.id, friendRequest: false, source, sourceDevice: 1,