From e30addb819fdd9b81f01daf154e9b4682441a9f5 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Tue, 14 Sep 2021 15:51:48 +0200 Subject: [PATCH] Social Trading: event for getting the timeline --- Desktop/UI/WebApp.js | 227 ++++++++++-------- .../Test-Network-Client-Profile.json | 8 +- .../Test-Network-Node-Profile.json | 8 +- Projects/Social-Trading/NT/Globals/Memory.js | 3 +- .../NT/Modules/ClientInterface.js | 6 + .../NT/Modules/Queries/Events.js | 6 +- Projects/Social-Trading/NT/Modules/Query.js | 2 +- .../NT/Utilities/QueriesValidations.js | 6 +- 8 files changed, 150 insertions(+), 116 deletions(-) diff --git a/Desktop/UI/WebApp.js b/Desktop/UI/WebApp.js index 037b5d7f8..bce4eb31d 100644 --- a/Desktop/UI/WebApp.js +++ b/Desktop/UI/WebApp.js @@ -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(), diff --git a/Projects/Governance/Plugins/User-Profiles/Test-Network-Client-Profile.json b/Projects/Governance/Plugins/User-Profiles/Test-Network-Client-Profile.json index 3279c2cd7..ca0070a33 100644 --- a/Projects/Governance/Plugins/User-Profiles/Test-Network-Client-Profile.json +++ b/Projects/Governance/Plugins/User-Profiles/Test-Network-Client-Profile.json @@ -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, diff --git a/Projects/Governance/Plugins/User-Profiles/Test-Network-Node-Profile.json b/Projects/Governance/Plugins/User-Profiles/Test-Network-Node-Profile.json index 10d3ae958..2ef8dfc42 100644 --- a/Projects/Governance/Plugins/User-Profiles/Test-Network-Node-Profile.json +++ b/Projects/Governance/Plugins/User-Profiles/Test-Network-Node-Profile.json @@ -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, diff --git a/Projects/Social-Trading/NT/Globals/Memory.js b/Projects/Social-Trading/NT/Globals/Memory.js index 6a42e762a..b0b2b9458 100644 --- a/Projects/Social-Trading/NT/Globals/Memory.js +++ b/Projects/Social-Trading/NT/Globals/Memory.js @@ -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. } } diff --git a/Projects/Social-Trading/NT/Modules/ClientInterface.js b/Projects/Social-Trading/NT/Modules/ClientInterface.js index 742b7541f..f37d898a0 100644 --- a/Projects/Social-Trading/NT/Modules/ClientInterface.js +++ b/Projects/Social-Trading/NT/Modules/ClientInterface.js @@ -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 = { diff --git a/Projects/Social-Trading/NT/Modules/Queries/Events.js b/Projects/Social-Trading/NT/Modules/Queries/Events.js index 1732fe794..b8085c5b9 100644 --- a/Projects/Social-Trading/NT/Modules/Queries/Events.js +++ b/Projects/Social-Trading/NT/Modules/Queries/Events.js @@ -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) } diff --git a/Projects/Social-Trading/NT/Modules/Query.js b/Projects/Social-Trading/NT/Modules/Query.js index 8f9ab4633..9c0831118 100644 --- a/Projects/Social-Trading/NT/Modules/Query.js +++ b/Projects/Social-Trading/NT/Modules/Query.js @@ -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 diff --git a/Projects/Social-Trading/NT/Utilities/QueriesValidations.js b/Projects/Social-Trading/NT/Utilities/QueriesValidations.js index 21d7932b7..5dc208fb7 100644 --- a/Projects/Social-Trading/NT/Utilities/QueriesValidations.js +++ b/Projects/Social-Trading/NT/Utilities/QueriesValidations.js @@ -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 */