From 047dad1c4cf969c62ed6ca42c86adab65d7af6b8 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Wed, 23 Dec 2015 11:47:31 +0100 Subject: [PATCH] implemented writing comments --- lib/boards-api.js | 42 ++++++++++++++++++++ webapp/components/comment.jsx | 73 +++++++++++++++++++++++++++++++++-- webapp/components/post.jsx | 13 ++++++- webapp/pages/commentpage.jsx | 16 +++++--- webapp/pages/postpage.jsx | 7 +++- webapp/style.css | 1 + 6 files changed, 139 insertions(+), 13 deletions(-) diff --git a/lib/boards-api.js b/lib/boards-api.js index 1fa9dfd..f2842b2 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -207,6 +207,48 @@ BoardsAPI.prototype.createPost = function (post, board, done) { } ], done) } +BoardsAPI.prototype.createComment = function (comment, parent, done) { + try { + comment.date = parseInt((new Date()).getTime() / 1000, 10) + comment.op = this.id + comment.parent = parent + var comment_str = JSON.stringify(comment) + } catch (e) { + console.log('Error, invalid Post:', e) + return done(e) + } + if (!comment.text) return done('empty comment') + console.log('Commenting:', comment) + var dest = '/ipfs-boards-profile/comments/' + parent + '/' + comment.date + '.json' + var commenthash + asyncjs.waterfall([ + // Create required directories + cb => this.ipfs.files.mkdir('/ipfs-boards-profile/comments/' + parent + '/', { p: true }, cb), + (e, cb) => { + // Remove old comment file if present + this.ipfs.files.rm(dest, { r: true }, res => cb()) + }, + (cb) => { + // Serialize comment and add to IPFS + this.ipfs.add(new Buffer(comment_str), cb) + }, + (res, cb) => { + // Move post into mfs + console.log('added Comment to IPFS:', res.Hash) + commenthash = res.Hash + var spath = '/ipfs/' + res.Hash + this.ipfs.files.cp([spath, dest], cb) + }, + (e, cb) => this.ipfs.files.stat('/', cb), + (res, cb) => { + var profile_hash = res.Hash + console.log('Publishing profile...') + this.ipfs.name.publish(profile_hash, err => { + done(err, commenthash) + }) + } + ], done) +} BoardsAPI.prototype.delete = function (opts, done) { var url = '/ipfs-boards-profile/' diff --git a/webapp/components/comment.jsx b/webapp/components/comment.jsx index 1dddf58..6f7b362 100644 --- a/webapp/components/comment.jsx +++ b/webapp/components/comment.jsx @@ -4,6 +4,62 @@ var Icon = require('icon.jsx') var Clock = require('clock.jsx') var Link = require('react-router').Link var UserID = require('userID.jsx') +var { Error, Success } = require('status-components.jsx') + +var CommentEditor = React.createClass({ + getInitialState () { + return { } + }, + componentDidMount () { + this.init(this.props) + }, + componentWillReceiveProps (props) { + this.init(props) + }, + init (props) { + this.setState({ api: props.api }) + }, + handleChange (event) { + var obj = {} + obj[event.target.id] = event.target.value + this.setState(obj) + }, + save () { + var boards = this.props.api + var comment = { text: this.state.text } + this.setState({ loading: true }) + boards.createComment(comment, this.props.parent, (err, hash) => { + if (err) { + this.setState({ loading: false, error: err }) + } else { + this.setState({ loading: false, success: true, hash }) + } + }) + }, + render () { + if (this.state.error) { + return + } else if (this.state.loading) { + return
+ +

Publishing Comment

+
+ } else if (this.state.success) { + var url = '/@' + this.props.adminID + '/' + this.props.board + '/' + (this.props.post || this.props.parent) + '/' + this.state.hash + return + View + + } else { + return
+