From 471f29184846956373eb99894800dbb73823dcc5 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Wed, 26 Jul 2023 03:45:35 +0530 Subject: [PATCH] floCloudAPI v2.4.4: edit comment feature - senders can now edit the comment of data they sent (by re-signing the new data) --- floCloudAPI.js | 64 +++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/floCloudAPI.js b/floCloudAPI.js index c2d2c3f..2cf5ed4 100644 --- a/floCloudAPI.js +++ b/floCloudAPI.js @@ -1,4 +1,4 @@ -(function (EXPORTS) { //floCloudAPI v2.4.3a +(function (EXPORTS) { //floCloudAPI v2.4.4 /* FLO Cloud operations to send/request application data*/ 'use strict'; const floCloudAPI = EXPORTS; @@ -609,49 +609,39 @@ }) } */ - /*(NEEDS UPDATE) - //edit comment of data in supernode cloud (mutable comments only) - floCloudAPI.editApplicationData = function(vectorClock, newComment, oldData, options = {}) { + //edit comment of data in supernode cloud (sender only) + floCloudAPI.editApplicationData = function (vectorClock, comment_edit, options = {}) { return new Promise((resolve, reject) => { - let p0 - if (!oldData) { - options.atVectorClock = vectorClock; - options.callback = false; - p0 = requestApplicationData(false, options) - } else - p0 = Promise.resolve({ - vectorClock: { - ...oldData - } - }) - p0.then(d => { - if (d.senderID != user.id) - return reject("Invalid requestorID") - else if (!d.comment.startsWith("EDIT:")) - return reject("Data immutable") - let data = { + //request the data from cloud for resigning + let req_options = Object.assign({}, options); + req_options.atVectorClock = vectorClock; + requestApplicationData(undefined, req_options).then(result => { + if (!result.length) + return reject("Data not found"); + let data = result[0]; + if (data.senderID !== user.id) + return reject("Only sender can edit comment"); + data.comment = comment_edit; + let hashcontent = ["receiverID", "time", "application", "type", "message", "comment"] + .map(d => data[d]).join("|"); + let re_sign = user.sign(hashcontent); + var request = { + receiverID: options.receiverID || DEFAULT.adminID, requestorID: user.id, - receiverID: d.receiverID, + pubKey: user.public, time: Date.now(), - application: d.application, - edit: { - vectorClock: vectorClock, - comment: newComment - } + vectorClock: vectorClock, + edit: comment_edit, + re_sign: re_sign } - d.comment = data.edit.comment; - let hashcontent = ["receiverID", "time", "application", "type", "message", - "comment" - ] - .map(x => d[x]).join("|") - data.edit.sign = user.sign(hashcontent) - singleRequest(data.receiverID, data) - .then(result => resolve("Data comment updated")) + let request_hash = ["time", "vectorClock", "edit", "re_sign"].map(d => request[d]).join("|"); + request.sign = user.sign(request_hash); + singleRequest(request.receiverID, request) + .then(result => resolve(result)) .catch(error => reject(error)) - }) + }).catch(error => reject(error)) }) } - */ //tag data in supernode cloud (subAdmin access only) floCloudAPI.tagApplicationData = function (vectorClock, tag, options = {}) {