From 45d6bd109902f9c960a24edd2fb5db052e7ad5f3 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sat, 25 Jan 2020 15:07:42 -0400 Subject: [PATCH] disconnect action --- services/gunDB/contact-api/actions.js | 53 ++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index e8c42a51..6ee5d514 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -10,6 +10,7 @@ const ErrorCode = require('./errorCode') const Getters = require('./getters') const Key = require('./key') const Utils = require('./utils') +const { promisifyGunNode: p } = Utils const { isHandshakeRequest } = require('./schema') /** * @typedef {import('./SimpleGUN').GUNNode} GUNNode @@ -1047,6 +1048,55 @@ const saveSeedBackup = async (mnemonicPhrase, user, SEA) => { }) } +/** + * @param {string} pub + * @returns {Promise} + */ +const disconnect = async pub => { + const user = p(getUser()) + if (!(await Utils.successfulHandshakeAlreadyExists(pub))) { + throw new Error('No handshake exists for this pub') + } + + const outGoingID = /** @type {string} */ (await Utils.recipientToOutgoingID( + pub + )) + + await user + .get(Key.USER_TO_INCOMING) + .get(pub) + .put(null) + + await user + .get(Key.RECIPIENT_TO_OUTGOING) + .get(pub) + .put(null) + + await user + .get(Key.USER_TO_LAST_REQUEST_SENT) + .get(pub) + .put(null) + + const msgs = getUser() + .get(Key.OUTGOINGS) + .get(outGoingID) + .get(Key.MESSAGES) + + msgs + .once() + .map() + .once((_, key) => { + msgs.get(key).put(null) + }) + + // give it a bit of time so it can delete the messages + return new Promise(res => { + setTimeout(() => { + res() + }, 500) + }) +} + module.exports = { INITIAL_MSG, __createOutgoingFeed, @@ -1063,5 +1113,6 @@ module.exports = { sendPayment, generateOrderAddress, setBio, - saveSeedBackup + saveSeedBackup, + disconnect }