From b87bcb6bcb77ea0a4268df4f6d5b0dbac45ca990 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Sun, 13 Jun 2021 12:21:00 -0400 Subject: [PATCH] No hardcoded events in gun socket rpc --- services/gunDB/sockets/index.js | 210 +------------------------------- 1 file changed, 3 insertions(+), 207 deletions(-) diff --git a/services/gunDB/sockets/index.js b/services/gunDB/sockets/index.js index 4aa27d73..1166df21 100644 --- a/services/gunDB/sockets/index.js +++ b/services/gunDB/sockets/index.js @@ -9,14 +9,16 @@ const uuidv4 = require('uuid/v4') const { getGun, getUser, isAuthenticated } = require('../Mediator') const { deepDecryptIfNeeded } = require('../rpc') const Subscriptions = require('./subscriptions') +<<<<<<< HEAD const GunEvents = require('../contact-api/events') const GunActions = require('../../gunDB/contact-api/actions') +======= +>>>>>>> a8235aa... No hardcoded events in gun socket rpc const { encryptedEmit, encryptedOn, encryptedCallback } = require('../../../utils/ECC/socket') -const auth = require('../../auth/auth') const ALLOWED_GUN_METHODS = [ 'map', @@ -37,28 +39,6 @@ const ALLOWED_GUN_METHODS = [ * @typedef {{ reconnect: boolean, token: string }} SubscriptionOptions */ -/** - * @param {string} token - * @returns {Promise} - */ -const isValidToken = async token => { - const validation = await auth.validateToken(token) - - if (typeof validation !== 'object') { - return false - } - - if (validation === null) { - return false - } - - if (typeof validation.valid !== 'boolean') { - return false - } - - return validation.valid -} - /** * @param {string} root */ @@ -189,84 +169,6 @@ const queryListenerCallback = ({ } } -/** - * @param {Object} GunSocketOptions - * @param {() => (import('./subscriptions').Unsubscribe | void)} GunSocketOptions.handler - * @param {string} GunSocketOptions.subscriptionId - * @param {string} GunSocketOptions.encryptionId - * @param {import('socket.io').Socket} GunSocketOptions.socket - * @returns {(options: SubscriptionOptions, response: (error?: any, data?: any) => void) => Promise} - */ -const wrap = ({ handler, subscriptionId, encryptionId, socket }) => async ( - { reconnect, token }, - response -) => { - try { - logger.info('Subscribe function executing...') - if (!isAuthenticated()) { - logger.warn('GunDB is not yet authenticated') - socket.emit(Common.Constants.ErrorCode.NOT_AUTH) - return - } - - const callback = encryptedCallback(socket, response) - const emit = encryptedEmit(socket) - const subscription = Subscriptions.get({ - deviceId: encryptionId, - subscriptionId - }) - - if (subscription && !reconnect) { - const error = { - field: 'subscription', - message: - "You're already subscribed to this event, you can re-subscribe again by setting 'reconnect' to true " - } - logger.error('Duplicate subscription:', error) - callback(error) - return - } - - if (subscription && reconnect) { - if (subscription.unsubscribe) { - subscription.unsubscribe() - } - - Subscriptions.remove({ - deviceId: encryptionId, - subscriptionId - }) - } - - if (!subscription || reconnect) { - const isAuth = await isValidToken(token) - - if (!isAuth) { - logger.warn('invalid token specified') - emit(Common.Constants.ErrorCode.NOT_AUTH) - return - } - - const unsubscribe = handler() - - if (unsubscribe) { - Subscriptions.attachUnsubscribe({ - deviceId: encryptionId, - subscriptionId, - unsubscribe - }) - } - } - - callback(null, { - message: 'Subscribed successfully!', - success: true - }) - } catch (error) { - logger.error('Socket wrapper error:', error) - } -} - /** @param {import('socket.io').Socket} socket */ const startSocket = socket => { try { @@ -331,112 +233,6 @@ const startSocket = socket => { } }) - const onChats = () => { - return GunEvents.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 - } - ) - - emit('chats', processed) - }) - } - - on( - 'subscribe:chats', - wrap({ - handler: onChats, - encryptionId, - subscriptionId: 'chats', - socket - }) - ) - - const onSentRequests = () => { - return GunEvents.onSimplerSentRequests(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 - } - ) - emit('sentRequests', processed) - }) - } - - on( - 'subscribe:sentRequests', - wrap({ - handler: onSentRequests, - encryptionId, - subscriptionId: 'sentRequests', - socket - }) - ) - - const onReceivedRequests = () => { - return GunEvents.onSimplerReceivedRequests(receivedReqs => { - const processed = receivedReqs.map(({ id, requestorPK, timestamp }) => { - /** @type {Common.Schema.SimpleReceivedRequest} */ - const stripped = { - id, - requestorAvatar: null, - requestorDisplayName: null, - requestorPK, - timestamp - } - - return stripped - }) - - emit('receivedRequests', processed) - }) - } - - on( - 'subscribe:receivedRequests', - wrap({ - handler: onReceivedRequests, - encryptionId, - subscriptionId: 'receivedRequests', - socket - }) - ) - on('unsubscribe', ({ subscriptionId }, response) => { const callback = encryptedCallback(socket, response) Subscriptions.remove({ deviceId: encryptionId, subscriptionId })