From 3d685eb7142d633d1306bc036891e6eec8a74f93 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 29 Jan 2020 19:19:02 -0400 Subject: [PATCH] log --- services/gunDB/contact-api/streams/index.js | 147 +++++++++++--------- 1 file changed, 80 insertions(+), 67 deletions(-) diff --git a/services/gunDB/contact-api/streams/index.js b/services/gunDB/contact-api/streams/index.js index ce5502d3..65fdbe5a 100644 --- a/services/gunDB/contact-api/streams/index.js +++ b/services/gunDB/contact-api/streams/index.js @@ -120,6 +120,8 @@ const notifyIncomingsListeners = () => { /** @type {Set} */ const pubFeedPairsWithIncomingListeners = new Set() +let subbed = false + /** * @param {IncomingsListener} cb */ @@ -129,81 +131,92 @@ const onIncoming = cb => { const user = require('../../Mediator').getUser() const SEA = require('../../Mediator').mySEA - user.get(Key.USER_TO_INCOMING).open(uti => { - if (typeof uti !== 'object' || uti === null) { - return - } - - Object.entries(uti).forEach(async ([pub, encFeed]) => { - if (typeof encFeed !== 'string') { + if (!subbed) { + user.get(Key.USER_TO_INCOMING).open(uti => { + if (typeof uti !== 'object' || uti === null) { return } - const ourSecret = await SEA.secret(await Utils.pubToEpub(pub), user._.sea) - const mySecret = await Utils.mySecret() - const feed = await SEA.decrypt(encFeed, mySecret) + Object.entries(uti).forEach(async ([pub, encFeed]) => { + if (typeof encFeed !== 'string') { + return + } + const ourSecret = await SEA.secret( + await Utils.pubToEpub(pub), + user._.sea + ) + const mySecret = await Utils.mySecret() - if (pubFeedPairsWithIncomingListeners.add(pub + '--' + feed)) { - require('../../Mediator') - .getGun() - .user(pub) - .get(Key.OUTGOINGS) - .get(feed) - .open(async data => { - if (data === null) { - pubToIncoming[pub] = null - return - } + const feed = await SEA.decrypt(encFeed, mySecret) - if (typeof data !== 'object') { - return - } - - if (typeof data.with !== 'string') { - return - } - - if (typeof data.messages !== 'object') { - return - } - - if (data.messages === null) { - return - } - - const msgs = /** @type {[string, Schema.Message][]} */ (Object.entries( - data.messages - ).filter(([_, msg]) => Schema.isMessage(msg))) - - // eslint-disable-next-line require-atomic-updates - pubToIncoming[pub] = await Utils.asyncMap( - msgs, - async ([msgid, msg]) => { - let decryptedBody = '' - - if (msg.body === INITIAL_MSG) { - decryptedBody = INITIAL_MSG - } else { - decryptedBody = await SEA.decrypt(msg.body, ourSecret) - } - - /** @type {Schema.ChatMessage} */ - const finalMsg = { - body: decryptedBody, - id: msgid, - outgoing: false, - timestamp: msg.timestamp - } - - return finalMsg + if (pubFeedPairsWithIncomingListeners.add(pub + '--' + feed)) { + require('../../Mediator') + .getGun() + .user(pub) + .get(Key.OUTGOINGS) + .get(feed) + .open(async data => { + if (data === null) { + pubToIncoming[pub] = null + return } - ) - notifyIncomingsListeners() - }) - } + if (typeof data !== 'object') { + return + } + + if (typeof data.with !== 'string') { + return + } + + if (typeof data.messages !== 'object') { + return + } + + if (data.messages === null) { + return + } + + const msgs = /** @type {[string, Schema.Message][]} */ (Object.entries( + data.messages + ).filter(([_, msg]) => Schema.isMessage(msg))) + + // eslint-disable-next-line require-atomic-updates + pubToIncoming[pub] = await Utils.asyncMap( + msgs, + async ([msgid, msg]) => { + let decryptedBody = '' + + if (msg.body === INITIAL_MSG) { + decryptedBody = INITIAL_MSG + } else { + decryptedBody = await SEA.decrypt(msg.body, ourSecret) + } + + /** @type {Schema.ChatMessage} */ + const finalMsg = { + body: decryptedBody, + id: msgid, + outgoing: false, + timestamp: msg.timestamp + } + + return finalMsg + } + ) + + console.log('--------------------------------') + console.log(`msgs: ${JSON.stringify(msgs)}`) + console.log('--------------------------------') + + notifyIncomingsListeners() + }) + } + }) }) - }) + + subbed = true + } return () => { incomingsListeners.delete(cb)