diff --git a/Network/Queries/Events.js b/Network/Queries/Events.js index 87b85f484..b55def0e0 100644 --- a/Network/Queries/Events.js +++ b/Network/Queries/Events.js @@ -1,5 +1,8 @@ exports.newEvents = function newEvents() { + /* + This is the query executed to fill the timeline of a certain User or Bot Profile. + */ let thisObject = { profile: undefined, initialIndex: undefined, @@ -17,68 +20,9 @@ exports.newEvents = function newEvents() { } function initialize(queryReceived) { - /* - Validate User Profile - */ - if (queryReceived.emitterUserProfileId !== undefined) { - thisObject.profile = NT.memory.maps.USER_PROFILES_BY_ID.get(queryReceived.emitterUserProfileId) - } else { - thisObject.profile = NT.memory.maps.USER_PROFILES_BY_HANDLE.get(queryReceived.emitterUserProfileHandle) - } - if (thisObject.profile === undefined) { - throw ('Emitter User Profile Not Found.') - } - /* - Validate Initial Index - */ - if (queryReceived.initialIndex === undefined) { - throw ('Initial Index Undefined.') - } - - if (queryReceived.initialIndex === NT.globals.constants.queries.INITIAL_INDEX_LAST) { - queryReceived.initialIndex = thisObject.profile.posts.length - 1 - } - - if (queryReceived.initialIndex === NT.globals.constants.queries.INITIAL_INDEX_FIRST) { - queryReceived.initialIndex = 0 - } - - if (isNaN(queryReceived.initialIndex) === true) { - throw ('Initial Event Is Not a Number.') - } - - thisObject.initialIndex = queryReceived.initialIndex - /* - Validate Amount Requested - */ - if (queryReceived.amountRequested === undefined) { - throw ('Amount Requested Undefined.') - } - - if (isNaN(queryReceived.amountRequested) === true) { - throw ('Amount Requested Is Not a Number.') - } - - if (queryReceived.amountRequested < NT.globals.constants.queries.MIN_AMOUNT_REQUESTED) { - throw ('Amount Requested Below Min.') - } - - if (queryReceived.amountRequested > NT.globals.constants.queries.MAX_AMOUNT_REQUESTED) { - throw ('Amount Requested Above Max.') - } - /* - Validate Direction - */ - if (queryReceived.direction === undefined) { - throw ('Direction Undefined.') - } - - if (queryReceived.direction !== NT.globals.constants.queries.DIRECTION_FUTURE && queryReceived.direction !== NT.globals.constants.queries.DIRECTION_PAST) { - throw ('Direction Not Supported.') - } - - thisObject.direction = queryReceived.direction + NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) } @@ -109,71 +53,149 @@ exports.newEvents = function newEvents() { For an event to be returned at the response of this query, it needs to be related to the profile making the query. How it can be related? - 1. The Emitter or Target profile must be the same as the Query Profile. - 2. The Emitter or Traget profile must be at the Following map of the Query Profile. - 3. The Emmiter or Target post must be belong to any profile at the Following of the Query Profile. + 1. The Emitter or Target profile must be the same as the Context Profile. + 2. The Emitter or Traget profile must be at the Following map of the Context Profile. + 3. The Emmiter or Target post must be belong to any profile at the Following of the Context Profile. Any of the above happening, means that indeed it is related. */ let emitterUserProfile = NT.memory.maps.USER_PROFILES_BY_ID.get(eventReceived.emitterUserProfileId) let targetUserProfile = NT.memory.maps.USER_PROFILES_BY_ID.get(eventReceived.targetUserProfileId) - let emitterPost = NT.memory.maps.POSTS.get(eventReceived.emitterPostHash) - let targetPost = NT.memory.maps.POSTS.get(eventReceived.targetPostHash) + let emitterBotProfile = emitterUserProfile.bots.get(eventReceived.emitterBotProfileId) + let targetBotProfile = targetUserProfile.bots.get(eventReceived.targetBotProfileId) + let emitterPost = emitterUserProfile.posts.get(eventReceived.emitterPostHash) + let targetPost = targetUserProfile.posts.get(eventReceived.targetPostHash) /* - Test #1 : The Emitter or Target profile must be the same as the Query Profile. + Test #1 : The Emitter or Target profile must be the same as the Context Profile. */ - if (emitterUserProfile !== undefined) { - if (emitterUserProfile.userProfileId === thisObject.profile.userProfileId) { - addToResponse(event) - return + if (thisObject.profile.botProfileId === undefined) { + /* + The context is a User Profile + */ + if (emitterUserProfile !== undefined) { + if (emitterUserProfile.userProfileId === thisObject.profile.userProfileId) { + addToResponse(event) + return + } + } + if (targetUserProfile !== undefined) { + if (targetUserProfile.userProfileId === thisObject.profile.userProfileId) { + addToResponse(event) + return + } + } + } else { + /* + The context is a Bot Profile + */ + if (emitterBotProfile !== undefined) { + if (emitterBotProfile.botProfileId === thisObject.profile.botProfileId) { + addToResponse(event) + return + } + } + if (targetBotProfile !== undefined) { + if (targetBotProfile.botProfileId === thisObject.profile.botProfileId) { + addToResponse(event) + return + } } } - if (targetUserProfile !== undefined) { - if (targetUserProfile.userProfileId === thisObject.profile.userProfileId) { - addToResponse(event) - return + + /* + Test #2 : The Emitter or Traget profile must be at the Following map of the Context Profile. + */ + if (thisObject.profile.botProfileId === undefined) { + /* + The context is a User Profile + */ + if (emitterUserProfile !== undefined) { + if (thisObject.profile.following.get(emitterUserProfile.userProfileId) !== undefined) { + addToResponse(event) + return + } + } + if (targetUserProfile !== undefined) { + if (thisObject.profile.following.get(targetUserProfile.userProfileId) !== undefined) { + addToResponse(event) + return + } + } + } else { + /* + The context is a Bot Profile + */ + if (emitterBotProfile !== undefined) { + if (thisObject.profile.following.get(emitterBotProfile.botProfileId) !== undefined) { + addToResponse(event) + return + } + } + if (targetBotProfile !== undefined) { + if (thisObject.profile.following.get(targetBotProfile.botProfileId) !== undefined) { + addToResponse(event) + return + } } } /* - Test #2 : The Emitter or Traget profile must be at the Following map of the Query Profile. + Test #3 : The Emmiter or Target post must be belong to any profile at the Following of the Context Profile. */ - if (emitterUserProfile !== undefined) { - if (thisObject.profile.following.get(emitterUserProfile.userProfileId) !== undefined) { - addToResponse(event) - return + if (thisObject.profile.botProfileId === undefined) { + /* + The context is a User Profile + */ + if (emitterPost !== undefined) { + if (thisObject.profile.following.get(emitterPost.emitterUserProfileId) !== undefined) { + addToResponse(event) + return + } } - } - if (targetUserProfile !== undefined) { - if (thisObject.profile.following.get(targetUserProfile.userProfileId) !== undefined) { - addToResponse(event) - return + if (emitterPost !== undefined) { + if (thisObject.profile.following.get(emitterPost.targetUserProfileId) !== undefined) { + addToResponse(event) + return + } } - } - /* - Test #3 : The Emmiter or Target post must be belong to any profile at the Following of the Query Profile. - */ - if (emitterPost !== undefined) { - if (thisObject.profile.following.get(emitterPost.emitterUserProfileId) !== undefined) { - addToResponse(event) - return + if (targetPost !== undefined) { + if (thisObject.profile.following.get(targetPost.emitterUserProfileId) !== undefined) { + addToResponse(event) + return + } } - } - if (emitterPost !== undefined) { - if (thisObject.profile.following.get(emitterPost.targetUserProfileId) !== undefined) { - addToResponse(event) - return + if (targetPost !== undefined) { + if (thisObject.profile.following.get(targetPost.targetUserProfileId) !== undefined) { + addToResponse(event) + return + } } - } - if (targetPost !== undefined) { - if (thisObject.profile.following.get(targetPost.emitterUserProfileId) !== undefined) { - addToResponse(event) - return + } else { + /* + The context is a Bot Profile + */ + if (emitterPost !== undefined) { + if (thisObject.profile.following.get(emitterPost.emitterBotProfileId) !== undefined) { + addToResponse(event) + return + } } - } - if (targetPost !== undefined) { - if (thisObject.profile.following.get(targetPost.targetUserProfileId) !== undefined) { - addToResponse(event) - return + if (emitterPost !== undefined) { + if (thisObject.profile.following.get(emitterPost.targetBotProfileId) !== undefined) { + addToResponse(event) + return + } + } + if (targetPost !== undefined) { + if (thisObject.profile.following.get(targetPost.emitterBotProfileId) !== undefined) { + addToResponse(event) + return + } + } + if (targetPost !== undefined) { + if (thisObject.profile.following.get(targetPost.targetBotProfileId) !== undefined) { + addToResponse(event) + return + } } } @@ -184,28 +206,44 @@ exports.newEvents = function newEvents() { eventType: event.eventType, emitterUserProfileId: event.emitterUserProfileId, targetUserProfileId: event.targetUserProfileId, + emitterBotProfileId: event.emitterBotProfileId, + targetBotProfileId: event.targetBotProfileId, emitterPostHash: event.emitterPostHash, targetPostHash: event.targetPostHash, timestamp: event.timestamp, - botId: event.botId, botAsset: event.botAsset, - botExchange: event.botExchange + botExchange: event.botExchange, + botEnabled: event.botEnabled } if (emitterUserProfile !== undefined) { - let query = NT.modules.QUERY_PROFILE_STATS.newUserProfileStats() + let query = NT.modules.QUERY_USER_PROFILE_STATS.newUserProfileStats() query.initialize({ targetUserProfileId: event.emitterUserProfileId }) eventResponse.emitterUserProfile = query.execute() query.finalize() } if (targetUserProfile !== undefined) { - let query = NT.modules.QUERY_PROFILE_STATS.newUserProfileStats() + let query = NT.modules.QUERY_USER_PROFILE_STATS.newUserProfileStats() query.initialize({ targetUserProfileId: event.targetUserProfileId }) eventResponse.targetUserProfile = query.execute() query.finalize() } + if (emitterBotProfile !== undefined) { + let query = NT.modules.QUERY_BOT_PROFILE_STATS.newBotProfileStats() + query.initialize({ targetUserProfileId: event.emitterUserProfileId, targetBotProfileId: emitterBotProfileId }) + eventResponse.emitterBotProfile = query.execute() + query.finalize() + } + + if (targetBotProfile !== undefined) { + let query = NT.modules.QUERY_BOT_PROFILE_STATS.newBotProfileStats() + query.initialize({ targetUserProfileId: event.targetUserProfileId, targetBotProfileId: targetBotProfileId }) + eventResponse.targetBotProfile = query.execute() + query.finalize() + } + if (emitterPost !== undefined) { eventResponse.emitterPost = addPost(emitterPost) } @@ -220,11 +258,13 @@ exports.newEvents = function newEvents() { let postResponse = { emitterUserProfileId: post.emitterUserProfileId, targetUserProfileId: post.targetUserProfileId, + emitterBotProfileId: post.emitterBotProfileId, + targetBotProfileId: post.targetBotProfileId, emitterPostHash: post.emitterPostHash, targetPostHash: post.targetPostHash, postType: post.postType, timestamp: post.timestamp, - repliesCount: post.replies.length, + repliesCount: post.replies.size, reactions: Array.from(post.reactions) } return postResponse diff --git a/Network/Queries/PostReplies.js b/Network/Queries/PostReplies.js index 41a4f06a9..f5221fa2c 100644 --- a/Network/Queries/PostReplies.js +++ b/Network/Queries/PostReplies.js @@ -3,6 +3,9 @@ exports.newPostReplies = function newPostReplies() { Each Post regardless if it is authored by a User or Bot Profile, can have replies. This query is designed for Network Clients to fetch the posts metadata that are replies to a certain post. + + This is the query executed at the Network Client to fill the page + of a certain post, with all its replies. */ let thisObject = { profile: undefined, diff --git a/Network/Queries/ProfileFollowers.js b/Network/Queries/ProfileFollowers.js index 3fc0a3a9d..bf704f978 100644 --- a/Network/Queries/ProfileFollowers.js +++ b/Network/Queries/ProfileFollowers.js @@ -1,5 +1,8 @@ exports.newProfileFollowers = function newProfileFollowers() { - + /* + This module represents the query that allows a Network Client to know + all the User or Bot profiles that are following a certain User or Bot profile. + */ let thisObject = { profile: undefined, initialIndex: undefined, diff --git a/Network/Queries/ProfileFollowing.js b/Network/Queries/ProfileFollowing.js index 4fea1534d..845ee2c09 100644 --- a/Network/Queries/ProfileFollowing.js +++ b/Network/Queries/ProfileFollowing.js @@ -1,5 +1,8 @@ exports.newProfileFollowing = function newProfileFollowing() { - + /* + This module represents the query that allows a Network Client to know + all the User or Bot profiles that are being followed by a certain User or Bot profile. + */ let thisObject = { profile: undefined, initialIndex: undefined, diff --git a/Network/Queries/ProfilePosts.js b/Network/Queries/ProfilePosts.js index bbeded5d4..7f6255341 100644 --- a/Network/Queries/ProfilePosts.js +++ b/Network/Queries/ProfilePosts.js @@ -3,6 +3,9 @@ exports.newProfilePosts = function newProfilePosts() { Each User or Bot Profile can have posts. This query is designed for Network Clients to fetch the posts metadata they need from the Social Graph. + + This is the query to be executed to fill a Profile page + with all of its posts. */ let thisObject = { profile: undefined, diff --git a/Network/SocialGraphNetwokNode.js b/Network/SocialGraphNetwokNode.js index db8ae95c4..044ce2a2c 100644 --- a/Network/SocialGraphNetwokNode.js +++ b/Network/SocialGraphNetwokNode.js @@ -1,4 +1,15 @@ +/* +The Superalgos Network will have 3 types of nodes: + * Social Graph Nodes + * Search Index Nodes + * Private Message Nodes + +This module is the starting point of the Social Graph Network Node. + +This type of node is responsible for mantaining the whole Social Graph +or relationships between User and Bot profiles and also between their posts. +*/ global.NT = { memory: { diff --git a/Network/Utilities/QueriesValidations.js b/Network/Utilities/QueriesValidations.js index 618c1759b..60a244a3d 100644 --- a/Network/Utilities/QueriesValidations.js +++ b/Network/Utilities/QueriesValidations.js @@ -1,5 +1,7 @@ exports.newProfileFollowers = function newProfileFollowers() { - + /* + This module have a few funcions that are often needed at Queries Modules. + */ let thisObject = { profilesValidations: profilesValidations, postValidations: postValidations, diff --git a/Network/clientsInterface.js b/Network/clientsInterface.js index 1cc17e4d6..2f48f80df 100644 --- a/Network/clientsInterface.js +++ b/Network/clientsInterface.js @@ -1,5 +1,12 @@ exports.newClientInterface = function newClientInterface() { + /* + This module represents the Interface the Network Node have + with Network Clients connected to it. There are two things + Network Clients can do with this Network Node: + * Send an Event that happened at the Network Client to the Network. + * Make a Query to the Network Node for info needed at the Network Client. + */ let thisObject = { eventReceived: eventReceived, queryReceived: queryReceived, @@ -19,7 +26,7 @@ exports.newClientInterface = function newClientInterface() { function eventReceived(eventMessage) { /* - We expect here a JSON string with the following properties: + We expect here a JSON string with some or all of the following properties: { "eventId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", @@ -49,7 +56,7 @@ exports.newClientInterface = function newClientInterface() { function queryReceived(queryMessage) { /* - We expect here a JSON string with the following properties: + We expect here a JSON string with some or all of the following properties: { "equeryId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",