diff --git a/Desktop/DesktopApp.js b/Desktop/DesktopApp.js index aac336353..19b8726a0 100644 --- a/Desktop/DesktopApp.js +++ b/Desktop/DesktopApp.js @@ -15,9 +15,9 @@ exports.newDesktopApp = function newDesktopApp() { Let's start the Network Interfaces */ DK.webSocketsClient = SA.projects.network.modules.webSocketsClient.newNetworkModulesWebSocketsClient() - DK.webSocketsClient.initialize() + await DK.webSocketsClient.initialize() - let request = { + let queryMessage = { equeryId: "123", queryType: 0, emitterUserProfileId: DK.TEST_NETWORK_CLIENT_USER_PROFILE_ID, @@ -25,14 +25,24 @@ exports.newDesktopApp = function newDesktopApp() { amountRequested: 10, direction: 'Past' } - DK.webSocketsClient.sendMessage( - JSON.stringify(request), - onResponse - ) - function onResponse(message) { - let profiles = JSON.parse(message) + let query = { + requestType: 'Query', + queryMessage : JSON.stringify(queryMessage ) + } + + await DK.webSocketsClient.sendMessage( + JSON.stringify(query) + ) + .then(showProfiles) + .catch(onError) + + function showProfiles(profiles){ console.log(profiles) } + + function onError(errorMessage) { + console.log('[ERROR] Query not executed. ' + errorMessage) + } } } diff --git a/Projects/Network/NT/Modules/WebSocketsInterface.js b/Projects/Network/NT/Modules/WebSocketsInterface.js index f40faaed3..003e6798b 100644 --- a/Projects/Network/NT/Modules/WebSocketsInterface.js +++ b/Projects/Network/NT/Modules/WebSocketsInterface.js @@ -94,6 +94,14 @@ exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSock } break } + default: { + let response = { + result: 'Error', + message: 'messageType Not Supported.' + } + socket.send(JSON.stringify(response)) + break + } } } catch (err) { console.log('[ERROR] Web Sockets Interface -> setUpWebSocketServer -> err.stack = ' + err.stack) diff --git a/Projects/Network/SA/Modules/WebSocketsClient.js b/Projects/Network/SA/Modules/WebSocketsClient.js index 06f6a09b1..8d47316f6 100644 --- a/Projects/Network/SA/Modules/WebSocketsClient.js +++ b/Projects/Network/SA/Modules/WebSocketsClient.js @@ -7,7 +7,6 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets } let socketClient - let callbackFunction let web3 = new SA.nodeModules.web3() let called = {} @@ -25,7 +24,7 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets called = undefined } - function initialize() { + async function initialize() { /* Here we will pick a Network Node from all users profiles available that do have a Network Node running. // TODO In the meantime, we will assume that we have chosen the following Network Node to connect to. @@ -44,203 +43,204 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets socketClient = new SA.nodeModules.ws('ws://' + selectedNetworkNode.host + ':' + selectedNetworkNode.port) - setUpWebsocketClient() + await setUpWebsocketClient() } - function setUpWebsocketClient() { - try { - let handshakeDone = false + async function setUpWebsocketClient() { - socketClient.onopen = () => { onConnection() } - socketClient.onerror = err => { onError(err) } + return new Promise(connectToNewtwork) - function onConnection() { + function connectToNewtwork(resolve, reject) { - handshakeProcedure() + try { - function handshakeProcedure() { + socketClient.onopen = () => { onConnection() } + socketClient.onerror = err => { onError(err) } - let callerTimestamp + function onConnection() { - stepOneRequest() + handshakeProcedure() - function stepOneRequest() { - /* - Send Handshake Message Step One: - - At Step One we will just say hello, identify ourselves - as a Network Client, and send the Network Node our - User Profile Handle. - - This Handle will be signed by the Network Node to prove - it's ouwn identity, and later we will sign it's own handle - to prove ours. - */ - socketClient.onmessage = socketMessage => { stepOneResponse(socketMessage) } + function handshakeProcedure() { - callerTimestamp = (new Date()).valueOf() + let callerTimestamp - let message = { - messageType: 'Handshake', - callerRole: 'Network Client', - callerProfileHandle: DK.TEST_NETWORK_CLIENT_USER_PROFILE_HANDLE, - callerTimestamp: callerTimestamp, - step: 'One' - } - socketClient.send(JSON.stringify(message)) - } + stepOneRequest() - function stepOneResponse(socketMessage) { - let response = JSON.parse(socketMessage.data) + function stepOneRequest() { + /* + Send Handshake Message Step One: + + At Step One we will just say hello, identify ourselves + as a Network Client, and send the Network Node our + User Profile Handle. + + This Handle will be signed by the Network Node to prove + it's ouwn identity, and later we will sign it's own handle + to prove ours. + */ + socketClient.onmessage = socketMessage => { stepOneResponse(socketMessage) } - if (response.result !== 'Ok') { - console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message) - return + callerTimestamp = (new Date()).valueOf() + + let message = { + messageType: 'Handshake', + callerRole: 'Network Client', + callerProfileHandle: DK.TEST_NETWORK_CLIENT_USER_PROFILE_HANDLE, + callerTimestamp: callerTimestamp, + step: 'One' + } + socketClient.send(JSON.stringify(message)) } - let signature = JSON.parse(response.signature) - called.blockchainAccount = web3.eth.accounts.recover(signature) - /* - We will check that the signature received produces a valid Blockchain Account. - */ - if (called.blockchainAccount === undefined) { - console.log('[ERROR] Web Sockets Client -> stepOneResponse -> Signature does not produce a valid Blockchain Account.') - return - } - /* - We will check that the blockchain account taken from the signature matches - the one we have on record for the user profile of the Network Node we are calling. - */ - if (called.blockchainAccount !== selectedNetworkNode.blockchainAccount) { - console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node called does not have the expected Profile Handle.') - return + function stepOneResponse(socketMessage) { + let response = JSON.parse(socketMessage.data) + + if (response.result !== 'Ok') { + console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message) + reject() + } + + let signature = JSON.parse(response.signature) + called.blockchainAccount = web3.eth.accounts.recover(signature) + /* + We will check that the signature received produces a valid Blockchain Account. + */ + if (called.blockchainAccount === undefined) { + console.log('[ERROR] Web Sockets Client -> stepOneResponse -> Signature does not produce a valid Blockchain Account.') + reject() + } + /* + We will check that the blockchain account taken from the signature matches + the one we have on record for the user profile of the Network Node we are calling. + */ + if (called.blockchainAccount !== selectedNetworkNode.blockchainAccount) { + console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node called does not have the expected Profile Handle.') + reject() + } + + let signedMessage = JSON.parse(signature.message) + + /* + We will check that the Network Node that responded has the same User Profile Handle + that we have on record, otherwise something is wrong and we should not proceed. + */ + if (signedMessage.calledProfileHandle !== selectedNetworkNode.userProfileHandle) { + console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node called does not have the expected Profile Handle.') + reject() + } + /* + We will check that the profile handle we sent to the Network Node, is returned at the + signed message, to avoid man in the middle attackts. + */ + if (signedMessage.callerProfileHandle !== DK.TEST_NETWORK_CLIENT_USER_PROFILE_HANDLE) { + console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node callerProfileHandle does not match my own userProfileHandle.') + reject() + } + /* + We will also check that the callerTimestamp we sent to the Network Node, is returned at the + signed message, also to avoid man in the middle attackts. + */ + if (signedMessage.callerTimestamp !== callerTimestamp) { + console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node callerTimestamp does not match my own callerTimestamp.') + reject() + } + + /* + All validations passed, it seems we can continue with Step Two. + */ + stepTwoRequest(signedMessage) } - let signedMessage = JSON.parse(signature.message) + function stepTwoRequest(signedMessage) { + /* + Send Handshake Message Step Two: + + Here we will sign a message with the Network Node profile + handle and timestamp to prove our own identity. + */ + socketClient.onmessage = socketMessage => { stepTwoResponse(socketMessage) } - /* - We will check that the Network Node that responded has the same User Profile Handle - that we have on record, otherwise something is wrong and we should not proceed. - */ - if (signedMessage.calledProfileHandle !== selectedNetworkNode.userProfileHandle) { - console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node called does not have the expected Profile Handle.') - return - } - /* - We will check that the profile handle we sent to the Network Node, is returned at the - signed message, to avoid man in the middle attackts. - */ - if (signedMessage.callerProfileHandle !== DK.TEST_NETWORK_CLIENT_USER_PROFILE_HANDLE) { - console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node callerProfileHandle does not match my own userProfileHandle.') - return - } - /* - We will also check that the callerTimestamp we sent to the Network Node, is returned at the - signed message, also to avoid man in the middle attackts. - */ - if (signedMessage.callerTimestamp !== callerTimestamp) { - console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node callerTimestamp does not match my own callerTimestamp.') - return + let signature = web3.eth.accounts.sign(JSON.stringify(signedMessage), DK.TEST_NETWORK_CLIENT_USER_PROFILE_PRIVATE_KEY) + + let message = { + messageType: 'Handshake', + signature: JSON.stringify(signature), + step: 'Two' + } + socketClient.send(JSON.stringify(message)) } - /* - All validations passed, it seems we can continue with Step Two. - */ - stepTwoRequest(signedMessage) - } + function stepTwoResponse(socketMessage) { + let response = JSON.parse(socketMessage.data) - function stepTwoRequest(signedMessage) { - /* - Send Handshake Message Step Two: - - Here we will sign a message with the Network Node profile - handle and timestamp to prove our own identity. - */ - socketClient.onmessage = socketMessage => { stepTwoResponse(socketMessage) } - - let signature = web3.eth.accounts.sign(JSON.stringify(signedMessage), DK.TEST_NETWORK_CLIENT_USER_PROFILE_PRIVATE_KEY) - - let message = { - messageType: 'Handshake', - signature: JSON.stringify(signature), - step: 'Two' + if (response.result !== 'Ok') { + console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message) + reject() + } + console.log('[INFO] Web Sockets Client -> stepTwoResponse -> response.message = ' + response.message) + /* + This was the end of the Handshake producere. We are connected to the + Network Node and from now on, all response messages will be received + at this following function. + */ + resolve() } - socketClient.send(JSON.stringify(message)) - } - - function stepTwoResponse(socketMessage) { - let response = JSON.parse(socketMessage.data) - - if (response.result !== 'Ok') { - console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message) - return - } - console.log('[INFO] Web Sockets Client -> stepTwoResponse -> response.message = ' + response.message) - /* - This was the end of the Handshake producere. We are connected to the - Network Node and from now on, all response messages will be received - at this following function. - */ - socketClient.onmessage = socketMessage => { onMenssage(socketMessage) } } } + + function onError(err) { + console.log('[ERROR] Web Sockets Client -> onError -> err.message = ' + err.message) + console.log('[ERROR] Web Sockets Client -> onError -> err.stack = ' + err.stack) + } + + } catch (err) { + console.log('[ERROR] Web Sockets Client -> setUpWebsocketClient -> err.stack = ' + err.stack) } - async function onMenssage(socketMessage) { + } + } + + function sendMessage(message) { + + return new Promise(sendSocketMessage) + + function sendSocketMessage(resolve, reject) { + + if (socketClient.readyState !== 1) { // 1 means connected and ready. + console.log('[ERROR] Web Sockets Client -> sendMessage -> Cannot send message while connection is closed.') + reject('Websockets Connection Not Ready.') + } + + let socketMessage = { + messageType: 'Request', + payload: message + } + socketClient.onmessage = socketMessage => { onMenssage(socketMessage) } + socketClient.send( + JSON.stringify(socketMessage) + ) + + function onMenssage(socketMessage) { try { let response = JSON.parse(socketMessage.data) /* Chack if we are waiting for the Handshake response. */ - if (handshakeDone === false) { - if (response.result === 'Ok') { - handshakeDone = true - return - } else { - console.log('[ERROR] Web Sockets Client -> onMenssage -> handshakeDone -> response.message = ' + response.message) - return - } - } - callbackFunction.receiveResponse(response.message) - callbackFunction = undefined + if (response.result === 'Ok') { + resolve(response.data) + } else { + console.log('[ERROR] Web Sockets Client -> onMenssage -> response.message = ' + response.message) + reject(response.message) + } } catch (err) { callbackFunction = undefined console.log('[ERROR] Web Sockets Client -> err.stack = ' + err.stack) } } - - function onError(err) { - console.log('[ERROR] Web Sockets Client -> onError -> err.message = ' + err.message) - console.log('[ERROR] Web Sockets Client -> onError -> err.stack = ' + err.stack) - } - - } catch (err) { - console.log('[ERROR] Web Sockets Client -> setUpWebsocketClient -> err.stack = ' + err.stack) - } - } - - function sendMessage(message, callback) { - if (callbackFunction !== undefined) { - console.log('[ERROR] Web Sockets Client -> sendMessage -> Cannot send more messages while waiting for a response.') - return false - } - - if (socketClient.readyState !== 1) { // 1 means connected and ready. - console.log('[ERROR] Web Sockets Client -> sendMessage -> Cannot send message while connection is closed.') - return false - } - - try { - callbackFunction = callback - socketClient.send(message) - return true - } catch (err) { - callbackFunction = false - console.log('[ERROR] Web Sockets Client -> sendMessage -> err.stack = ' + err.stack) - return false } } } \ No newline at end of file diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index 9f9fd3da6..f1bac7bfd 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -1400,7 +1400,7 @@ }, { "name": "Query", - "propertyName": "Query", + "propertyName": "query", "functionName": "newSocialTradingModulesQuery", "fileName": "Query.js" }, diff --git a/Projects/Social-Trading/NT/Modules/ClientInterface.js b/Projects/Social-Trading/NT/Modules/ClientInterface.js index bbfd8b8f1..9a0e7b57f 100644 --- a/Projects/Social-Trading/NT/Modules/ClientInterface.js +++ b/Projects/Social-Trading/NT/Modules/ClientInterface.js @@ -35,23 +35,23 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule return response } - if (messageHeader.messageType === undefined) { + if (messageHeader.requestType === undefined) { let response = { result: 'Error', - message: 'Client Interface messageType Not Provided.' + message: 'Client Interface requestType Not Provided.' } return response } - if (messageHeader.messageType !== 'Event' && messageHeader.messageType !== 'Query') { + if (messageHeader.requestType !== 'Event' && messageHeader.requestType !== 'Query') { let response = { result: 'Error', - message: 'Client Interface messageType Not Supported.' + message: 'Client Interface requestType Not Supported.' } return response } - switch (messageHeader.messageType) { + switch (messageHeader.requestType) { case 'Event': { return await eventReceived(messageHeader.eventMessage, userProfile) } @@ -190,9 +190,15 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule Here we will process the query. This does not change the state of the Social Graph. */ try { - let query = NT.projects.socialTrading.modules.queriesQuery.newSocialTradingModulesQuery() + let query = NT.projects.socialTrading.modules.query.newSocialTradingModulesQuery() query.initialize(queryReceived) - return query.execute() + + let response = { + result: 'Ok', + message: 'Client Interface Query Processed.', + data: query.execute() + } + return response } catch (err) { /* @@ -200,7 +206,7 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule */ let response = { result: 'Error', - message: 'Client Interface ' + err.message + message: err.message } return response } diff --git a/Projects/Social-Trading/NT/Modules/Queries/Events.js b/Projects/Social-Trading/NT/Modules/Queries/Events.js index a68d7215b..981cb9323 100644 --- a/Projects/Social-Trading/NT/Modules/Queries/Events.js +++ b/Projects/Social-Trading/NT/Modules/Queries/Events.js @@ -21,8 +21,8 @@ exports.newSocialTradingModulesQueriesEvents = function newSocialTradingModulesQ function initialize(queryReceived) { - NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) - NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) + NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, NT.memory.arrays.EVENTS) } diff --git a/Projects/Social-Trading/NT/Modules/Queries/PostReplies.js b/Projects/Social-Trading/NT/Modules/Queries/PostReplies.js index 4c0b63ecf..3a18bc421 100644 --- a/Projects/Social-Trading/NT/Modules/Queries/PostReplies.js +++ b/Projects/Social-Trading/NT/Modules/Queries/PostReplies.js @@ -8,6 +8,7 @@ exports.newSocialTradingModulesQueriesPostReplies = function newSocialTradingMod of a certain post, with all its replies. */ let thisObject = { + array: undefined, profile: undefined, post: undefined, initialIndex: undefined, @@ -21,27 +22,30 @@ exports.newSocialTradingModulesQueriesPostReplies = function newSocialTradingMod return thisObject function finalize() { + thisObject.array = undefined thisObject.profile = undefined thisObject.post = undefined } function initialize(queryReceived) { - NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) - NT.utilities.queriesValidations.postValidations(queryReceived, thisObject) - NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) + thisObject.array = Array.from(thisObject.post.replies) + + NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.projects.socialTrading.utilities.queriesValidations.postValidations(queryReceived, thisObject) + NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, thisObject.array) } function execute() { let response = [] - let array = Array.from(thisObject.post.replies) + switch (thisObject.direction) { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (post === undefined) { break } addToResponse(arrayItem) } @@ -49,7 +53,7 @@ exports.newSocialTradingModulesQueriesPostReplies = function newSocialTradingMod } case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (post === undefined) { break } addToResponse(arrayItem) } diff --git a/Projects/Social-Trading/NT/Modules/Queries/ProfileFollowers.js b/Projects/Social-Trading/NT/Modules/Queries/ProfileFollowers.js index 5b500c24c..dccdcfd2d 100644 --- a/Projects/Social-Trading/NT/Modules/Queries/ProfileFollowers.js +++ b/Projects/Social-Trading/NT/Modules/Queries/ProfileFollowers.js @@ -4,6 +4,7 @@ exports.newSocialTradingModulesQueriesProfileFollowers = function newSocialTradi all the User or Bot profiles that are following a certain User or Bot profile. */ let thisObject = { + array: undefined, profile: undefined, initialIndex: undefined, amountRequested: undefined, @@ -16,25 +17,27 @@ exports.newSocialTradingModulesQueriesProfileFollowers = function newSocialTradi return thisObject function finalize() { + thisObject.array = undefined thisObject.profile = undefined } function initialize(queryReceived) { - NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) - NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) + thisObject.array = Array.from(thisObject.profile.followers) + + NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, thisObject.array) } function execute() { let response = [] - let array = Array.from(thisObject.profile.followers) switch (thisObject.direction) { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } @@ -42,7 +45,7 @@ exports.newSocialTradingModulesQueriesProfileFollowers = function newSocialTradi } case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } diff --git a/Projects/Social-Trading/NT/Modules/Queries/ProfileFollowing.js b/Projects/Social-Trading/NT/Modules/Queries/ProfileFollowing.js index aa21b6193..a1624dedb 100644 --- a/Projects/Social-Trading/NT/Modules/Queries/ProfileFollowing.js +++ b/Projects/Social-Trading/NT/Modules/Queries/ProfileFollowing.js @@ -4,6 +4,7 @@ exports.newSocialTradingModulesQueriesProfileFollowing = function newSocialTradi all the User or Bot profiles that are being followed by a certain User or Bot profile. */ let thisObject = { + array: undefined, profile: undefined, initialIndex: undefined, amountRequested: undefined, @@ -16,25 +17,27 @@ exports.newSocialTradingModulesQueriesProfileFollowing = function newSocialTradi return thisObject function finalize() { + thisObject.array = undefined thisObject.profile = undefined } function initialize(queryReceived) { - NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) - NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) + thisObject.array = Array.from(thisObject.profile.following) + + NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, thisObject.array) } function execute() { let response = [] - let array = Array.from(thisObject.profile.following) switch (thisObject.direction) { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } @@ -42,7 +45,7 @@ exports.newSocialTradingModulesQueriesProfileFollowing = function newSocialTradi } case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } diff --git a/Projects/Social-Trading/NT/Modules/Queries/ProfilePosts.js b/Projects/Social-Trading/NT/Modules/Queries/ProfilePosts.js index c14dcd49f..45ab02b3b 100644 --- a/Projects/Social-Trading/NT/Modules/Queries/ProfilePosts.js +++ b/Projects/Social-Trading/NT/Modules/Queries/ProfilePosts.js @@ -8,6 +8,7 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi with all of its posts. */ let thisObject = { + array: undefined, profile: undefined, initialIndex: undefined, amountRequested: undefined, @@ -20,24 +21,26 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi return thisObject function finalize() { + thisObject.array = undefined thisObject.profile = undefined } function initialize(queryReceived) { - NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) - NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) + thisObject.array = Array.from(thisObject.profile.posts) + + NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, thisObject.array) } function execute() { let response = [] - let array = Array.from(thisObject.profile.posts) switch (thisObject.direction) { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } @@ -45,7 +48,7 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi } case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } diff --git a/Projects/Social-Trading/NT/Modules/Queries/UserProfiles.js b/Projects/Social-Trading/NT/Modules/Queries/UserProfiles.js index 7454cba54..f90ef2f7d 100644 --- a/Projects/Social-Trading/NT/Modules/Queries/UserProfiles.js +++ b/Projects/Social-Trading/NT/Modules/Queries/UserProfiles.js @@ -1,10 +1,11 @@ -exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradingModulesQueriesUserProfileStats() { +exports.newSocialTradingModulesQueriesUserProfiles = function newSocialTradingModulesQueriesUserProfiles() { /* This query returns a list of user profiles ordered by Ranking. It is useful to bootstrap new users and provide them with alternative of who to follow. */ let thisObject = { + array: undefined, initialIndex: undefined, amountRequested: undefined, direction: undefined, @@ -16,24 +17,29 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi return thisObject function finalize() { + thisObject.array = undefined } function initialize(queryReceived) { - NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) + thisObject.array = Array.from( + NT.projects.socialTrading.globals.memory.maps.USER_PROFILES_BY_ID, + x => x[1] + ) + thisObject.array.sort((a, b) => (a["ranking"] > b["ranking"]) ? 1 : -1) + + NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, thisObject.array) } function execute() { let response = [] - let array = Array.from(NT.projects.socialTrading.globals.memory.maps.USER_PROFILES_BY_ID) - array.sort((a, b) => (a["ranking"].toLowerCase() > b["ranking"].toLowerCase()) ? 1 : -1) switch (thisObject.direction) { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } @@ -41,7 +47,7 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi } case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { - let arrayItem = array[i] + let arrayItem = thisObject.array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } diff --git a/Projects/Social-Trading/NT/Modules/Query.js b/Projects/Social-Trading/NT/Modules/Query.js index b8e6f28f7..a45835cad 100644 --- a/Projects/Social-Trading/NT/Modules/Query.js +++ b/Projects/Social-Trading/NT/Modules/Query.js @@ -40,67 +40,61 @@ exports.newSocialTradingModulesQuery = function newSocialTradingModulesQuery() { thisObject.queryType = queryReceived.queryType switch (thisObject.queryType) { - + case QUERY_TYPES.USER_PROFILES: { thisObject.query = NT.projects.socialTrading.modules.queriesUserProfiles.newSocialTradingModulesQueriesUserProfiles() - query.initialize(queryReceived) - query.finalize() + thisObject.query.initialize(queryReceived) break } case QUERY_TYPES.USER_PROFILE_STATS: { thisObject.query = NT.projects.socialTrading.modules.queriesUserProfileStats.newSocialTradingModulesQueriesUserProfileStats() - query.initialize(queryReceived) - query.finalize() + thisObject.query.initialize(queryReceived) break } case QUERY_TYPES.BOT_PROFILE_STATS: { thisObject.query = NT.projects.socialTrading.modules.queriesBotProfileStats.newSocialTradingModulesQueriesBotProfileStats() - query.initialize(queryReceived) - query.finalize() + thisObject.query.initialize(queryReceived) break } case QUERY_TYPES.PROFILE_POSTS: { thisObject.query = NT.projects.socialTrading.modules.queriesProfilePosts.newSocialTradingModulesQueriesProfilePosts() - query.initialize(queryReceived) - query.finalize() + thisObject.query.initialize(queryReceived) break } case QUERY_TYPES.POST_REPLIES: { thisObject.query = NT.projects.socialTrading.modules.queriesPostReplies.newSocialTradingModulesQueriesPostReplies() - query.initialize(queryReceived) - query.finalize() + thisObject.query.initialize(queryReceived) break } case QUERY_TYPES.PROFILE_FOLLOWERS: { thisObject.query = NT.projects.socialTrading.modules.queriesProfileFollowers.newSocialTradingModulesQueriesProfileFollowers() - query.initialize(queryReceived) - query.finalize() + thisObject.query.initialize(queryReceived) break } case QUERY_TYPES.PROFILE_FOLLOWING: { thisObject.query = NT.projects.socialTrading.modules.queriesProfileFollowing.newSocialTradingModulesQueriesProfileFollowing() - query.initialize(queryReceived) - query.finalize() + thisObject.query.initialize(queryReceived) break } case QUERY_TYPES.maps.EVENTS: { thisObject.query = NT.projects.socialTrading.modules.queriesEvents.newSocialTradingModulesQueriesEvents() - query.initialize(queryReceived) - query.finalize() + thisObject.query.initialize(queryReceived) break } - default: throw('Query Type Not Supported.') + default: throw ('Query Type Not Supported.') } } function execute() { - return thisObject.query.execute() + let response = thisObject.query.execute() + thisObject.query.finalize() + return response } } \ No newline at end of file diff --git a/Projects/Social-Trading/NT/Utilities/QueriesValidations.js b/Projects/Social-Trading/NT/Utilities/QueriesValidations.js index 722b54835..20c00c0cf 100644 --- a/Projects/Social-Trading/NT/Utilities/QueriesValidations.js +++ b/Projects/Social-Trading/NT/Utilities/QueriesValidations.js @@ -43,7 +43,7 @@ exports.newSocialTradingUtilitiesQueriesValidations = function newSocialTradingU } } - function arrayValidations(queryReceived, thisObject) { + function arrayValidations(queryReceived, thisObject, array) { /* Validate Initial Index */ @@ -52,7 +52,7 @@ exports.newSocialTradingUtilitiesQueriesValidations = function newSocialTradingU } if (queryReceived.initialIndex === NT.projects.socialTrading.globals.queryConstants.INITIAL_INDEX_LAST) { - queryReceived.initialIndex = thisObject.profile.posts.length - 1 + queryReceived.initialIndex = array.length - 1 } if (queryReceived.initialIndex === NT.projects.socialTrading.globals.queryConstants.INITIAL_INDEX_FIRST) { @@ -82,6 +82,8 @@ exports.newSocialTradingUtilitiesQueriesValidations = function newSocialTradingU if (queryReceived.amountRequested > NT.projects.socialTrading.globals.queryConstants.MAX_AMOUNT_REQUESTED) { throw ('Amount Requested Above Max.') } + + thisObject.amountRequested = queryReceived.amountRequested /* Validate Direction */