diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index ff93a2489..f7ed65049 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -5,17 +5,19 @@ global.NT = { USER_PROFILES: new Map(), // Here we will store the user profiles and their statistics. POSTS: new Map(), // This are all the posts we are keeping track of. EVENTS: new Map(), // This is the registry of all events received that prevents processing them more than once. - TIMELINE: [] // A time ordered list of Posts and Trades. + TIMELINE: [], // A time ordered list of Posts and Trades. + NETWORK_CLIENTS: [], // These are the Network Clients connected to this Network Node. + NETWORK_PEERS: [], // These are the Network Peers connected to this Network Node. }, modules: { - EVENT: require('./UserProfile.js'), + USER_PROFILE: require('./UserProfile.js'), EVENT: require('./Event.js'), POST: require('./Post.js'), - BOT: require('./Bot.js') + BOT: require('./Bot.js'), + QUERY: require('./Query.js'), + PROFILE_STATS_QUERY: require('./ProfileStatsQuery.js'), + PROFILE_POSTS_QUERY: require('./ProfilePostsQuery.js'), + POST_REPLIES_QUERY: require('./ProfileRepliesQuery.js'), + TIMELINE_CHUNK_QUERY: require('./TimelineChunkQuery.js') } -} - -/* - We will add this event to the timeline -*/ -NT.memory.TIMELINE.push() \ No newline at end of file +} \ No newline at end of file diff --git a/Network/ProfileStatsQuery.js b/Network/ProfileStatsQuery.js new file mode 100644 index 000000000..459cb7179 --- /dev/null +++ b/Network/ProfileStatsQuery.js @@ -0,0 +1,43 @@ +exports.newProfileStatsQuery = function newProfileStatsQuery() { + + let thisObject = { + profile: undefined, + execute: execute, + initialize: initialize, + finalize: finalize + } + + return thisObject + + function finalize() { + + } + + function initialize(queryReceived) { + + thisObject.profile = NT.memory.PROFILES.get(queryReceived.targetUserProfileId) + + if (thisObject.profile === undefined) { + throw ('Target User Profile Not Found.') + } + + } + + function execute() { + + return { + "userProfieId": thisObject.profile.userProfieId, + "githubUsername": thisObject.profile.githubUsername, + "blockchainAccount": thisObject.profile.blockchainAccount, + "ranking": thisObject.profile.ranking, + "multiMediaPostFollowingCount": thisObject.profile.multiMediaPostFollowingCount, + "multiMediaPostFollowersCount": thisObject.profile.multiMediaPostFollowersCount, + "tradePostsFollowingCount": thisObject.profile.tradePostsFollowingCount, + "tradePostsFollowersCount": thisObject.profile.tradePostsFollowersCount, + "emitterEventsCount": thisObject.profile.emitterEventsCount, + "targetEventsCount": thisObject.profile.targetEventsCount, + "postsCount": thisObject.profile.postsCount, + "botsCount": thisObject.profile.botsCount + } + } +} \ No newline at end of file diff --git a/Network/Query.js b/Network/Query.js new file mode 100644 index 000000000..a77806943 --- /dev/null +++ b/Network/Query.js @@ -0,0 +1,63 @@ +exports.newQuery = function newQuery() { + + let thisObject = { + query: undefined, + queryType: undefined, + execute: execute, + initialize: initialize, + finalize: finalize + } + + const QUERY_TYPES = { + + PROFILE_STATS: 0, + PROFILE_POSTS: 1, + POST_REPLIES: 2, + TIMELINE_CHUNK: 3 + + } + + return thisObject + + function finalize() { + thisObject.query = undefined + thisObject.queryType = undefined + } + + function initialize(queryReceived) { + + thisObject.queryType = queryReceived.queryType + + switch (thisObject.queryType) { + + case QUERY_TYPES.PROFILE_STATS: { + thisObject.query = NT.modules.PROFILE_STATS_QUERY.newProfileStatsQuery() + query.initialize(queryReceived) + break + } + + case QUERY_TYPES.PROFILE_POSTS: { + thisObject.query = NT.modules.PROFILE_POSTS_QUERY.newProfileStatsQuery() + query.initialize(queryReceived) + break + } + + case QUERY_TYPES.POST_REPLIES: { + thisObject.query = NT.modules.POST_REPLIES_QUERY.newProfileStatsQuery() + query.initialize(queryReceived) + break + } + + case QUERY_TYPES.TIMELINE_CHUNK: { + thisObject.query = NT.modules.TIMELINE_CHUNK_QUERY.newProfileStatsQuery() + query.initialize(queryReceived) + break + } + default: throw('Query Type Not Supported.') + } + } + + function execute() { + return thisObject.query.execute() + } +} \ No newline at end of file diff --git a/Network/clientsInterface.js b/Network/clientsInterface.js index 09f12e545..d5d222802 100644 --- a/Network/clientsInterface.js +++ b/Network/clientsInterface.js @@ -1,9 +1,10 @@ exports.newClientInterface = function newClientInterface() { let thisObject = { + eventReceived: eventReceived, + queryReceived: queryReceived, initialize: initialize, - finalize: finalize, - run: run + finalize: finalize } return thisObject @@ -16,7 +17,55 @@ exports.newClientInterface = function newClientInterface() { } - function run() { - - } + function eventReceived(eventMessage) { + /* + We expect here a JSON string with the following properties: + + { + "eventId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "eventType": 10, + "emitterUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "targetUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "emitterPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "targetPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "timestamp": 124234234234, + "botId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "botAsset": "BTC", + "botExchange": "Binance" + } + */ + + let eventReceived = JSON.parse(eventMessage) + if (NT.memory.EVENTS.get(eventReceived.eventId) !== undefined) { + throw ('Event Already Exists.') + } + + let event = NT.modules.EVENT.newEvent() + event.initialize(eventReceived) + NT.memory.EVENTS.set(eventReceived.eventId, event) + } + + function queryReceived(queryMessage) { + /* + We expect here a JSON string with the following properties: + + { + "equeryId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "queryType": 10, + "emitterUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "targetUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "emitterPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "targetPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "timestamp": 124234234234, + "botId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", + "botAsset": "BTC", + "botExchange": "Binance" + } + */ + + let queryReceived = JSON.parse(queryMessage) + let query = NT.modules.QUERY.newQuery() + query.initialize(queryReceived) + return query.execute() + } } \ No newline at end of file diff --git a/Network/event.js b/Network/event.js index fe2311e53..4e4785d39 100644 --- a/Network/event.js +++ b/Network/event.js @@ -56,38 +56,20 @@ exports.newEvent = function newEvent() { thisObject.targetUserProfile = undefined } - function initialize(eventMessage) { - /* - We expect here a JSON string with the following properties: + function initialize(eventReceived) { - { - "eventId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", - "eventType": 10, - "emitterUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", - "targetUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", - "emitterPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", - "targetPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", - "timestamp": 124234234234, - "botId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db", - "botAsset": "BTC", - "botExchange": "Binance" - } - */ - - let event = JSON.parse(eventMessage) - - thisObject.emitterUserProfile = NT.memory.USER_PROFILES.get(event.emitterUserProfileId) + thisObject.emitterUserProfile = NT.memory.USER_PROFILES.get(eventReceived.emitterUserProfileId) if (thisObject.emitterUserProfile === undefined) { throw ('Emitter User Profile Not Found.') } thisObject.emitterUserProfile.emitterEventsCount++ - thisObject.targetUserProfile = NT.memory.USER_PROFILES.get(event.targetUserProfileId) + thisObject.targetUserProfile = NT.memory.USER_PROFILES.get(eventReceived.targetUserProfileId) if (thisObject.targetUserProfile === undefined) { /* We thow an exception when it does not have a target user profile and is required*/ if ( - event.eventType !== EVENT_TYPES.NEW_MULTI_MEDIA_POST && - event.eventType !== EVENT_TYPES.NEW_TRADE_POST + eventReceived.eventType !== EVENT_TYPES.NEW_MULTI_MEDIA_POST && + eventReceived.eventType !== EVENT_TYPES.NEW_TRADE_POST ) { throw ('Target User Profile Not Found.') } @@ -95,44 +77,50 @@ exports.newEvent = function newEvent() { thisObject.targetUserProfile.targetEventsCount++ } - thisObject.eventId = event.eventId - thisObject.eventType = event.eventType - thisObject.asset = event.asset - thisObject.timestamp = event.timestamp + thisObject.eventId = eventReceived.eventId + thisObject.eventType = eventReceived.eventType + thisObject.asset = eventReceived.asset + thisObject.timestamp = eventReceived.timestamp /* Is is a new post? */ if ( - event.eventType === EVENT_TYPES.NEW_MULTI_MEDIA_POST || - event.eventType === EVENT_TYPES.REPLY_TO_MULTI_MEDIA_POST || - event.eventType === EVENT_TYPES.REPOST_MULTI_MEDIA || - event.eventType === EVENT_TYPES.QUOTE_REPOST_MULTI_MEDIA || - event.eventType === EVENT_TYPES.NEW_TRADE_POST || - event.eventType === EVENT_TYPES.REPLY_TO_TRADE_POST || - event.eventType === EVENT_TYPES.RE_POST_TRADE || - event.eventType === EVENT_TYPES.QUOTE_REPOST_TRADE + eventReceived.eventType === EVENT_TYPES.NEW_MULTI_MEDIA_POST || + eventReceived.eventType === EVENT_TYPES.REPLY_TO_MULTI_MEDIA_POST || + eventReceived.eventType === EVENT_TYPES.REPOST_MULTI_MEDIA || + eventReceived.eventType === EVENT_TYPES.QUOTE_REPOST_MULTI_MEDIA || + eventReceived.eventType === EVENT_TYPES.NEW_TRADE_POST || + eventReceived.eventType === EVENT_TYPES.REPLY_TO_TRADE_POST || + eventReceived.eventType === EVENT_TYPES.RE_POST_TRADE || + eventReceived.eventType === EVENT_TYPES.QUOTE_REPOST_TRADE ) { - let post = NT.modules.POST.newPost() - post.initialize( - event.emitterPostHash, - event.targetPostHash, - thisObject.eventType, - thisObject.emitterUserProfile, - thisObject.asset, - thisObject.timestamp - ) + if ( + eventReceived.eventType === EVENT_TYPES.NEW_MULTI_MEDIA_POST || + eventReceived.eventType === EVENT_TYPES.REPLY_TO_MULTI_MEDIA_POST || + eventReceived.eventType === EVENT_TYPES.REPOST_MULTI_MEDIA || + eventReceived.eventType === EVENT_TYPES.QUOTE_REPOST_MULTI_MEDIA + ) { + thisObject.emitterUserProfile.addPost( + eventReceived.emitterPostHash, + eventReceived.targetPostHash, + thisObject.eventType, + thisObject.emitterUserProfile, + thisObject.timestamp + ) + } + return } /* Is is a following? */ if ( - event.eventType === EVENT_TYPES.FOLLOW_USER_PROFILE_MULTI_MEDIA_POSTS || - event.eventType === EVENT_TYPES.UNFOLLOW_USER_PROFILE_MULTI_MEDIA_POSTS || - event.eventType === EVENT_TYPES.FOLLOW_USER_PROFILE_TRADE_POSTS || - event.eventType === EVENT_TYPES.UNFOLLOW_USER_PROFILE_TRADE_POSTS + eventReceived.eventType === EVENT_TYPES.FOLLOW_USER_PROFILE_MULTI_MEDIA_POSTS || + eventReceived.eventType === EVENT_TYPES.UNFOLLOW_USER_PROFILE_MULTI_MEDIA_POSTS || + eventReceived.eventType === EVENT_TYPES.FOLLOW_USER_PROFILE_TRADE_POSTS || + eventReceived.eventType === EVENT_TYPES.UNFOLLOW_USER_PROFILE_TRADE_POSTS ) { - switch (event.eventType) { + switch (eventReceived.eventType) { case EVENT_TYPES.FOLLOW_USER_PROFILE_MULTI_MEDIA_POSTS: { thisObject.emitterUserProfile.addMultiMediaPostsFollowing( thisObject.targetUserProfile @@ -176,51 +164,51 @@ exports.newEvent = function newEvent() { Is is a Reaction? */ if ( - event.eventType === EVENT_TYPES.ADD_REACTION_LIKE || - event.eventType === EVENT_TYPES.ADD_REACTION_LOVE || - event.eventType === EVENT_TYPES.ADD_REACTION_HAHA || - event.eventType === EVENT_TYPES.ADD_REACTION_WOW || - event.eventType === EVENT_TYPES.ADD_REACTION_SAD || - event.eventType === EVENT_TYPES.ADD_REACTION_ANGRY || - event.eventType === EVENT_TYPES.ADD_REACTION_HUG + eventReceived.eventType === EVENT_TYPES.ADD_REACTION_LIKE || + eventReceived.eventType === EVENT_TYPES.ADD_REACTION_LOVE || + eventReceived.eventType === EVENT_TYPES.ADD_REACTION_HAHA || + eventReceived.eventType === EVENT_TYPES.ADD_REACTION_WOW || + eventReceived.eventType === EVENT_TYPES.ADD_REACTION_SAD || + eventReceived.eventType === EVENT_TYPES.ADD_REACTION_ANGRY || + eventReceived.eventType === EVENT_TYPES.ADD_REACTION_HUG ) { - let targetPost = NT.memory.POSTS.get(event.targetPostHash) + let targetPost = NT.memory.POSTS.get(eventReceived.targetPostHash) if (targetPost === undefined) { throw ('Target Post Not Found') } - targetPost.addReaction(event.eventType - 100) + targetPost.addReaction(eventReceived.eventType - 100) return } if ( - event.eventType === EVENT_TYPES.REMOVE_REACTION_LIKE || - event.eventType === EVENT_TYPES.REMOVE_REACTION_LOVE || - event.eventType === EVENT_TYPES.REMOVE_REACTION_HAHA || - event.eventType === EVENT_TYPES.REMOVE_REACTION_WOW || - event.eventType === EVENT_TYPES.REMOVE_REACTION_SAD || - event.eventType === EVENT_TYPES.REMOVE_REACTION_ANGRY || - event.eventType === EVENT_TYPES.REMOVE_REACTION_HUG + eventReceived.eventType === EVENT_TYPES.REMOVE_REACTION_LIKE || + eventReceived.eventType === EVENT_TYPES.REMOVE_REACTION_LOVE || + eventReceived.eventType === EVENT_TYPES.REMOVE_REACTION_HAHA || + eventReceived.eventType === EVENT_TYPES.REMOVE_REACTION_WOW || + eventReceived.eventType === EVENT_TYPES.REMOVE_REACTION_SAD || + eventReceived.eventType === EVENT_TYPES.REMOVE_REACTION_ANGRY || + eventReceived.eventType === EVENT_TYPES.REMOVE_REACTION_HUG ) { - let targetPost = NT.memory.POSTS.get(event.targetPostHash) + let targetPost = NT.memory.POSTS.get(eventReceived.targetPostHash) if (targetPost === undefined) { throw ('Target Post Not Found') } - targetPost.removeReaction(event.eventType - 200) + targetPost.removeReaction(eventReceived.eventType - 200) return } /* Is is a Bot? */ if ( - event.eventType === EVENT_TYPES.ADD_BOT || - event.eventType === EVENT_TYPES.REMOVE_BOT || - event.eventType === EVENT_TYPES.ENABLE_BOT || - event.eventType === EVENT_TYPES.DISABLE_BOT + eventReceived.eventType === EVENT_TYPES.ADD_BOT || + eventReceived.eventType === EVENT_TYPES.REMOVE_BOT || + eventReceived.eventType === EVENT_TYPES.ENABLE_BOT || + eventReceived.eventType === EVENT_TYPES.DISABLE_BOT ) { - switch (event.eventType) { + switch (eventReceived.eventType) { case EVENT_TYPES.ADD_BOT: { thisObject.emitterUserProfile.addBot( thisObject.botId, diff --git a/Network/post.js b/Network/post.js index 95bbe1eb1..809f70c0f 100644 --- a/Network/post.js +++ b/Network/post.js @@ -1,10 +1,10 @@ exports.newPost = function newPost() { let thisObject = { - postHash: undefined, + emitterPostHash: undefined, + targetPostHash: undefined, postType: undefined, userProfile: undefined, - asset: undefined, timestamp: undefined, replies: undefined, targetPost: undefined, @@ -48,10 +48,10 @@ exports.newPost = function newPost() { return thisObject function finalize() { - thisObject.postHash = undefined + thisObject.emitterPostHash = undefined + thisObject.targetPostHash = undefined thisObject.postType = undefined thisObject.userProfile = undefined - thisObject.asset = undefined thisObject.timestamp = undefined thisObject.replies = undefined thisObject.targetPost = undefined @@ -62,7 +62,6 @@ exports.newPost = function newPost() { targetPostHash, postType, userProfile, - asset, timestamp ) { @@ -70,8 +69,9 @@ exports.newPost = function newPost() { thisObject.targetPostHash = targetPostHash thisObject.postType = postType thisObject.userProfile = userProfile - thisObject.asset = asset thisObject.timestamp = timestamp + + thisObject.replies = [] /* Let's find the Target Post */ diff --git a/Network/userProfile.js b/Network/userProfile.js index 40208b147..ec935aabf 100644 --- a/Network/userProfile.js +++ b/Network/userProfile.js @@ -16,7 +16,11 @@ exports.newUserProfile = function newUserProfile() { emitterEventsCount: undefined, targetEventsCount: undefined, postsCount: undefined, + botsCount: undefined, bots: undefined, + posts: undefined, + addPost: addPost, + removePost: removePost, addMultiMediaPostsFollowing: addMultiMediaPostsFollowing, removeMultiMediaPostsFollowing: removeMultiMediaPostsFollowing, addTradePostsFollowing: addTradePostsFollowing, @@ -42,6 +46,7 @@ exports.newUserProfile = function newUserProfile() { thisObject.tradePostsFollowing = undefined thisObject.tradePostsFollowers = undefined + thisObject.posts = undefined thisObject.bots = undefined } @@ -60,8 +65,58 @@ exports.newUserProfile = function newUserProfile() { thisObject.emitterEventsCount = 0 thisObject.targetEventsCount = 0 thisObject.postsCount = 0 + thisObject.botsCount = 0 - thisObject.bots = new Map() + thisObject.posts = undefined + thisObject.bots = undefined + } + + function addPost( + emitterPostHash, + targetPostHash, + postType, + userProfile, + timestamp + ) { + if (thisObject.posts.get(emitterPostHash) !== undefined) { + throw ('Post Already Exists.') + } + + if (NT.memory.POSTS.get(emitterPostHash) !== undefined) { + throw ('Post Already Exists.') + } + + let post = NT.modules.POST.newPost() + post.initialize( + emitterPostHash, + targetPostHash, + postType, + userProfile, + timestamp + ) + + thisObject.posts.set(emitterPostHash, post) + NT.memory.POSTS.set(emitterPostHash, post) + thisObject.postsCount++ + } + + function removePost( + emitterPostHash + ) { + if (thisObject.posts.get(emitterPostHash) === undefined) { + throw ('Post Does Not Exist.') + } + + if (NT.memory.POSTS.get(emitterPostHash) === undefined) { + throw ('Post Does Not Exist.') + } + + let post = thisObject.posts.get(emitterPostHash) + post.finalize() + + thisObject.posts.delete(emitterPostHash) + NT.memory.POSTS.delete(emitterPostHash) + thisObject.postsCount-- } function addMultiMediaPostsFollowing( @@ -152,6 +207,7 @@ exports.newUserProfile = function newUserProfile() { botExchange ) thisObject.bots.set(botId, bot) + thisObject.botsCount++ } function removeBot( @@ -160,8 +216,9 @@ exports.newUserProfile = function newUserProfile() { if (thisObject.bots.get(botId) === undefined) { throw ('Bot Does Not Exist.') } - + thisObject.bots.get(botId).finalize() thisObject.bots.delete(botId) + thisObject.botsCount-- } function enableBot(