mirror of
https://github.com/fazo96/ipfs-boards
synced 2025-03-11 21:38:38 +01:00
improvements, removed @me, added post editing link and post deletion
This commit is contained in:
parent
086e6da860
commit
1a713c8559
@ -207,6 +207,56 @@ BoardsAPI.prototype.createPost = function (post, board, done) {
|
||||
], done)
|
||||
}
|
||||
|
||||
BoardsAPI.prototype.delete = function (opts, done) {
|
||||
var url = '/ipfs-boards-profile/'
|
||||
console.log('Deleting', opts)
|
||||
if (opts.what === 'post') url += 'posts/' + opts.board + '/'
|
||||
if (opts.what === 'comment') url += 'comments/' + opts.parent + '/'
|
||||
if (opts.what === 'comment' || opts.what === 'post') {
|
||||
this.ipfs.files.ls(url, (err, res) => {
|
||||
if (err) return done(err)
|
||||
if (!res || !res.Entries) return done('invalid response')
|
||||
var list = res.Entries
|
||||
for (var item in list) {
|
||||
if (list[item].Hash === opts.hash) {
|
||||
url = url + list[item].Name
|
||||
return this.ipfs.files.rm(url, {}, err => {
|
||||
if (err) return done(err)
|
||||
this.ipfs.files.stat('/', (err, res) => {
|
||||
if (err) return done(err)
|
||||
console.log('Publishing profile...')
|
||||
this.ipfs.name.publish(res.Hash, done)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
return done('post does not exist or was already deleted')
|
||||
})
|
||||
} else if (opts.what === 'board' || opts.what === 'profile') {
|
||||
if (opts.what === 'board') {
|
||||
url += 'boards/' + opts.board
|
||||
}
|
||||
this.ipfs.files.rm(url, { r: true }, err => {
|
||||
if (err) return done(err)
|
||||
this.ipfs.files.stat('/', (err, res) => {
|
||||
if (err) return done(err)
|
||||
console.log('Publishing profile...')
|
||||
this.ipfs.name.publish(res.Hash, done)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
done('what?')
|
||||
}
|
||||
}
|
||||
|
||||
BoardsAPI.prototype.deletePost = function (hash, board, done) {
|
||||
this.delete({ what: 'post', hash, board }, done)
|
||||
}
|
||||
|
||||
BoardsAPI.prototype.deleteComment = function (hash, parent, done) {
|
||||
this.delete({ what: 'comment', hash, parent }, done)
|
||||
}
|
||||
|
||||
BoardsAPI.prototype.backupCache = function () {
|
||||
if (window && window.localStorage !== undefined) {
|
||||
// Use localStorage to store the IPNS cache
|
||||
@ -434,7 +484,6 @@ BoardsAPI.prototype.downloadPost = function (hash, adminID, board, op, done) {
|
||||
done = op
|
||||
op = undefined
|
||||
}
|
||||
console.log('Downloading post', hash)
|
||||
this.ipfs.cat(hash, (err2, r) => {
|
||||
if (err2) {
|
||||
this.ee.emit('error', err2)
|
||||
@ -442,8 +491,10 @@ BoardsAPI.prototype.downloadPost = function (hash, adminID, board, op, done) {
|
||||
if (done && done.apply) done(err2)
|
||||
} else {
|
||||
replyAsObj(r, true, (err, post) => {
|
||||
if (err) return
|
||||
// TODO: add JSON parsing error handling
|
||||
if (err) {
|
||||
if (done && done.apply) return done(err)
|
||||
return
|
||||
}
|
||||
post.hash = hash
|
||||
if (op) post.op = op // Inject op
|
||||
if (board) {
|
||||
|
@ -9,13 +9,13 @@ module.exports = function (boardsAPI) {
|
||||
},
|
||||
componentDidMount () {
|
||||
boardsAPI.use(boards => {
|
||||
if (boards.isInit) this.setState({ api: true })
|
||||
if (boards.isInit) this.setState({ api: true, userid: boards.getMyID() })
|
||||
boards.getEventEmitter().on('init', err => {
|
||||
if (!this.isMounted()) return
|
||||
if (err) {
|
||||
this.setState({ loading: false, api: false })
|
||||
} else {
|
||||
this.setState({ api: true })
|
||||
this.setState({ api: true, userid: boards.getMyID() })
|
||||
}
|
||||
})
|
||||
})
|
||||
@ -23,7 +23,7 @@ module.exports = function (boardsAPI) {
|
||||
extraButtons: function () {
|
||||
if (this.state.api) {
|
||||
return <span>
|
||||
<Link className="nounderline" to="/@me"><Icon name="user" className="fa-2x light"/></Link>
|
||||
<Link className="nounderline" to={'/@' + this.state.userid}><Icon name="user" className="fa-2x light"/></Link>
|
||||
<Link className="nounderline" to="/users"><Icon name="globe" className="fa-2x light"/></Link>
|
||||
</span>
|
||||
} else if (this.state.loading) {
|
||||
|
@ -19,7 +19,7 @@ module.exports = React.createClass({
|
||||
init (props) {
|
||||
var boards = props.api
|
||||
if (!boards) return this.setState({ error: 'Could not connect to IPFS' })
|
||||
this.setState({ loading: true })
|
||||
this.setState({ loading: true, userid: boards.getMyID() })
|
||||
boards.downloadPost(props.hash, props.adminID, props.board, (err, hash, date, post) => {
|
||||
this.setState({ error: err, post: post, loading: false })
|
||||
})
|
||||
@ -35,6 +35,19 @@ module.exports = React.createClass({
|
||||
return '/post/' + this.props.hash
|
||||
}
|
||||
},
|
||||
editorLink () {
|
||||
if (this.state.post.op === this.state.userid) {
|
||||
var board = this.props.board || this.state.post.board
|
||||
if (board) {
|
||||
var url = '/edit/board/' + board + '/post/' + this.props.hash
|
||||
return <Link to={url} className="nounderline">
|
||||
<Icon name="edit" className="not-first"/> Edit
|
||||
</Link>
|
||||
} else {
|
||||
return <span/>
|
||||
}
|
||||
} else return <span/>
|
||||
},
|
||||
getContent () {
|
||||
if (this.state.error) {
|
||||
return <Error className="content" error={this.state.error} />
|
||||
@ -51,6 +64,7 @@ module.exports = React.createClass({
|
||||
<UserID id={this.state.post.op} api={this.props.api} ></UserID>
|
||||
<Clock className="not-first" date={this.state.post.date} />
|
||||
<Icon name="comments" className="not-first" /> <Link className="nounderline" to={this.postLink()}>View</Link>
|
||||
{this.editorLink()}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
@ -16,13 +16,24 @@ var Error = React.createClass({
|
||||
|
||||
var Loading = React.createClass({
|
||||
render () {
|
||||
return <div>
|
||||
<div className="text-center">
|
||||
<Icon className="center-block fa-spin fa-3x light" name="refresh" />
|
||||
<h4 className="top-half-em">{this.props.title}</h4>
|
||||
{ this.props.children }
|
||||
// TODO: merge these (duplicated code) and use css to get desired style
|
||||
if (this.props.small) {
|
||||
return <div>
|
||||
<div className="text-center">
|
||||
<Icon className="center-block fa-spin fa-2x light" name="refresh" />
|
||||
<h5 className="top-half-em">{this.props.title}</h5>
|
||||
{ this.props.children }
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
} else {
|
||||
return <div>
|
||||
<div className="text-center">
|
||||
<Icon className="center-block fa-spin fa-3x light" name="refresh" />
|
||||
<h4 className="top-half-em">{this.props.title}</h4>
|
||||
{ this.props.children }
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@ -31,11 +42,23 @@ var Saving = React.createClass({
|
||||
return <div>
|
||||
<div className="text-center">
|
||||
<Icon className="center-block fa-spin fa-3x light" name="refresh" />
|
||||
<h4 className="top-half-em">Publishing...</h4>
|
||||
<h4 className="top-half-em">{ this.props.title || 'Publishing' }</h4>
|
||||
{ this.props.children }
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = { Error, Loading, Saving }
|
||||
var Success = React.createClass({
|
||||
render () {
|
||||
return <div>
|
||||
<div className="text-center">
|
||||
<Icon className="center-block fa-3x light" name="check" />
|
||||
<h4 className="top-half-em">{ this.props.title || 'Done' }</h4>
|
||||
{ this.props.children }
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = { Error, Loading, Saving, Success }
|
||||
|
@ -2,7 +2,7 @@ var React = require('react')
|
||||
var GetIPFS = require('getipfs.jsx')
|
||||
var Icon = require('icon.jsx')
|
||||
var Link = require('react-router').Link
|
||||
var { Error, Loading, Saving } = require('status-components.jsx')
|
||||
var { Error, Loading, Saving, Success } = require('status-components.jsx')
|
||||
|
||||
module.exports = function (boardsAPI) {
|
||||
return React.createClass({
|
||||
@ -29,11 +29,12 @@ module.exports = function (boardsAPI) {
|
||||
},
|
||||
downloadPost (boards) {
|
||||
this.setState({ loading: true })
|
||||
boards.downloadPost(this.props.params.posthash, (err, p) => {
|
||||
boards.downloadPost(this.props.params.posthash, (err, hash, date, post) => {
|
||||
if (err) {
|
||||
this.setState({ error: err, loading: false })
|
||||
} else {
|
||||
this.setState({ loading: false, title: p.title, text: p.text })
|
||||
console.log(post)
|
||||
this.setState({ loading: false, title: post.title, text: post.text })
|
||||
}
|
||||
})
|
||||
},
|
||||
@ -43,7 +44,7 @@ module.exports = function (boardsAPI) {
|
||||
this.setState(obj)
|
||||
},
|
||||
skip () {
|
||||
this.setState({ loading: false, updating: false, error: false })
|
||||
this.setState({ loading: false, updating: false, error: false, success: false })
|
||||
},
|
||||
refresh () {
|
||||
boardsAPI.use(b => this.downloadPost(b))
|
||||
@ -61,12 +62,22 @@ module.exports = function (boardsAPI) {
|
||||
})
|
||||
})
|
||||
},
|
||||
delete () {
|
||||
this.setState({ deleting: true })
|
||||
boardsAPI.use(boards => {
|
||||
boards.deletePost(this.props.params.posthash, this.props.params.boardname, err => {
|
||||
if (!err) console.log('Post deleted')
|
||||
this.setState({ deleting: false, error: err, success: true })
|
||||
})
|
||||
})
|
||||
},
|
||||
additionalButtons () {
|
||||
if (this.state.api && 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>
|
||||
<button onClick={this.delete} className="button not-first">Delete</button>
|
||||
</span>
|
||||
} else {
|
||||
return <span></span>
|
||||
@ -78,6 +89,11 @@ module.exports = function (boardsAPI) {
|
||||
return <Error error={this.state.error} >
|
||||
<button className="button button-primary center-block" onClick={this.skip}>Continue</button>
|
||||
</Error>
|
||||
} else if (this.state.deleting) {
|
||||
return <Loading title="Deleting Post">
|
||||
<p>Pressing the Skip button will not abort the Delete operation.</p>
|
||||
<button className="button button-primary center-block" onClick={this.skip}>Skip</button>
|
||||
</Loading>
|
||||
} else if (this.state.loading) {
|
||||
return <Loading title="Downloading Post">
|
||||
<button className="button button-primary center-block" onClick={this.skip}>Skip</button>
|
||||
@ -87,10 +103,11 @@ module.exports = function (boardsAPI) {
|
||||
<p>Pressing the Skip button will not abort the publish operation.</p>
|
||||
<button className="button button-primary center-block" onClick={this.skip}>Skip</button>
|
||||
</Saving>
|
||||
} else if (this.state.success) {
|
||||
return <Success title="Post Deleted">
|
||||
<p><b>Note:</b> due to a bug in go-ipfs, you may need to wait up to a minute for changes to appear.</p>
|
||||
</Success>
|
||||
} else {
|
||||
if (this.state.userid && this.props.params.boardname) {
|
||||
var boardurl = '/@' + this.state.userid + '/' + this.props.params.boardname
|
||||
}
|
||||
return (
|
||||
<div className="editor">
|
||||
<h2><Icon name="pencil" className="light" />
|
||||
@ -112,7 +129,6 @@ module.exports = function (boardsAPI) {
|
||||
</div>
|
||||
<div className="buttons">
|
||||
<button className="button button-primary" onClick={this.save}>Publish</button>
|
||||
{boardurl ? <Link className="button not-first" to={boardurl}>View Board</Link> : <span></span> }
|
||||
{this.additionalButtons()}
|
||||
</div>
|
||||
</div>
|
||||
|
@ -36,7 +36,7 @@ module.exports = function (boardsAPI) {
|
||||
},
|
||||
init (boards) {
|
||||
if (this.state.init) return
|
||||
this.setState({ api: boards })
|
||||
this.setState({ api: boards, userid: boards.getMyID() })
|
||||
this.getProfile(boards)
|
||||
},
|
||||
handleChange (event) {
|
||||
@ -101,7 +101,7 @@ module.exports = function (boardsAPI) {
|
||||
<div className="buttons">
|
||||
<button className="button button-primary" onClick={this.save}>Publish</button>
|
||||
<button onClick={this.refresh} className="button not-first">Refresh</button>
|
||||
<Link to='/@me' className="button not-first">View</Link>
|
||||
<Link to={'/@' + this.state.userid} className="button not-first">View</Link>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
@ -13,13 +13,11 @@ module.exports = function (boardsAPI) {
|
||||
componentDidMount () {
|
||||
boardsAPI.use(boards => {
|
||||
if (boards.isInit) {
|
||||
this.setState({ api: boards, id: boards.id })
|
||||
this.init(boards)
|
||||
}
|
||||
var ee = boards.getEventEmitter()
|
||||
ee.on('init', err => {
|
||||
if (!err && this.isMounted()) {
|
||||
this.setState({ api: boards, id: boards.id })
|
||||
this.init(boards)
|
||||
}
|
||||
})
|
||||
@ -50,10 +48,8 @@ module.exports = function (boardsAPI) {
|
||||
},
|
||||
init (boards) {
|
||||
if (this.state.init) return
|
||||
if (boards.isInit || this.state.api) {
|
||||
this.downloadProfile(boards, this.props)
|
||||
this.setState({ init: true })
|
||||
}
|
||||
this.setState({ init: true, api: boards, id: boards.id })
|
||||
this.downloadProfile(boards, this.props)
|
||||
},
|
||||
linkToEditor () {
|
||||
var uid = this.props.params.userid
|
||||
|
Loading…
Reference in New Issue
Block a user