Profile Posts Query Implemented

This commit is contained in:
Luis Fernando Molina committed 2021-08-26 18:20:07 +02:00
1 parent dba49aa67d
commit 685c2b30d6
6 files changed
+148 -24

No files matched your search

+4 -4
View File
@@ -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')
}
}
+122
View File
@@ -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
}
}
@@ -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,
+4 -4
View File
@@ -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
}
+3
View File
@@ -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)
+9 -12
View File
@@ -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--
}