mirror of
https://github.com/fazo96/ipfs-boards
synced 2025-01-25 14:54:19 +01:00
finished post editor
This commit is contained in:
parent
dcf02abb7d
commit
789076277a
@ -10,6 +10,7 @@ Needs to be browserified to work in the browser
|
|||||||
var EventEmitter = require('wolfy87-eventemitter')
|
var EventEmitter = require('wolfy87-eventemitter')
|
||||||
var asyncjs = require('async')
|
var asyncjs = require('async')
|
||||||
var semver = require('semver')
|
var semver = require('semver')
|
||||||
|
var moment = require('moment')
|
||||||
|
|
||||||
function asObj (str, done) {
|
function asObj (str, done) {
|
||||||
if (str.toString) str = str.toString()
|
if (str.toString) str = str.toString()
|
||||||
@ -165,6 +166,48 @@ BoardsAPI.prototype.createBoard = function (board, done) {
|
|||||||
], 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 () {
|
BoardsAPI.prototype.backupCache = function () {
|
||||||
if (window && window.localStorage !== undefined) {
|
if (window && window.localStorage !== undefined) {
|
||||||
// Use localStorage to store the IPNS cache
|
// 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) {
|
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)
|
console.log('Downloading post', hash)
|
||||||
this.ipfs.cat(hash, (err2, r) => {
|
this.ipfs.cat(hash, (err2, r) => {
|
||||||
if (err2) {
|
if (err2) {
|
||||||
|
@ -25,7 +25,17 @@ module.exports = function (boardsAPI) {
|
|||||||
init (boards) {
|
init (boards) {
|
||||||
if (this.state.init) return
|
if (this.state.init) return
|
||||||
this.setState({ api: boards, init: true })
|
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) {
|
handleChange (event) {
|
||||||
var obj = {}
|
var obj = {}
|
||||||
@ -36,17 +46,24 @@ module.exports = function (boardsAPI) {
|
|||||||
this.setState({ loading: false, updating: false, error: false })
|
this.setState({ loading: false, updating: false, error: false })
|
||||||
},
|
},
|
||||||
refresh () {
|
refresh () {
|
||||||
this.setState({ loading: true })
|
boardsAPI.use(b => this.downloadPost(b))
|
||||||
// boardsAPI.use(b => this.getBoardSettings(b))
|
|
||||||
// TODO: DOWNLOAD POST
|
|
||||||
},
|
},
|
||||||
save () {
|
save () {
|
||||||
this.setState({ updating: true })
|
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 () {
|
additionalButtons () {
|
||||||
if (this.state.api && this.props.params.posthash) {
|
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>
|
return <span>
|
||||||
<button onClick={this.refresh} className="button not-first">Refresh</button>
|
<button onClick={this.refresh} className="button not-first">Refresh</button>
|
||||||
<Link to={url} className="button not-first">View</Link>
|
<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'}
|
{this.props.params.posthash ? ' Edit Post' : ' New Post'}
|
||||||
</h2>
|
</h2>
|
||||||
<p>This App uses IPFS to store your Posts. When you are offline,
|
<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>
|
others.</p>
|
||||||
<p><b>Warning:</b> due to a bug in go-ipfs, it may take up to a minute
|
<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
|
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" />
|
<input className="u-full-width" type="text" id="title" value={this.state.title} onChange={this.handleChange} placeholder="Choose a title" />
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label htmlFor="desc">Content</label>
|
<label htmlFor="text">Content</label>
|
||||||
<textarea className="u-full-width" id="desc" value={this.state.desc} onChange={this.handleChange} placeholder="Write your post. Markdown is supported :)" />
|
<textarea className="u-full-width" id="text" value={this.state.text} onChange={this.handleChange} placeholder="Write your post. Markdown is supported :)" />
|
||||||
</div>
|
</div>
|
||||||
<div className="buttons">
|
<div className="buttons">
|
||||||
<button className="button button-primary" onClick={this.save}>Publish</button>
|
<button className="button button-primary" onClick={this.save}>Publish</button>
|
||||||
|
Loading…
Reference in New Issue
Block a user