From 07076c27ae371ae1b0c3f179ce1ed372ea429299 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Fri, 25 Jan 2019 14:56:54 +1100 Subject: [PATCH] Moved friend status to window, added start of p2p api stuff --- app/sql.js | 11 +++++++--- js/models/conversations.js | 14 ++----------- js/modules/data.js | 10 +++------ js/modules/loki_p2p_api.js | 32 ++++++++++++++++++++++++++++ libloki/api.js | 35 ++++++++++++++++++++++++------- libtextsecure/message_receiver.js | 35 +++++++++++++++++++++---------- preload.js | 4 ++++ protos/SignalService.proto | 4 ++-- 8 files changed, 103 insertions(+), 42 deletions(-) create mode 100644 js/modules/loki_p2p_api.js diff --git a/app/sql.js b/app/sql.js index 7ace662fb..3a1b11cfe 100644 --- a/app/sql.js +++ b/app/sql.js @@ -90,7 +90,7 @@ module.exports = { updateConversation, removeConversation, getAllConversations, - getAllFriendIds, + getPubKeysWithFriendStatus, getAllConversationIds, getAllPrivateConversations, getAllGroupsInvolvingId, @@ -1281,10 +1281,15 @@ async function getAllConversations() { return map(rows, row => jsonToObject(row.json)); } -async function getAllFriendIds() { +async function getPubKeysWithFriendStatus(status) { // TODO: Maybe don't have this hardcoded to 4 (friends status in the enum) const rows = await db.all( - 'SELECT id FROM conversations WHERE friendRequestStatus = 4 ORDER BY id ASC;' + `SELECT id FROM conversations WHERE + friendRequestStatus = $status + ORDER BY id ASC;`, + { + $status: status, + } ); return map(rows, row => row.id); } diff --git a/js/models/conversations.js b/js/models/conversations.js index d4274e3af..411ef228f 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -41,18 +41,8 @@ } = window.Signal.Migrations; // Possible conversation friend states - const FriendRequestStatusEnum = Object.freeze({ - // New conversation, no messages sent or received - none: 0, - // This state is used to lock the input early while sending - pendingSend: 1, - // Friend request sent, awaiting response - requestSent: 2, - // Friend request received, awaiting user input - requestReceived: 3, - // We did it! - friends: 4, - }); + const FriendRequestStatusEnum = + window.libloki.friends.friendRequestStatusEnum; // Possible session reset states const SessionResetEnum = Object.freeze({ diff --git a/js/modules/data.js b/js/modules/data.js index 91547b1f0..d8100bb4a 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -120,7 +120,7 @@ module.exports = { _removeConversations, getAllConversations, - getAllFriendIds, + getPubKeysWithFriendStatus, getAllConversationIds, getAllPrivateConversations, getAllGroupsInvolvingId, @@ -722,12 +722,8 @@ async function _removeConversations(ids) { await channels.removeConversation(ids); } -async function getAllFriendIds() { - const ids = (await channels.getAllFriendIds()).map(c => - setifyProperty(c, 'swarmNodes') - ); - - return ids; +async function getPubKeysWithFriendStatus(status) { + return channels.getPubKeysWithFriendStatus(status); } async function getAllConversations({ ConversationCollection }) { diff --git a/js/modules/loki_p2p_api.js b/js/modules/loki_p2p_api.js new file mode 100644 index 000000000..c83ea2ce0 --- /dev/null +++ b/js/modules/loki_p2p_api.js @@ -0,0 +1,32 @@ +// const fetch = require('node-fetch'); + +class LokiP2pAPI { + constructor() { + this.contactP2pDetails = {}; + } + + addContactP2pDetails(pubKey, address, port) { + this.contactP2pDetails[pubKey] = { + address, + port, + }; + } + + getContactP2pDetails(pubKey) { + if (this.contactP2pDetails[pubKey]) { + return this.contactP2pDetails[pubKey]; + } + return null; + } + + removeContactP2pDetails(pubKey, address, port) { + this.contactP2pDetails[pubKey] = { + address, + port, + }; + } +} + +module.exports = { + LokiP2pAPI, +}; diff --git a/libloki/api.js b/libloki/api.js index 60682bf34..0cf13d755 100644 --- a/libloki/api.js +++ b/libloki/api.js @@ -9,18 +9,21 @@ } async function broadcastOnlineStatus() { - const friendKeys = await window.Signal.Data.getAllFriendIds(); + const friendKeys = await window.Signal.Data.getPubKeysWithFriendStatus( + friendRequestStatusEnum.friends + ); friendKeys.forEach(pubKey => { - sendOnlineBroadcastMessage(pubKey) + sendOnlineBroadcastMessage(pubKey); }); } async function sendOnlineBroadcastMessage(pubKey) { - const onlineBroadcastMessage = new textsecure.protobuf.OnlineBroadcastMessage({ - snappAddress: 'testAddress', - port: parseInt(window.localServerPort, 10), - timestamp: Date.now(), - }); + const onlineBroadcastMessage = new textsecure.protobuf.OnlineBroadcastMessage( + { + p2pAddress: 'testAddress', + p2pPort: parseInt(window.localServerPort, 10), + } + ); const content = new textsecure.protobuf.Content({ onlineBroadcastMessage, }); @@ -88,10 +91,28 @@ } } + // Possible conversation friend states + const friendRequestStatusEnum = Object.freeze({ + // New conversation, no messages sent or received + none: 0, + // This state is used to lock the input early while sending + pendingSend: 1, + // Friend request sent, awaiting response + requestSent: 2, + // Friend request received, awaiting user input + requestReceived: 3, + // We did it! + friends: 4, + }); + window.libloki.api = { sendFriendRequestAccepted, sendEmptyMessage, sendOnlineBroadcastMessage, broadcastOnlineStatus, }; + + window.libloki.friends = { + friendRequestStatusEnum, + }; })(); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index a076a52cf..9b162ff2f 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -82,13 +82,11 @@ MessageReceiver.prototype.extend({ } }); - this.localServer - .start(window.localServerPort) - .then(port => { - window.log.info(`Local Server started at localhost:${port}`); - window.libloki.api.broadcastOnlineStatus(); - this.localServer.on('message', this.httpPollingResource.handleMessage); - }); + this.localServer.start(window.localServerPort).then(port => { + window.log.info(`Local Server started at localhost:${port}`); + window.libloki.api.broadcastOnlineStatus(); + this.localServer.on('message', this.httpPollingResource.handleMessage); + }); // TODO: Rework this socket stuff to work with online messaging const useWebSocket = false; @@ -133,7 +131,10 @@ MessageReceiver.prototype.extend({ } if (this.localServer) { - this.localServer.removeListener('message', this.httpPollingResource.handleMessage); + this.localServer.removeListener( + 'message', + this.httpPollingResource.handleMessage + ); this.localServer = null; } }, @@ -712,7 +713,10 @@ MessageReceiver.prototype.extend({ .then(handleSessionReset); break; case textsecure.protobuf.Envelope.Type.ONLINE_BROADCAST: - window.log.info('Online broadcast message from', this.getEnvelopeId(envelope)); + window.log.info( + 'Online broadcast message from', + this.getEnvelopeId(envelope) + ); promise = captureActiveSession() .then(() => sessionCipher.decryptWhisperMessage(ciphertext)) .then(this.unpad) @@ -904,7 +908,13 @@ MessageReceiver.prototype.extend({ }) ); }, - handleOnlineBroadcastMessage(envelope, onlineBroadcastMessage) { + async handleOnlineBroadcastMessage(envelope, onlineBroadcastMessage) { + const { p2pAddress, p2pPort } = onlineBroadcastMessage; + window.LokiP2pAPI.addContactP2pDetails( + envelope.source, + p2pAddress, + p2pPort + ); return this.removeFromCache(envelope); }, handleDataMessage(envelope, msg) { @@ -1022,7 +1032,10 @@ MessageReceiver.prototype.extend({ content.preKeyBundleMessage ); if (content.onlineBroadcastMessage) - return this.handleOnlineBroadcastMessage(envelope, content.onlineBroadcastMessage); + return this.handleOnlineBroadcastMessage( + envelope, + content.onlineBroadcastMessage + ); if (content.syncMessage) return this.handleSyncMessage(envelope, content.syncMessage); if (content.dataMessage) diff --git a/preload.js b/preload.js index 78e221457..67affe39b 100644 --- a/preload.js +++ b/preload.js @@ -276,6 +276,10 @@ window.LokiSnodeAPI = new LokiSnodeAPI({ swarmServerPort: config.swarmServerPort, }); +const { LokiP2pAPI } = require('./js/modules/loki_p2p_api'); + +window.LokiP2pAPI = new LokiP2pAPI(); + const { LokiMessageAPI } = require('./js/modules/loki_message_api'); window.LokiMessageAPI = new LokiMessageAPI({ diff --git a/protos/SignalService.proto b/protos/SignalService.proto index aef66ec6d..4c15268c9 100644 --- a/protos/SignalService.proto +++ b/protos/SignalService.proto @@ -40,8 +40,8 @@ message Content { } message OnlineBroadcastMessage { - optional string snappAddress = 1; - optional uint32 port = 2; + optional string p2pAddress = 1; + optional uint32 p2pPort = 2; } message PreKeyBundleMessage {