From 685c2b30d6b8e954f81e2ac0412503c72596b177 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 26 Aug 2021 18:20:07 +0200 Subject: [PATCH] Profile Posts Query Implemented --- Network/NetwokNode.js | 8 +- Network/Queries/ProfilePosts.js | 122 ++++++++++++++++++ .../ProfileStats.js} | 10 +- Network/Query.js | 8 +- Network/post.js | 3 + Network/userProfile.js | 21 ++- 6 files changed, 148 insertions(+), 24 deletions(-) create mode 100644 Network/Queries/ProfilePosts.js rename Network/{ProfileStatsQuery.js => Queries/ProfileStats.js} (83%) diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index 4bb49d8da..20a2d0fef 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -16,9 +16,9 @@ global.NT = { POST: require('./Post.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') + QUERY_PROFILE_STATS: require('./Queries/ProfileStats.js'), + QUERY_PROFILE_POSTS: require('./Queries/ProfilePosts.js'), + QUERY_POST_REPLIES: require('./Queries/ProfileReplies.js'), + QUERY_TIMELINE_CHUNK: require('./Queries/TimelineChunk.js') } } \ No newline at end of file diff --git a/Network/Queries/ProfilePosts.js b/Network/Queries/ProfilePosts.js new file mode 100644 index 000000000..e80302005 --- /dev/null +++ b/Network/Queries/ProfilePosts.js @@ -0,0 +1,122 @@ +exports.newProfilePosts = function newProfilePosts() { + + let thisObject = { + profile: undefined, + initialPostIndex: undefined, + amountRequested: undefined, + direction: undefined, + execute: execute, + initialize: initialize, + finalize: finalize + } + + const INITIAL_POST_INDEX_FIRST = 'First' + const INITIAL_POST_INDEX_LAST = 'Last' + const MIN_AMOUNT_REQUESTED = 1 + const MAX_AMOUNT_REQUESTED = 100 + const DIRECTION_FUTURE = 'Future' + const DIRECTION_PAST = 'Past' + + return thisObject + + function finalize() { + + } + + function initialize(queryReceived) { + /* + Validate User Profile + */ + if (queryReceived.targetUserProfileId !== undefined) { + thisObject.profile = NT.memory.USER_PROFILES_BY_ID.get(queryReceived.targetUserProfileId) + } else { + thisObject.profile = NT.memory.USER_PROFILES_BY_HANDLE.get(queryReceived.targetUserProfileHandle) + } + + if (thisObject.profile === undefined) { + throw ('Target User Profile Not Found.') + } + /* + Validate Initial Post Index + */ + if (queryReceived.initialPostIndex === undefined) { + throw ('Initial Post Index Undefined.') + } + + if (queryReceived.initialPostIndex === INITIAL_POST_INDEX_LAST) { + queryReceived.initialPostIndex = thisObject.posts.length - 1 + } + + if (queryReceived.initialPostIndex === INITIAL_POST_INDEX_FIRST) { + queryReceived.initialPostIndex = 0 + } + + if (isNaN(queryReceived.initialPostIndex) === true) { + throw ('Initial Post Is Not a Number.') + } + + thisObject.initialPostIndex = queryReceived.initialPostIndex + /* + 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 < MIN_AMOUNT_REQUESTED) { + throw ('Amount Requested Below Min.') + } + + if (queryReceived.amountRequested > MAX_AMOUNT_REQUESTED) { + throw ('Amount Requested Above Max.') + } + /* + Validate Amount Requested + */ + if (queryReceived.direction === undefined) { + throw ('Direction Undefined.') + } + + if (queryReceived.direction !== DIRECTION_FUTURE && queryReceived.direction !== DIRECTION_PAST) { + throw ('Direction Not Supported.') + } + + thisObject.direction = queryReceived.direction + + } + + function execute() { + + let response = [] + switch (thisObject.direction) { + case DIRECTION_FUTURE: { + for (let i = thisObject.initialPostIndex; i < thisObject.initialPostIndex + thisObject.amountRequested; i++) { + let post = thisObject.profile.posts[i] + if (post === undefined) { break } + + let postResponse = { + "emitterPostHash": post.emitterPostHash, + "targetPostHash": post.targetPostHash, + "postType": post.postType, + "userProfile": post.userProfile.userProfieId, + "timestamp": post.timestamp, + "repliesCount": post.replies.length, + "targetPostHash": post.targetPost.emitterPostHash, + "reactionsCount": [] + } + + for (let i = 0; i < post.reactionTypesCount; i++) { + postResponse.reactionsCount.push(post.reactionsCount.get(i)) + } + + response.push(postResponse) + } + } + } + return response + } +} \ No newline at end of file diff --git a/Network/ProfileStatsQuery.js b/Network/Queries/ProfileStats.js similarity index 83% rename from Network/ProfileStatsQuery.js rename to Network/Queries/ProfileStats.js index 2de22735e..f4bb73e19 100644 --- a/Network/ProfileStatsQuery.js +++ b/Network/Queries/ProfileStats.js @@ -1,4 +1,4 @@ -exports.newProfileStatsQuery = function newProfileStatsQuery() { +exports.newProfileStats = function newProfileStats() { let thisObject = { profile: undefined, @@ -14,11 +14,13 @@ exports.newProfileStatsQuery = function newProfileStatsQuery() { } function initialize(queryReceived) { - + /* + Validate User Profile + */ if (queryReceived.targetUserProfileId !== undefined) { thisObject.profile = NT.memory.USER_PROFILES_BY_ID.get(queryReceived.targetUserProfileId) } else { - thisObject.profile = NT.memory.USER_PROFILES_BY_ID.get(queryReceived.targetUserProfileHandle) + thisObject.profile = NT.memory.USER_PROFILES_BY_HANDLE.get(queryReceived.targetUserProfileHandle) } if (thisObject.profile === undefined) { @@ -31,7 +33,7 @@ exports.newProfileStatsQuery = function newProfileStatsQuery() { return { "userProfieId": thisObject.profile.userProfieId, - "userHandle": thisObject.profile.userHandle, + "userProfileHandle": thisObject.profile.userProfileHandle, "blockchainAccount": thisObject.profile.blockchainAccount, "ranking": thisObject.profile.ranking, "multiMediaPostFollowingCount": thisObject.profile.multiMediaPostFollowingCount, diff --git a/Network/Query.js b/Network/Query.js index a77806943..aa852b0e6 100644 --- a/Network/Query.js +++ b/Network/Query.js @@ -31,25 +31,25 @@ exports.newQuery = function newQuery() { switch (thisObject.queryType) { case QUERY_TYPES.PROFILE_STATS: { - thisObject.query = NT.modules.PROFILE_STATS_QUERY.newProfileStatsQuery() + thisObject.query = NT.modules.QUERY_PROFILE_STATS.newProfileStats() query.initialize(queryReceived) break } case QUERY_TYPES.PROFILE_POSTS: { - thisObject.query = NT.modules.PROFILE_POSTS_QUERY.newProfileStatsQuery() + thisObject.query = NT.modules.QUERY_PROFILE_POSTS.newProfilePosts() query.initialize(queryReceived) break } case QUERY_TYPES.POST_REPLIES: { - thisObject.query = NT.modules.POST_REPLIES_QUERY.newProfileStatsQuery() + thisObject.query = NT.modules.QUERY_POST_REPLIES.newPostReplies() query.initialize(queryReceived) break } case QUERY_TYPES.TIMELINE_CHUNK: { - thisObject.query = NT.modules.TIMELINE_CHUNK_QUERY.newProfileStatsQuery() + thisObject.query = NT.modules.QUERY_TIMELINE_CHUNK.newTimelineChunk() query.initialize(queryReceived) break } diff --git a/Network/post.js b/Network/post.js index 809f70c0f..97509e6e3 100644 --- a/Network/post.js +++ b/Network/post.js @@ -9,6 +9,7 @@ exports.newPost = function newPost() { replies: undefined, targetPost: undefined, reactionsCount: undefined, + reactionTypesCount: undefined, addReaction: addReaction, removeReactions: removeReaction, initialize: initialize, @@ -36,6 +37,8 @@ exports.newPost = function newPost() { REACTION_HUG: 6 } + thisObject.reactionTypesCount = 7 + thisObject.reactionsCount = new Map() thisObject.reactionsCount.set(REACTION_TYPES.REACTION_LIKE, 0) thisObject.reactionsCount.set(REACTION_TYPES.REACTION_LOVE, 0) diff --git a/Network/userProfile.js b/Network/userProfile.js index ab4a3fe50..d1f2863e1 100644 --- a/Network/userProfile.js +++ b/Network/userProfile.js @@ -2,7 +2,7 @@ exports.newUserProfile = function newUserProfile() { let thisObject = { userProfieId: undefined, - userHandle: undefined, + userProfileHandle: undefined, blockchainAccount: undefined, multiMediaPostsFollowing: undefined, multiMediaPostsFollowers: undefined, @@ -67,7 +67,7 @@ exports.newUserProfile = function newUserProfile() { thisObject.postsCount = 0 thisObject.botsCount = 0 - thisObject.posts = undefined + thisObject.posts = [] thisObject.bots = undefined } @@ -78,10 +78,6 @@ exports.newUserProfile = function newUserProfile() { userProfile, timestamp ) { - if (thisObject.posts.get(emitterPostHash) !== undefined) { - throw ('Post Already Exists.') - } - if (NT.memory.POSTS.get(emitterPostHash) !== undefined) { throw ('Post Already Exists.') } @@ -95,7 +91,7 @@ exports.newUserProfile = function newUserProfile() { timestamp ) - thisObject.posts.set(emitterPostHash, post) + thisObject.posts.push(post) NT.memory.POSTS.set(emitterPostHash, post) thisObject.postsCount++ } @@ -103,10 +99,6 @@ exports.newUserProfile = function newUserProfile() { 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.') } @@ -114,7 +106,12 @@ exports.newUserProfile = function newUserProfile() { let post = thisObject.posts.get(emitterPostHash) post.finalize() - thisObject.posts.delete(emitterPostHash) + for (let i = thisObject.posts.length - 1; i >= 0; i-- { + if (thisObject.posts[i].emitterPostHash === emitterPostHash) { + thisObject.posts.splice(i, 1) + break + } + } NT.memory.POSTS.delete(emitterPostHash) thisObject.postsCount-- }