2015-11-11 15:51:07 +01:00
|
|
|
/*
|
|
|
|
This file contains the IPFS Boards API. It's a simple abstraction over the
|
|
|
|
js-ipfs-api that also provides an additional level of caching for this
|
|
|
|
particular application. Let's hope it turns out decent
|
|
|
|
*/
|
|
|
|
|
2015-11-11 09:18:36 +01:00
|
|
|
function asObj(str,done){
|
|
|
|
var obj
|
|
|
|
try {
|
|
|
|
obj = JSON.parse(str)
|
|
|
|
} catch (e) {
|
2015-11-11 12:38:10 +01:00
|
|
|
return done(e,null)
|
2015-11-11 09:18:36 +01:00
|
|
|
}
|
2015-11-11 12:38:10 +01:00
|
|
|
done(null,obj)
|
2015-11-11 09:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function replyAsObj(res,isJson,done){
|
|
|
|
if(res.readable){
|
|
|
|
// Is a stream
|
2015-11-14 12:06:37 +01:00
|
|
|
console.log('got stream')
|
2015-11-11 09:18:36 +01:00
|
|
|
res.setEncoding('utf8')
|
|
|
|
var data = ''
|
|
|
|
res.on('data',d => {
|
2015-11-14 12:06:37 +01:00
|
|
|
console.log('got stream data:',d)
|
2015-11-11 09:18:36 +01:00
|
|
|
data += d
|
|
|
|
})
|
|
|
|
res.on('end',() => {
|
|
|
|
if(isJson) {
|
|
|
|
asObj(data,done)
|
|
|
|
} else {
|
|
|
|
done(null,data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2015-11-14 12:06:37 +01:00
|
|
|
console.log('got string:',res)
|
2015-11-11 09:18:36 +01:00
|
|
|
// Is a string
|
|
|
|
if(isJson){
|
|
|
|
asObj(res,done)
|
|
|
|
} else {
|
|
|
|
done(null,res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function BoardsAPI(ipfs){
|
|
|
|
this.ipfs = ipfs
|
|
|
|
this.version = 'dev'
|
2015-11-11 15:22:58 +01:00
|
|
|
this.users = {} // userID : profileHash
|
|
|
|
this.posts = {} // boardName : postsList
|
|
|
|
this.comments = {} // objectID : comments
|
|
|
|
}
|
|
|
|
|
|
|
|
BoardsAPI.prototype.downloadJSON = function(addr,done){
|
|
|
|
this.ipfs.cat(addr,(err,res) => {
|
|
|
|
if(err) return done(err)
|
|
|
|
replyAsObj(res,true,done)
|
|
|
|
})
|
2015-11-11 09:18:36 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 12:38:10 +01:00
|
|
|
// This function works but needs a little rethinking.
|
|
|
|
BoardsAPI.prototype.resolveIPNS = function(n,done){
|
|
|
|
var cached = this.users[n]
|
|
|
|
if(cached){
|
2015-11-14 12:06:37 +01:00
|
|
|
console.log('Returning cached',n,'is',this.users[n])
|
2015-11-11 12:38:10 +01:00
|
|
|
done(null,cached)
|
|
|
|
}
|
|
|
|
this.ipfs.name.resolve(n,(err,r) => {
|
2015-11-14 13:12:48 +01:00
|
|
|
if(!err) console.log('Resolved',n,'to',r.Path)
|
2015-11-11 12:38:10 +01:00
|
|
|
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
|
|
|
|
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(){
|
2015-11-11 09:18:36 +01:00
|
|
|
// Look at our peers
|
2015-11-11 12:38:10 +01:00
|
|
|
this.ipfs.swarm.peers((err,r) => {
|
|
|
|
if(err) return console.log(err)
|
|
|
|
replyAsObj(r,true,(e,reply) => {
|
2015-11-11 15:22:58 +01:00
|
|
|
console.log('Checking',reply.Strings.length,'peers')
|
2015-11-11 12:38:10 +01:00
|
|
|
reply.Strings.forEach(this.isUser.bind(this))
|
2015-11-11 09:18:36 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
BoardsAPI.prototype.getProfile = function(userID,done){
|
2015-11-14 12:06:37 +01:00
|
|
|
console.log('profile requested for',userID)
|
|
|
|
this.resolveIPNS(userID,(err,url) => {
|
2015-11-11 09:18:36 +01:00
|
|
|
if(err){
|
|
|
|
done(err,null)
|
2015-11-14 12:06:37 +01:00
|
|
|
} else this.ipfs.cat(url+'/profile.json',(err2,res) => {
|
|
|
|
console.log('got something')
|
|
|
|
if(err2){
|
|
|
|
done(err2,null)
|
|
|
|
} else {
|
|
|
|
// It already returns a JSON?
|
|
|
|
done(null,res)
|
|
|
|
}
|
|
|
|
})
|
2015-11-11 09:18:36 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
BoardsAPI.prototype.getName = function(userID,done){
|
|
|
|
this.ipfs.cat(userID+'/name',(err,res) => {
|
|
|
|
if(err){
|
|
|
|
done(err,null)
|
|
|
|
} else {
|
|
|
|
replyAsObj(res,false,done)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
BoardsAPI.prototype.getBoardSettings = function(userID,board,done){
|
|
|
|
var url = userID+'/boards/'+board+'/settings.json'
|
2015-11-14 10:46:42 +01:00
|
|
|
console.log('Getting Board Settings:',url)
|
2015-11-11 09:18:36 +01:00
|
|
|
this.ipfs.cat(url,function(err,res){
|
2015-11-11 12:38:10 +01:00
|
|
|
console.log('Done')
|
2015-11-11 09:18:36 +01:00
|
|
|
if(err){
|
|
|
|
done(err,{})
|
|
|
|
} else {
|
|
|
|
replyAsObj(res,true,done)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-11 15:22:58 +01:00
|
|
|
BoardsAPI.prototype.getBoardPostList = function(board,administratorID,done){
|
|
|
|
this.getBoardSettings(administratorID,board,(err,res) => {
|
|
|
|
// NEEDS: board settings structure definition
|
|
|
|
})
|
2015-11-11 09:18:36 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 15:22:58 +01:00
|
|
|
BoardsAPI.prototype.getUserPostList = function(user,board,done){
|
|
|
|
this.resolveIPNS(user,(err,url) => {
|
|
|
|
if(err){
|
|
|
|
done(err)
|
|
|
|
} else this.ipfs.ls(url+'/boards/'+board,(e,r) => {
|
|
|
|
if(e){
|
|
|
|
done(e)
|
|
|
|
} else {
|
|
|
|
var l = r.map(i => {
|
|
|
|
return { date: i.Name, hash: i.Hash }
|
|
|
|
})
|
|
|
|
done(null,r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2015-11-11 09:18:36 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 15:22:58 +01:00
|
|
|
BoardsAPI.prototype.getCommentsFor = function(parent,done){
|
|
|
|
|
2015-11-11 09:18:36 +01:00
|
|
|
}
|
|
|
|
|
2015-11-11 15:22:58 +01:00
|
|
|
// Work only in writable mode:
|
|
|
|
|
2015-11-11 12:38:10 +01:00
|
|
|
BoardsAPI.prototype.createPost = function(post,board,done){
|
2015-11-11 09:18:36 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-11 12:38:10 +01:00
|
|
|
BoardsAPI.prototype.createComment = function(parent,comment,done){
|
2015-11-11 09:18:36 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-11 12:38:10 +01:00
|
|
|
BoardsAPI.prototype.createUpvote = function(parent,done){
|
2015-11-11 09:18:36 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// API for managing the administrations to be done later
|
|
|
|
|
|
|
|
// Initialize API
|
|
|
|
BoardsAPI.prototype.init = function(done){
|
|
|
|
this.ipfs.id( (err, res) => {
|
|
|
|
if(err){
|
|
|
|
console.log(err)
|
|
|
|
done(err)
|
|
|
|
} else {
|
|
|
|
console.log('I am',res.ID)
|
|
|
|
this.id = res.ID
|
2015-11-11 12:38:10 +01:00
|
|
|
this.isUser(res.ID)
|
2015-11-11 09:18:36 +01:00
|
|
|
console.log('Version is',this.version)
|
|
|
|
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err,r) => {
|
|
|
|
this.version_hash = r[0].Hash
|
|
|
|
console.log('Version hash is',this.version_hash)
|
|
|
|
done(null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BoardsAPI
|