follow profile working from Web App to Network Node

This commit is contained in:
Luis Fernando Molina committed 2021-09-10 14:03:52 +02:00
1 parent 2f4aa33cfe
commit db348b3f51
3 files changed
+101 -17

No files matched your search

+74 -14
View File
@@ -131,39 +131,58 @@ function newWebApp() {
*/
function onError(errorMessage) {
console.log('[ERROR] Query not executed. ' + errorMessage)
console.log('[ERROR] query = ' + JSON.stringify(query))
}
}
function setupEventHandlers() {
/*
Add events to process button clicks.
Add events to process button clicks , and mouseWheel.
*/
document.addEventListener("click", onClick)
document.addEventListener('mousewheel', onMouseWheel, false)
function onClick(event) {
async function onClick(event) {
if (event.target && event.target.nodeName === "BUTTON") {
switch (event.target.action) {
case 'Follow Profile': {
let span = document.getElementById('profile-to-follow-span-' + event.target.userProfileId)
let button = document.getElementById('profile-to-follow-button-' + event.target.userProfileId)
span.setAttribute("class", "profile-to-unfollow-span")
button.setAttribute("class", "profile-to-unfollow-button")
button.action = 'Unfollow Profile'
await followUserProfile(event.target.userProfileId)
.then(updateButton)
.catch(onError)
function updateButton() {
let span = document.getElementById('profile-to-follow-span-' + event.target.userProfileId)
let button = document.getElementById('profile-to-follow-button-' + event.target.userProfileId)
span.setAttribute("class", "profile-to-unfollow-span")
button.setAttribute("class", "profile-to-unfollow-button")
button.action = 'Unfollow Profile'
}
break
}
case 'Unfollow Profile': {
let span = document.getElementById('profile-to-follow-span-' + event.target.userProfileId)
let button = document.getElementById('profile-to-follow-button-' + event.target.userProfileId)
span.setAttribute("class", "profile-to-follow-span")
button.setAttribute("class", "profile-to-follow-button")
button.action = 'Follow Profile'
await unfollowUserProfile(event.target.userProfileId).catch(onError)
.then(updateButton)
.catch(onError)
function updateButton() {
let span = document.getElementById('profile-to-follow-span-' + event.target.userProfileId)
let button = document.getElementById('profile-to-follow-button-' + event.target.userProfileId)
span.setAttribute("class", "profile-to-follow-span")
button.setAttribute("class", "profile-to-follow-button")
button.action = 'Follow Profile'
}
break
}
}
}
/*
Error Handling
*/
function onError(errorMessage) {
console.log('[ERROR] Click event failed. ' + errorMessage)
}
}
function onMouseWheel(event) {
@@ -234,9 +253,50 @@ function newWebApp() {
tblBody.appendChild(row)
table.appendChild(tblBody)
htmlElement.appendChild(table)
table.setAttribute("class", "profile-to-follow-table")
}
async function followUserProfile(userProfileId) {
return new Promise((resolve, reject) => { asyncCall(resolve, reject) })
async function asyncCall(resolve, reject) {
let eventMessage
let event
/*
Test Query User Profiles.
*/
eventMessage = {
eventType: SA.projects.socialTrading.globals.eventTypes.FOLLOW_USER_PROFILE,
eventId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(),
targetUserProfileId: userProfileId
}
event = {
requestType: 'Event',
eventMessage: JSON.stringify(eventMessage)
}
await UI.projects.socialTrading.modules.webSocketsClient.sendMessage(
JSON.stringify(event)
)
.then(onResponse)
.catch(onError)
async function onResponse(reponse) {
resolve()
}
/*
Error Handling
*/
function onError(errorMessage) {
console.log('[ERROR] Event not executed. ' + errorMessage)
console.log('[ERROR] event = ' + JSON.stringify(event))
reject(errorMessage)
}
}
}
}
@@ -64,11 +64,25 @@ exports.newDesktopModulesWebSocketsInterface = function newDesktopModulesWebSock
return
}
let response = await webAppInterface.messageReceived(messageHeader.payload)
webApp.socket.send(JSON.stringify(response))
await webAppInterface.messageReceived(messageHeader.payload)
.then(sendResponseToWebApp)
.catch(onError)
function sendResponseToWebApp(response) {
webApp.socket.send(JSON.stringify(response))
}
function onError(errorMessage) {
console.log('[ERROR] Web Sockets Interface -> onMenssage -> errorMessage = ' + errorMessage)
let response = {
result: 'Error',
message: errorMessage
}
webApp.socket.send(JSON.stringify(response))
}
} catch (err) {
console.log('[ERROR] Web Sockets Interface -> setUpWebSocketServer -> err.stack = ' + err.stack)
console.log('[ERROR] Web Sockets Interface -> onMenssage -> err.stack = ' + err.stack)
}
}
}
@@ -103,6 +103,16 @@ exports.newSocialTradingModulesClientInterface = function newSocialTradingModule
return response
}
/*
We will not accept events that don't have an eventId.
*/
if (eventReceived.eventId === undefined) {
let response = {
result: 'Error',
message: 'eventId Not Provided.'
}
return response
}
/*
We will not accept events that have already been processed.
*/