From 9c3e8c4d30419ae0be9dd227980bc240a30b27aa Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Mon, 20 Jun 2016 20:11:12 +0200 Subject: [PATCH] post and board creation updates --- lib/boards-api.js | 67 ++++++++++++++++++++++++++++------ webapp/components/postlist.jsx | 1 + webapp/pages/board-editor.jsx | 19 +++++++--- webapp/pages/getipfs.jsx | 1 - webapp/pages/profile.jsx | 1 + 5 files changed, 70 insertions(+), 19 deletions(-) diff --git a/lib/boards-api.js b/lib/boards-api.js index c5f1e8f..979b758 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -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) diff --git a/webapp/components/postlist.jsx b/webapp/components/postlist.jsx index cdd82d6..0f1fab3 100644 --- a/webapp/components/postlist.jsx +++ b/webapp/components/postlist.jsx @@ -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 diff --git a/webapp/pages/board-editor.jsx b/webapp/pages/board-editor.jsx index e944f4f..3ebb0a7 100644 --- a/webapp/pages/board-editor.jsx +++ b/webapp/pages/board-editor.jsx @@ -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 } - }) + })) } diff --git a/webapp/pages/getipfs.jsx b/webapp/pages/getipfs.jsx index d701ba3..9478342 100644 --- a/webapp/pages/getipfs.jsx +++ b/webapp/pages/getipfs.jsx @@ -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) { diff --git a/webapp/pages/profile.jsx b/webapp/pages/profile.jsx index e37a9bf..233e581 100644 --- a/webapp/pages/profile.jsx +++ b/webapp/pages/profile.jsx @@ -56,6 +56,7 @@ module.exports = function (boardsAPI) {
This is your profile
+