Social Trading: event for getting the timeline

This commit is contained in:
Luis Fernando Molina committed 2021-09-14 15:51:48 +02:00
1 parent d641a7e3b4
commit e30addb819
8 files changed
+150 -116

No files matched your search

+127 -100
View File
@@ -16,7 +16,8 @@ function newWebApp() {
setupRootObject(UI, 'UI')
setupRootObject(SA, 'SA')
await UI.projects.socialTrading.modules.webSocketsClient.initialize()
setupHomePage()
loadWUserProfileTimeline()
loadWhoToFollow()
setupEventHandlers()
} catch (err) {
console.log('[ERROR] initialize -> err.stack = ' + err.stack)
@@ -94,21 +95,16 @@ function newWebApp() {
}
}
async function setupHomePage() {
let queryMessage
let query
/*
Test Query User Profiles.
*/
queryMessage = {
queryType: SA.projects.socialTrading.globals.queryTypes.UNFOLLOWED_USER_PROFILES,
async function loadWUserProfileTimeline() {
let queryMessage = {
queryType: SA.projects.socialTrading.globals.queryTypes.EVENTS,
emitterUserProfileId: undefined,
initialIndex: SA.projects.socialTrading.globals.queryConstants.INITIAL_INDEX_FIRST,
amountRequested: 3,
direction: SA.projects.socialTrading.globals.queryConstants.DIRECTION_UP
initialIndex: SA.projects.socialTrading.globals.queryConstants.INITIAL_INDEX_LAST,
amountRequested: 100,
direction: SA.projects.socialTrading.globals.queryConstants.DIRECTION_PAST
}
query = {
let query = {
requestType: 'Query',
queryMessage: JSON.stringify(queryMessage)
}
@@ -116,23 +112,121 @@ function newWebApp() {
await UI.projects.socialTrading.modules.webSocketsClient.sendMessage(
JSON.stringify(query)
)
.then(showProfiles)
.then(addToContentDiv)
.catch(onError)
async function showProfiles(profiles) {
console.log(profiles)
document.getElementById('context-cell')
addWhoToFollowTable(profiles)
}
/*
Error Handling
*/
function onError(errorMessage) {
console.log('[ERROR] Query not executed. ' + errorMessage)
console.log('[ERROR] query = ' + JSON.stringify(query))
}
function addToContentDiv(events) {
let contentDiv = document.getElementById('content-div')
for (let i = 0; i < events.length; i++) {
let event = events[i]
let postDiv = document.createElement("div")
postDiv.setAttribute("class", "post-div")
let textNode = document.createTextNode('POST #' + i)
postDiv.appendChild(textNode)
contentDiv.appendChild(postDiv)
}
}
}
async function loadWhoToFollow() {
let queryMessage = {
queryType: SA.projects.socialTrading.globals.queryTypes.UNFOLLOWED_USER_PROFILES,
emitterUserProfileId: undefined,
initialIndex: SA.projects.socialTrading.globals.queryConstants.INITIAL_INDEX_FIRST,
amountRequested: 3,
direction: SA.projects.socialTrading.globals.queryConstants.DIRECTION_UP
}
let query = {
requestType: 'Query',
queryMessage: JSON.stringify(queryMessage)
}
await UI.projects.socialTrading.modules.webSocketsClient.sendMessage(
JSON.stringify(query)
)
.then(addWhoToFollowTable)
.catch(onError)
function onError(errorMessage) {
console.log('[ERROR] Query not executed. ' + errorMessage)
console.log('[ERROR] query = ' + JSON.stringify(query))
}
function addWhoToFollowTable(profiles) {
let contextCell = document.getElementById('who-to-follow-cell')
let table = document.createElement("table")
let tblBody = document.createElement("tbody")
for (let i = 0; i < profiles.length; i++) {
let profile = profiles[i]
let row = document.createElement("tr")
let cell = document.createElement("td")
addProfileToFollowTable(cell, profile)
row.appendChild(cell)
tblBody.appendChild(row)
}
table.appendChild(tblBody)
contextCell.appendChild(table)
table.setAttribute("class", "who-to-follow-table")
function addProfileToFollowTable(htmlElement, profile) {
let table = document.createElement("table")
let tblBody = document.createElement("tbody")
let row = document.createElement("tr")
{
let cell = document.createElement("td")
let textNode = document.createTextNode('Profile Picture')
cell.appendChild(textNode)
row.appendChild(cell)
}
{
let cell = document.createElement("td")
let textNode = document.createTextNode(profile.userProfileHandle)
cell.appendChild(textNode)
row.appendChild(cell)
}
{
let cell = document.createElement("td")
let span = document.createElement("span")
let button = document.createElement("button")
let text = document.createTextNode('Follow')
span.setAttribute("id", "profile-to-follow-span-" + profile.userProfileId)
button.setAttribute("id", "profile-to-follow-button-" + profile.userProfileId)
button.name = 'Follow Profile'
button.userProfileId = profile.userProfileId
span.setAttribute("class", "profile-to-follow-span")
button.setAttribute("class", "profile-to-follow-button")
button.appendChild(text)
span.appendChild(button)
cell.appendChild(span)
row.appendChild(cell)
}
tblBody.appendChild(row)
table.appendChild(tblBody)
htmlElement.appendChild(table)
table.setAttribute("class", "profile-to-follow-table")
}
}
}
function setupEventHandlers() {
@@ -150,7 +244,7 @@ function newWebApp() {
let textArea = document.getElementById("new-post-text-area")
await sendNewPostEvent(
textArea.value
)
)
.then(updateTextArea)
.catch(onError)
@@ -163,7 +257,7 @@ function newWebApp() {
await sendTargetUserProfileEvent(
event.target.userProfileId,
SA.projects.socialTrading.globals.eventTypes.FOLLOW_USER_PROFILE
)
)
.then(updateButton)
.catch(onError)
@@ -180,7 +274,7 @@ function newWebApp() {
await sendTargetUserProfileEvent(
event.target.userProfileId,
SA.projects.socialTrading.globals.eventTypes.UNFOLLOW_USER_PROFILE
)
)
.then(updateButton)
.catch(onError)
@@ -210,84 +304,17 @@ function newWebApp() {
}
}
function addWhoToFollowTable(profiles) {
let contextCell = document.getElementById('who-to-follow-cell')
let table = document.createElement("table")
let tblBody = document.createElement("tbody")
for (let i = 0; i < profiles.length; i++) {
let profile = profiles[i]
let row = document.createElement("tr")
let cell = document.createElement("td")
addProfileToFollowTable(cell, profile)
row.appendChild(cell)
tblBody.appendChild(row)
}
table.appendChild(tblBody)
contextCell.appendChild(table)
table.setAttribute("class", "who-to-follow-table")
}
function addProfileToFollowTable(htmlElement, profile) {
let table = document.createElement("table")
let tblBody = document.createElement("tbody")
let row = document.createElement("tr")
{
let cell = document.createElement("td")
let cellText = document.createTextNode('Profile Picture')
cell.appendChild(cellText)
row.appendChild(cell)
}
{
let cell = document.createElement("td")
let cellText = document.createTextNode(profile.userProfileHandle)
cell.appendChild(cellText)
row.appendChild(cell)
}
{
let cell = document.createElement("td")
let span = document.createElement("span")
let button = document.createElement("button")
let text = document.createTextNode('Follow')
span.setAttribute("id", "profile-to-follow-span-" + profile.userProfileId)
button.setAttribute("id", "profile-to-follow-button-" + profile.userProfileId)
button.name = 'Follow Profile'
button.userProfileId = profile.userProfileId
span.setAttribute("class", "profile-to-follow-span")
button.setAttribute("class", "profile-to-follow-button")
button.appendChild(text)
span.appendChild(button)
cell.appendChild(span)
row.appendChild(cell)
}
tblBody.appendChild(row)
table.appendChild(tblBody)
htmlElement.appendChild(table)
table.setAttribute("class", "profile-to-follow-table")
}
async function sendTargetUserProfileEvent(
userProfileId,
eventType
) {
eventType
) {
return new Promise((resolve, reject) => { asyncCall(resolve, reject) })
async function asyncCall(resolve, reject) {
let eventMessage
let event
eventMessage = {
eventType: eventType,
eventId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(),
@@ -315,15 +342,15 @@ function newWebApp() {
}
async function sendNewPostEvent(
postText
) {
postText
) {
return new Promise((resolve, reject) => { asyncCall(resolve, reject) })
async function asyncCall(resolve, reject) {
let eventMessage
let event
eventMessage = {
eventType: SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST,
eventId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(),
@@ -62,12 +62,12 @@
"id": "baa2f02c-2920-4edb-a103-d452fc61d82f",
"savedPayload": {
"position": {
"x": 20588.235294117643,
"y": 18250
"x": 30769.230769230762,
"y": 21250
},
"targetPosition": {
"x": 20588.235294117643,
"y": 18250
"x": 30769.230769230762,
"y": 21250
},
"floatingObject": {
"isPinned": false,
@@ -62,12 +62,12 @@
"id": "36c2b050-631f-4291-987c-418adc94317d",
"savedPayload": {
"position": {
"x": 17647.05882352941,
"y": 21251
"x": 28846.15384615384,
"y": 18250
},
"targetPosition": {
"x": 17647.05882352941,
"y": 21251
"x": 28846.15384615384,
"y": 18250
},
"floatingObject": {
"isPinned": false,
+2 -1
View File
@@ -7,11 +7,12 @@ exports.newSocialTradingGlobalsMemory = function () {
USER_PROFILES_BY_ID: new Map(), // Here we will store the user profiles by User Profile Id.
USER_PROFILES_BY_HANDLE: new Map(), // Here we will store the user profiles by User Profile Handle.
USER_PROFILES_BY_BLOCHAIN_ACCOUNT: new Map(), // Here we will store the user profiles by Blockchain Account.
EVENTS: new Map(), // This is the registry of all events received that prevents processing them more than once.
EVENTS: new Map() // This is the registry of all events received that prevents processing them more than once.
},
arrays: {
NETWORK_CLIENTS: [], // These are the Network Clients connected to this Network Node ordered by Ranking.
NETWORK_PEERS: [], // These are the Network Peers connected to this Network Node ordered by Ranking.
EVENTS: [] // This is the registry of all events in order that prevents ordering them more than once.
}
}
@@ -142,6 +142,9 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule
Any exception that happens while trying to change the state of the Social Graph,
will be returned to the caller without doing anything else here.
*/
if (err.stack !== undefined) {
console.log('[ERROR] Client Interface -> err.stack = ' + err.stack)
}
let errorMessage = err.message
if (errorMessage === undefined) { errorMessage = err }
let response = {
@@ -215,6 +218,9 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule
/*
Any exception that happens while trying to execute the query.
*/
if (err.stack !== undefined) {
console.log('[ERROR] Client Interface -> err.stack = ' + err.stack)
}
let errorMessage = err.message
if (errorMessage === undefined) { errorMessage = err }
let response = {
@@ -22,7 +22,7 @@ exports.newSocialTradingModulesQueriesEvents = function newSocialTradingModulesQ
function initialize(queryReceived) {
NT.projects.socialTrading.utilities.queriesValidations.profilesValidations(queryReceived, thisObject)
NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, NT.memory.arrays.EVENTS)
NT.projects.socialTrading.utilities.queriesValidations.arrayValidations(queryReceived, thisObject, NT.projects.socialTrading.globals.memory.arrays.EVENTS)
}
@@ -31,7 +31,7 @@ exports.newSocialTradingModulesQueriesEvents = function newSocialTradingModulesQ
switch (thisObject.direction) {
case SA.projects.socialTrading.globals.queryConstants.DIRECTION_FUTURE: {
for (let i = thisObject.initialIndex; i < thisObject.initialIndex + thisObject.amountRequested; i++) {
let event = NT.memory.arrays.EVENTS[i]
let event = NT.projects.socialTrading.globals.memory.arrays.EVENTS[i]
if (event === undefined) { break }
checkEventContext(event)
}
@@ -39,7 +39,7 @@ exports.newSocialTradingModulesQueriesEvents = function newSocialTradingModulesQ
}
case SA.projects.socialTrading.globals.queryConstants.DIRECTION_PAST: {
for (let i = thisObject.initialIndex; i > thisObject.initialIndex - thisObject.amountRequested; i--) {
let event = NT.memory.arrays.EVENTS[i]
let event = NT.projects.socialTrading.globals.memory.arrays.EVENTS[i]
if (event === undefined) { break }
checkEventContext(event)
}
+1 -1
View File
@@ -76,7 +76,7 @@ exports.newSocialTradingModulesQuery = function newSocialTradingModulesQuery() {
break
}
case SA.projects.socialTrading.globals.queryTypes.maps.EVENTS: {
case SA.projects.socialTrading.globals.queryTypes.EVENTS: {
thisObject.query = NT.projects.socialTrading.modules.queriesEvents.newSocialTradingModulesQueriesEvents()
thisObject.query.initialize(queryReceived)
break
@@ -16,11 +16,11 @@ exports.newSocialTradingUtilitiesQueriesValidations = function newSocialTradingU
*/
if (queryReceived.targetUserProfileId !== undefined) {
thisObject.profile = NT.projects.socialTrading.globals.memory.maps.USER_PROFILES_BY_ID.get(queryReceived.targetUserProfileId)
if (thisObject.profile === undefined) {
throw ('Target User Profile Not Found.')
}
}
if (thisObject.profile === undefined) {
throw ('Target User Profile Not Found.')
}
/*
Validate Bot Profile
*/