diff --git a/lib/boards-api.js b/lib/boards-api.js index fc6537d..1f890fe 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -10,6 +10,7 @@ Needs to be browserified to work in the browser var EventEmitter = require('wolfy87-eventemitter') var asyncjs = require('async') var semver = require('semver') +var moment = require('moment') function asObj (str, done) { if (str.toString) str = str.toString() @@ -165,6 +166,48 @@ BoardsAPI.prototype.createBoard = function (board, done) { ], done) } +BoardsAPI.prototype.createPost = function (post, board, done) { + try { + post.date = moment().unix() + post.op = this.id + var post_str = JSON.stringify(post) + } catch (e) { + console.log('Error, invalid Post:', e) + return done(e) + } + if (!post.text) return done('empty post') + console.log('Posting:', post) + var dest = '/ipfs-boards-profile/posts/' + board + '/' + post.date + '.json' + var posthash + asyncjs.waterfall([ + // Create required directories + cb => this.ipfs.files.mkdir('/ipfs-boards-profile/posts/' + board + '/', { p: true }, cb), + (e, cb) => { + // Remove old post file if present + this.ipfs.files.rm(dest, { r: true }, res => cb()) + }, + (cb) => { + // Serialize post and add to IPFS + this.ipfs.add(new Buffer(post_str), cb) + }, + (res, cb) => { + // Move post into mfs + console.log('added Post to IPFS:', res.Hash) + posthash = 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, posthash) + }) + } + ], done) +} + BoardsAPI.prototype.backupCache = function () { if (window && window.localStorage !== undefined) { // Use localStorage to store the IPNS cache @@ -380,6 +423,9 @@ BoardsAPI.prototype.getBoardSettings = function (userID, board, done) { } BoardsAPI.prototype.downloadPost = function (hash, adminID, board, op, done) { + if (typeof adminID === 'function') done = adminID + if (typeof board === 'function') done = board + if (typeof op === 'function') done = op console.log('Downloading post', hash) this.ipfs.cat(hash, (err2, r) => { if (err2) { diff --git a/webapp/pages/post-editor.jsx b/webapp/pages/post-editor.jsx index 363f999..08e21e5 100644 --- a/webapp/pages/post-editor.jsx +++ b/webapp/pages/post-editor.jsx @@ -25,7 +25,17 @@ module.exports = function (boardsAPI) { init (boards) { if (this.state.init) return this.setState({ api: boards, init: true }) - // TODO: DOWNLOAD POST + if (this.props.params.posthash) this.downloadPost(boards) + }, + downloadPost (boards) { + this.setState({ loading: true }) + boards.downloadPost(this.props.params.posthash, (err, p) => { + if (err) { + this.setState({ error: err, loading: false }) + } else { + this.setState({ loading: false, title: p.title, text: p.text }) + } + }) }, handleChange (event) { var obj = {} @@ -36,17 +46,24 @@ module.exports = function (boardsAPI) { this.setState({ loading: false, updating: false, error: false }) }, refresh () { - this.setState({ loading: true }) - // boardsAPI.use(b => this.getBoardSettings(b)) - // TODO: DOWNLOAD POST + boardsAPI.use(b => this.downloadPost(b)) }, save () { this.setState({ updating: true }) - // TODO: SAVE POST IMPL + var post = { + title: this.state.title, + text: this.state.text + } + boardsAPI.use(boards => { + boards.createPost(post, this.props.params.boardname, err => { + this.setState({ error: err, updating: false }) + // Should redirect to new post hash + }) + }) }, additionalButtons () { if (this.state.api && this.props.params.posthash) { - var url = '/@' + this.state.api.getMyID() + '/' + this.props.params.boardname + '/post/' + this.props.params.posthash + var url = '/@' + this.state.api.getMyID() + '/' + this.props.params.boardname + '/' + this.props.params.posthash return View @@ -77,7 +94,7 @@ module.exports = function (boardsAPI) { {this.props.params.posthash ? ' Edit Post' : ' New Post'}

This App uses IPFS to store your Posts. When you are offline, - other users or servers that viewed your content will serve it to + other users or servers that viewed your text will serve it to others.

Warning: due to a bug in go-ipfs, it may take up to a minute for your changes to be visibile. Your Post will not appear or appear @@ -87,8 +104,8 @@ module.exports = function (boardsAPI) {

- -