initialIndex simplified
This commit is contained in:
1 parent
104499ab57
commit
777b57703a
4 files changed
+158
-28
No files matched your search
@@ -0,0 +1,131 @@
|
||||
exports.newEvents = function newEvents() {
|
||||
|
||||
let thisObject = {
|
||||
profile: undefined,
|
||||
initialIndex: undefined,
|
||||
amountRequested: undefined,
|
||||
direction: undefined,
|
||||
execute: execute,
|
||||
initialize: initialize,
|
||||
finalize: finalize
|
||||
}
|
||||
|
||||
const INITIAL_EVENT_INDEX_FIRST = 'First'
|
||||
const INITIAL_EVENT_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.emitterUserProfileId !== undefined) {
|
||||
thisObject.profile = NT.memory.maps.USER_PROFILES_BY_ID.get(queryReceived.emitterUserProfileId)
|
||||
} else {
|
||||
thisObject.profile = NT.memory.maps.USER_PROFILES_BY_HANDLE.get(queryReceived.emitterUserProfileHandle)
|
||||
}
|
||||
|
||||
if (thisObject.profile === undefined) {
|
||||
throw ('Emitter User Profile Not Found.')
|
||||
}
|
||||
/*
|
||||
Validate Initial Index
|
||||
*/
|
||||
if (queryReceived.initialIndex === undefined) {
|
||||
throw ('Initial Index Undefined.')
|
||||
}
|
||||
|
||||
if (queryReceived.initialIndex === INITIAL_EVENT_INDEX_LAST) {
|
||||
queryReceived.initialIndex = thisObject.profile.maps.POSTS.length - 1
|
||||
}
|
||||
|
||||
if (queryReceived.initialIndex === INITIAL_EVENT_INDEX_FIRST) {
|
||||
queryReceived.initialIndex = 0
|
||||
}
|
||||
|
||||
if (isNaN(queryReceived.initialIndex) === true) {
|
||||
throw ('Initial Event 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 = []
|
||||
switch (thisObject.direction) {
|
||||
case DIRECTION_FUTURE: {
|
||||
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
|
||||
let post = thisObject.profile.maps.POSTS[i]
|
||||
if (post === undefined) { break }
|
||||
addPostToResponse(post)
|
||||
}
|
||||
}
|
||||
case DIRECTION_PAST: {
|
||||
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
|
||||
let post = thisObject.profile.maps.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ exports.newPostReplies = function newPostReplies() {
|
||||
|
||||
let thisObject = {
|
||||
post: undefined,
|
||||
initialReplyIndex: undefined,
|
||||
initialIndex: undefined,
|
||||
amountRequested: undefined,
|
||||
direction: undefined,
|
||||
execute: execute,
|
||||
@@ -20,7 +20,7 @@ exports.newPostReplies = function newPostReplies() {
|
||||
return thisObject
|
||||
|
||||
function finalize() {
|
||||
|
||||
thisObject.post = undefined
|
||||
}
|
||||
|
||||
function initialize(queryReceived) {
|
||||
@@ -33,25 +33,25 @@ exports.newPostReplies = function newPostReplies() {
|
||||
throw ('Target Post Not Found.')
|
||||
}
|
||||
/*
|
||||
Validate Initial Reply Index
|
||||
Validate Initial Index
|
||||
*/
|
||||
if (queryReceived.initialReplyIndex === undefined) {
|
||||
throw ('Initial Reply Index Undefined.')
|
||||
if (queryReceived.initialIndex === undefined) {
|
||||
throw ('Initial Index Undefined.')
|
||||
}
|
||||
|
||||
if (queryReceived.initialReplyIndex === INITIAL_REPLY_INDEX_LAST) {
|
||||
queryReceived.initialReplyIndex = thisObject.poat.replies.length - 1
|
||||
if (queryReceived.initialIndex === INITIAL_REPLY_INDEX_LAST) {
|
||||
queryReceived.initialIndex = thisObject.poat.replies.length - 1
|
||||
}
|
||||
|
||||
if (queryReceived.initialReplyIndex === INITIAL_REPLY_INDEX_FIRST) {
|
||||
queryReceived.initialReplyIndex = 0
|
||||
if (queryReceived.initialIndex === INITIAL_REPLY_INDEX_FIRST) {
|
||||
queryReceived.initialIndex = 0
|
||||
}
|
||||
|
||||
if (isNaN(queryReceived.initialReplyIndex) === true) {
|
||||
if (isNaN(queryReceived.initialIndex) === true) {
|
||||
throw ('Initial Reply Is Not a Number.')
|
||||
}
|
||||
|
||||
thisObject.initialReplyIndex = queryReceived.initialReplyIndex
|
||||
thisObject.initialIndex = queryReceived.initialIndex
|
||||
/*
|
||||
Validate Amount Requested
|
||||
*/
|
||||
@@ -89,14 +89,14 @@ exports.newPostReplies = function newPostReplies() {
|
||||
let response = []
|
||||
switch (thisObject.direction) {
|
||||
case DIRECTION_FUTURE: {
|
||||
for (let i = thisObject.initialReplyIndex; i < thisObject.initialReplyIndex + thisObject.amountRequested; i++) {
|
||||
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + 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--) {
|
||||
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
|
||||
let post = thisObject.post.replies[i]
|
||||
if (post === undefined) { break }
|
||||
addPostToResponse(post)
|
||||
|
||||
@@ -2,7 +2,7 @@ exports.newProfilePosts = function newProfilePosts() {
|
||||
|
||||
let thisObject = {
|
||||
profile: undefined,
|
||||
initialPostIndex: undefined,
|
||||
initialIndex: undefined,
|
||||
amountRequested: undefined,
|
||||
direction: undefined,
|
||||
execute: execute,
|
||||
@@ -20,7 +20,7 @@ exports.newProfilePosts = function newProfilePosts() {
|
||||
return thisObject
|
||||
|
||||
function finalize() {
|
||||
|
||||
thisObject.profile = undefined
|
||||
}
|
||||
|
||||
function initialize(queryReceived) {
|
||||
@@ -37,25 +37,25 @@ exports.newProfilePosts = function newProfilePosts() {
|
||||
throw ('Target User Profile Not Found.')
|
||||
}
|
||||
/*
|
||||
Validate Initial Post Index
|
||||
Validate Initial Index
|
||||
*/
|
||||
if (queryReceived.initialPostIndex === undefined) {
|
||||
throw ('Initial Post Index Undefined.')
|
||||
if (queryReceived.initialIndex === undefined) {
|
||||
throw ('Initial Index Undefined.')
|
||||
}
|
||||
|
||||
if (queryReceived.initialPostIndex === INITIAL_POST_INDEX_LAST) {
|
||||
queryReceived.initialPostIndex = thisObject.profile.maps.POSTS.length - 1
|
||||
if (queryReceived.initialIndex === INITIAL_POST_INDEX_LAST) {
|
||||
queryReceived.initialIndex = thisObject.profile.maps.POSTS.length - 1
|
||||
}
|
||||
|
||||
if (queryReceived.initialPostIndex === INITIAL_POST_INDEX_FIRST) {
|
||||
queryReceived.initialPostIndex = 0
|
||||
if (queryReceived.initialIndex === INITIAL_POST_INDEX_FIRST) {
|
||||
queryReceived.initialIndex = 0
|
||||
}
|
||||
|
||||
if (isNaN(queryReceived.initialPostIndex) === true) {
|
||||
if (isNaN(queryReceived.initialIndex) === true) {
|
||||
throw ('Initial Post Is Not a Number.')
|
||||
}
|
||||
|
||||
thisObject.initialPostIndex = queryReceived.initialPostIndex
|
||||
thisObject.initialIndex = queryReceived.initialIndex
|
||||
/*
|
||||
Validate Amount Requested
|
||||
*/
|
||||
@@ -93,14 +93,14 @@ exports.newProfilePosts = function newProfilePosts() {
|
||||
let response = []
|
||||
switch (thisObject.direction) {
|
||||
case DIRECTION_FUTURE: {
|
||||
for (let i = thisObject.initialPostIndex; i < thisObject.initialPostIndex + thisObject.amountRequested; i++) {
|
||||
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
|
||||
let post = thisObject.profile.maps.POSTS[i]
|
||||
if (post === undefined) { break }
|
||||
addPostToResponse(post)
|
||||
}
|
||||
}
|
||||
case DIRECTION_PAST: {
|
||||
for (let i = thisObject.initialPostIndex; i > thisObject.initialPostIndex - thisObject.amountRequested; i--) {
|
||||
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
|
||||
let post = thisObject.profile.maps.POSTS[i]
|
||||
if (post === undefined) { break }
|
||||
addPostToResponse(post)
|
||||
|
||||
@@ -62,8 +62,7 @@ exports.newClientInterface = function newClientInterface() {
|
||||
"botId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
|
||||
"botAsset": "BTC",
|
||||
"botExchange": "Binance",
|
||||
"initialReplyIndex": 25,
|
||||
"initialPostIndex": 35,
|
||||
"initialIndex": 35,
|
||||
"amountRequested": 50,
|
||||
"direction": "Past"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user