User Profile re-written

This commit is contained in:
Luis Fernando Molina committed 2021-08-29 09:16:30 +02:00
1 parent b7fdc7feb7
commit 1cd04a056b
10 files changed
+308 -118

No files matched your search

+12 -2
View File
@@ -23,7 +23,17 @@ global.NT = {
QUERY_USER_PROFILE_STATS: require('./Queries/UserProfileStats.js'),
QUERY_BOT_PROFILE_STATS: require('./Queries/BotProfileStats.js'),
QUERY_PROFILE_POSTS: require('./Queries/ProfilePosts.js'),
QUERY_POST_REPLIES: require('./Queries/ProfileReplies.js'),
QUERY_PROFILE_FOLLOWERS: require('./Queries/ProfileFollowers.js'),
//QUERY_PROFILE_FOLLOWING: require('./Queries/ProfileFollowing.js'),
//QUERY_POST_REPLIES: require('./Queries/ProfileReplies.js'),
QUERY_EVENTS: require('./Queries/Events.js')
}
}
}
const map = new Map([[1, { a: 2 }], [2, { a: 4 }], [4, { a: 8 }]])
map.set(3, { a: 6 })
map.delete(2)
map.set(2, { a: 12 })
let array = Array.from(map)
console.log(JSON.stringify(array))
-1
View File
@@ -41,7 +41,6 @@ exports.newBotProfileStats = function newBotProfileStats() {
return {
"botProfileId": thisObject.profile.botProfileId,
"botProfileHandle": thisObject.profile.botProfileHandle,
"ranking": thisObject.profile.ranking,
"followingCount": thisObject.profile.followingCount,
"followersCount": thisObject.profile.followersCount,
"emitterEventsCount": thisObject.profile.emitterEventsCount,
+142
View File
@@ -0,0 +1,142 @@
exports.newProfileFollowers = function newProfileFollowers() {
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)
} else {
thisObject.profile = NT.memory.maps.USER_PROFILES_BY_HANDLE.get(queryReceived.targetUserProfileHandle)
}
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 = []
switch (thisObject.direction) {
case DIRECTION_FUTURE: {
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
let arrayItem = thisObject.profile.arrays.followers[i]
if (arrayItem === undefined) { break }
addToResponse(arrayItem)
}
break
}
case DIRECTION_PAST: {
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
let arrayItem = thisObject.profile.arrays.followers[i]
if (arrayItem === undefined) { break }
addToResponse(arrayItem)
}
break
}
}
return response
function addToResponse(post) {
let postResponse = {
emitterUserProfileId: post.emitterUserProfileId,
targetUserProfileId: post.targetUserProfileId,
emitterPostHash: post.emitterPostHash,
targetPostHash: post.targetPostHash,
postType: post.postType,
timestamp: post.timestamp,
repliesCount: post.replies.length,
reactionsCount: []
}
for (let i = 0; i < post.reactionTypesCount; i++) {
postResponse.reactionsCount.push(post.reactionsCount.get(i))
}
response.push(postResponse)
}
}
}
+15 -6
View File
@@ -36,6 +36,15 @@ exports.newProfilePosts = function newProfilePosts() {
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
*/
@@ -94,17 +103,17 @@ exports.newProfilePosts = function newProfilePosts() {
switch (thisObject.direction) {
case DIRECTION_FUTURE: {
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
let post = thisObject.profile.posts[i]
if (post === undefined) { break }
addToResponse(post)
let arrayItem = thisObject.profile.arrays.posts[i]
if (arrayItem === undefined) { break }
addToResponse(arrayItem)
}
break
}
case DIRECTION_PAST: {
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
let post = thisObject.profile.posts[i]
if (post === undefined) { break }
addToResponse(post)
let arrayItem = thisObject.profile.arrays.posts[i]
if (arrayItem === undefined) { break }
addToResponse(arrayItem)
}
break
}
+18 -2
View File
@@ -13,8 +13,10 @@ exports.newQuery = function newQuery() {
USER_PROFILE_STATS: 0,
BOT_PROFILE_STATS: 1,
PROFILE_POSTS: 2,
POST_REPLIES: 3,
EVENTS: 4
PROFILE_FOLLOWERS: 3,
PROFILE_FOLLOWING: 4,
POST_REPLIES: 5,
EVENTS: 6
}
@@ -59,6 +61,20 @@ exports.newQuery = function newQuery() {
break
}
case QUERY_TYPES.PROFILE_FOLLOWERS: {
thisObject.query = NT.modules.QUERY_PROFILE_FOLLOWERS.newProfileFollowers()
query.initialize(queryReceived)
query.finalize()
break
}
case QUERY_TYPES.PROFILE_FOLLOWING: {
thisObject.query = NT.modules.QUERY_PROFILE_FOLLOWING.newProfileFollowing()
query.initialize(queryReceived)
query.finalize()
break
}
case QUERY_TYPES.maps.EVENTS: {
thisObject.query = NT.modules.QUERY_EVENTS.newEvents()
query.initialize(queryReceived)
+36 -8
View File
@@ -6,14 +6,23 @@ exports.newUserProfile = function newUserProfile() {
botAsset: undefined,
botExchange: undefined,
enabled: undefined,
following: undefined,
followers: undefined,
arrays: {
following: undefined,
followers: undefined,
posts: undefined,
bots: undefined
},
maps: {
following: undefined,
followers: undefined,
posts: undefined,
bots: undefined
},
followingCount: undefined,
followersCount: undefined,
emitterEventsCount: undefined,
targetEventsCount: undefined,
postsCount: undefined,
posts: undefined,
addPost: addPost,
removePost: removePost,
addFollowing: addFollowing,
@@ -27,16 +36,35 @@ exports.newUserProfile = function newUserProfile() {
return thisObject
function finalize() {
thisObject.following = undefined
thisObject.followers = undefined
thisObject.posts = undefined
thisObject.arrays = {
following: undefined,
followers: undefined,
posts: undefined,
bots: undefined
}
thisObject.maps = {
following: undefined,
followers: undefined,
posts: undefined,
bots: undefined
}
}
function initialize() {
thisObject.following = new Map()
thisObject.followers = new Map()
thisObject.arrays = {
following: [],
followers: [],
posts: []
}
thisObject.maps = {
following: new Map(),
followers: new Map(),
posts: new Map()
}
thisObject.followingCount = 0
thisObject.followersCount = 0
+9 -9
View File
@@ -107,7 +107,7 @@ exports.newEvent = function newEvent() {
if (emitterBotProfileId !== undefined) {
let botProfile = emitterUserProfile.bots.get(emitterBotProfileId)
if (
emitterBotProfile === undefined
botProfile === undefined
) {
throw ('Emitter BOt Profile Not Found.')
}
@@ -202,37 +202,37 @@ exports.newEvent = function newEvent() {
switch (thisObject.eventType) {
case EVENT_TYPES.FOLLOW_USER_PROFILE: {
emitterUserProfile.addFollowing(
targetUserProfile
targetUserProfileId
)
targetUserProfile.addFollower(
emitterUserProfile
emitterUserProfileId
)
break
}
case EVENT_TYPES.UNFOLLOW_USER_PROFILE: {
emitterUserProfile.removeFollowing(
targetUserProfile
targetUserProfileId
)
targetUserProfile.removeFollower(
emitterUserProfile
emitterUserProfileId
)
break
}
case EVENT_TYPES.FOLLOW_BOT_PROFILE: {
emitterUserProfile.addTradePostsFollowing(
targetUserProfile
targetBotProfileId
)
targetUserProfile.addTradePostsFollower(
emitterUserProfile
emitterUserProfileId
)
break
}
case EVENT_TYPES.UNFOLLOW_BOT_PROFILE: {
emitterUserProfile.removeTradePostsFollowing(
targetUserProfile
targetBotProfileId
)
targetUserProfile.removeTradePostsFollower(
emitterUserProfile
emitterBotProfileId
)
break
}
+70 -84
View File
@@ -1,30 +1,33 @@
exports.newUserProfile = function newUserProfile() {
let thisObject = {
userProfileId: undefined,
userProfileHandle: undefined,
blockchainAccount: undefined,
ranking: undefined,
following: undefined,
followers: undefined,
followingCount: undefined,
followersCount: undefined,
posts: undefined,
bots: undefined,
emitterEventsCount: undefined,
targetEventsCount: undefined,
postsCount: undefined,
botsCount: undefined,
bots: undefined,
posts: undefined,
addPost: addPost,
removePost: removePost,
addFollowing: addFollowing,
removeFollowing: removeFollowing,
addFollower: addFollower,
removeFollower: removeFollower,
addBot: addBot,
removeBot: removeBot,
enableBot: enableBot,
disableBot: disableBot,
initialize: initialize,
finalize: finalize
}
@@ -32,28 +35,24 @@ exports.newUserProfile = function newUserProfile() {
return thisObject
function finalize() {
thisObject.following = undefined
thisObject.followers = undefined
thisObject.posts = undefined
thisObject.bots = undefined
}
function initialize() {
thisObject.following = new Map()
thisObject.followers = new Map()
thisObject.followingCount = 0
thisObject.followersCount = 0
thisObject.posts = new Map()
thisObject.bots = new Map()
thisObject.emitterEventsCount = 0
thisObject.targetEventsCount = 0
thisObject.postsCount = 0
thisObject.botsCount = 0
thisObject.posts = []
thisObject.bots = []
}
function addPost(
@@ -64,140 +63,127 @@ exports.newUserProfile = function newUserProfile() {
postType,
timestamp
) {
if (NT.memory.maps.POSTS.get(emitterPostHash) !== undefined) {
if (thisObject.posts.get(emitterPostHash) !== undefined) {
throw ('Post Already Exists.')
} else {
let post = NT.modules.POST.newPost()
post.initialize(
emitterUserProfileId,
targetUserProfileId,
undefined,
undefined,
emitterPostHash,
targetPostHash,
postType,
timestamp
)
thisObject.posts.set(emitterPostHash, post)
}
let post = NT.modules.POST.newPost()
post.initialize(
emitterUserProfileId,
targetUserProfileId,
undefined,
undefined,
emitterPostHash,
targetPostHash,
postType,
timestamp
)
thisObject.posts.push(post)
NT.memory.maps.POSTS.set(emitterPostHash, post)
thisObject.postsCount++
}
function removePost(
emitterPostHash
) {
if (NT.memory.maps.POSTS.get(emitterPostHash) === undefined) {
if (thisObject.posts.get(emitterPostHash) === undefined) {
throw ('Post Does Not Exist.')
} else {
let post = thisObject.posts.get(emitterPostHash)
post.finalize()
thisObject.posts.delete(emitterPostHash)
}
let post = thisObject.posts.get(emitterPostHash)
post.finalize()
for (let i = thisObject.posts.length - 1; i >= 0; i--) {
if (thisObject.posts[i].emitterPostHash === emitterPostHash) {
thisObject.posts.splice(i, 1)
break
}
}
NT.memory.maps.POSTS.delete(emitterPostHash)
thisObject.postsCount--
}
function addFollowing(
userProfile
targetUserProfileId
) {
if (thisObject.following.get(userProfile.id) === undefined) {
thisObject.following.set(userProfile.id, userProfile)
thisObject.followingCount++
if (thisObject.following.get(targetUserProfileId) === undefined) {
thisObject.following.set(targetUserProfileId, targetUserProfileId)
} else {
throw ('Already Following.')
}
}
function removeFollowing(
userProfile
targetUserProfileId
) {
if (thisObject.following.get(userProfile.id) !== undefined) {
thisObject.following.delete(userProfile.id)
thisObject.followingCount--
if (thisObject.following.get(targetUserProfileId) !== undefined) {
thisObject.following.delete(targetUserProfileId)
} else {
throw ('Not Following.')
}
}
function addFollower(
userProfile
emitterUserProfileId
) {
if (thisObject.followers.get(userProfile.id) === undefined) {
thisObject.followers.set(userProfile.id, userProfile)
thisObject.followersCount++
if (thisObject.followers.get(emitterUserProfileId) === undefined) {
thisObject.followers.set(emitterUserProfileId, emitterUserProfileId)
}
}
function removeFollower(
userProfile
emitterUserProfileId
) {
if (thisObject.followers.get(userProfile.id) !== undefined) {
thisObject.followers.delete(userProfile.id)
thisObject.followersCount--
if (thisObject.followers.get(emitterUserProfileId) !== undefined) {
thisObject.followers.delete(emitterUserProfileId)
}
}
function addBot(
botId,
botProfileId,
botAsset,
botExchange
) {
if (thisObject.bots.get(botId) !== undefined) {
let bot = thisObject.bots.get(botProfileId)
if (bot !== undefined) {
throw ('Bot Already Exists.')
} else {
let bot = NT.modules.BOTS.newBot()
bot.initialize(
botProfileId,
botAsset,
botExchange
)
thisObject.bots.set(botProfileId, bot)
}
let bot = NT.modules.BOTS.newBot()
bot.initialize(
botId,
botAsset,
botExchange
)
thisObject.bots.set(botId, bot)
thisObject.botsCount++
}
function removeBot(
botId
botProfileId
) {
if (thisObject.bots.get(botId) === undefined) {
let bot = thisObject.bots.get(botProfileId)
if (bot === undefined) {
throw ('Bot Does Not Exist.')
} else {
thisObject.bots.get(botProfileId).finalize()
thisObject.bots.delete(botProfileId)
}
thisObject.bots.get(botId).finalize()
thisObject.bots.delete(botId)
thisObject.botsCount--
}
function enableBot(
botId
botProfileId
) {
let bot = thisObject.bots.get(botId)
let bot = thisObject.bots.get(botProfileId)
if (bot === undefined) {
throw ('Bot Does Not Exist.')
}
if (bot.enabled === true) {
throw ('Bot Already Enabled.')
}
bot.enabled = true
}
function disableBot(
botId
botProfileId
) {
let bot = thisObject.bots.get(botId)
let bot = thisObject.bots.get(botProfileId)
if (bot === undefined) {
throw ('Bot Does Not Exist.')
}
if (bot.enabled === false) {
throw ('Bot Already Disabled.')
}
bot.enabled = false
}
}
@@ -13154,17 +13154,17 @@
"id": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"savedPayload": {
"position": {
"x": 30356,
"y": 23119
"x": 3571.4285714285716,
"y": 25500
},
"targetPosition": {
"x": 30356,
"y": 23119
"x": 3571.4285714285716,
"y": 25500
},
"floatingObject": {
"isPinned": false,
"isFrozen": false,
"isCollapsed": false,
"isCollapsed": true,
"angleToParent": 1,
"distanceToParent": 3,
"arrangementStyle": 0
@@ -56,7 +56,7 @@ function newGovernanceUserProfileSpace() {
const X_STEP = SPACE_WIDTH / (userProfiles.length + 1)
const Y_STEP = 3000
const Y_LEVEL = SPACE_HEIGHT * 0.45
const Y_LEVEL = SPACE_HEIGHT * 0.425
let xOffset = X_STEP
let yOffset = 0