mirror of
https://github.com/fazo96/ipfs-boards
synced 2025-03-11 21:38:38 +01:00
updated IPNS resolving system
This commit is contained in:
parent
0fd0141068
commit
742a48ceb3
@ -51,39 +51,49 @@ function BoardsAPI(ipfs){
|
||||
this.ipfs = ipfs
|
||||
this.version = 'dev'
|
||||
this.users = {} // userID : profileHash
|
||||
this.posts = {} // boardName : postsList
|
||||
this.comments = {} // objectID : comments
|
||||
this.resolving_ipns = {} // to check if a resolve is already in progress
|
||||
this.ee = new EventEmitter()
|
||||
}
|
||||
|
||||
// Rewrote this to use event emitters. Should also add periodic rechecking
|
||||
BoardsAPI.prototype.resolveIPNS = function(n,handler){
|
||||
this.ee.on(n,handler)
|
||||
if(handler) this.ee.on(n,handler)
|
||||
var cached = this.users[n]
|
||||
//console.log('Cached is',cached)
|
||||
if(cached){
|
||||
this.ee.emit(n,cached)
|
||||
}
|
||||
this.ipfs.name.resolve(n,(err,r) => {
|
||||
if(!err) console.log('Resolved',n,'to',r.Path)
|
||||
if(err){
|
||||
// Communicate error
|
||||
this.ee.emit(n,undefined,err)
|
||||
} else if(!cached){
|
||||
//console.log('oldcache',this.users)
|
||||
//console.log('Setting cache for',n,'to',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)
|
||||
}
|
||||
})
|
||||
if(this.resolving_ipns[n] != true){
|
||||
this.resolving_ipns[n] = true
|
||||
this.ipfs.name.resolve(n,(err,r) => {
|
||||
setTimeout(_ => {
|
||||
console.log('Launching automatic check for IPNS address',n)
|
||||
this.prototype.resolveIPNS(n)
|
||||
},20*1000)
|
||||
if(!err) console.log('Resolved',n,'to',r.Path)
|
||||
if(err){
|
||||
// Communicate error
|
||||
this.ee.emit(n,undefined,err)
|
||||
} else if(!cached){
|
||||
//console.log('oldcache',this.users)
|
||||
//console.log('Setting cache for',n,'to',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
|
||||
}
|
||||
|
||||
BoardsAPI.prototype.getIPNS = function(){
|
||||
|
||||
}
|
||||
|
||||
BoardsAPI.prototype.isUserProfile = function(addr,done){
|
||||
this.ipfs.cat(addr+'/ipfs-boards-version.txt',(err,r) => {
|
||||
if(err) return done(false)
|
||||
|
Loading…
Reference in New Issue
Block a user