From 6b6e7ce24010672cb6d71260b6cab522b051f8eb Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Fri, 24 Jan 2020 19:50:31 -0400 Subject: [PATCH] seed backup event --- services/gunDB/Mediator/index.js | 27 +++++++++++++++++++++++++++ services/gunDB/contact-api/events.js | 28 +++++++++++++++++++++++++++- services/gunDB/event-constants.js | 3 ++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 32ac03d5..2d765d67 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -309,6 +309,7 @@ class Mediator { socket.on(Event.ON_RECEIVED_REQUESTS, this.onReceivedRequests) socket.on(Event.ON_SENT_REQUESTS, this.onSentRequests) socket.on(Event.ON_BIO, this.onBio) + socket.on(Event.ON_SEED_BACKUP, this.onSeedBackup) socket.on(IS_GUN_AUTH, this.isGunAuth) } @@ -943,6 +944,32 @@ class Mediator { }) } } + + /** + * @param {Readonly<{ token: string }>} body + */ + onSeedBackup = async body => { + try { + const { token } = body + + await throwOnInvalidToken(token) + + API.Events.onSeedBackup(seedBackup => { + this.socket.emit(Event.ON_SEED_BACKUP, { + ok: true, + msg: seedBackup, + origBody: body + }) + }, user) + } catch (err) { + console.log(err) + this.socket.emit(Event.ON_SEED_BACKUP, { + ok: false, + msg: err.message, + origBody: body + }) + } + } } /** diff --git a/services/gunDB/contact-api/events.js b/services/gunDB/contact-api/events.js index 6b5540ee..3c25ae60 100644 --- a/services/gunDB/contact-api/events.js +++ b/services/gunDB/contact-api/events.js @@ -1230,6 +1230,31 @@ const onBio = (cb, user) => { }) } +/** @type {string|null} */ +let currentSeedBackup = null + +/** + * @param {(seedBackup: string|null) => void} cb + * @param {UserGUNNode} user + * @throws {Error} If user hasn't been auth. + * @returns {void} + */ +const onSeedBackup = (cb, user) => { + if (!user.is) { + throw new Error(ErrorCode.NOT_AUTH) + } + + const callb = debounce(cb, DEBOUNCE_WAIT_TIME) + callb(currentSeedBackup) + + user.get(Key.SEED_BACKUP).on(seedBackup => { + if (typeof seedBackup === 'string' || seedBackup === null) { + currentSeedBackup = seedBackup + callb(seedBackup) + } + }) +} + module.exports = { __onSentRequestToUser, __onUserToIncoming, @@ -1242,5 +1267,6 @@ module.exports = { onChats, onSimplerReceivedRequests, onSimplerSentRequests, - onBio + onBio, + onSeedBackup } diff --git a/services/gunDB/event-constants.js b/services/gunDB/event-constants.js index 0c9c578f..c4ffdae4 100644 --- a/services/gunDB/event-constants.js +++ b/services/gunDB/event-constants.js @@ -6,7 +6,8 @@ const Events = { ON_HANDSHAKE_ADDRESS: "ON_HANDSHAKE_ADDRESS", ON_RECEIVED_REQUESTS: "ON_RECEIVED_REQUESTS", ON_SENT_REQUESTS: "ON_SENT_REQUESTS", - ON_BIO: "ON_BIO" + ON_BIO: "ON_BIO", + ON_SEED_BACKUP: "ON_SEED_BACKUP", }; module.exports = Events;