1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-24 14:44:19 +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) => { (cb) => {
// Serialize Board Settings and add to IPFS // 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) => { (res, cb) => {
// Move Board into mfs // Move Board into mfs
console.log('added Board Settings to IPFS:', res.Hash) var hash = res[0].Hash
var spath = '/ipfs/' + res.Hash console.log('added Board Settings to IPFS:', hash)
var spath = '/ipfs/' + hash
this.ipfs.files.cp([spath, dest], cb) this.ipfs.files.cp([spath, dest], cb)
}, },
(e, cb) => this.ipfs.files.stat('/', 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 // Remove old post file if present
this.ipfs.files.rm(dest, { r: true }, res => cb()) 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) => { (cb) => {
// Serialize post and add to IPFS // 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) => { (res, cb) => {
// Move post into mfs // Move post into mfs
console.log('added Post to IPFS:', res.Hash) var hash = res[0].Hash
posthash = res.Hash console.log('added Post to IPFS:', hash)
var spath = '/ipfs/' + res.Hash posthash = hash
var spath = '/ipfs/' + hash
this.ipfs.files.cp([spath, dest], cb) this.ipfs.files.cp([spath, dest], cb)
}, },
(e, cb) => this.ipfs.files.stat('/', cb), (e, cb) => this.ipfs.files.stat('/', cb),
@ -251,13 +285,18 @@ BoardsAPI.prototype.createComment = function (comment, parent, done) {
}, },
(cb) => { (cb) => {
// Serialize comment and add to IPFS // 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) => { (res, cb) => {
// Move post into mfs // Move post into mfs
console.log('added Comment to IPFS:', res.Hash) var hash = res[0].Hash
commenthash = res.Hash console.log('added Comment to IPFS:', hash)
var spath = '/ipfs/' + res.Hash commenthash = hash
var spath = '/ipfs/' + hash
this.ipfs.files.cp([spath, dest], cb) this.ipfs.files.cp([spath, dest], cb)
}, },
(e, cb) => this.ipfs.files.stat('/', cb), (e, cb) => this.ipfs.files.stat('/', cb),
@ -872,7 +911,11 @@ BoardsAPI.prototype.init = function (done) {
this.id = res.ID this.id = res.ID
this.resolveIPNS(res.ID) this.resolveIPNS(res.ID)
console.log('Version is', this.version) 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) { if (err2) {
this.ee.emit('error', err2) this.ee.emit('error', err2)
console.log('Error while calculating version hash:', 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) console.log('Post discarded cause date in the future:', hash)
}*/ }*/
// Don't drop posts in the future due to date sync problems! // Don't drop posts in the future due to date sync problems!
// TODO: delete old version of post from list?
this.setState({ posts }) this.setState({ posts })
} }
props = props || this.props props = props || this.props

View File

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

View File

@ -35,7 +35,6 @@ module.exports = React.createClass({
} else this.startTimer() } else this.startTimer()
}, },
startTimer () { startTimer () {
console.log('start timer')
this.timer = setTimeout(_ => { this.timer = setTimeout(_ => {
console.log('Connection to go-ipfs has timed out (probably due to CORS)') console.log('Connection to go-ipfs has timed out (probably due to CORS)')
if (this.isMounted() && !this.state.connected && !this.state.limited) { 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> <h6>This is your profile</h6>
<div className='iconbar'> <div className='iconbar'>
<Link className='nounderline' to='/edit/profile'><Icon name='edit' className='fa-2x light'/></Link> <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> </div>
<hr/> <hr/>
</div> </div>