From e5f458fd79f1f6df67cb4e2fe2bc6c3ffa96032c Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 11 Nov 2020 17:50:28 -0400 Subject: [PATCH 1/3] contact sockets --- src/sockets.js | 140 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/src/sockets.js b/src/sockets.js index e09da0be..432ee300 100644 --- a/src/sockets.js +++ b/src/sockets.js @@ -16,6 +16,7 @@ const { isAuthenticated } = require('../services/gunDB/Mediator') const { deepDecryptIfNeeded } = require('../services/gunDB/rpc') +const GunEvents = require('../services/gunDB/contact-api/events') /** * @typedef {import('../services/gunDB/Mediator').SimpleSocket} SimpleSocket * @typedef {import('../services/gunDB/contact-api/SimpleGUN').ValidDataValue} ValidDataValue @@ -491,5 +492,144 @@ module.exports = ( } ) + // TODO: do this through rpc + + const emptyUnsub = () => {} + + let chatsUnsub = emptyUnsub + + io.of('chats').on('connect', async socket => { + try { + if (!isAuthenticated()) { + logger.info( + 'not authenticated in gun for chats socket, will send NOT_AUTH' + ) + socket.emit(Common.Constants.ErrorCode.NOT_AUTH) + + return + } + + logger.info('now checking token for chats socket') + const { token } = socket.handshake.query + const isAuth = await isValidToken(token) + + if (!isAuth) { + logger.warn('invalid token for chats socket') + socket.emit(Common.Constants.ErrorCode.NOT_AUTH) + return + } + + if (chatsUnsub !== emptyUnsub) { + logger.error( + 'Tried to set chats socket twice, this might be due to an app restart and the old socket not being recycled by socket.io in time, will disable the older subscription, which means the old socket wont work and data will be sent to this new socket instead' + ) + chatsUnsub() + chatsUnsub = emptyUnsub + } + + GunEvents.onChats(chats => { + socket.emit(Common.Constants.Event.ON_CHATS, chats) + }) + + socket.on('disconnect', () => { + chatsUnsub() + chatsUnsub = emptyUnsub + }) + } catch (e) { + logger.error('Error inside chats socket connect: ' + e.message) + socket.emit('$error', e.message) + } + }) + + let sentReqsUnsub = emptyUnsub + + io.of('sentReqs').on('connect', async socket => { + try { + if (!isAuthenticated()) { + logger.info( + 'not authenticated in gun for sentReqs socket, will send NOT_AUTH' + ) + socket.emit(Common.Constants.ErrorCode.NOT_AUTH) + + return + } + + logger.info('now checking token for sentReqs socket') + const { token } = socket.handshake.query + const isAuth = await isValidToken(token) + + if (!isAuth) { + logger.warn('invalid token for sentReqs socket') + socket.emit(Common.Constants.ErrorCode.NOT_AUTH) + return + } + + if (sentReqsUnsub !== emptyUnsub) { + logger.error( + 'Tried to set sentReqs socket twice, this might be due to an app restart and the old socket not being recycled by socket.io in time, will disable the older subscription, which means the old socket wont work and data will be sent to this new socket instead' + ) + sentReqsUnsub() + sentReqsUnsub = emptyUnsub + } + + GunEvents.onSimplerSentRequests(sentReqs => { + socket.emit(Common.Constants.Event.ON_SENT_REQUESTS, sentReqs) + }) + + socket.on('disconnect', () => { + sentReqsUnsub() + sentReqsUnsub = emptyUnsub + }) + } catch (e) { + logger.error('Error inside sentReqs socket connect: ' + e.message) + socket.emit('$error', e.message) + } + }) + + let receivedReqsUnsub = emptyUnsub + + io.of('receivedReqs').on('connect', async socket => { + try { + if (!isAuthenticated()) { + logger.info( + 'not authenticated in gun for receivedReqs socket, will send NOT_AUTH' + ) + socket.emit(Common.Constants.ErrorCode.NOT_AUTH) + + return + } + + logger.info('now checking token for receivedReqs socket') + const { token } = socket.handshake.query + const isAuth = await isValidToken(token) + + if (!isAuth) { + logger.warn('invalid token for receivedReqs socket') + socket.emit(Common.Constants.ErrorCode.NOT_AUTH) + return + } + + if (receivedReqsUnsub !== emptyUnsub) { + logger.error( + 'Tried to set receivedReqs socket twice, this might be due to an app restart and the old socket not being recycled by socket.io in time, will disable the older subscription, which means the old socket wont work and data will be sent to this new socket instead' + ) + receivedReqsUnsub() + receivedReqsUnsub = emptyUnsub + } + + GunEvents.onSimplerReceivedRequests(receivedReqs => { + socket.emit(Common.Constants.Event.ON_RECEIVED_REQUESTS, receivedReqs) + }) + + socket.on('disconnect', () => { + receivedReqsUnsub() + receivedReqsUnsub = emptyUnsub + }) + } catch (e) { + logger.error('Error inside receivedReqs socket connect: ' + e.message) + socket.emit('$error', e.message) + } + }) + return io } From 0a765ce2d7ddc2d43ac707089354eba29790d0e6 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 12 Nov 2020 14:28:31 -0400 Subject: [PATCH 2/3] event name --- src/sockets.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/sockets.js b/src/sockets.js index 432ee300..a5dd5f9a 100644 --- a/src/sockets.js +++ b/src/sockets.js @@ -527,8 +527,8 @@ module.exports = ( chatsUnsub = emptyUnsub } - GunEvents.onChats(chats => { - socket.emit(Common.Constants.Event.ON_CHATS, chats) + chatsUnsub = GunEvents.onChats(chats => { + socket.emit('$shock', chats) }) socket.on('disconnect', () => { @@ -572,8 +572,8 @@ module.exports = ( sentReqsUnsub = emptyUnsub } - GunEvents.onSimplerSentRequests(sentReqs => { - socket.emit(Common.Constants.Event.ON_SENT_REQUESTS, sentReqs) + sentReqsUnsub = GunEvents.onSimplerSentRequests(sentReqs => { + socket.emit('$shock', sentReqs) }) socket.on('disconnect', () => { @@ -617,8 +617,8 @@ module.exports = ( receivedReqsUnsub = emptyUnsub } - GunEvents.onSimplerReceivedRequests(receivedReqs => { - socket.emit(Common.Constants.Event.ON_RECEIVED_REQUESTS, receivedReqs) + receivedReqsUnsub = GunEvents.onSimplerReceivedRequests(receivedReqs => { + socket.emit('$shock', receivedReqs) }) socket.on('disconnect', () => { From 2a04702f42caf1c197e8d34c881b6ce160952038 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Thu, 12 Nov 2020 17:16:05 -0400 Subject: [PATCH 3/3] lighten events --- src/sockets.js | 91 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 9 deletions(-) diff --git a/src/sockets.js b/src/sockets.js index a5dd5f9a..bdddc64a 100644 --- a/src/sockets.js +++ b/src/sockets.js @@ -527,9 +527,37 @@ module.exports = ( chatsUnsub = emptyUnsub } - chatsUnsub = GunEvents.onChats(chats => { - socket.emit('$shock', chats) - }) + /** + * @param {Common.Schema.Chat[]} chats + */ + const onChats = chats => { + const processed = chats.map( + ({ + didDisconnect, + id, + lastSeenApp, + messages, + recipientPublicKey + }) => { + /** @type {Common.Schema.Chat} */ + const stripped = { + didDisconnect, + id, + lastSeenApp, + messages, + recipientAvatar: null, + recipientDisplayName: null, + recipientPublicKey + } + + return stripped + } + ) + + socket.emit('$shock', processed) + } + + chatsUnsub = GunEvents.onChats(onChats) socket.on('disconnect', () => { chatsUnsub() @@ -572,9 +600,36 @@ module.exports = ( sentReqsUnsub = emptyUnsub } - sentReqsUnsub = GunEvents.onSimplerSentRequests(sentReqs => { - socket.emit('$shock', sentReqs) - }) + /** + * @param {Common.Schema.SimpleSentRequest[]} sentReqs + */ + const onSentReqs = sentReqs => { + const processed = sentReqs.map( + ({ + id, + recipientChangedRequestAddress, + recipientPublicKey, + timestamp + }) => { + /** + * @type {Common.Schema.SimpleSentRequest} + */ + const stripped = { + id, + recipientAvatar: null, + recipientChangedRequestAddress, + recipientDisplayName: null, + recipientPublicKey, + timestamp + } + + return stripped + } + ) + socket.emit('$shock', processed) + } + + sentReqsUnsub = GunEvents.onSimplerSentRequests(onSentReqs) socket.on('disconnect', () => { sentReqsUnsub() @@ -617,9 +672,27 @@ module.exports = ( receivedReqsUnsub = emptyUnsub } - receivedReqsUnsub = GunEvents.onSimplerReceivedRequests(receivedReqs => { - socket.emit('$shock', receivedReqs) - }) + /** + * @param {Common.Schema.SimpleReceivedRequest[]} receivedReqs + */ + const onReceivedReqs = receivedReqs => { + const processed = receivedReqs.map(({ id, requestorPK, timestamp }) => { + /** @type {Common.Schema.SimpleReceivedRequest} */ + const stripped = { + id, + requestorAvatar: null, + requestorDisplayName: null, + requestorPK, + timestamp + } + + return stripped + }) + + socket.emit('$shock', processed) + } + + receivedReqsUnsub = GunEvents.onSimplerReceivedRequests(onReceivedReqs) socket.on('disconnect', () => { receivedReqsUnsub()