From 865e553319d1867a58d82627234400e694a942a4 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 15 Jan 2020 23:43:04 -0400 Subject: [PATCH] bio --- services/gunDB/Mediator/index.js | 54 +++++++++++++++++++++++++++ services/gunDB/action-constants.js | 3 +- services/gunDB/contact-api/actions.js | 36 +++++++++++++++++- services/gunDB/contact-api/events.js | 29 +++++++++++++- services/gunDB/contact-api/key.js | 2 + services/gunDB/event-constants.js | 3 +- 6 files changed, 123 insertions(+), 4 deletions(-) diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 79b671cf..ef1b3d98 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -299,6 +299,7 @@ class Mediator { socket.on(Action.SEND_PAYMENT, this.sendPayment) socket.on(Action.SET_AVATAR, this.setAvatar) socket.on(Action.SET_DISPLAY_NAME, this.setDisplayName) + socket.on(Action.SET_BIO, this.setBio) socket.on(Event.ON_AVATAR, this.onAvatar) socket.on(Event.ON_BLACKLIST, this.onBlacklist) @@ -307,6 +308,7 @@ class Mediator { socket.on(Event.ON_HANDSHAKE_ADDRESS, this.onHandshakeAddress) 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(IS_GUN_AUTH, this.isGunAuth) } @@ -889,6 +891,58 @@ class Mediator { }) } } + + /** + * @param {Readonly<{ token: string }>} body + */ + onBio = async body => { + try { + const { token } = body + + await throwOnInvalidToken(token) + + API.Events.onBio(bio => { + this.socket.emit(Event.ON_BIO, { + msg: bio, + ok: true, + origBody: body + }) + }, user) + } catch (err) { + console.log(err) + this.socket.emit(Event.ON_BIO, { + ok: false, + msg: err.message, + origBody: body + }) + } + } + + /** + * @param {Readonly<{ bio: string|null , token: string }>} body + */ + setBio = async body => { + try { + const { bio, token } = body + + await throwOnInvalidToken(token) + + await API.Actions.setBio(bio, user) + + this.socket.emit(Action.SET_BIO, { + ok: true, + msg: null, + origBody: body + }) + } catch (err) { + console.log(err) + this.socket.emit(Action.SET_BIO, { + ok: false, + msg: err.message, + origBody: body + }) + } + } } /** diff --git a/services/gunDB/action-constants.js b/services/gunDB/action-constants.js index 440e7bcf..546bd5b7 100644 --- a/services/gunDB/action-constants.js +++ b/services/gunDB/action-constants.js @@ -7,7 +7,8 @@ const Actions = { SEND_MESSAGE: "SEND_MESSAGE", SEND_PAYMENT: "SEND_PAYMENT", SET_AVATAR: "SET_AVATAR", - SET_DISPLAY_NAME: "SET_DISPLAY_NAME" + SET_DISPLAY_NAME: "SET_DISPLAY_NAME", + SET_BIO: "SET_BIO" }; module.exports = Actions; diff --git a/services/gunDB/contact-api/actions.js b/services/gunDB/contact-api/actions.js index b6b7de2b..bef61340 100644 --- a/services/gunDB/contact-api/actions.js +++ b/services/gunDB/contact-api/actions.js @@ -989,6 +989,39 @@ const generateOrderAddress = user => }) }) +/** + * @param {string|null} bio + * @param {UserGUNNode} user + * @throws {TypeError} Rejects if avatar is not an string or an empty string. + * @returns {Promise} + */ +const setBio = (bio, user) => + new Promise((resolve, reject) => { + if (!user.is) { + throw new Error(ErrorCode.NOT_AUTH) + } + + if (typeof bio === 'string' && bio.length === 0) { + throw new TypeError( + "'bio' must be an string and have length greater than one or be null" + ) + } + + if (typeof bio !== 'string' && bio !== null) { + throw new TypeError( + "'bio' must be an string and have length greater than one or be null" + ) + } + + user.get(Key.BIO).put(bio, ack => { + if (ack.err) { + reject(new Error(ack.err)) + } else { + resolve() + } + }) + }) + module.exports = { INITIAL_MSG, __createOutgoingFeed, @@ -1003,5 +1036,6 @@ module.exports = { setAvatar, setDisplayName, sendPayment, - generateOrderAddress + generateOrderAddress, + setBio } diff --git a/services/gunDB/contact-api/events.js b/services/gunDB/contact-api/events.js index 175d4fb2..6b5540ee 100644 --- a/services/gunDB/contact-api/events.js +++ b/services/gunDB/contact-api/events.js @@ -1204,6 +1204,32 @@ const onSimplerSentRequests = async (cb, gun, user, SEA) => { }) } +/** @type {string|null} */ +let currentBio = null + +/** + * @param {(bio: string|null) => void} cb + * @param {UserGUNNode} user Pass only for testing purposes. + * @throws {Error} If user hasn't been auth. + * @returns {void} + */ +const onBio = (cb, user) => { + if (!user.is) { + throw new Error(ErrorCode.NOT_AUTH) + } + + const callb = debounce(cb, DEBOUNCE_WAIT_TIME) + // Initial value if avvatar is undefined in gun + callb(currentBio) + + user.get(Key.BIO).on(bio => { + if (typeof bio === 'string' || bio === null) { + currentBio = bio + callb(bio) + } + }) +} + module.exports = { __onSentRequestToUser, __onUserToIncoming, @@ -1215,5 +1241,6 @@ module.exports = { onOutgoing, onChats, onSimplerReceivedRequests, - onSimplerSentRequests + onSimplerSentRequests, + onBio } diff --git a/services/gunDB/contact-api/key.js b/services/gunDB/contact-api/key.js index 52d7de8b..2cec102c 100644 --- a/services/gunDB/contact-api/key.js +++ b/services/gunDB/contact-api/key.js @@ -30,3 +30,5 @@ exports.CURRENT_ORDER_ADDRESS = 'currentOrderAddress' exports.ORDER_NODES = 'orderNodes' exports.ORDER_TO_RESPONSE = 'orderToResponse' + +exports.BIO = 'bio' diff --git a/services/gunDB/event-constants.js b/services/gunDB/event-constants.js index 6a910689..0c9c578f 100644 --- a/services/gunDB/event-constants.js +++ b/services/gunDB/event-constants.js @@ -5,7 +5,8 @@ const Events = { ON_DISPLAY_NAME: "ON_DISPLAY_NAME", ON_HANDSHAKE_ADDRESS: "ON_HANDSHAKE_ADDRESS", ON_RECEIVED_REQUESTS: "ON_RECEIVED_REQUESTS", - ON_SENT_REQUESTS: "ON_SENT_REQUESTS" + ON_SENT_REQUESTS: "ON_SENT_REQUESTS", + ON_BIO: "ON_BIO" }; module.exports = Events;