Query Post Replies Implemented

This commit is contained in:
Luis Fernando Molina committed 2021-08-27 10:52:09 +02:00
1 parent 830a92c486
commit 658bfd2b0d
3 files changed
+161 -21

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
exports.newPostReplies = function newPostReplies() {
let thisObject = {
post: undefined,
initialReplyIndex: undefined,
amountRequested: undefined,
direction: undefined,
execute: execute,
initialize: initialize,
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() {
}
function initialize(queryReceived) {
/*
Validate Post
*/
thisObject.post = NT.memory.POSTS.get(queryReceived.targetPostHash)
if (thisObject.post === undefined) {
throw ('Target Post Not Found.')
}
/*
Validate Initial Reply Index
*/
if (queryReceived.initialReplyIndex === undefined) {
throw ('Initial Reply Index Undefined.')
}
if (queryReceived.initialReplyIndex === INITIAL_REPLY_INDEX_LAST) {
queryReceived.initialReplyIndex = thisObject.poat.replies.length - 1
}
if (queryReceived.initialReplyIndex === INITIAL_REPLY_INDEX_FIRST) {
queryReceived.initialReplyIndex = 0
}
if (isNaN(queryReceived.initialReplyIndex) === true) {
throw ('Initial Reply Is Not a Number.')
}
thisObject.initialReplyIndex = queryReceived.initialReplyIndex
/*
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 = []
switch (thisObject.direction) {
case DIRECTION_FUTURE: {
for (let i = thisObject.initialReplyIndex; i < thisObject.initialReplyIndex + thisObject.amountRequested; i++) {
let post = thisObject.post.replies[i]
if (post === undefined) { break }
addPostToResponse(post)
}
}
case DIRECTION_PAST: {
for (let i = thisObject.initialReplyIndex; i > thisObject.initialReplyIndex - thisObject.amountRequested; i--) {
let post = thisObject.post.replies[i]
if (post === undefined) { break }
addPostToResponse(post)
}
}
}
return response
function addPostToResponse(post) {
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)
}
}
}
+29 -20
View File
@@ -44,7 +44,7 @@ exports.newProfilePosts = function newProfilePosts() {
}
if (queryReceived.initialPostIndex === INITIAL_POST_INDEX_LAST) {
queryReceived.initialPostIndex = thisObject.posts.length - 1
queryReceived.initialPostIndex = thisObject.profile.posts.length - 1
}
if (queryReceived.initialPostIndex === INITIAL_POST_INDEX_FIRST) {
@@ -75,7 +75,7 @@ exports.newProfilePosts = function newProfilePosts() {
throw ('Amount Requested Above Max.')
}
/*
Validate Amount Requested
Validate Direction
*/
if (queryReceived.direction === undefined) {
throw ('Direction Undefined.')
@@ -90,33 +90,42 @@ exports.newProfilePosts = function newProfilePosts() {
}
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)
addPostToResponse(post)
}
}
case DIRECTION_PAST: {
for (let i = thisObject.initialPostIndex; i > thisObject.initialPostIndex - thisObject.amountRequested; i--) {
let post = thisObject.profile.posts[i]
if (post === undefined) { break }
addPostToResponse(post)
}
}
}
return response
function addPostToResponse(post) {
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)
}
}
}
+5 -1
View File
@@ -61,7 +61,11 @@ exports.newClientInterface = function newClientInterface() {
"timestamp": 124234234234,
"botId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"botAsset": "BTC",
"botExchange": "Binance"
"botExchange": "Binance",
"initialReplyIndex": 25,
"initialPostIndex": 35,
"amountRequested": 50,
"direction": "Past"
}
*/