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
+155 -116

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)
+51 -51
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,12 +43,16 @@ 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() {
return new Promise(connectToNewtwork)
function connectToNewtwork(resolve, reject) {
try { try {
let handshakeDone = false
socketClient.onopen = () => { onConnection() } socketClient.onopen = () => { onConnection() }
socketClient.onerror = err => { onError(err) } socketClient.onerror = err => { onError(err) }
@@ -95,7 +98,7 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
if (response.result !== 'Ok') { if (response.result !== 'Ok') {
console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message) console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message)
return reject()
} }
let signature = JSON.parse(response.signature) let signature = JSON.parse(response.signature)
@@ -105,7 +108,7 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
*/ */
if (called.blockchainAccount === undefined) { if (called.blockchainAccount === undefined) {
console.log('[ERROR] Web Sockets Client -> stepOneResponse -> Signature does not produce a valid Blockchain Account.') 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 We will check that the blockchain account taken from the signature matches
@@ -113,7 +116,7 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
*/ */
if (called.blockchainAccount !== selectedNetworkNode.blockchainAccount) { if (called.blockchainAccount !== selectedNetworkNode.blockchainAccount) {
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 -> The Network Node called does not have the expected Profile Handle.')
return reject()
} }
let signedMessage = JSON.parse(signature.message) let signedMessage = JSON.parse(signature.message)
@@ -124,7 +127,7 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
*/ */
if (signedMessage.calledProfileHandle !== selectedNetworkNode.userProfileHandle) { if (signedMessage.calledProfileHandle !== selectedNetworkNode.userProfileHandle) {
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 -> The Network Node called does not have the expected Profile Handle.')
return reject()
} }
/* /*
We will check that the profile handle we sent to the Network Node, is returned at the We will check that the profile handle we sent to the Network Node, is returned at the
@@ -132,7 +135,7 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
*/ */
if (signedMessage.callerProfileHandle !== DK.TEST_NETWORK_CLIENT_USER_PROFILE_HANDLE) { 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.') console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node callerProfileHandle does not match my own userProfileHandle.')
return reject()
} }
/* /*
We will also check that the callerTimestamp we sent to the Network Node, is returned at the We will also check that the callerTimestamp we sent to the Network Node, is returned at the
@@ -140,7 +143,7 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
*/ */
if (signedMessage.callerTimestamp !== callerTimestamp) { if (signedMessage.callerTimestamp !== callerTimestamp) {
console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node callerTimestamp does not match my own callerTimestamp.') console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node callerTimestamp does not match my own callerTimestamp.')
return reject()
} }
/* /*
@@ -173,7 +176,7 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
if (response.result !== 'Ok') { if (response.result !== 'Ok') {
console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message) console.log('[ERROR] Web Sockets Client -> stepOneResponse -> response.message = ' + response.message)
return reject()
} }
console.log('[INFO] Web Sockets Client -> stepTwoResponse -> response.message = ' + response.message) console.log('[INFO] Web Sockets Client -> stepTwoResponse -> response.message = ' + response.message)
/* /*
@@ -181,37 +184,11 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
Network Node and from now on, all response messages will be received Network Node and from now on, all response messages will be received
at this following function. at this following function.
*/ */
socketClient.onmessage = socketMessage => { onMenssage(socketMessage) } resolve()
} }
} }
} }
async 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
} catch (err) {
callbackFunction = undefined
console.log('[ERROR] Web Sockets Client -> err.stack = ' + err.stack)
}
}
function onError(err) { function onError(err) {
console.log('[ERROR] Web Sockets Client -> onError -> err.message = ' + err.message) console.log('[ERROR] Web Sockets Client -> onError -> err.message = ' + err.message)
console.log('[ERROR] Web Sockets Client -> onError -> err.stack = ' + err.stack) console.log('[ERROR] Web Sockets Client -> onError -> err.stack = ' + err.stack)
@@ -220,27 +197,50 @@ exports.newNetworkModulesWebSocketsClient = function newNetworkModulesWebSockets
} catch (err) { } catch (err) {
console.log('[ERROR] Web Sockets Client -> setUpWebsocketClient -> err.stack = ' + err.stack) console.log('[ERROR] Web Sockets Client -> setUpWebsocketClient -> err.stack = ' + err.stack)
} }
}
} }
function sendMessage(message, callback) { function sendMessage(message) {
if (callbackFunction !== undefined) {
console.log('[ERROR] Web Sockets Client -> sendMessage -> Cannot send more messages while waiting for a response.') return new Promise(sendSocketMessage)
return false
} function sendSocketMessage(resolve, reject) {
if (socketClient.readyState !== 1) { // 1 means connected and ready. if (socketClient.readyState !== 1) { // 1 means connected and ready.
console.log('[ERROR] Web Sockets Client -> sendMessage -> Cannot send message while connection is closed.') console.log('[ERROR] Web Sockets Client -> sendMessage -> Cannot send message while connection is closed.')
return false 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 {
callbackFunction = callback
socketClient.send(message) let response = JSON.parse(socketMessage.data)
return true /*
Chack if we are waiting for the Handshake response.
*/
if (response.result === 'Ok') {
resolve(response.data)
} else {
console.log('[ERROR] Web Sockets Client -> onMenssage -> response.message = ' + response.message)
reject(response.message)
}
} catch (err) { } catch (err) {
callbackFunction = false callbackFunction = undefined
console.log('[ERROR] Web Sockets Client -> sendMessage -> err.stack = ' + err.stack) console.log('[ERROR] Web Sockets Client -> 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)
} }
+11 -17
View File
@@ -43,57 +43,49 @@ exports.newSocialTradingModulesQuery = function newSocialTradingModulesQuery() {
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.')
@@ -101,6 +93,8 @@ exports.newSocialTradingModulesQuery = function newSocialTradingModulesQuery() {
} }
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
*/ */