From 541370c6a8a7c7e696358daff099954a4f343ffa Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sun, 29 Aug 2021 17:11:14 +0200 Subject: [PATCH] Queries Following and Followers --- Network/Queries/ProfileFollowers.js | 21 ++--- Network/Queries/ProfileFollowing.js | 132 ++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 Network/Queries/ProfileFollowing.js diff --git a/Network/Queries/ProfileFollowers.js b/Network/Queries/ProfileFollowers.js index 829a5f212..915f65c1a 100644 --- a/Network/Queries/ProfileFollowers.js +++ b/Network/Queries/ProfileFollowers.js @@ -29,8 +29,6 @@ exports.newProfileFollowers = function newProfileFollowers() { */ if (queryReceived.targetUserProfileId !== undefined) { thisObject.profile = NT.memory.maps.USER_PROFILES_BY_ID.get(queryReceived.targetUserProfileId) - } else { - thisObject.profile = NT.memory.maps.USER_PROFILES_BY_HANDLE.get(queryReceived.targetUserProfileHandle) } if (thisObject.profile === undefined) { @@ -99,11 +97,14 @@ exports.newProfileFollowers = function newProfileFollowers() { } function execute() { + let response = [] + let array = Array.from(thisObject.profile.followers) + switch (thisObject.direction) { case DIRECTION_FUTURE: { for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { - let arrayItem = thisObject.profile.arrays.followers[i] + let arrayItem = array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } @@ -111,7 +112,7 @@ exports.newProfileFollowers = function newProfileFollowers() { } case DIRECTION_PAST: { for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { - let arrayItem = thisObject.profile.arrays.followers[i] + let arrayItem = array[i] if (arrayItem === undefined) { break } addToResponse(arrayItem) } @@ -120,16 +121,10 @@ exports.newProfileFollowers = function newProfileFollowers() { } return response - function addToResponse(post) { + function addToResponse(arrayItem) { let postResponse = { - emitterUserProfileId: post.emitterUserProfileId, - targetUserProfileId: post.targetUserProfileId, - emitterPostHash: post.emitterPostHash, - targetPostHash: post.targetPostHash, - postType: post.postType, - timestamp: post.timestamp, - repliesCount: post.replies.length, - reactions: Array.from(post.reactions) + emitterUserProfileId: arrayItem.emitterUserProfileId, + emitterBotProfileId: arrayItem.emitterBotProfileId } response.push(postResponse) } diff --git a/Network/Queries/ProfileFollowing.js b/Network/Queries/ProfileFollowing.js new file mode 100644 index 000000000..8c0336190 --- /dev/null +++ b/Network/Queries/ProfileFollowing.js @@ -0,0 +1,132 @@ +exports.newProfileFollowing = function newProfileFollowing() { + + let thisObject = { + profile: undefined, + initialIndex: 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() { + thisObject.profile = undefined + } + + function initialize(queryReceived) { + /* + Validate User Profile + */ + if (queryReceived.targetUserProfileId !== undefined) { + thisObject.profile = NT.memory.maps.USER_PROFILES_BY_ID.get(queryReceived.targetUserProfileId) + } + + if (thisObject.profile === undefined) { + throw ('Target User Profile Not Found.') + } + /* + Validate Bot Profile + */ + if (queryReceived.targetBotProfileId !== undefined) { + thisObject.profile = thisObject.profile.bots.get(queryReceived.targetBotProfileId) + if (thisObject.profile === undefined) { + throw ('Target Bot Profile Not Found.') + } + } + /* + Validate Initial Index + */ + if (queryReceived.initialIndex === undefined) { + throw ('Initial Index Undefined.') + } + + if (queryReceived.initialIndex === INITIAL_POST_INDEX_LAST) { + queryReceived.initialIndex = thisObject.profile.posts.length - 1 + } + + if (queryReceived.initialIndex === INITIAL_POST_INDEX_FIRST) { + queryReceived.initialIndex = 0 + } + + if (isNaN(queryReceived.initialIndex) === true) { + throw ('Initial Post 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 < MIN_AMOUNT_REQUESTED) { + throw ('Amount Requested Below Min.') + } + + if (queryReceived.amountRequested > MAX_AMOUNT_REQUESTED) { + throw ('Amount Requested Above Max.') + } + /* + Validate Direction + */ + 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 = [] + let array = Array.from(thisObject.profile.following) + + switch (thisObject.direction) { + case DIRECTION_FUTURE: { + for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) { + let arrayItem = array[i] + if (arrayItem === undefined) { break } + addToResponse(arrayItem) + } + break + } + case DIRECTION_PAST: { + for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) { + let arrayItem = array[i] + if (arrayItem === undefined) { break } + addToResponse(arrayItem) + } + break + } + } + return response + + function addToResponse(arrayItem) { + let postResponse = { + emitterUserProfileId: arrayItem.emitterUserProfileId, + emitterBotProfileId: arrayItem.emitterBotProfileId + } + response.push(postResponse) + } + } +} \ No newline at end of file