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

updated IPNS resolving system

This commit is contained in:
Enrico Fasoli 2015-11-14 18:38:46 +01:00
parent 0fd0141068
commit 742a48ceb3

View File

@ -51,39 +51,49 @@ function BoardsAPI(ipfs){
this.ipfs = ipfs this.ipfs = ipfs
this.version = 'dev' this.version = 'dev'
this.users = {} // userID : profileHash this.users = {} // userID : profileHash
this.posts = {} // boardName : postsList this.resolving_ipns = {} // to check if a resolve is already in progress
this.comments = {} // objectID : comments
this.ee = new EventEmitter() this.ee = new EventEmitter()
} }
// Rewrote this to use event emitters. Should also add periodic rechecking // Rewrote this to use event emitters. Should also add periodic rechecking
BoardsAPI.prototype.resolveIPNS = function(n,handler){ BoardsAPI.prototype.resolveIPNS = function(n,handler){
this.ee.on(n,handler) if(handler) this.ee.on(n,handler)
var cached = this.users[n] var cached = this.users[n]
//console.log('Cached is',cached) //console.log('Cached is',cached)
if(cached){ if(cached){
this.ee.emit(n,cached) this.ee.emit(n,cached)
} }
this.ipfs.name.resolve(n,(err,r) => { if(this.resolving_ipns[n] != true){
if(!err) console.log('Resolved',n,'to',r.Path) this.resolving_ipns[n] = true
if(err){ this.ipfs.name.resolve(n,(err,r) => {
// Communicate error setTimeout(_ => {
this.ee.emit(n,undefined,err) console.log('Launching automatic check for IPNS address',n)
} else if(!cached){ this.prototype.resolveIPNS(n)
//console.log('oldcache',this.users) },20*1000)
//console.log('Setting cache for',n,'to',r.Path) if(!err) console.log('Resolved',n,'to',r.Path)
this.users[n] = r.Path if(err){
this.ee.emit(n,r.Path) // Communicate error
} else if(cached !== r.Path){ this.ee.emit(n,undefined,err)
// Update cache, emit new value to listeners } else if(!cached){
//console.log('Setting cache for',n,'from',this.users[n],'to',r.Path) //console.log('oldcache',this.users)
this.users[n] = r.Path //console.log('Setting cache for',n,'to',r.Path)
this.ee.emit(n,r.Path) this.users[n] = r.Path
} this.ee.emit(n,r.Path)
}) } else if(cached !== r.Path){
// Update cache, emit new value to listeners
//console.log('Setting cache for',n,'from',this.users[n],'to',r.Path)
this.users[n] = r.Path
this.ee.emit(n,r.Path)
}
})
}
return this.ee return this.ee
} }
BoardsAPI.prototype.getIPNS = function(){
}
BoardsAPI.prototype.isUserProfile = function(addr,done){ BoardsAPI.prototype.isUserProfile = function(addr,done){
this.ipfs.cat(addr+'/ipfs-boards-version.txt',(err,r) => { this.ipfs.cat(addr+'/ipfs-boards-version.txt',(err,r) => {
if(err) return done(false) if(err) return done(false)