Superalgos Network: Query User Profiles debugged

This commit is contained in:
Luis Fernando Molina committed 2021-09-03 12:23:42 +02:00
1 parent 2645df775e
commit 67ec986422
13 files changed
+266 -227

No files matched your search

+18 -8
View File
@@ -15,9 +15,9 @@ exports.newDesktopApp = function newDesktopApp() {
Let's start the Network Interfaces Let's start the Network Interfaces
*/ */
DK.webSocketsClient = SA.projects.network.modules.webSocketsClient.newNetworkModulesWebSocketsClient() DK.webSocketsClient = SA.projects.network.modules.webSocketsClient.newNetworkModulesWebSocketsClient()
DK.webSocketsClient.initialize() await DK.webSocketsClient.initialize()
let request = { let queryMessage = {
equeryId: "123", equeryId: "123",
queryType: 0, queryType: 0,
emitterUserProfileId: DK.TEST_NETWORK_CLIENT_USER_PROFILE_ID, emitterUserProfileId: DK.TEST_NETWORK_CLIENT_USER_PROFILE_ID,
@@ -25,14 +25,24 @@ exports.newDesktopApp = function newDesktopApp() {
amountRequested: 10, amountRequested: 10,
direction: 'Past' direction: 'Past'
} }
DK.webSocketsClient.sendMessage(
JSON.stringify(request),
onResponse
)
function onResponse(message) { let query = {
let profiles = JSON.parse(message) requestType: 'Query',
queryMessage : JSON.stringify(queryMessage )
}
await DK.webSocketsClient.sendMessage(
JSON.stringify(query)
)
.then(showProfiles)
.catch(onError)
function showProfiles(profiles){
console.log(profiles) console.log(profiles)
} }
function onError(errorMessage) {
console.log('[ERROR] Query not executed. ' + errorMessage)
}
} }
} }
@@ -94,6 +94,14 @@ exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSock
} }
break break
} }
default: {
let response = {
result: 'Error',
message: 'messageType Not Supported.'
}
socket.send(JSON.stringify(response))
break
}
} }
} catch (err) { } catch (err) {
console.log('[ERROR] Web Sockets Interface -> setUpWebSocketServer -> err.stack = ' + err.stack) console.log('[ERROR] Web Sockets Interface -> setUpWebSocketServer -> err.stack = ' + err.stack)
+160 -160
View File
@@ -7,7 +7,6 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
} }
let socketClient let socketClient
let callbackFunction
let web3 = new SA.nodeModules.web3() let web3 = new SA.nodeModules.web3()
let called = {} let called = {}
@@ -25,7 +24,7 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
called = undefined 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 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. 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) socketClient = new SA.nodeModules.ws('ws://' + selectedNetworkNode.host + ':' + selectedNetworkNode.port)
setUpWebsocketClient() await setUpWebsocketClient()
} }
function setUpWebsocketClient() { async function setUpWebsocketClient() {
try {
let handshakeDone = false
socketClient.onopen = () => { onConnection() } return new Promise(connectToNewtwork)
socketClient.onerror = err => { onError(err) }
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() { function handshakeProcedure() {
/*
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) }
callerTimestamp = (new Date()).valueOf() let callerTimestamp
let message = { stepOneRequest()
messageType: 'Handshake',
callerRole: 'Network Client',
callerProfileHandle: DK.TEST_NETWORK_CLIENT_USER_PROFILE_HANDLE,
callerTimestamp: callerTimestamp,
step: 'One'
}
socketClient.send(JSON.stringify(message))
}
function stepOneResponse(socketMessage) { function stepOneRequest() {
let response = JSON.parse(socketMessage.data) /*
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') { callerTimestamp = (new Date()).valueOf()
console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message)
return 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) function stepOneResponse(socketMessage) {
called.blockchainAccount = web3.eth.accounts.recover(signature) let response = JSON.parse(socketMessage.data)
/*
We will check that the signature received produces a valid Blockchain Account. if (response.result !== 'Ok') {
*/ console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message)
if (called.blockchainAccount === undefined) { reject()
console.log('[ERROR] Web Sockets Client -> stepOneResponse -> Signature does not produce a valid Blockchain Account.') }
return
} let signature = JSON.parse(response.signature)
/* called.blockchainAccount = web3.eth.accounts.recover(signature)
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. We will check that the signature received produces a valid Blockchain Account.
*/ */
if (called.blockchainAccount !== selectedNetworkNode.blockchainAccount) { if (called.blockchainAccount === undefined) {
console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node called does not have the expected Profile Handle.') console.log('[ERROR] Web Sockets Client -> stepOneResponse -> Signature does not produce a valid Blockchain Account.')
return 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) }
/* let signature = web3.eth.accounts.sign(JSON.stringify(signedMessage), DK.TEST_NETWORK_CLIENT_USER_PROFILE_PRIVATE_KEY)
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. let message = {
*/ messageType: 'Handshake',
if (signedMessage.calledProfileHandle !== selectedNetworkNode.userProfileHandle) { signature: JSON.stringify(signature),
console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node called does not have the expected Profile Handle.') step: 'Two'
return }
} socketClient.send(JSON.stringify(message))
/*
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
} }
/* function stepTwoResponse(socketMessage) {
All validations passed, it seems we can continue with Step Two. let response = JSON.parse(socketMessage.data)
*/
stepTwoRequest(signedMessage)
}
function stepTwoRequest(signedMessage) { if (response.result !== 'Ok') {
/* console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message)
Send Handshake Message Step Two: reject()
}
Here we will sign a message with the Network Node profile console.log('[INFO] Web Sockets Client -> stepTwoResponse -> response.message = ' + response.message)
handle and timestamp to prove our own identity. /*
*/ This was the end of the Handshake producere. We are connected to the
socketClient.onmessage = socketMessage => { stepTwoResponse(socketMessage) } Network Node and from now on, all response messages will be received
at this following function.
let signature = web3.eth.accounts.sign(JSON.stringify(signedMessage), DK.TEST_NETWORK_CLIENT_USER_PROFILE_PRIVATE_KEY) */
resolve()
let message = {
messageType: 'Handshake',
signature: JSON.stringify(signature),
step: 'Two'
} }
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 { try {
let response = JSON.parse(socketMessage.data) let response = JSON.parse(socketMessage.data)
/* /*
Chack if we are waiting for the Handshake response. 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) if (response.result === 'Ok') {
callbackFunction = undefined resolve(response.data)
} else {
console.log('[ERROR] Web Sockets Client -> onMenssage -> response.message = ' + response.message)
reject(response.message)
}
} catch (err) { } catch (err) {
callbackFunction = undefined callbackFunction = undefined
console.log('[ERROR] Web Sockets Client -> err.stack = ' + err.stack) 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
} }
} }
} }
+1 -1
View File
@@ -1400,7 +1400,7 @@
}, },
{ {
"name": "Query", "name": "Query",
"propertyName": "Query", "propertyName": "query",
"functionName": "newSocialTradingModulesQuery", "functionName": "newSocialTradingModulesQuery",
"fileName": "Query.js" "fileName": "Query.js"
}, },
@@ -35,23 +35,23 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule
return response return response
} }
if (messageHeader.messageType === undefined) { if (messageHeader.requestType === undefined) {
let response = { let response = {
result: 'Error', result: 'Error',
message: 'Client Interface messageType Not Provided.' message: 'Client Interface requestType Not Provided.'
} }
return response return response
} }
if (messageHeader.messageType !== 'Event' && messageHeader.messageType !== 'Query') { if (messageHeader.requestType !== 'Event' && messageHeader.requestType !== 'Query') {
let response = { let response = {
result: 'Error', result: 'Error',
message: 'Client Interface messageType Not Supported.' message: 'Client Interface requestType Not Supported.'
} }
return response return response
} }
switch (messageHeader.messageType) { switch (messageHeader.requestType) {
case 'Event': { case 'Event': {
return await eventReceived(messageHeader.eventMessage, userProfile) 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. Here we will process the query. This does not change the state of the Social Graph.
*/ */
try { try {
let query = NT.projects.socialTrading.modules.queriesQuery.newSocialTradingModulesQuery() let query = NT.projects.socialTrading.modules.query.newSocialTradingModulesQuery()
query.initialize(queryReceived) query.initialize(queryReceived)
return query.execute()
let response = {
result: 'Ok',
message: 'Client Interface Query Processed.',
data: query.execute()
}
return response
} catch (err) { } catch (err) {
/* /*
@@ -200,7 +206,7 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule
*/ */
let response = { let response = {
result: 'Error', result: 'Error',
message: 'Client Interface ' + err.message message: err.message
} }
return response return response
} }
@@ -21,8 +21,8 @@ exports.newSocialTradingModulesQueriesEvents = function newSocialTradingModulesQ
function initialize(queryReceived) { function initialize(queryReceived) {
NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject)
NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, NT.memory.arrays.EVENTS)
} }
@@ -8,6 +8,7 @@ exports.newSocialTradingModulesQueriesPostReplies = function newSocialTradingMod
of a certain post, with all its replies. of a certain post, with all its replies.
*/ */
let thisObject = { let thisObject = {
array: undefined,
profile: undefined, profile: undefined,
post: undefined, post: undefined,
initialIndex: undefined, initialIndex: undefined,
@@ -21,27 +22,30 @@ exports.newSocialTradingModulesQueriesPostReplies = function newSocialTradingMod
return thisObject return thisObject
function finalize() { function finalize() {
thisObject.array = undefined
thisObject.profile = undefined thisObject.profile = undefined
thisObject.post = undefined thisObject.post = undefined
} }
function initialize(queryReceived) { function initialize(queryReceived) {
NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) thisObject.array = Array.from(thisObject.post.replies)
NT.utilities.queriesValidations.postValidations(queryReceived, thisObject)
NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) 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() { function execute() {
let response = [] let response = []
let array = Array.from(thisObject.post.replies)
switch (thisObject.direction) { switch (thisObject.direction) {
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: {
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (post === undefined) { break } if (post === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
@@ -49,7 +53,7 @@ exports.newSocialTradingModulesQueriesPostReplies = function newSocialTradingMod
} }
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: {
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (post === undefined) { break } if (post === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
@@ -4,6 +4,7 @@ exports.newSocialTradingModulesQueriesProfileFollowers = function newSocialTradi
all the User or Bot profiles that are following a certain User or Bot profile. all the User or Bot profiles that are following a certain User or Bot profile.
*/ */
let thisObject = { let thisObject = {
array: undefined,
profile: undefined, profile: undefined,
initialIndex: undefined, initialIndex: undefined,
amountRequested: undefined, amountRequested: undefined,
@@ -16,25 +17,27 @@ exports.newSocialTradingModulesQueriesProfileFollowers = function newSocialTradi
return thisObject return thisObject
function finalize() { function finalize() {
thisObject.array = undefined
thisObject.profile = undefined thisObject.profile = undefined
} }
function initialize(queryReceived) { function initialize(queryReceived) {
NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) thisObject.array = Array.from(thisObject.profile.followers)
NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject)
NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject)
NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, thisObject.array)
} }
function execute() { function execute() {
let response = [] let response = []
let array = Array.from(thisObject.profile.followers)
switch (thisObject.direction) { switch (thisObject.direction) {
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: {
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (arrayItem === undefined) { break } if (arrayItem === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
@@ -42,7 +45,7 @@ exports.newSocialTradingModulesQueriesProfileFollowers = function newSocialTradi
} }
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: {
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (arrayItem === undefined) { break } if (arrayItem === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
@@ -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. all the User or Bot profiles that are being followed by a certain User or Bot profile.
*/ */
let thisObject = { let thisObject = {
array: undefined,
profile: undefined, profile: undefined,
initialIndex: undefined, initialIndex: undefined,
amountRequested: undefined, amountRequested: undefined,
@@ -16,25 +17,27 @@ exports.newSocialTradingModulesQueriesProfileFollowing = function newSocialTradi
return thisObject return thisObject
function finalize() { function finalize() {
thisObject.array = undefined
thisObject.profile = undefined thisObject.profile = undefined
} }
function initialize(queryReceived) { function initialize(queryReceived) {
NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) thisObject.array = Array.from(thisObject.profile.following)
NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject)
NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject)
NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, thisObject.array)
} }
function execute() { function execute() {
let response = [] let response = []
let array = Array.from(thisObject.profile.following)
switch (thisObject.direction) { switch (thisObject.direction) {
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: {
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (arrayItem === undefined) { break } if (arrayItem === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
@@ -42,7 +45,7 @@ exports.newSocialTradingModulesQueriesProfileFollowing = function newSocialTradi
} }
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: {
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (arrayItem === undefined) { break } if (arrayItem === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
@@ -8,6 +8,7 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi
with all of its posts. with all of its posts.
*/ */
let thisObject = { let thisObject = {
array: undefined,
profile: undefined, profile: undefined,
initialIndex: undefined, initialIndex: undefined,
amountRequested: undefined, amountRequested: undefined,
@@ -20,24 +21,26 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi
return thisObject return thisObject
function finalize() { function finalize() {
thisObject.array = undefined
thisObject.profile = undefined thisObject.profile = undefined
} }
function initialize(queryReceived) { function initialize(queryReceived) {
NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) thisObject.array = Array.from(thisObject.profile.posts)
NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject)
NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject)
NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, thisObject.array)
} }
function execute() { function execute() {
let response = [] let response = []
let array = Array.from(thisObject.profile.posts)
switch (thisObject.direction) { switch (thisObject.direction) {
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: {
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (arrayItem === undefined) { break } if (arrayItem === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
@@ -45,7 +48,7 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi
} }
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: {
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (arrayItem === undefined) { break } if (arrayItem === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
@@ -1,10 +1,11 @@
exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradingModulesQueriesUserProfileStats() { exports.newSocialTradingModulesQueriesUserProfiles = function newSocialTradingModulesQueriesUserProfiles() {
/* /*
This query returns a list of user profiles ordered This query returns a list of user profiles ordered
by Ranking. It is useful to bootstrap new users by Ranking. It is useful to bootstrap new users
and provide them with alternative of who to follow. and provide them with alternative of who to follow.
*/ */
let thisObject = { let thisObject = {
array: undefined,
initialIndex: undefined, initialIndex: undefined,
amountRequested: undefined, amountRequested: undefined,
direction: undefined, direction: undefined,
@@ -16,24 +17,29 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi
return thisObject return thisObject
function finalize() { function finalize() {
thisObject.array = undefined
} }
function initialize(queryReceived) { 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() { function execute() {
let response = [] 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) { switch (thisObject.direction) {
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: {
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (arrayItem === undefined) { break } if (arrayItem === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
@@ -41,7 +47,7 @@ exports.newSocialTradingModulesQueriesUserProfileStats = function newSocialTradi
} }
case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: { case NT.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: {
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
let arrayItem = array[i] let arrayItem = thisObject.array[i]
if (arrayItem === undefined) { break } if (arrayItem === undefined) { break }
addToResponse(arrayItem) addToResponse(arrayItem)
} }
+13 -19
View File
@@ -40,67 +40,61 @@ exports.newSocialTradingModulesQuery = function newSocialTradingModulesQuery() {
thisObject.queryType = queryReceived.queryType thisObject.queryType = queryReceived.queryType
switch (thisObject.queryType) { switch (thisObject.queryType) {
case QUERY_TYPES.USER_PROFILES: { case QUERY_TYPES.USER_PROFILES: {
thisObject.query = NT.projects.socialTrading.modules.queriesUserProfiles.newSocialTradingModulesQueriesUserProfiles() thisObject.query = NT.projects.socialTrading.modules.queriesUserProfiles.newSocialTradingModulesQueriesUserProfiles()
query.initialize(queryReceived) thisObject.query.initialize(queryReceived)
query.finalize()
break break
} }
case QUERY_TYPES.USER_PROFILE_STATS: { case QUERY_TYPES.USER_PROFILE_STATS: {
thisObject.query = NT.projects.socialTrading.modules.queriesUserProfileStats.newSocialTradingModulesQueriesUserProfileStats() thisObject.query = NT.projects.socialTrading.modules.queriesUserProfileStats.newSocialTradingModulesQueriesUserProfileStats()
query.initialize(queryReceived) thisObject.query.initialize(queryReceived)
query.finalize()
break break
} }
case QUERY_TYPES.BOT_PROFILE_STATS: { case QUERY_TYPES.BOT_PROFILE_STATS: {
thisObject.query = NT.projects.socialTrading.modules.queriesBotProfileStats.newSocialTradingModulesQueriesBotProfileStats() thisObject.query = NT.projects.socialTrading.modules.queriesBotProfileStats.newSocialTradingModulesQueriesBotProfileStats()
query.initialize(queryReceived) thisObject.query.initialize(queryReceived)
query.finalize()
break break
} }
case QUERY_TYPES.PROFILE_POSTS: { case QUERY_TYPES.PROFILE_POSTS: {
thisObject.query = NT.projects.socialTrading.modules.queriesProfilePosts.newSocialTradingModulesQueriesProfilePosts() thisObject.query = NT.projects.socialTrading.modules.queriesProfilePosts.newSocialTradingModulesQueriesProfilePosts()
query.initialize(queryReceived) thisObject.query.initialize(queryReceived)
query.finalize()
break break
} }
case QUERY_TYPES.POST_REPLIES: { case QUERY_TYPES.POST_REPLIES: {
thisObject.query = NT.projects.socialTrading.modules.queriesPostReplies.newSocialTradingModulesQueriesPostReplies() thisObject.query = NT.projects.socialTrading.modules.queriesPostReplies.newSocialTradingModulesQueriesPostReplies()
query.initialize(queryReceived) thisObject.query.initialize(queryReceived)
query.finalize()
break break
} }
case QUERY_TYPES.PROFILE_FOLLOWERS: { case QUERY_TYPES.PROFILE_FOLLOWERS: {
thisObject.query = NT.projects.socialTrading.modules.queriesProfileFollowers.newSocialTradingModulesQueriesProfileFollowers() thisObject.query = NT.projects.socialTrading.modules.queriesProfileFollowers.newSocialTradingModulesQueriesProfileFollowers()
query.initialize(queryReceived) thisObject.query.initialize(queryReceived)
query.finalize()
break break
} }
case QUERY_TYPES.PROFILE_FOLLOWING: { case QUERY_TYPES.PROFILE_FOLLOWING: {
thisObject.query = NT.projects.socialTrading.modules.queriesProfileFollowing.newSocialTradingModulesQueriesProfileFollowing() thisObject.query = NT.projects.socialTrading.modules.queriesProfileFollowing.newSocialTradingModulesQueriesProfileFollowing()
query.initialize(queryReceived) thisObject.query.initialize(queryReceived)
query.finalize()
break break
} }
case QUERY_TYPES.maps.EVENTS: { case QUERY_TYPES.maps.EVENTS: {
thisObject.query = NT.projects.socialTrading.modules.queriesEvents.newSocialTradingModulesQueriesEvents() thisObject.query = NT.projects.socialTrading.modules.queriesEvents.newSocialTradingModulesQueriesEvents()
query.initialize(queryReceived) thisObject.query.initialize(queryReceived)
query.finalize()
break break
} }
default: throw('Query Type Not Supported.') default: throw ('Query Type Not Supported.')
} }
} }
function execute() { function execute() {
return thisObject.query.execute() let response = thisObject.query.execute()
thisObject.query.finalize()
return response
} }
} }
@@ -43,7 +43,7 @@ exports.newSocialTradingUtilitiesQueriesValidations = function newSocialTradingU
} }
} }
function arrayValidations(queryReceived, thisObject) { function arrayValidations(queryReceived, thisObject, array) {
/* /*
Validate Initial Index Validate Initial Index
*/ */
@@ -52,7 +52,7 @@ exports.newSocialTradingUtilitiesQueriesValidations = function newSocialTradingU
} }
if (queryReceived.initialIndex === NT.projects.socialTrading.globals.queryConstants.INITIAL_INDEX_LAST) { 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) { 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) { if (queryReceived.amountRequested > NT.projects.socialTrading.globals.queryConstants.MAX_AMOUNT_REQUESTED) {
throw ('Amount Requested Above Max.') throw ('Amount Requested Above Max.')
} }
thisObject.amountRequested = queryReceived.amountRequested
/* /*
Validate Direction Validate Direction
*/ */