From 013266b3f7c04f90789fe7afa5b64f13cf29001d Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Sat, 12 Dec 2015 12:51:09 +0100 Subject: [PATCH] implemented comments to dinamyc structures, closes #53 --- lib/boards-api.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/boards-api.js b/lib/boards-api.js index 9bf59e0..b940c27 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -384,37 +384,50 @@ BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){ return this.ee } -BoardsAPI.prototype.downloadComment = function(hash,adminID,board,done){ +BoardsAPI.prototype.downloadComment = function(hash,adminID,board,target){ this.ipfs.cat(hash,(err2,r) => { if(err2){ this.ee.emit('error',err2) console.log('Could not download comment',hash,'of',board+'@'+adminID) - if(done && done.apply) done(err2) } else { // TODO: add JSON parsing error handling var cmnt = JSON.parse(r.toString()) cmnt.hash = hash this.ee.emit(hash,cmnt,adminID,board) - this.ee.emit('comment for '+cmnt.parent,cmnt) - if(done && done.apply) done(null,cmnt) + this.ee.emit('comment for '+(target || cmnt.parent),cmnt) } }) return this.ee } -BoardsAPI.prototype.getCommentsFor = function(parent,board,adminID){ +BoardsAPI.prototype.getCommentsFor = function(parent,board,adminID,target){ if(!parent || !board || !adminID){ return console.log('malformed arguments:',parent,board,adminID) } - this.ee.on('approved comments for '+board+'@'+adminID,ret => { - ret.forEach(item => this.downloadComment(item.hash,adminID,board)) + // figure out if there's a previous version of the item + this.ipfs.cat(parent, (err,res) => { + if(err){ + this.ee.emit('error',err) + } else { + replyAsObj(res,true,(err2,obj) => { + if(err2){ + this.ee.emit('error',err2) + } else if(typeof obj.previous == 'string'){ + this.getCommentsFor(obj.previous,board,adminID,parent) + } + }) + } }) // get the admin's comments this.getUserCommentList(parent,adminID,(err,res) => { if(!err){ - res.forEach(item => this.downloadComment(item.hash,adminID,board)) + res.forEach(item => this.downloadComment(item.hash,adminID,board,target)) } }) + // Handle approved comments + this.ee.on('approved comments for '+board+'@'+adminID,ret => { + ret.forEach(item => this.downloadComment(item.hash,adminID,board,target)) + }) this.getAllowedContentProducers(adminID,board,{ comments: true }) }