1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-10 12:24:20 +01:00

finished post editor

This commit is contained in:
Enrico Fasoli 2015-12-17 19:24:51 +01:00
parent dcf02abb7d
commit 789076277a
2 changed files with 72 additions and 9 deletions

View File

@ -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) {

View File

@ -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 <span>
<button onClick={this.refresh} className="button not-first">Refresh</button>
<Link to={url} className="button not-first">View</Link>
@ -77,7 +94,7 @@ module.exports = function (boardsAPI) {
{this.props.params.posthash ? ' Edit Post' : ' New Post'}
</h2>
<p>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.</p>
<p><b>Warning:</b> 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) {
<input className="u-full-width" type="text" id="title" value={this.state.title} onChange={this.handleChange} placeholder="Choose a title" />
</div>
<div>
<label htmlFor="desc">Content</label>
<textarea className="u-full-width" id="desc" value={this.state.desc} onChange={this.handleChange} placeholder="Write your post. Markdown is supported :)" />
<label htmlFor="text">Content</label>
<textarea className="u-full-width" id="text" value={this.state.text} onChange={this.handleChange} placeholder="Write your post. Markdown is supported :)" />
</div>
<div className="buttons">
<button className="button button-primary" onClick={this.save}>Publish</button>