1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-25 14:54:19 +01:00

improved IPNS resolution, implemented users page

This commit is contained in:
Enrico Fasoli 2015-11-18 16:56:07 +01:00
parent 2448630e4b
commit 2ebd4c8a89
3 changed files with 79 additions and 25 deletions

View File

@ -109,7 +109,7 @@ possible for lange texts without duplicating data.
#### Versioning
just the version ID written in the version file
ipfs:boards:version:version_name
## Boards

View File

@ -49,7 +49,7 @@ function replyAsObj(res,isJson,done){
function BoardsAPI(ipfs){
this.ipfs = ipfs
this.version = 'dev'
this.version = 'ipfs:boards:version:dev'
this.baseurl = '/ipfs-boards-profile/'
this.users = {} // userID : profileHash
this.resolving_ipns = {} // to check if a resolve is already in progress
@ -81,6 +81,9 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
var cached = this.users[n]
if(cached){
this.ee.emit(n,cached)
console.log(n,'was cached')
} else {
console.log(n,'not cached')
}
if(this.resolving_ipns[n] != true){
this.resolving_ipns[n] = true
@ -89,16 +92,22 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
// Communicate error
this.ee.emit('error',err)
} else {
var url = r.Hash
if(this.users[n] != url) this.isUserProfile(r.Hash,isit => {
if(isit == this.version){
var url = r.Path
if(url === undefined){
console.log('UNDEFINED URL',r)
}
if(this.users[n] != url) this.isUserProfile(url,(isit,err) => {
if(isit){
console.log(n,'is a user')
if(this.users[n] != url){
this.users[n] = url
this.ee.emit(n,url)
}
this.users[n] = url
this.ee.emit(n,url)
this.backupCache()
} else {
console.log(n,'not a user')
this.ee.emit(n,undefined,'not a valid profile: '+err)
}
this.resolving_ipns[n] = false
return true // Remove from listeners
})
}
})
@ -109,11 +118,14 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
BoardsAPI.prototype.isUserProfile = function(addr,done){
if(addr === undefined) return console.log('Asked to check if undefined is a profile')
this.ipfs.cat(addr+this.baseurl+'ipfs-boards-version.txt',(err,r) => {
if(err) return done(false)
if(err) return done(false,err)
replyAsObj(r,false,(_,res) => {
var v = res.trim()
console.log('Version for',addr,'is',v)
done(v)
console.log('Version in profile snapshot',addr,'is',v)
if(v === this.version)
done(true)
else
done(false,'version mismatch: is "'+v+'" but should be "'+this.version+'"')
})
})
}
@ -125,10 +137,25 @@ BoardsAPI.prototype.searchUsers = function(){
replyAsObj(r,true,(e,reply) => {
console.log('Checking',reply.Strings.length,'peers')
reply.Strings.forEach(item => {
this.resolveIPNS(item)
var ss = item.split('/')
var n = ss[ss.length-1]
this.resolveIPNS(n)
})
})
})
// Look for who has the correct version file, they probably have a profile
// Disabled at the moment
/*
this.ipfs.dht.findprovs(this.version_hash, (err,res) => {
if(err){
console.log('DHT FINDPROVS err',err)
} else if(res.readable){
console.log('DHT FINDPROVS stream',res)
} else {
console.log('DHT FINDPROVS string',res)
}
})
*/
return this.ee
}
@ -146,6 +173,7 @@ BoardsAPI.prototype.getProfile = function(userID,done){
done(err2,null)
} else {
// It already returns a JSON?
this.ee.emit('profile for '+userID,res)
done(null,res)
}
})
@ -305,12 +333,13 @@ BoardsAPI.prototype.init = function(done){
this.id = res.ID
this.resolveIPNS(res.ID)
console.log('Version is',this.version)
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err2,r) => {
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),{n: true},(err2,r) => {
if(err2){
this.ee.emit('error',err2)
console.log('Error while calculating version hash:',err2)
done(err2)
} else {
if(r && r.Hash) this.version_hash = r.Hash
if(r && r[0] && r[0].Hash) this.version_hash = r[0].Hash
console.log('Version hash is',this.version_hash)
done(null)

View File

@ -102,7 +102,7 @@ var PostList = React.createClass({
var UserID = React.createClass({
getInitialState: function(){
return { name: '@'+this.props.id }
return { }
},
componentDidMount: function(){
boards.getProfile(this.props.id, (err,res) => {
@ -112,10 +112,17 @@ var UserID = React.createClass({
}
})
},
getContent: function(){
if(this.state.name){
return (<Icon name="user" />)
} else {
return '@'
}
},
render: function(){
return (<div className="board">
<Link className="light nounderline" to={'/@'+this.props.id}>
<Icon name="user"/> {this.state.name}
{this.getContent()}{this.state.name || this.props.id}
</Link>
</div>)
}
@ -184,21 +191,23 @@ var Profile = React.createClass({
return { name: '...', boards: [] }
},
componentDidMount: function(){
var ee = boards.getProfile(this.props.params.userid, (err,res) => {
console.log('About to ask for profile for',this.props.params.userid)
var ee = boards.getEventEmitter()
ee.on('boards for '+this.props.params.userid,l => {
if(!this.isMounted()) return true
this.setState({ boards: l })
})
boards.getProfile(this.props.params.userid,(err,res) => {
if(!this.isMounted()) return true
if(err){
this.setState({
name: '?',
error: 'Invalid profile'
name: <Icon name="ban" />,
description: err
})
} else {
this.setState({ name: res.name, description: res.description })
}
})
ee.on('boards for '+this.props.params.userid,l => {
if(!this.isMounted()) return true
this.setState({ boards: l })
})
},
linkToEditor: function(){
if(this.props.params.userid === boards.id){
@ -213,7 +222,6 @@ var Profile = React.createClass({
return (<div className="profile">
{this.linkToEditor()}
<h1>{this.state.name}</h1>
<p>{this.state.error}</p>
<Markdown source={this.state.description} skipHtml={true} />
<hr/>
<h5 className="light">@{this.props.params.userid}</h5>
@ -249,8 +257,25 @@ var Board = React.createClass({
})
var Users = React.createClass({
getInitialState: function(){
return { users: [ boards.id ] }
},
componentDidMount: function(){
boards.searchUsers().on('user',(id) => {
if(this.isMounted() && this.state.users.indexOf(id) < 0)
this.setState({ users: this.state.users.concat(id) })
})
},
render: function(){
return <NotImplemented />
return <div>
<h1><Icon name="users" /> Users</h1>
<p>Found <b>{this.state.users.length}</b> users, looking for more...</p>
<ul>
{this.state.users.map(user => {
return <UserID key={user} id={user} />
})}
</ul>
</div>
}
})