This commit is contained in:
Daniel Lugo 2020-01-15 23:43:04 -04:00
parent 0030b62ba3
commit 865e553319
6 changed files with 123 additions and 4 deletions

View file

@ -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
})
}
}
}
/**

View file

@ -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;

View file

@ -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<void>}
*/
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
}

View file

@ -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
}

View file

@ -30,3 +30,5 @@ exports.CURRENT_ORDER_ADDRESS = 'currentOrderAddress'
exports.ORDER_NODES = 'orderNodes'
exports.ORDER_TO_RESPONSE = 'orderToResponse'
exports.BIO = 'bio'

View file

@ -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;