mirror of
https://github.com/fazo96/ipfs-boards
synced 2025-01-10 12:24:20 +01:00
fixing api to comply with protocol and adding more error communication
This commit is contained in:
parent
c32dcbf73d
commit
a5b33ca6bc
@ -17,7 +17,7 @@ guy, his computer won't be overwhelmed because the data will be passed from comp
|
||||
|
||||
### Data Storage
|
||||
|
||||
Each user exposes a folder called `ipfs-boards` in the root of the IPNS
|
||||
Each user exposes a folder called `ipfs-boards-profile` in the root of the IPNS
|
||||
publication, containing:
|
||||
|
||||
- boards
|
||||
|
@ -50,6 +50,7 @@ function replyAsObj(res,isJson,done){
|
||||
function BoardsAPI(ipfs){
|
||||
this.ipfs = ipfs
|
||||
this.version = 'dev'
|
||||
this.baseurl = '/ipfs-boards-profile/'
|
||||
this.users = {} // userID : profileHash
|
||||
this.resolving_ipns = {} // to check if a resolve is already in progress
|
||||
this.ee = new EventEmitter()
|
||||
@ -106,7 +107,7 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
|
||||
}
|
||||
|
||||
BoardsAPI.prototype.isUserProfile = function(addr,done){
|
||||
this.ipfs.cat(addr+'/ipfs-boards-version.txt',(err,r) => {
|
||||
this.ipfs.cat(addr+this.baseurl+'ipfs-boards-version.txt',(err,r) => {
|
||||
if(err) return done(false)
|
||||
replyAsObj(r,false,(_,res) => {
|
||||
var v = res.trim()
|
||||
@ -158,11 +159,13 @@ BoardsAPI.prototype.getProfile = function(userID,done){
|
||||
console.log('profile requested for',userID)
|
||||
this.resolveIPNS(userID,(url,err) => {
|
||||
if(err){
|
||||
this.ee.emit('error',err)
|
||||
done(err,null)
|
||||
} else {
|
||||
// Download actual profile
|
||||
this.ipfs.cat(url+'/profile.json',(err2,res) => {
|
||||
this.ipfs.cat(url+this.baseurl+'profile.json',(err2,res) => {
|
||||
if(err2){
|
||||
this.ee.emit('error',err2)
|
||||
done(err2,null)
|
||||
} else {
|
||||
// It already returns a JSON?
|
||||
@ -170,14 +173,16 @@ BoardsAPI.prototype.getProfile = function(userID,done){
|
||||
}
|
||||
})
|
||||
// Get other info
|
||||
this.ipfs.ls(url+'/boards/',(err2,res) => {
|
||||
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 }
|
||||
})
|
||||
this.ee.emit('boards',l)
|
||||
} else console.log(err2)
|
||||
} else {
|
||||
this.ee.emit('error',err2)
|
||||
}
|
||||
})
|
||||
}
|
||||
return true // remove myself from listeners
|
||||
@ -188,11 +193,13 @@ BoardsAPI.prototype.getProfile = function(userID,done){
|
||||
BoardsAPI.prototype.getBoardSettings = function(userID,board,done){
|
||||
this.resolveIPNS(userID,(r,e) => {
|
||||
if(e){
|
||||
this.ee.emit('error',e)
|
||||
done(e)
|
||||
} else {
|
||||
var url = r+'/boards/'+board+'/settings.json'
|
||||
var url = r+this.baseurl+'boards/'+board+'/settings.json'
|
||||
this.ipfs.cat(url,(err,res) => {
|
||||
if(err){
|
||||
this.ee.emit('error',err)
|
||||
done(err)
|
||||
} else {
|
||||
// It's already json...
|
||||
@ -211,12 +218,13 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){
|
||||
// For now we only list admin's posts
|
||||
})
|
||||
*/
|
||||
this.getUserPostListInBoard(adminID,board,(err,res) =>{
|
||||
this.getUserPostListInBoard(adminID,board,(err,res) => {
|
||||
if(err){
|
||||
console.log(err)
|
||||
} else res.forEach(item => {
|
||||
this.ipfs.cat(item.hash,(err2,r) => {
|
||||
if(err2){
|
||||
this.ee.emit('error',err2)
|
||||
console.log('Could not download post',item,'of',board+'@'+adminID)
|
||||
} else {
|
||||
// It already returns a JSON?
|
||||
@ -229,16 +237,17 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){
|
||||
}
|
||||
|
||||
BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
|
||||
var ee = new EventEmitter()
|
||||
this.resolveIPNS(user,(url,err) => {
|
||||
if(err){
|
||||
this.ee.emit('error',err)
|
||||
done(err)
|
||||
} else this.ipfs.ls(url+'/posts/'+board,(e,r) => {
|
||||
} else this.ipfs.ls(url+this.baseurl+'posts/'+board,(e,r) => {
|
||||
if(e){
|
||||
this.ee.emit('error',e)
|
||||
done(e)
|
||||
} else if(r && !r.split){
|
||||
console.log('Found',r.Objects[0].Links.length,'posts in',board,'at',user)
|
||||
ee.emit('post count',board,user,r.Objects[0].Links.length)
|
||||
this.ee.emit('post count',board,user,r.Objects[0].Links.length)
|
||||
var l = r.Objects[0].Links.map(i => {
|
||||
return { date: i.Name, hash: i.Hash }
|
||||
})
|
||||
@ -247,11 +256,12 @@ BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
|
||||
})
|
||||
return true // remove myself from listeners
|
||||
})
|
||||
return ee
|
||||
return this.ee
|
||||
}
|
||||
|
||||
BoardsAPI.prototype.getCommentsFor = function(parent){
|
||||
BoardsAPI.prototype.getCommentsFor = function(parent,board,adminID){
|
||||
// Create an EventEmitter, start looking and emit an event for every new comment
|
||||
// Consider the rules of @adminID#board
|
||||
}
|
||||
|
||||
// API for publishing content and managing to be done later...
|
||||
@ -260,17 +270,19 @@ BoardsAPI.prototype.getCommentsFor = function(parent){
|
||||
BoardsAPI.prototype.init = function(done){
|
||||
this.ipfs.id( (err, res) => {
|
||||
if(err){
|
||||
console.log(err)
|
||||
console.log('Error while getting OWN ID:',err)
|
||||
this.ee.emit('error',err)
|
||||
done(err)
|
||||
} else if(res.ID){
|
||||
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) => {
|
||||
if(err){
|
||||
console.log('Error while calculating version hash:',err)
|
||||
done(err)
|
||||
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err2,r) => {
|
||||
if(err2){
|
||||
this.ee.emit('error',err2)
|
||||
console.log('Error while calculating version hash:',err2)
|
||||
done(err2)
|
||||
} else {
|
||||
if(r && r[0] && r[0].Hash) this.version_hash = r[0].Hash
|
||||
console.log('Version hash is',this.version_hash)
|
||||
|
Loading…
Reference in New Issue
Block a user