1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-03-11 21:38:38 +01:00

it's not perfect but whitelist support is in

This commit is contained in:
Enrico Fasoli 2015-12-04 11:41:37 +01:00
parent d8f744301f
commit 4c675235dd
4 changed files with 34 additions and 19 deletions

View File

@ -45,7 +45,7 @@ function replyAsObj(res,isJson,done){
}
})
} else if(res.split || res.toString){
console.log('got string or buffer:',res)
//console.log('got string or buffer:',res)
if(res.toString) res = res.toString()
// Is a string
if(isJson){
@ -237,16 +237,16 @@ BoardsAPI.prototype.getBoardSettings = function(userID,board){
if(settings.whitelist == true){
// Get the whitelist
var url = r+this.baseurl+'boards/'+board+'/whitelist'
this.ipfs.cat(url,(err,whitelist) => {
this.ipfs.cat(url,(err,res) => {
if(err){
this.ee.emit('error',err)
// Emit an empty whitelist.
this.emit('whitelist for '+board+'@'+userID,[])
} else {
this.ee.emit('whitelist for '+board+'@'+userID,[])
} else replyAsObj(res,false,(err,whitelist) => {
// Send whitelist
var w = whitelist.split(' ')
this.emit('whitelist for '+board+'@'+userID,w)
}
var w = whitelist.split(' ').map(x => x.trim())
this.ee.emit('whitelist for '+board+'@'+userID,w)
})
})
}
if(!settings.whitelist_only && !settings.approval_required && settings.blacklist == true){
@ -271,22 +271,24 @@ BoardsAPI.prototype.getBoardSettings = function(userID,board){
}
BoardsAPI.prototype.downloadPost = function(hash,adminID,board,op,done){
console.log('Downloading post',hash)
this.ipfs.cat(hash,(err2,r) => {
if(err2){
this.ee.emit('error',err2)
console.log('Could not download post',hash,'of',board+'@'+adminID)
if(done && done.apply) done(err2)
} else {
// TODO: add JSON parsing error handling
var post = JSON.parse(r.toString())
post.hash = hash
if(op) post.op = op // Inject op
if(board){
if(adminID) this.ee.emit('post in '+board+'@'+adminID,post,hash)
else this.ee.emit('post in '+board,post,hash)
}
this.ee.emit(hash,post,adminID,board)
if(done && done.apply) done(null,post)
replyAsObj(r,true,(err,post) => {
// TODO: add JSON parsing error handling
post.hash = hash
if(op) post.op = op // Inject op
if(board){
if(adminID) this.ee.emit('post in '+board+'@'+adminID,post,hash)
else this.ee.emit('post in '+board,post,hash)
}
this.ee.emit(hash,post,adminID,board)
if(done && done.apply) done(null,post)
})
}
})
return this.ee
@ -336,7 +338,7 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){
// download posts for each user in whitelist
whitelist.forEach(item => {
this.getUserPostListInBoard(item,board,(err,postList) => {
postList.forEach( i => this.downloadPost(i,adminID,board))
postList.forEach( i => this.downloadPost(i.hash,adminID,board,item))
})
})
})

View File

@ -11,6 +11,7 @@ module.exports = React.createClass({
return (b.date || 0) - (a.date || 0)
},
init: function(boards){
if(this.state.init) return
this.setState({ api: true })
boards.getPostsInBoard(this.props.admin,this.props.board)
.on('post in '+this.props.board+(this.props.admin?'@'+this.props.admin:''),(post,hash) => {
@ -27,6 +28,7 @@ module.exports = React.createClass({
}
this.setState({ posts })
})
this.setState({ init: true })
},
componentDidMount: function(){
var boards = this.props.api

View File

@ -9,7 +9,7 @@ var GetIPFS = require('getipfs.jsx')
module.exports = function(boardsAPI){
return React.createClass({
getInitialState: function(){
return { name: this.props.params.boardname, api: false }
return { name: this.props.params.boardname, api: false, whitelist: [] }
},
componentDidMount: function(){
boardsAPI.use(boards => {
@ -28,6 +28,9 @@ module.exports = function(boardsAPI){
}
})
if(this.props.params.userid){
ee.on('whitelist for '+this.props.params.boardname+'@'+this.props.params.userid,(whitelist) => {
if(this.isMounted()) this.setState({ whitelist })
})
ee.on('settings for '+this.props.params.boardname+'@'+this.props.params.userid, (res) => {
if(!this.isMounted()) return true
if(res) this.setState({ name: res.fullname, description: res.description })
@ -53,6 +56,9 @@ module.exports = function(boardsAPI){
<h2>{this.state.name}</h2>
<Markdown source={this.state.description} skipHtml={true} />
{this.props.params.userid?<h5><UserID id={this.props.params.userid} api={this.state.boards} /></h5>:<p></p>}
<div className="whitelist">
{this.state.whitelist.map(i => <UserID id={i} api={this.state.boards} />)}
</div>
<hr />
<PostList board={this.props.params.boardname} admin={this.props.params.userid} api={this.state.boards} />
</div>)

View File

@ -70,6 +70,11 @@ a:hover {
margin-right: .25rem;
}
.whitelist .user-id {
display: inline;
margin-right: .3em
}
.post {
display: block;
max-width: 65rem;