diff --git a/lib/boards-api.js b/lib/boards-api.js index 883d22d..c2c27e3 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -258,20 +258,23 @@ BoardsAPI.prototype.getBoardSettings = function(userID,board){ return this.ee } +BoardsAPI.prototype.downloadPost = function(hash,adminID,board,op){ + this.ipfs.cat(hash,(err2,r) => { + if(err2){ + this.ee.emit('error',err2) + console.log('Could not download post',hash,'of',board+'@'+adminID) + } else { + // It already returns a JSON? + var post = r + if(op) post.op = op // Inject op + if(board) this.ee.emit('post in '+board+'@'+adminID,post,hash) + this.ee.emit(hash,post,adminID,board) + } + }) + return this.ee +} + BoardsAPI.prototype.getPostsInBoard = function(adminID,board){ - var downloadPost = (hash,op) => { - this.ipfs.cat(hash,(err2,r) => { - if(err2){ - this.ee.emit('error',err2) - console.log('Could not download post',hash,'of',board+'@'+adminID) - } else { - // It already returns a JSON? - var post = r - if(op) post.op = op // Inject op - this.ee.emit('post in '+board+'@'+adminID,post,hash) - } - }) - } this.getBoardSettings(adminID,board) this.ee.on('settings for'+board+'@'+adminID,function(settings,addr){ // Download posts based on settings @@ -288,7 +291,7 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){ }) this.emit('approved posts for '+board+'@'+adminID,ret) // Automatically download approved posts - ret.forEach(item => downloadPost(item.hash)) + ret.forEach(item => this.downloadPost(item.hash,adminID,board)) } }) if(settings.whitelist == true){ @@ -304,7 +307,7 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){ this.getUserPostListInBoard(adminID,board,(err,res) => { if(err){ console.log(err) - } else res.forEach(item => downloadPost(item.hash,adminID)) + } else res.forEach(item => this.downloadPost(item.hash,adminID,board,adminID)) }) return this.ee }