Post Text fetched from github and shown at the timeline.
This commit is contained in:
1 parent
760c8d9196
commit
8757d26487
3 files changed
+74
-20
No files matched your search
@@ -139,23 +139,23 @@ function newWebApp() {
|
||||
|
||||
let postDiv = document.createElement("div")
|
||||
postDiv.setAttribute("class", "post-div")
|
||||
let textNode
|
||||
let textNode
|
||||
|
||||
switch (event.eventType) {
|
||||
case SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST: {
|
||||
textNode = document.createTextNode( event.emitterUserProfile.userProfileHandle + " Posted ")
|
||||
textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " POSTED " + event.postText)
|
||||
break
|
||||
}
|
||||
case SA.projects.socialTrading.globals.eventTypes.FOLLOW_USER_PROFILE: {
|
||||
textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " Followed " + event.targetUserProfile.userProfileHandle)
|
||||
textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " FOLLOWED " + event.targetUserProfile.userProfileHandle)
|
||||
break
|
||||
}
|
||||
case SA.projects.socialTrading.globals.eventTypes.UNFOLLOW_USER_PROFILE: {
|
||||
textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " Unfollowed " + event.targetUserProfile.userProfileHandle)
|
||||
textNode = document.createTextNode(event.emitterUserProfile.userProfileHandle + " UNFOLLOWED " + event.targetUserProfile.userProfileHandle)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
postDiv.appendChild(textNode)
|
||||
contentDiv.appendChild(postDiv)
|
||||
}
|
||||
@@ -384,7 +384,7 @@ function newWebApp() {
|
||||
eventMessage = {
|
||||
eventType: SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST,
|
||||
eventId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(),
|
||||
text: postText,
|
||||
postText: postText,
|
||||
timestamp: (new Date()).valueOf()
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -48,7 +48,8 @@ exports.newDesktopRoot = function newDesktopRoot() {
|
||||
open: require('open'),
|
||||
http: require('http'),
|
||||
octokit: require("@octokit/rest"),
|
||||
simpleGit: require('simple-git')
|
||||
simpleGit: require('simple-git'),
|
||||
nodeFetch: require('node-fetch')
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -47,15 +47,34 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule
|
||||
queryMessage.emitterUserProfileId = DK.TEST_NETWORK_CLIENT_USER_PROFILE_ID
|
||||
messageHeader.queryMessage = JSON.stringify(queryMessage)
|
||||
|
||||
// console.log((new Date()).toISOString(), '- Web App Interface', '- Query Message Received', JSON.stringify(queryMessage))
|
||||
let response
|
||||
|
||||
let response = {
|
||||
result: 'Ok',
|
||||
message: 'Web App Interface Query Processed.',
|
||||
data: await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader))
|
||||
// console.log((new Date()).toISOString(), '- Web App Interface', '- Query Message Received', JSON.stringify(queryMessage))
|
||||
|
||||
if (queryMessage.queryType !== SA.projects.socialTrading.globals.queryTypes.EVENTS) {
|
||||
response = {
|
||||
result: 'Ok',
|
||||
message: 'Web App Interface Query Processed.',
|
||||
data: await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader))
|
||||
}
|
||||
} else {
|
||||
|
||||
let events = await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader))
|
||||
for (let i = 0; i < events.length; i++) {
|
||||
let event = events[i]
|
||||
if (event.eventType === SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST) {
|
||||
event.postText = await getPostText(event.emitterUserProfile.userProfileHandle, event.emitterPost.emitterPostHash, event.timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
response = {
|
||||
result: 'Ok',
|
||||
message: 'Web App Interface Query Processed.',
|
||||
data: events
|
||||
}
|
||||
}
|
||||
|
||||
// console.log((new Date()).toISOString(), '- Web App Interface', '- Query Respose Sent', JSON.stringify(response))
|
||||
// console.log((new Date()).toISOString(), '- Web App Interface', '- Query Respose Sent', JSON.stringify(response))
|
||||
|
||||
return response
|
||||
}
|
||||
@@ -100,8 +119,8 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule
|
||||
and a hash of the content was generated, and that is what is going to
|
||||
the Network Node.
|
||||
*/
|
||||
eventMessage.emitterPostHash = await savePostAtStorage(eventMessage.text, commitMessage, eventMessage.timestamp)
|
||||
eventMessage.text = undefined
|
||||
eventMessage.emitterPostHash = await savePostAtStorage(eventMessage.postText, commitMessage, eventMessage.timestamp)
|
||||
eventMessage.postText = undefined
|
||||
}
|
||||
|
||||
eventMessage.emitterUserProfileId = DK.TEST_NETWORK_CLIENT_USER_PROFILE_ID
|
||||
@@ -124,19 +143,18 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule
|
||||
}
|
||||
}
|
||||
|
||||
async function savePostAtStorage(text, commitMessage, timestamp) {
|
||||
async function savePostAtStorage(postText, commitMessage, timestamp) {
|
||||
const { createHash } = await import('crypto')
|
||||
const hash = createHash('sha256')
|
||||
let post = {
|
||||
timestamp: timestamp,
|
||||
text: text
|
||||
postText: postText
|
||||
}
|
||||
|
||||
const fileContent = JSON.stringify(post, undefined, 4)
|
||||
const fileHash = hash.update(fileContent).digest('hex')
|
||||
const fileName = fileHash + ".json"
|
||||
|
||||
let filePath = './My-Social-Trading-Data/User-Posts/' + SA.projects.foundations.utilities.filesAndDirectories.pathFromDate(timestamp)
|
||||
const filePath = './My-Social-Trading-Data/User-Posts/' + SA.projects.foundations.utilities.filesAndDirectories.pathFromDate(timestamp)
|
||||
|
||||
SA.projects.foundations.utilities.filesAndDirectories.mkDirByPathSync(filePath + '/')
|
||||
SA.nodeModules.fs.writeFileSync(filePath + '/' + fileName, fileContent)
|
||||
@@ -154,4 +172,39 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule
|
||||
|
||||
return fileHash
|
||||
}
|
||||
|
||||
async function getPostText(userProfileHandle, postHash, timestamp) {
|
||||
let promise = new Promise((resolve, reject) => {
|
||||
//https://raw.githubusercontent.com/Luis-Fernando-Molina/My-Social-Trading-Data/main/User-Posts/2021/09/17/0608070d8d2aab12e63cf3aa871918a04e237edff4fc0f886d158311811cf7c9.json
|
||||
|
||||
const userProfileHandle = "Luis-Fernando-Molina" /// TODO Delete this line
|
||||
|
||||
const fileName = postHash + ".json"
|
||||
const filePath = 'My-Social-Trading-Data/main/User-Posts/' + SA.projects.foundations.utilities.filesAndDirectories.pathFromDate(timestamp)
|
||||
|
||||
const fetch = SA.nodeModules.nodeFetch
|
||||
let url = 'https://raw.githubusercontent.com/' + userProfileHandle + '/' + filePath + '/' + fileName
|
||||
|
||||
fetch(url)
|
||||
.then((response) => {
|
||||
|
||||
if (response.status != 200) {
|
||||
reject('Github.com responded with status ' + response.status)
|
||||
return
|
||||
}
|
||||
|
||||
response.text().then(body => {
|
||||
post = JSON.parse(body)
|
||||
resolve(post.postText)
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
resolve('Post Text could not be fetched. ' + err.message)
|
||||
})
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
return promise
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user