diff --git a/lib/boards-api.js b/lib/boards-api.js index ae1cb45..71cc7f6 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -79,29 +79,27 @@ BoardsAPI.prototype.backupCache = function(){ BoardsAPI.prototype.resolveIPNS = function(n,handler){ if(handler && handler.apply) this.ee.on(n,handler) var cached = this.users[n] - //console.log('Cached is',cached) if(cached){ this.ee.emit(n,cached) } if(this.resolving_ipns[n] != true){ this.resolving_ipns[n] = true this.ipfs.name.resolve(n,(err,r) => { - this.resolving_ipns[n] = false - setTimeout(_ => { - console.log('Launched automatic check for IPNS address',n) - this.resolveIPNS(n) - },10*1000) - if(!err) console.log('Resolved',n,'to',r.Path) if(err){ // Communicate error - this.ee.emit(n,undefined,err) - } else if(cached !== r.Path){ - //console.log('oldcache',this.users) - //console.log('Setting cache for',n,'to',r.Path) - this.users[n] = r.Path - this.backupCache() - console.log('Address for',n,'was updated to',r.Path) - this.ee.emit(n,r.Path) + this.ee.emit('error',err) + } else { + var url = r.Hash + if(this.users[n] != url) this.isUserProfile(r.Hash,isit => { + if(isit == this.version){ + console.log(n,'is a user') + if(this.users[n] != url){ + this.users[n] = url + this.ee.emit(n,url) + } + } + this.resolving_ipns[n] = false + }) } }) } @@ -109,6 +107,7 @@ 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) replyAsObj(r,false,(_,res) => { @@ -119,26 +118,6 @@ BoardsAPI.prototype.isUserProfile = function(addr,done){ }) } -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,(url,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) - }) - return true // remove myself from listeners - }) -} - BoardsAPI.prototype.searchUsers = function(){ // Look at our peers this.ipfs.swarm.peers((err,r) => { @@ -146,11 +125,7 @@ BoardsAPI.prototype.searchUsers = function(){ replyAsObj(r,true,(e,reply) => { console.log('Checking',reply.Strings.length,'peers') reply.Strings.forEach(item => { - this.isUser(item,(isit,addr,url) => { - if(isit){ - this.ee.emit('found user',addr,url) - } - }) + this.resolveIPNS(item) }) }) }) @@ -177,7 +152,6 @@ BoardsAPI.prototype.getProfile = function(userID,done){ // Get other info this.ipfs.ls(url+this.baseurl+'boards/',(err2,res) => { if(!err2){ - console.log('RES',res) var l = res.Objects[0].Links.map(i => { return { name: i.Name, hash: i.Hash } }) @@ -329,7 +303,7 @@ BoardsAPI.prototype.init = function(done){ } else if(res.ID){ console.log('I am',res.ID) this.id = res.ID - this.isUser(res.ID) + this.resolveIPNS(res.ID) console.log('Version is',this.version) this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err2,r) => { if(err2){ diff --git a/webapp/app.jsx b/webapp/app.jsx index 7dd6704..6c7bc61 100644 --- a/webapp/app.jsx +++ b/webapp/app.jsx @@ -3,6 +3,7 @@ var ReactDOM = require('react-dom') var Router = require('react-router').Router var Route = require('react-router').Route var IndexRoute = require('react-router').IndexRoute +var Redirect = require('react-router').Redirect var Link = require('react-router').Link var MarkdownLib = require('react-markdown') @@ -59,8 +60,9 @@ var Navbar = React.createClass({
{this.state.error}