1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-10 12:24:20 +01:00
ipfs-boards/lib/boards-api.js

207 lines
4.5 KiB
JavaScript
Raw Normal View History

2015-11-11 09:18:36 +01:00
// Write an API to aggregate data without duplication and making accessing content easy. Use the IPFS http api
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
res.setEncoding('utf8')
var data = ''
res.on('data',d => {
data += d
})
res.on('end',() => {
if(isJson) {
asObj(data,done)
} else {
done(null,data)
}
})
} else {
// 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){
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(){
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){
this.ipfs.cat(userID+'/profile.json',(err,res) => {
if(err){
done(err,null)
} else {
replyAsObj(res,true,done)
}
})
}
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-11 12:38:10 +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