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

post and board creation updates

This commit is contained in:
Enrico Fasoli 2016-06-20 20:11:12 +02:00
parent 890f3d4b37
commit 9c3e8c4d30
5 changed files with 70 additions and 19 deletions

View File

@ -170,12 +170,18 @@ BoardsAPI.prototype.createBoard = function (board, done) {
},
(cb) => {
// Serialize Board Settings and add to IPFS
this.ipfs.add(new Buffer(settingsStr), cb)
// TODO: fix usage of ipfs.add
var file = {
path: 'settings.json',
content: settingsStr
}
this.ipfs.add(file, cb)
},
(res, cb) => {
// Move Board into mfs
console.log('added Board Settings to IPFS:', res.Hash)
var spath = '/ipfs/' + res.Hash
var hash = res[0].Hash
console.log('added Board Settings to IPFS:', hash)
var spath = '/ipfs/' + hash
this.ipfs.files.cp([spath, dest], cb)
},
(e, cb) => this.ipfs.files.stat('/', cb),
@ -207,15 +213,43 @@ BoardsAPI.prototype.createPost = function (post, board, done) {
// Remove old post file if present
this.ipfs.files.rm(dest, { r: true }, res => cb())
},
(cb) => {
if (post.previous) {
// Remove previous post from post list in profile
// First fetch the previous post
this.cat(post.previous, (err, res) => {
if (err) {
console.log('Previous post unreachable')
done()
}
try {
var prevPost = JSON.parse(res)
} catch (e) {
console.log('Previous post is not valid:', e)
cb()
}
var prevPostDest = '/ipfs-boards-profile/posts/' + board + '/' + prevPost.date + '.json'
this.ipfs.files.rm(prevPostDest, err => {
if (err) console.log('Previous post could not be deleted:', err, prevPostDest)
cb()
})
})
} else cb()
},
(cb) => {
// Serialize post and add to IPFS
this.ipfs.add(new Buffer(postStr), cb)
var file = {
path: 'post.json',
content: postStr
}
this.ipfs.add(file, cb)
},
(res, cb) => {
// Move post into mfs
console.log('added Post to IPFS:', res.Hash)
posthash = res.Hash
var spath = '/ipfs/' + res.Hash
var hash = res[0].Hash
console.log('added Post to IPFS:', hash)
posthash = hash
var spath = '/ipfs/' + hash
this.ipfs.files.cp([spath, dest], cb)
},
(e, cb) => this.ipfs.files.stat('/', cb),
@ -251,13 +285,18 @@ BoardsAPI.prototype.createComment = function (comment, parent, done) {
},
(cb) => {
// Serialize comment and add to IPFS
this.ipfs.add(new Buffer(commentStr), cb)
var file = {
path: 'comment.json',
content: commentStr
}
this.ipfs.add(commentStr, cb)
},
(res, cb) => {
// Move post into mfs
console.log('added Comment to IPFS:', res.Hash)
commenthash = res.Hash
var spath = '/ipfs/' + res.Hash
var hash = res[0].Hash
console.log('added Comment to IPFS:', hash)
commenthash = hash
var spath = '/ipfs/' + hash
this.ipfs.files.cp([spath, dest], cb)
},
(e, cb) => this.ipfs.files.stat('/', cb),
@ -872,7 +911,11 @@ BoardsAPI.prototype.init = function (done) {
this.id = res.ID
this.resolveIPNS(res.ID)
console.log('Version is', this.version)
this.ipfs.add(new Buffer(this.version), (err2, r) => {
var file = {
path: 'version',
content: this.version
}
this.ipfs.add(file, (err2, r) => {
if (err2) {
this.ee.emit('error', err2)
console.log('Error while calculating version hash:', err2)

View File

@ -29,6 +29,7 @@ module.exports = React.createClass({
console.log('Post discarded cause date in the future:', hash)
}*/
// Don't drop posts in the future due to date sync problems!
// TODO: delete old version of post from list?
this.setState({ posts })
}
props = props || this.props

View File

@ -1,14 +1,17 @@
var React = require('react')
var GetIPFS = require('getipfs.jsx')
var Icon = require('icon.jsx')
var Link = require('react-router').Link
var { Link, withRouter } = require('react-router')
var { Error, Loading, Saving } = require('status-components.jsx')
module.exports = function (boardsAPI) {
return React.createClass({
return withRouter(React.createClass({
getInitialState () {
return { }
},
componentWillReceiveProps (props) {
this.getBoardSettings(this.state.api)
},
componentDidMount () {
boardsAPI.use(boards => {
boards.init()
@ -63,9 +66,13 @@ module.exports = function (boardsAPI) {
description: this.state.desc
}
this.setState({ updating: true })
boards.createBoard(board, (err) => {
this.setState({ updating: false })
console.log('CREATE:', err)
boards.createBoard(board, err => {
if (err) {
this.setState({ updating: false, error: err })
} else {
this.setState({ updating: false })
this.props.router.go('/edit/board/' + board.id)
}
})
},
additionalButtons () {
@ -131,5 +138,5 @@ module.exports = function (boardsAPI) {
}
} else return <GetIPFS api={this.state.api} />
}
})
}))
}

View File

@ -35,7 +35,6 @@ module.exports = React.createClass({
} else this.startTimer()
},
startTimer () {
console.log('start timer')
this.timer = setTimeout(_ => {
console.log('Connection to go-ipfs has timed out (probably due to CORS)')
if (this.isMounted() && !this.state.connected && !this.state.limited) {

View File

@ -56,6 +56,7 @@ module.exports = function (boardsAPI) {
<h6>This is your profile</h6>
<div className='iconbar'>
<Link className='nounderline' to='/edit/profile'><Icon name='edit' className='fa-2x light'/></Link>
<Link className='nounderline' to='/edit/board'><Icon name='plus-circle' className='fa-2x light'/></Link>
</div>
<hr/>
</div>