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

user detection works finally

This commit is contained in:
Enrico Fasoli 2015-11-11 12:38:10 +01:00
parent 00593e9e44
commit 49e8a75e3d
2 changed files with 95 additions and 22 deletions

View File

@ -5,9 +5,9 @@ function asObj(str,done){
try {
obj = JSON.parse(str)
} catch (e) {
done(e,null)
return done(e,null)
}
if(obj != undefined) done(null,obj)
done(null,obj)
}
function replyAsObj(res,isJson,done){
@ -38,23 +38,61 @@ function replyAsObj(res,isJson,done){
function BoardsAPI(ipfs){
this.ipfs = ipfs
this.version = 'dev'
this.users = {}
}
BoardsAPI.prototype.searchUsers = function(done){
// This function works but needs a little rethinking.
BoardsAPI.prototype.resolveIPNS = function(n,done){
var cached = this.users[n]
if(cached){
done(null,cached)
}
this.ipfs.name.resolve(n,(err,r) => {
console.log('Resolved',n,'to',r)
if(err){
done(err)
} else if(!cached){
done(err,r.Path)
} else if(cached !== r.Path){
// Update cache
this.users[n] = r.Path
}
})
}
BoardsAPI.prototype.isUserProfile = function(addr,done){
this.ipfs.cat(addr+'/ipfs-boards-version.txt',(err,r) => {
if(err) return done(false)
replyAsObj(r,false,(_,res) => {
var v = res.trim()
console.log('Version for',addr,'is',v)
done(v)
})
})
}
BoardsAPI.prototype.isUser = function(s){
var ss = s.split('/')
var addr = ss[ss.length-1]
// Try to see if they run IPFS Boards
console.log('Is',addr,'a user?')
this.resolveIPNS(addr,(err,url) => {
if(err) 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
}
})
})
}
BoardsAPI.prototype.searchUsers = function(){
// Look at our peers
this.ipfs.swarm.peers(function(err,r){
var peers = r.Strings.forEach(function(s){
var ss = s.split('/')
var addr = ss[ss.length-1]
// Try to see if they run IPFS Boards
this.ipfs.cat(addr+'/ipfs-boards-version.txt',function(err,r){
if(err) return console.log('Search Err:',err)
replyAsObj(r,false,(_,res) => {
// He does!
// TODO: store found users in a list?
console.log('Found user:',addr,'using version',res)
})
})
this.ipfs.swarm.peers((err,r) => {
if(err) return console.log(err)
replyAsObj(r,true,(e,reply) => {
reply.Strings.forEach(this.isUser.bind(this))
})
})
}
@ -81,7 +119,9 @@ BoardsAPI.prototype.getName = function(userID,done){
BoardsAPI.prototype.getBoardSettings = function(userID,board,done){
var url = userID+'/boards/'+board+'/settings.json'
console.log("Getting Board Settings:",url)
this.ipfs.cat(url,function(err,res){
console.log('Done')
if(err){
done(err,{})
} else {
@ -90,27 +130,27 @@ BoardsAPI.prototype.getBoardSettings = function(userID,board,done){
})
}
BoardsAPI.prototype.getBoardPosts(board,administratorID,done){
BoardsAPI.prototype.getBoardPosts = function(board,administratorID,done){
// Returns a stream
}
BoardsAPI.prototype.getUserPosts(user,board,done){
BoardsAPI.prototype.getUserPosts = function(user,board,done){
// Returns a stream
}
BoardsAPI.prototype.getComments(parent,board,done){
BoardsAPI.prototype.getComments = function(parent,board,done){
// Returns a stream
}
BoardsAPI.prototype.createPost(post,board,done){
BoardsAPI.prototype.createPost = function(post,board,done){
}
BoardsAPI.prototype.createComment(parent,comment,done){
BoardsAPI.prototype.createComment = function(parent,comment,done){
}
BoardsAPI.prototype.createUpvote(parent,done){
BoardsAPI.prototype.createUpvote = function(parent,done){
}
@ -125,6 +165,7 @@ BoardsAPI.prototype.init = function(done){
} else {
console.log('I am',res.ID)
this.id = res.ID
this.isUser(res.ID)
console.log('Version is',this.version)
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err,r) => {
this.version_hash = r[0].Hash

View File

@ -10,8 +10,40 @@ var boards = new BoardsAPI(ipfs)
// Serve web app
app.use(express.static('../webapp'))
// Generates a callback function to pass the API response to HTTP
var apiToHandler = function(request,response){
return (function(a,b){
var req = a
var res = b
return function(err,r){
if(err){
res.status(500)
res.send(err)
} else {
if(r.split){
res.send(r)
} else {
res.json(r)
}
}
}
})(request,response)
}
// Create gateways to access the BoardsAPI
app.get('/@:user',(req,res) => {
boards.getProfile(req.params.user,apiToHandler(req,res))
})
app.get('/@:user/:board',(req,res) => {
boards.getBoardSettings(req.params.user,req.params.board,apiToHandler(req,res))
})
//boards.searchUsers()
// Start the web server
function startWebServer(){
// Start http server
app.listen(3000,function(){