diff --git a/Network/Queries/PostReplies.js b/Network/Queries/PostReplies.js index 6a27a1cc5..6eb79e96c 100644 --- a/Network/Queries/PostReplies.js +++ b/Network/Queries/PostReplies.js @@ -5,6 +5,7 @@ exports.newPostReplies = function newPostReplies() { fetch the posts metadata that are replies to a certain post. */ let thisObject = { + profile: undefined, post: undefined, initialIndex: undefined, amountRequested: undefined, @@ -14,98 +15,21 @@ exports.newPostReplies = function newPostReplies() { finalize: finalize } - const INITIAL_REPLY_INDEX_FIRST = 'First' - const INITIAL_REPLY_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 thisObject.post = undefined } function initialize(queryReceived) { - let profile - /* - Validate User Profile - */ - if (queryReceived.targetUserProfileId !== undefined) { - profile = NT.memory.maps.USER_PROFILES_BY_ID.get(queryReceived.targetUserProfileId) - } - if (profile === undefined) { - throw ('Target User Profile Not Found.') - } - /* - Validate Bot Profile - */ - if (queryReceived.targetBotProfileId !== undefined) { - profile = profile.bots.get(queryReceived.targetBotProfileId) - if (profile === undefined) { - throw ('Target Bot Profile Not Found.') - } - } - /* - Validate Post - */ - thisObject.post = profile.posts.get(queryReceived.targetPostHash) - - if (thisObject.post === undefined) { - throw ('Target Post Not Found.') - } - /* - Validate Initial Index - */ - if (queryReceived.initialIndex === undefined) { - throw ('Initial Index Undefined.') - } - - if (queryReceived.initialIndex === INITIAL_REPLY_INDEX_LAST) { - queryReceived.initialIndex = thisObject.poat.replies.length - 1 - } - - if (queryReceived.initialIndex === INITIAL_REPLY_INDEX_FIRST) { - queryReceived.initialIndex = 0 - } - - if (isNaN(queryReceived.initialIndex) === true) { - throw ('Initial Reply 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 + NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.utilities.queriesValidations.postValidations(queryReceived, thisObject) + NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) } diff --git a/Network/Queries/ProfileFollowers.js b/Network/Queries/ProfileFollowers.js index 915f65c1a..371bfde95 100644 --- a/Network/Queries/ProfileFollowers.js +++ b/Network/Queries/ProfileFollowers.js @@ -10,10 +10,6 @@ exports.newProfileFollowers = function newProfileFollowers() { 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' @@ -24,75 +20,9 @@ exports.newProfileFollowers = function newProfileFollowers() { } 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 + NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) } diff --git a/Network/Queries/ProfileFollowing.js b/Network/Queries/ProfileFollowing.js index 8c0336190..09c6d29d7 100644 --- a/Network/Queries/ProfileFollowing.js +++ b/Network/Queries/ProfileFollowing.js @@ -10,10 +10,6 @@ exports.newProfileFollowing = function newProfileFollowing() { 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' @@ -24,75 +20,9 @@ exports.newProfileFollowing = function newProfileFollowing() { } 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 + NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) } diff --git a/Network/Queries/ProfilePosts.js b/Network/Queries/ProfilePosts.js index 60e83d135..02e8fcfe0 100644 --- a/Network/Queries/ProfilePosts.js +++ b/Network/Queries/ProfilePosts.js @@ -14,10 +14,6 @@ exports.newProfilePosts = function newProfilePosts() { 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' @@ -28,76 +24,9 @@ exports.newProfilePosts = function newProfilePosts() { } 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 + NT.utilities.queriesValidations.profilesValidations(queryReceived, thisObject) + NT.utilities.queriesValidations.arrayValidations(queryReceived, thisObject) } function execute() { diff --git a/Network/SocialGraphNetwokNode.js b/Network/SocialGraphNetwokNode.js index c1611edbe..7073ba1a9 100644 --- a/Network/SocialGraphNetwokNode.js +++ b/Network/SocialGraphNetwokNode.js @@ -26,6 +26,9 @@ global.NT = { //QUERY_PROFILE_FOLLOWING: require('./Queries/ProfileFollowing.js'), //QUERY_POST_REPLIES: require('./Queries/ProfileReplies.js'), QUERY_EVENTS: require('./Queries/Events.js') + }, + utilities: { + queriesValidations: require('./Utilities/QueriesValidations.js').newProfileQueries() } } diff --git a/Network/Utilities/QueriesValidations.js b/Network/Utilities/QueriesValidations.js new file mode 100644 index 000000000..ba0934ec9 --- /dev/null +++ b/Network/Utilities/QueriesValidations.js @@ -0,0 +1,104 @@ +exports.newProfileFollowers = function newProfileFollowers() { + + let thisObject = { + profilesValidations: profilesValidations, + postValidations: postValidations, + arrayValidations: arrayValidations + } + + 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 profilesValidations(queryReceived, thisObject) { + /* + 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.') + } + } + } + + function postValidations(queryReceived, thisObject) { + /* + Validate Post + */ + thisObject.post = thisObject.profile.posts.get(queryReceived.targetPostHash) + + if (thisObject.post === undefined) { + throw ('Target Post Not Found.') + } + } + + function arrayValidations(queryReceived, thisObject) { + /* + 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 + + } +} \ No newline at end of file