seed backup event

This commit is contained in:
Daniel Lugo 2020-01-24 19:50:31 -04:00
parent f7dc0bd1df
commit 6b6e7ce240
3 changed files with 56 additions and 2 deletions

View file

@ -309,6 +309,7 @@ class Mediator {
socket.on(Event.ON_RECEIVED_REQUESTS, this.onReceivedRequests) socket.on(Event.ON_RECEIVED_REQUESTS, this.onReceivedRequests)
socket.on(Event.ON_SENT_REQUESTS, this.onSentRequests) socket.on(Event.ON_SENT_REQUESTS, this.onSentRequests)
socket.on(Event.ON_BIO, this.onBio) socket.on(Event.ON_BIO, this.onBio)
socket.on(Event.ON_SEED_BACKUP, this.onSeedBackup)
socket.on(IS_GUN_AUTH, this.isGunAuth) 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
})
}
}
} }
/** /**

View file

@ -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 = { module.exports = {
__onSentRequestToUser, __onSentRequestToUser,
__onUserToIncoming, __onUserToIncoming,
@ -1242,5 +1267,6 @@ module.exports = {
onChats, onChats,
onSimplerReceivedRequests, onSimplerReceivedRequests,
onSimplerSentRequests, onSimplerSentRequests,
onBio onBio,
onSeedBackup
} }

View file

@ -6,7 +6,8 @@ const Events = {
ON_HANDSHAKE_ADDRESS: "ON_HANDSHAKE_ADDRESS", ON_HANDSHAKE_ADDRESS: "ON_HANDSHAKE_ADDRESS",
ON_RECEIVED_REQUESTS: "ON_RECEIVED_REQUESTS", ON_RECEIVED_REQUESTS: "ON_RECEIVED_REQUESTS",
ON_SENT_REQUESTS: "ON_SENT_REQUESTS", ON_SENT_REQUESTS: "ON_SENT_REQUESTS",
ON_BIO: "ON_BIO" ON_BIO: "ON_BIO",
ON_SEED_BACKUP: "ON_SEED_BACKUP",
}; };
module.exports = Events; module.exports = Events;