diff --git a/services/gunDB/contact-api/jobs/onAcceptedRequests.js b/services/gunDB/contact-api/jobs/onAcceptedRequests.js index 958c5401..44832195 100644 --- a/services/gunDB/contact-api/jobs/onAcceptedRequests.js +++ b/services/gunDB/contact-api/jobs/onAcceptedRequests.js @@ -1,6 +1,8 @@ /** * @format */ +const logger = require('winston') + const ErrorCode = require('../errorCode') const Key = require('../key') const Schema = require('../schema') @@ -12,6 +14,8 @@ const Utils = require('../utils') * @typedef {import('../SimpleGUN').UserGUNNode} UserGUNNode */ +let procid = 0 + /** * @throws {Error} NOT_AUTH * @param {UserGUNNode} user @@ -23,17 +27,16 @@ const onAcceptedRequests = (user, SEA) => { throw new Error(ErrorCode.NOT_AUTH) } - const mySecret = require('../../Mediator').getMySecret() - - if (typeof mySecret !== 'string') { - console.log("Jobs.onAcceptedRequests() -> typeof mySecret !== 'string'") - return - } + procid++ user .get(Key.STORED_REQS) .map() .once(async (storedReq, id) => { + logger.info( + `------------------------------------\nPROCID:${procid}\n---------------------------------------` + ) + const mySecret = require('../../Mediator').getMySecret() try { if (!Schema.isStoredRequest(storedReq)) { throw new TypeError( @@ -70,99 +73,101 @@ const onAcceptedRequests = (user, SEA) => { return } + const gun = require('../../Mediator').getGun() + const user = require('../../Mediator').getUser() + const recipientEpub = await Utils.pubToEpub(recipientPub) const ourSecret = await SEA.secret(recipientEpub, user._.sea) - if (typeof ourSecret !== 'string') { - throw new TypeError("typeof ourSecret !== 'string'") - } - - await Utils.tryAndWait( - (gun, user) => - new Promise((res, rej) => { - gun - .get(Key.HANDSHAKE_NODES) - .get(requestAddress) - .get(sentReqID) - .on(async sentReq => { - if (!Schema.isHandshakeRequest(sentReq)) { - rej( - new Error( - 'sent request found in handshake node not a handshake request' - ) - ) - return - } - - // The response can be decrypted with the same secret regardless of who - // wrote to it last (see HandshakeRequest definition). - // This could be our feed ID for the recipient, or the recipient's feed - // id if he accepted the request. - const feedID = await SEA.decrypt(sentReq.response, ourSecret) - - if (typeof feedID !== 'string') { - throw new TypeError("typeof feedID !== 'string'") - } - - const maybeFeedOnRecipientsOutgoings = await Utils.tryAndWait( - gun => - new Promise(res => { - gun - .user(recipientPub) - .get(Key.OUTGOINGS) - .get(feedID) - .once(feed => { - res(feed) - }) - }), - // retry on undefined, might be a false negative - v => typeof v === 'undefined' + await new Promise((res, rej) => { + gun + .get(Key.HANDSHAKE_NODES) + .get(requestAddress) + .get(sentReqID) + .on(async sentReq => { + if (!Schema.isHandshakeRequest(sentReq)) { + rej( + new Error( + 'sent request found in handshake node not a handshake request' ) + ) + return + } - const feedIDExistsOnRecipientsOutgoings = - typeof maybeFeedOnRecipientsOutgoings === 'object' && - maybeFeedOnRecipientsOutgoings !== null + // The response can be decrypted with the same secret regardless of who + // wrote to it last (see HandshakeRequest definition). + // This could be our feed ID for the recipient, or the recipient's feed + // id if he accepted the request. + const feedID = await SEA.decrypt(sentReq.response, ourSecret) - if (!feedIDExistsOnRecipientsOutgoings) { - return - } + if (typeof feedID !== 'string') { + throw new TypeError("typeof feedID !== 'string'") + } - const encryptedForMeIncomingID = await SEA.encrypt( - feedID, - mySecret - ) + logger.info(`onAcceptedRequests -> decrypted feed ID: ${feedID}`) - await new Promise((res, rej) => { - user - .get(Key.USER_TO_INCOMING) - .get(recipientPub) - .put(encryptedForMeIncomingID, ack => { - if (ack.err) { - rej(new Error(ack.err)) - } else { - res() - } + logger.info( + 'Will now try to access the other users outgoing feed' + ) + + const maybeFeedOnRecipientsOutgoings = await Utils.tryAndWait( + gun => + new Promise(res => { + gun + .user(recipientPub) + .get(Key.OUTGOINGS) + .get(feedID) + .once(feed => { + res(feed) }) - }) + }), + // retry on undefined, might be a false negative + v => typeof v === 'undefined' + ) - await new Promise((res, rej) => { - user - .get(Key.STORED_REQS) - .get(id) - .put(null, ack => { - if (ack.err) { - rej(new Error(ack.err)) - } else { - res() - } - }) - }) + const feedIDExistsOnRecipientsOutgoings = + typeof maybeFeedOnRecipientsOutgoings === 'object' && + maybeFeedOnRecipientsOutgoings !== null - // ensure this listeners gets called at least once - res() - }) + if (!feedIDExistsOnRecipientsOutgoings) { + return + } + + const encryptedForMeIncomingID = await SEA.encrypt( + feedID, + mySecret + ) + + await new Promise((res, rej) => { + user + .get(Key.USER_TO_INCOMING) + .get(recipientPub) + .put(encryptedForMeIncomingID, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) + + await new Promise((res, rej) => { + user + .get(Key.STORED_REQS) + .get(id) + .put(null, ack => { + if (ack.err) { + rej(new Error(ack.err)) + } else { + res() + } + }) + }) + + // ensure this listeners gets called at least once + res() }) - ) + }) } catch (err) { console.warn(`Jobs.onAcceptedRequests() -> ${err.message}`) console.log(err)