From 2448630e4b89e93674d91f6b039af1e5227b274a Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Wed, 18 Nov 2015 15:40:29 +0100 Subject: [PATCH] added few small features and rewrote user profile resolution --- lib/boards-api.js | 58 +++++++++++++---------------------------------- webapp/app.jsx | 17 +++++++++++--- webapp/board.html | 3 --- webapp/home.html | 1 - 4 files changed, 30 insertions(+), 49 deletions(-) delete mode 100644 webapp/board.html delete mode 100644 webapp/home.html 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({
-

Boards

+ {this.props.children ||

Boards

}
+ @@ -185,13 +187,11 @@ var Profile = React.createClass({ var ee = boards.getProfile(this.props.params.userid, (err,res) => { if(!this.isMounted()) return true if(err){ - console.log(err) this.setState({ name: '?', error: 'Invalid profile' }) } else { - console.log(res) this.setState({ name: res.name, description: res.description }) } }) @@ -200,8 +200,18 @@ var Profile = React.createClass({ this.setState({ boards: l }) }) }, + linkToEditor: function(){ + if(this.props.params.userid === boards.id){ + return
+
This is your profile
+
+
+ } + return '' + }, render: function(){ return (
+ {this.linkToEditor()}

{this.state.name}

{this.state.error}

@@ -323,6 +333,7 @@ boards.init(err => { + diff --git a/webapp/board.html b/webapp/board.html deleted file mode 100644 index 211b02e..0000000 --- a/webapp/board.html +++ /dev/null @@ -1,3 +0,0 @@ -
-
{{post}}
-
diff --git a/webapp/home.html b/webapp/home.html deleted file mode 100644 index 480ec0b..0000000 --- a/webapp/home.html +++ /dev/null @@ -1 +0,0 @@ -Landing Page