mirror of
https://github.com/fazo96/ipfs-boards
synced 2025-01-25 14:54:19 +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
|
### 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:
|
publication, containing:
|
||||||
|
|
||||||
- boards
|
- boards
|
||||||
|
@ -50,6 +50,7 @@ function replyAsObj(res,isJson,done){
|
|||||||
function BoardsAPI(ipfs){
|
function BoardsAPI(ipfs){
|
||||||
this.ipfs = ipfs
|
this.ipfs = ipfs
|
||||||
this.version = 'dev'
|
this.version = 'dev'
|
||||||
|
this.baseurl = '/ipfs-boards-profile/'
|
||||||
this.users = {} // userID : profileHash
|
this.users = {} // userID : profileHash
|
||||||
this.resolving_ipns = {} // to check if a resolve is already in progress
|
this.resolving_ipns = {} // to check if a resolve is already in progress
|
||||||
this.ee = new EventEmitter()
|
this.ee = new EventEmitter()
|
||||||
@ -106,7 +107,7 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
|
|||||||
}
|
}
|
||||||
|
|
||||||
BoardsAPI.prototype.isUserProfile = function(addr,done){
|
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)
|
if(err) return done(false)
|
||||||
replyAsObj(r,false,(_,res) => {
|
replyAsObj(r,false,(_,res) => {
|
||||||
var v = res.trim()
|
var v = res.trim()
|
||||||
@ -158,11 +159,13 @@ BoardsAPI.prototype.getProfile = function(userID,done){
|
|||||||
console.log('profile requested for',userID)
|
console.log('profile requested for',userID)
|
||||||
this.resolveIPNS(userID,(url,err) => {
|
this.resolveIPNS(userID,(url,err) => {
|
||||||
if(err){
|
if(err){
|
||||||
|
this.ee.emit('error',err)
|
||||||
done(err,null)
|
done(err,null)
|
||||||
} else {
|
} else {
|
||||||
// Download actual profile
|
// Download actual profile
|
||||||
this.ipfs.cat(url+'/profile.json',(err2,res) => {
|
this.ipfs.cat(url+this.baseurl+'profile.json',(err2,res) => {
|
||||||
if(err2){
|
if(err2){
|
||||||
|
this.ee.emit('error',err2)
|
||||||
done(err2,null)
|
done(err2,null)
|
||||||
} else {
|
} else {
|
||||||
// It already returns a JSON?
|
// It already returns a JSON?
|
||||||
@ -170,14 +173,16 @@ BoardsAPI.prototype.getProfile = function(userID,done){
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
// Get other info
|
// Get other info
|
||||||
this.ipfs.ls(url+'/boards/',(err2,res) => {
|
this.ipfs.ls(url+this.baseurl+'boards/',(err2,res) => {
|
||||||
if(!err2){
|
if(!err2){
|
||||||
console.log('RES',res)
|
console.log('RES',res)
|
||||||
var l = res.Objects[0].Links.map(i => {
|
var l = res.Objects[0].Links.map(i => {
|
||||||
return { name: i.Name, hash: i.Hash }
|
return { name: i.Name, hash: i.Hash }
|
||||||
})
|
})
|
||||||
this.ee.emit('boards',l)
|
this.ee.emit('boards',l)
|
||||||
} else console.log(err2)
|
} else {
|
||||||
|
this.ee.emit('error',err2)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return true // remove myself from listeners
|
return true // remove myself from listeners
|
||||||
@ -188,11 +193,13 @@ BoardsAPI.prototype.getProfile = function(userID,done){
|
|||||||
BoardsAPI.prototype.getBoardSettings = function(userID,board,done){
|
BoardsAPI.prototype.getBoardSettings = function(userID,board,done){
|
||||||
this.resolveIPNS(userID,(r,e) => {
|
this.resolveIPNS(userID,(r,e) => {
|
||||||
if(e){
|
if(e){
|
||||||
|
this.ee.emit('error',e)
|
||||||
done(e)
|
done(e)
|
||||||
} else {
|
} else {
|
||||||
var url = r+'/boards/'+board+'/settings.json'
|
var url = r+this.baseurl+'boards/'+board+'/settings.json'
|
||||||
this.ipfs.cat(url,(err,res) => {
|
this.ipfs.cat(url,(err,res) => {
|
||||||
if(err){
|
if(err){
|
||||||
|
this.ee.emit('error',err)
|
||||||
done(err)
|
done(err)
|
||||||
} else {
|
} else {
|
||||||
// It's already json...
|
// It's already json...
|
||||||
@ -211,12 +218,13 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){
|
|||||||
// For now we only list admin's posts
|
// For now we only list admin's posts
|
||||||
})
|
})
|
||||||
*/
|
*/
|
||||||
this.getUserPostListInBoard(adminID,board,(err,res) =>{
|
this.getUserPostListInBoard(adminID,board,(err,res) => {
|
||||||
if(err){
|
if(err){
|
||||||
console.log(err)
|
console.log(err)
|
||||||
} else res.forEach(item => {
|
} else res.forEach(item => {
|
||||||
this.ipfs.cat(item.hash,(err2,r) => {
|
this.ipfs.cat(item.hash,(err2,r) => {
|
||||||
if(err2){
|
if(err2){
|
||||||
|
this.ee.emit('error',err2)
|
||||||
console.log('Could not download post',item,'of',board+'@'+adminID)
|
console.log('Could not download post',item,'of',board+'@'+adminID)
|
||||||
} else {
|
} else {
|
||||||
// It already returns a JSON?
|
// It already returns a JSON?
|
||||||
@ -229,16 +237,17 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){
|
|||||||
}
|
}
|
||||||
|
|
||||||
BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
|
BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
|
||||||
var ee = new EventEmitter()
|
|
||||||
this.resolveIPNS(user,(url,err) => {
|
this.resolveIPNS(user,(url,err) => {
|
||||||
if(err){
|
if(err){
|
||||||
|
this.ee.emit('error',err)
|
||||||
done(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){
|
if(e){
|
||||||
|
this.ee.emit('error',e)
|
||||||
done(e)
|
done(e)
|
||||||
} else if(r && !r.split){
|
} else if(r && !r.split){
|
||||||
console.log('Found',r.Objects[0].Links.length,'posts in',board,'at',user)
|
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 => {
|
var l = r.Objects[0].Links.map(i => {
|
||||||
return { date: i.Name, hash: i.Hash }
|
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 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
|
// 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...
|
// API for publishing content and managing to be done later...
|
||||||
@ -260,17 +270,19 @@ BoardsAPI.prototype.getCommentsFor = function(parent){
|
|||||||
BoardsAPI.prototype.init = function(done){
|
BoardsAPI.prototype.init = function(done){
|
||||||
this.ipfs.id( (err, res) => {
|
this.ipfs.id( (err, res) => {
|
||||||
if(err){
|
if(err){
|
||||||
console.log(err)
|
console.log('Error while getting OWN ID:',err)
|
||||||
|
this.ee.emit('error',err)
|
||||||
done(err)
|
done(err)
|
||||||
} else if(res.ID){
|
} else if(res.ID){
|
||||||
console.log('I am',res.ID)
|
console.log('I am',res.ID)
|
||||||
this.id = res.ID
|
this.id = res.ID
|
||||||
this.isUser(res.ID)
|
this.isUser(res.ID)
|
||||||
console.log('Version is',this.version)
|
console.log('Version is',this.version)
|
||||||
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err,r) => {
|
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),(err2,r) => {
|
||||||
if(err){
|
if(err2){
|
||||||
console.log('Error while calculating version hash:',err)
|
this.ee.emit('error',err2)
|
||||||
done(err)
|
console.log('Error while calculating version hash:',err2)
|
||||||
|
done(err2)
|
||||||
} else {
|
} else {
|
||||||
if(r && r[0] && r[0].Hash) this.version_hash = r[0].Hash
|
if(r && r[0] && r[0].Hash) this.version_hash = r[0].Hash
|
||||||
console.log('Version hash is',this.version_hash)
|
console.log('Version hash is',this.version_hash)
|
||||||
|
Loading…
Reference in New Issue
Block a user