1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-10 12:24:20 +01:00

got post loading to work

This commit is contained in:
Enrico Fasoli 2015-11-14 15:03:38 +01:00
parent 46c2eb4309
commit 0f71407040
4 changed files with 128 additions and 33 deletions

View File

@ -12,7 +12,8 @@ var config = {
mainJs: 'webapp/app.jsx',
css: 'webapp/*.css',
js: ['webapp/*.js','webapp/*.jsx'],
html: 'webapp/*.html'
html: 'webapp/*.html',
jsLibs: 'lib/*.js'
},
dest: 'webapp/dist/'
}
@ -60,6 +61,7 @@ gulp.task('server',function(){
gulp.task('watch',function(){
gulp.watch(config.files.js,['js'])
gulp.watch(config.files.jsLibs,['js'])
gulp.watch(config.files.html,['html'])
gulp.watch(config.files.css,['css'])
})

View File

@ -2,8 +2,12 @@
This file contains the IPFS Boards API. It's a simple abstraction over the
js-ipfs-api that also provides an additional level of caching for this
particular application. Let's hope it turns out decent
Needs to be browserified to work in the browser
*/
var EventEmitter = require('wolfy87-eventemitter')
function asObj(str,done){
var obj
try {
@ -50,13 +54,6 @@ function BoardsAPI(ipfs){
this.comments = {} // objectID : comments
}
BoardsAPI.prototype.downloadJSON = function(addr,done){
this.ipfs.cat(addr,(err,res) => {
if(err) return done(err)
replyAsObj(res,true,done)
})
}
// This function works but needs a little rethinking.
BoardsAPI.prototype.resolveIPNS = function(n,done){
var cached = this.users[n]
@ -69,6 +66,7 @@ BoardsAPI.prototype.resolveIPNS = function(n,done){
if(err){
done(err)
} else if(!cached){
this.users[n] = r.Path
done(err,r.Path)
} else if(cached !== r.Path){
// Update cache
@ -88,47 +86,74 @@ BoardsAPI.prototype.isUserProfile = function(addr,done){
})
}
BoardsAPI.prototype.isUser = function(s){
BoardsAPI.prototype.isUser = function(s,done){
var ss = s.split('/')
var addr = ss[ss.length-1]
// Try to see if they run IPFS Boards
this.resolveIPNS(addr,(err,url) => {
if(err) return console.log('Cannot resolve',addr,':',err)
if(err){
if(done) done(false)
return console.log('Cannot resolve',addr,':',err)
}
this.isUserProfile(url,isit => {
if(isit == this.version){
console.log(addr,'is a user')
this.users[addr] = url
}
if(done) done(true,addr,url)
} else if(done) done(false)
})
})
}
BoardsAPI.prototype.searchUsers = function(){
var ee = new EventEmitter()
// Look at our peers
this.ipfs.swarm.peers((err,r) => {
if(err) return console.log(err)
replyAsObj(r,true,(e,reply) => {
console.log('Checking',reply.Strings.length,'peers')
reply.Strings.forEach(this.isUser.bind(this))
reply.Strings.forEach(item => {
this.isUser(item,(isit,addr,url) => {
if(isit){
ee.emit('found user',addr,url)
}
})
})
})
})
return ee
}
BoardsAPI.prototype.getProfile = function(userID,done){
var ee = new EventEmitter()
console.log('profile requested for',userID)
this.resolveIPNS(userID,(err,url) => {
if(err){
done(err,null)
} else this.ipfs.cat(url+'/profile.json',(err2,res) => {
console.log('got something')
if(err2){
done(err2,null)
} else {
// It already returns a JSON?
done(null,res)
}
})
} else {
// Download actual profile
this.ipfs.cat(url+'/profile.json',(err2,res) => {
console.log('got something')
if(err2){
done(err2,null)
} else {
// It already returns a JSON?
done(null,res)
}
})
// Get other info
this.ipfs.ls(url+'/boards/',(err2,res) => {
if(!err2){
console.log('RES',res)
var l = res.Objects[0].Links.map(i => {
return { name: i.Name, hash: i.Hash }
})
ee.emit('boards',l)
} else console.log(err2)
})
}
})
return ee
}
BoardsAPI.prototype.getName = function(userID,done){
@ -154,24 +179,45 @@ BoardsAPI.prototype.getBoardSettings = function(userID,board,done){
})
}
BoardsAPI.prototype.getBoardPostList = function(board,administratorID,done){
BoardsAPI.prototype.getPostsInBoard = function(adminID,board){
var ee = new EventEmitter()
/*
this.getBoardSettings(administratorID,board,(err,res) => {
// NEEDS: board settings structure definition
// For now we only list admin's posts
})
*/
this.getUserPostListInBoard(adminID,board,(err,res) =>{
if(err){
console.log(err)
} else res.forEach(item => {
this.ipfs.cat(item.hash,(err2,r) => {
console.log('got something')
if(err2){
console.log('Could not download post',item,'of',board+'@'+adminID)
} else {
// It already returns a JSON?
ee.emit('post',r)
}
})
})
})
return ee
}
BoardsAPI.prototype.getUserPostList = function(user,board,done){
BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
this.resolveIPNS(user,(err,url) => {
if(err){
done(err)
} else this.ipfs.ls(url+'/boards/'+board,(e,r) => {
} else this.ipfs.ls(url+'/posts/'+board,(e,r) => {
if(e){
done(e)
} else {
var l = r.map(i => {
} else if(r && !r.split){
console.log('Found',r.Objects[0].Links.length,'posts in',board,'at',user)
var l = r.Objects[0].Links.map(i => {
return { date: i.Name, hash: i.Hash }
})
done(null,r)
done(null,l)
}
})
})
@ -209,6 +255,9 @@ BoardsAPI.prototype.init = function(done){
this.isUser(res.ID)
console.log('Version is',this.version)
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err,r) => {
if(err){
console.log('Error while calculating version hash:',err)
}
this.version_hash = r[0].Hash
console.log('Version hash is',this.version_hash)
done(null)

View File

@ -10,7 +10,7 @@ boards.init(function(err){
if(err){
console.log(err)
} else {
BoardsAPIHttp(boards)
// BoardsAPIHttp(boards)
// setInterval(boards.searchUsers.bind(boards),3 * 60 * 1000)
// boards.searchUsers()
}

View File

@ -9,6 +9,9 @@ var ipfs = require('ipfs-api')('localhost',5001)
var BoardsAPI = require('../lib/boards-api.js')
var boards = new BoardsAPI(ipfs)
boards.init(_ => {
console.log('Boards init complete')
})
var Container = React.createClass({
render: function(){
@ -47,10 +50,10 @@ var Navbar = React.createClass({
var Profile = React.createClass({
getInitialState: function(){
return { name: '...' }
return { name: '...', boards: [] }
},
componentDidMount: function(){
boards.getProfile(this.props.params.userid, (err,res) => {
var ee = boards.getProfile(this.props.params.userid, (err,res) => {
if(err){
console.log(err)
this.setState({
@ -62,32 +65,73 @@ var Profile = React.createClass({
this.setState({ name: res.name })
}
})
ee.on('boards',l => {
console.log('Arrived boards',l)
this.setState({ boards: l })
})
},
render: function(){
return (<div className="profile">
<h1>{this.state.name}</h1>
<p>{this.state.error}</p>
<h5 className="light">@{this.props.params.userid}</h5>
<ul>
{this.state.boards.map(n => {
return <li key={this.props.params.userid+'/'+n.name}>
<Link to={'/@'+this.props.params.userid+'/'+n.name}>{n.name}</Link>
</li>
})}
</ul>
</div>)
}
})
var Board = React.createClass({
var PostList = React.createClass({
getInitialState: function(){
return { posts: [] }
},
componentDidMount: function(){
var ee = boards.getPostsInBoard(this.props.admin,this.props.board)
ee.on('post',(post) => {
var posts = this.state.posts
posts.push(post)
this.setState({ posts })
})
},
render: function(){
return (
<div></div>
<div className="postList">
{this.state.posts.map(post => {
return (<div className="post">
<h5 key={post.title}>{post.title}</h5>
<p>{post.text}</p>
</div>)
})}
</div>
)
}
})
var Board = React.createClass({
render: function(){
return (<div className="board">
<h2>{this.props.params.boardname}</h2>
<Link to={'/@'+this.props.params.userid}>
<h5 className="light">@{this.props.params.userid}</h5>
</Link>
<PostList board={this.props.params.boardname} admin={this.props.params.userid}/>
</div>)
}
})
ReactDOM.render(
<Router>
<Route path="/" component={App}>
<IndexRoute component={Homepage} />
<Route path="/@:userid" component={Profile} />
<Route path="/@:userid">
<IndexRoute component={Profile} />
<Route path=":boardname" component={Board}/>
</Route>
</Route>
</Router>, document.getElementById('root')
)