diff --git a/lib/boards-api.js b/lib/boards-api.js index dae9b10..414583b 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -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)