updated functions to differentiate data from users and subadmins

This commit is contained in:
Abhishek Sinha 2019-12-14 14:03:35 +05:30
parent 0627a65d78
commit 1cae597462
2 changed files with 79 additions and 35 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
playground/

View File

@ -167,7 +167,7 @@
//Required for blockchain API operators //Required for blockchain API operators
apiURL: { apiURL: {
FLO: ['https://explorer.mediciland.com/', 'https://livenet.flocha.in/', 'https://flosight.duckdns.org/', 'http://livenet-explorer.floexperiments.com/'], FLO: ['https://explorer.mediciland.com/', 'https://livenet.flocha.in/', 'https://flosight.duckdns.org/', 'http://livenet-explorer.floexperiments.com/'],
FLO_TEST: ['https://testnet-flosight.duckdns.org', 'https://testnet.flocha.in/'] FLO_TEST: ['https://testnet-flosight.duckdns.org/', 'https://testnet.flocha.in/']
}, },
adminID: "FMeiptdJNtYQEtzyYAVNP8fjsDJ1i4EPfE", adminID: "FMeiptdJNtYQEtzyYAVNP8fjsDJ1i4EPfE",
//sendAmt: 0.001, //sendAmt: 0.001,
@ -178,10 +178,12 @@
supernodes: {}, //each supnernode must be stored as floID : {uri:<uri>,pubKey:<publicKey>} supernodes: {}, //each supnernode must be stored as floID : {uri:<uri>,pubKey:<publicKey>}
//for cloud apps //for cloud apps
subAdmins: [], subAdmins: ['FTMMBXhn3K8UnVA3s6AiXhatwiaMUHffUo'],
application: "Content Collaboration", application: "RIBC",
vectorClock: {}, vectorClock: {},
appObjects: {} appObjects: {},
generalData: {},
generalVC: {}
} }
</script> </script>
@ -4283,7 +4285,7 @@
cursor += 2 + sig[cursor + 1]; cursor += 2 + sig[cursor + 1];
//if (cursor != sig.length) //if (cursor != sig.length)
// throw new Error("Extra bytes in signature"); // throw new Error("Extra bytes in signature");
var r = BigInteger.fromByteArrayUnsigned(rBa); var r = BigInteger.fromByteArrayUnsigned(rBa);
var s = BigInteger.fromByteArrayUnsigned(sBa); var s = BigInteger.fromByteArrayUnsigned(sBa);
@ -7100,12 +7102,29 @@
}) })
}, },
sendGeneralData: function(message, type, options = {}){
this.sendApplicationData(message, type, options)
.then(result => reactor.dispatchEvent('logHandle',result))
.catch(error => reactor.dispatchEvent('errorHandle',error))
},
//request General Data
requestGeneralData: function(type, options = {}){
var filterStr = JSON.stringify({application: options.application || floGlobals.application, type: type, comment: options.comment})
options.type = type
options.lowerVectorClock = options.lowerVectorClock || floGlobals.generalVC[filterStr] + 1
this.requestApplicationData(options)
.then(dataSet => reactor.dispatchEvent("storeGeneralData", {filterStr: filterStr, dataSet:dataSet}))
.catch(error => reactor.dispatchEvent('errorHandle',error))
},
//request an object data from supernode cloud //request an object data from supernode cloud
requestObjectData: function(objectName, options = {}){ requestObjectData: function(objectName, options = {}){
var request = { var request = {
receiverID: options.receiverID || floGlobals.adminID, receiverID: options.receiverID || floGlobals.adminID,
senderIDs: (options.senderIDs === false) ? false: options.senderIDs || floGlobals.subAdmins, senderIDs: (options.senderIDs === false) ? false: options.senderIDs || floGlobals.subAdmins,
application: options.application || floGlobals.application, application: options.application || floGlobals.application,
comment: options.comment,
type: `${objectName}@Reset`, type: `${objectName}@Reset`,
lowerVectorClock: floGlobals.vectorClock[objectName]+1, lowerVectorClock: floGlobals.vectorClock[objectName]+1,
mostRecent: true mostRecent: true
@ -7189,6 +7208,24 @@
} }
}); });
reactor.registerEvent('storeGeneralData');
reactor.addEventListener('storeGeneralData', function(event){
try{
dataSet = JSON.parse(event.dataSet);
console.log(dataSet)
if(!Array.isArray(floGlobals.generalData[event.filterStr]))
floGlobals.generalData[event.filterStr] = []
for(vc in dataSet){
floGlobals.generalData[event.filterStr].push({sender: dataSet[vc].senderID, message: dataSet[vc].message})
compactIDB.writeData("generalData", floGlobals.generalData[event.filterStr], event.filterStr)
floGlobals.generalVC[event.filterStr] = vc
compactIDB.writeData("generalVC", vc, event.filterStr)
}
}catch(error){
console.log(error)
}
});
reactor.registerEvent('errorHandle'); reactor.registerEvent('errorHandle');
reactor.addEventListener('errorHandle', function(event){ reactor.addEventListener('errorHandle', function(event){
console.log(event) console.log(event)
@ -7212,8 +7249,8 @@
console.log(result) console.log(result)
floSupernode.kBucket.launch(Object.keys(floGlobals.supernodes),floGlobals.SNStorageID).then(result => { floSupernode.kBucket.launch(Object.keys(floGlobals.supernodes),floGlobals.SNStorageID).then(result => {
console.log(result) console.log(result)
loadObjectsFromIDB().then(result => { loadDataFromIDB().then(result => {
console.log(result) console.log(result);
cloudArticleApp.retrieveLatestContent(); cloudArticleApp.retrieveLatestContent();
}).catch(error => console.log(error)) }).catch(error => console.log(error))
}).catch(error => console.log(error)) }).catch(error => console.log(error))
@ -7235,7 +7272,9 @@
credentials:{}, credentials:{},
//any other objectstores if needed //any other objectstores if needed
appObjects:{}, appObjects:{},
vectorClocks:{} vectorClocks:{},
generalData:{},
generalVC:{}
} }
compactIDB.initDB(floGlobals.application, obj) compactIDB.initDB(floGlobals.application, obj)
.then(result => resolve("Initiated supernode storage")) .then(result => resolve("Initiated supernode storage"))
@ -7287,14 +7326,16 @@
}) })
} }
function loadObjectsFromIDB(){ function loadDataFromIDB(){
return new Promise((resolve,reject) => { return new Promise((resolve,reject) => {
var promise1 = compactIDB.readAllData("appObjects") var loadData = ["appObjects", "vectorClocks", "generalData", "generalVC"]
var promise2 = compactIDB.readAllData("vectorClocks") var promises = []
Promise.all([promise1,promise2]).then(results => { for(var i = 0; i < loadData.length; i++)
floGlobals.appObjects = results[0] promises[i] = compactIDB.readAllData(loadData[i])
floGlobals.vectorClock = results[1] Promise.all(promises).then(results => {
resolve("Loaded appObjects and vectorClocks from IDB") for(var i = 0; i < loadData.length; i++)
floGlobals[loadData[i]] = results[i]
resolve("Loaded Data from IDB")
}).catch(error => reject(error)) }).catch(error => reject(error))
}) })
} }
@ -7375,18 +7416,14 @@
}) })
} }
function clearCredentials(type = 'Both'){ function clearCredentials(){
if(type === 'Both') var indexArr = localStorage.getItem("privKey")
return clearCredentials('privKey') + clearCredentials('serverPass') if(!indexArr)
else if(type === 'privKey' || type === 'serverPass'){ return `privKey credentials not found!`
var indexArr = localStorage.getItem(type) indexArr = JSON.parse(indexArr)
if(!indexArr) indexArr.forEach(i => compactIDB.removeData('credentials', i))
return `${type} credentials not found!\n` localStorage.removeItem("privKey")
indexArr = JSON.parse(indexArr) return `privKey credentials deleted!`
indexArr.forEach(i => compactIDB.removeData('credentials', indexArr[i]))
localStorage.removeItem(type)
return `${type} credentials deleted!\n`
}
} }
</script> </script>
@ -7481,34 +7518,36 @@
}, },
retrieveLatestContent: async function(receiverID=floGlobals.adminID, senderIDs=false) { retrieveLatestContent: async function(receiverID=floGlobals.adminID, senderIDs=floGlobals.subAdmins) {
floCloudAPI.requestObjectData(this.SUBJECT,{receiverID, senderIDs}); floCloudAPI.requestObjectData(this.SUBJECT,{receiverID, senderIDs});
await this.delay(5000); await this.delay(5000);
document.getElementById('current_data').innerHTML = ''; document.getElementById('current_data').innerHTML = '';
this.showFullContentOfArticle(floGlobals.appObjects[this.SUBJECT]); this.showFullContentOfArticle(floGlobals.appObjects[this.SUBJECT]);
}, },
createNewArticle: function(article_name, div='', number_of_sections=cloudArticleApp.numberOfSections) { createNewArticle: function(article_name, div='', number_of_sections=cloudArticleApp.numberOfSections) {
new_article = {}; new_article = {};
new_article[article_name] = {}; new_article[article_name] = {};
new_article[article_name].data = {};
for (let j=0; j<number_of_sections; j++) { for (let j=0; j<number_of_sections; j++) {
i = j+1; i = j+1;
let content = ''; let content = '';
if(div=='reset_data_div') { if(div=='reset_data_div') {
content = document.getElementById(`article_content${i}`).value; content = document.getElementById(`article_content${i}`).value;
new_article[article_name][`section${i}`] = {}; new_article[article_name].data[`section${i}`] = {};
new_article[article_name][`section${i}`]["iteration0"] = { new_article[article_name].data[`section${i}`]["iteration0"] = {
content: content, content: content,
content_creator: myFloID, content_creator: myFloID,
score: 0 score: 0
} }
} else if(div=='update_data_div') { } else if(div=='update_data_div') {
content = document.getElementById(`article_content_up`).value; content = document.getElementById(`article_content_up`).value;
new_article[article_name][`section${i}`] = {}; new_article[article_name].data[`section${i}`] = {};
if(i!==1) { if(i!==1) {
content = ''; content = '';
} }
new_article[article_name][`section${i}`]["iteration0"] = { new_article[article_name].data[`section${i}`]["iteration0"] = {
content: content, content: content,
content_creator: myFloID, content_creator: myFloID,
score: 0 score: 0
@ -7518,6 +7557,7 @@
} }
} }
return new_article; return new_article;
}, },
@ -7565,7 +7605,11 @@
console.log(floGlobals.appObjects[this.SUBJECT]); console.log(floGlobals.appObjects[this.SUBJECT]);
console.log(full_data); console.log(full_data);
floCloudAPI.updateObjectData(floGlobals.appObjects[this.SUBJECT], full_data, this.SUBJECT); if(floGlobals.subAdmins.includes(myFloID)) {
floCloudAPI.updateObjectData(floGlobals.appObjects[this.SUBJECT], full_data, this.SUBJECT);
} else {
floCloudAPI.sendGeneralData(full_data,"typeContentCollab",{receiverID:floGlobals.adminID, senderIDs:[myFloID]})
}
this.retrieveLatestContent(); this.retrieveLatestContent();
}, },
@ -7623,4 +7667,3 @@
</body> </body>
</html> </html>