diff --git a/lib/boards-api.js b/lib/boards-api.js index 1f890fe..8da1408 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -10,7 +10,6 @@ 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() @@ -168,7 +167,7 @@ BoardsAPI.prototype.createBoard = function (board, done) { BoardsAPI.prototype.createPost = function (post, board, done) { try { - post.date = moment().unix() + post.date = (new Date()).getTime() post.op = this.id var post_str = JSON.stringify(post) } catch (e) { @@ -423,9 +422,18 @@ 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 + if (typeof adminID === 'function') { + done = adminID + adminID = undefined + } + if (typeof board === 'function') { + done = board + board = undefined + } + if (typeof op === 'function') { + done = op + op = undefined + } console.log('Downloading post', hash) this.ipfs.cat(hash, (err2, r) => { if (err2) { @@ -439,11 +447,11 @@ BoardsAPI.prototype.downloadPost = function (hash, adminID, board, op, done) { post.hash = hash if (op) post.op = op // Inject op if (board) { - if (adminID) this.ee.emit('post in ' + board + '@' + adminID, post, hash) - else this.ee.emit('post in ' + board, post, hash) + if (adminID) this.ee.emit('post in ' + board + '@' + adminID, hash, post.date, post) + else this.ee.emit('post in ' + board, hash, post.date, post) } this.ee.emit(hash, post, adminID, board) - if (done && done.apply) done(null, post) + if (done && done.apply) done(null, hash, post.date, post) }) } }) @@ -484,17 +492,19 @@ BoardsAPI.prototype.getAllowedContentProducers = function (adminID, board, optio return this.ee } -BoardsAPI.prototype.getPostsInBoard = function (adminID, board) { +BoardsAPI.prototype.getPostsInBoard = function (adminID, board, opt) { + opt = opt || {} + var emitPost = i => this.ee.emit('post in ' + board + '@' + adminID, i.hash, i.date) if (adminID) { this.ee.on('approved posts for ' + board + '@' + adminID, ret => { // Automatically download approved posts - ret.forEach(item => this.downloadPost(item.hash, adminID, board)) + ret.forEach(emitPost) }) this.ee.on('whitelist for ' + board + '@' + adminID, whitelist => { // download posts for each user in whitelist whitelist.forEach(item => { this.getUserPostListInBoard(item, board, (err, postList) => { - if (!err) postList.forEach(i => this.downloadPost(i.hash, adminID, board, item)) + if (!err) postList.forEach(emitPost) }) }) }) @@ -502,7 +512,7 @@ BoardsAPI.prototype.getPostsInBoard = function (adminID, board) { this.getAllowedContentProducers(adminID, board, { posts: true }) // Get the admin's posts this.getUserPostListInBoard(adminID, board, (err, res) => { - if (!err) res.forEach(item => this.downloadPost(item.hash, adminID, board, adminID)) + if (!err) res.forEach(emitPost) }) } else { // TODO: Download all posts in board from everyone @@ -510,7 +520,7 @@ BoardsAPI.prototype.getPostsInBoard = function (adminID, board) { this.getUserPostListInBoard(this.id, board, (err, res) => { if (err) { console.log(err) - } else res.forEach(item => this.downloadPost(item.hash, undefined, board, this.id)) + } else res.forEach(emitPost) }) } return this.ee diff --git a/webapp/components/post.jsx b/webapp/components/post.jsx index a867d02..0a02da3 100644 --- a/webapp/components/post.jsx +++ b/webapp/components/post.jsx @@ -4,38 +4,58 @@ var Icon = require('icon.jsx') var Link = require('react-router').Link var Clock = require('clock.jsx') var UserID = require('userID.jsx') +var { Error, Loading } = require('status-components.jsx') module.exports = React.createClass({ - getInitialState: function () { - return { moment: false } + getInitialState () { + return { loading: true } }, - componentDidMount: function () { - require.ensure(['moment'], _ => { - if (this.isMounted()) this.setState({ moment: require('moment') }) + componentDidMount () { + this.init(this.props) + }, + componentWillReceiveProps (props) { + this.init(props) + }, + init (props) { + var boards = props.api + if (!boards) return this.setState({ error: 'Could not connect to IPFS' }) + this.setState({ loading: true }) + boards.downloadPost(props.hash, props.adminID, props.board, (err, hash, date, post) => { + this.setState({ error: err, post: post, loading: false }) }) }, - postLink: function () { - if (this.props.post.op) { + postLink () { + if (this.state.post.op) { if (this.props.board) { - return '/@' + this.props.post.op + '/' + this.props.board + '/' + this.props.post.hash + return '/@' + this.state.post.op + '/' + this.props.board + '/' + this.props.hash } else { - return '/@' + this.props.post.op + '/post/' + this.props.post.hash + return '/@' + this.state.post.op + '/post/' + this.props.hash } } else { - return '/post/' + this.props.post.hash + return '/post/' + this.props.hash } }, - render: function () { - return