From 6bbd1377ec96e1341a260246d69d0b8e7a854413 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Sat, 19 Dec 2015 17:43:55 +0100 Subject: [PATCH] implemented limited mode --- lib/boards-api.js | 36 +++++++++++++++++++------------ webapp/components/comment.jsx | 39 ++++++++++++++++++++++------------ webapp/components/navbar.jsx | 8 ++++--- webapp/components/post.jsx | 12 +++++++---- webapp/components/postlist.jsx | 37 +++++++++++++++++++++----------- webapp/components/userID.jsx | 8 ++++--- webapp/pages/board.jsx | 26 +++++++++++++---------- webapp/pages/postpage.jsx | 2 -- webapp/pages/profile.jsx | 12 ++++------- 9 files changed, 109 insertions(+), 71 deletions(-) diff --git a/lib/boards-api.js b/lib/boards-api.js index 81ecef7..055a60f 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -261,7 +261,10 @@ BoardsAPI.prototype.deleteComment = function (hash, parent, done) { BoardsAPI.prototype.cat = function (path, done) { if (this.limited) { // Download via gateway - if (path.indexOf('/ipfs/') !== 0) path = '/ipfs/' + path + if (path.indexOf('Qm') === 0) { + path = '/ipfs/' + path + } + console.log('Downloading via Gateway: ', path) wreck.get(path, (err, res, payload) => { console.log('GET:', err, res, payload) if (payload.toString) payload = payload.toString() @@ -291,8 +294,13 @@ BoardsAPI.prototype.backupCache = function () { } // Rewrote this to use event emitters. Should also add periodic rechecking +// TODO: maybe drop this entirely? We can cat and ls IPNS names now. BoardsAPI.prototype.resolveIPNS = function (n, handler) { if (handler && handler.apply) this.ee.on(n, handler) + if (this.limited) { + // In limited mode, don't solve addresses + return this.ee.emit(n, '/ipns/' + n) + } if (!this.resolvingIPNS[n]) { this.resolvingIPNS[n] = true this.ipfs.name.resolve(n, (err, r) => { @@ -333,7 +341,7 @@ BoardsAPI.prototype.resolveIPNS = function (n, handler) { BoardsAPI.prototype.isUserProfile = function (addr, done) { if (addr === undefined) return console.log('Asked to check if undefined is a profile') - this.ipfs.cat(addr + this.baseurl + 'ipfs-boards-version.txt', (err, r) => { + this.cat(addr + this.baseurl + 'ipfs-boards-version.txt', (err, r) => { if (err) return done(false, err) replyAsObj(r, false, (_, res) => { if (!res || !res.trim) { @@ -392,7 +400,7 @@ BoardsAPI.prototype.getProfile = function (userID, done) { done(err, null) } else { // Download actual profile - this.ipfs.cat(url + this.baseurl + 'profile.json', (err2, res) => { + this.cat(url + this.baseurl + 'profile.json', (err2, res) => { if (err2) { this.ee.emit('error', err2) done(err2, null) @@ -410,7 +418,7 @@ BoardsAPI.prototype.getProfile = function (userID, done) { } }) // Get other info - this.ipfs.ls(url + this.baseurl + 'boards/', (err2, res) => { + this.ls(url + this.baseurl + 'boards/', (err2, res) => { if (!err2) { var l = res.Objects[0].Links.map(i => { return { name: i.Name, hash: i.Hash } @@ -438,7 +446,7 @@ BoardsAPI.prototype.getBoardSettings = function (userID, board, done) { this.ee.emit('error', e) } else { var url = r + this.baseurl + 'boards/' + board + '/settings.json' - this.ipfs.cat(url, (err, resp) => { + this.cat(url, (err, resp) => { var settings try { settings = JSON.parse(resp.toString()) @@ -457,7 +465,7 @@ BoardsAPI.prototype.getBoardSettings = function (userID, board, done) { if (settings.whitelist === true) { // Get the whitelist var url = r + this.baseurl + 'boards/' + board + '/whitelist' - this.ipfs.cat(url, (err, res) => { + this.cat(url, (err, res) => { if (err) { this.ee.emit('error', err) // Emit an empty whitelist. @@ -479,7 +487,7 @@ BoardsAPI.prototype.getBoardSettings = function (userID, board, done) { if (!settings.whitelist_only && !settings.approval_required && settings.blacklist === true) { // Get the blacklist var u = r + this.baseurl + 'boards/' + board + '/blacklist' - this.ipfs.cat(u, (err, blacklist) => { + this.cat(u, (err, blacklist) => { if (err) { this.ee.emit('error', err) } else { @@ -510,7 +518,7 @@ BoardsAPI.prototype.downloadPost = function (hash, adminID, board, op, done) { done = op op = undefined } - this.ipfs.cat(hash, (err2, r) => { + this.cat(hash, (err2, r) => { if (err2) { this.ee.emit('error', err2) console.log('Could not download post', hash, 'of', board + '@' + adminID) @@ -537,7 +545,7 @@ BoardsAPI.prototype.downloadPost = function (hash, adminID, board, op, done) { BoardsAPI.prototype.retrieveListOfApproved = function (what, addr, adminID, board) { var a = addr + this.baseurl + 'boards/' + board + '/approved/' + what + '/' - this.ipfs.ls(a, (err, res) => { + this.ls(a, (err, res) => { if (err) { this.ee.emit('error', err) } else { @@ -609,7 +617,7 @@ BoardsAPI.prototype.getUserPostListInBoard = function (user, board, done) { this.ee.emit('error', err) done(err) } else { - this.ipfs.ls(url + this.baseurl + 'posts/' + board, (e, r) => { + this.ls(url + this.baseurl + 'posts/' + board, (e, r) => { if (e) { this.ee.emit('error', e) done(e) @@ -634,7 +642,7 @@ BoardsAPI.prototype.downloadComment = function (hash, adminID, board, target, do target = undefined } console.log('target', target) - this.ipfs.cat(hash, (err2, r) => { + this.cat(hash, (err2, r) => { if (err2) { this.ee.emit('error', err2) console.log('Could not download comment', hash, 'of', board + '@' + adminID) @@ -660,7 +668,7 @@ BoardsAPI.prototype.getCommentsFor = function (parent, board, adminID, target) { return console.log('malformed arguments:', parent, board, adminID) } // figure out if there's a previous version of the item - this.ipfs.cat(parent, (err, res) => { + this.cat(parent, (err, res) => { if (err) { this.ee.emit('error', err) } else { @@ -706,7 +714,7 @@ BoardsAPI.prototype.getUserCommentList = function (parent, user, done) { this.ee.emit('error', err) done(err) } else { - this.ipfs.ls(url + this.baseurl + 'comments/' + parent, (e, r) => { + this.ls(url + this.baseurl + 'comments/' + parent, (e, r) => { if (e) { this.ee.emit('error', e) done(e) @@ -728,7 +736,7 @@ BoardsAPI.prototype.getUserCommentList = function (parent, user, done) { BoardsAPI.prototype.isRunningFromGateway = function () { if (!window) return false - return window.location.pathname.indexOf('/ipfs/') === 0 + return window.location.pathname.indexOf('/ipfs/') === 0 || window.location.pathname.indexOf('/ipns/') === 0 } BoardsAPI.prototype.isNode = function () { diff --git a/webapp/components/comment.jsx b/webapp/components/comment.jsx index 2e4f193..1dddf58 100644 --- a/webapp/components/comment.jsx +++ b/webapp/components/comment.jsx @@ -53,29 +53,40 @@ var Comments = React.createClass({ return { comments: [] } }, componentDidMount () { - var boards = this.props.api - if (boards) { - boards.getEventEmitter().on('comment for ' + this.props.parent, cmnt => { - if (this.isMounted()) this.setState({ comments: this.state.comments.concat(cmnt) }) - }) - if (boards.isInit && this.isMounted()) { + if (this.props.api) this.init(this.props.api) + }, + componentWillReceiveProps (props) { + if (props.api) this.init(props.api) + }, + init (boards) { + boards.getEventEmitter().on('comment for ' + this.props.parent, cmnt => { + if (this.isMounted()) this.setState({ comments: this.state.comments.concat(cmnt) }) + }) + boards.getEventEmitter().on('init', (err, limited) => { + if (!this.isMounted()) return + if (!err) { boards.getCommentsFor(this.props.parent, this.props.board, this.props.adminID) } - boards.getEventEmitter().on('init', err => { - if (!err && this.isMounted()) { - boards.getCommentsFor(this.props.parent, this.props.board, this.props.adminID) - } - }) + if (limited) this.setState({ limited }) + }) + if (boards.isInit) { + boards.getCommentsFor(this.props.parent, this.props.board, this.props.adminID) } + if (boards.limited) this.setState({ limited: true }) }, getComments () { if (this.state.comments.length > 0) { return this.state.comments.map(cmnt => ()) - } - else return
+ } else return
}, render () { - return
{this.getComments()}
+ if (this.state.limited) { + return
+

Comments can't be displayed in limited mode

+
+ } else { + return
{this.getComments()}
+ } } }) diff --git a/webapp/components/navbar.jsx b/webapp/components/navbar.jsx index 79bfb45..c0b083a 100644 --- a/webapp/components/navbar.jsx +++ b/webapp/components/navbar.jsx @@ -10,12 +10,12 @@ module.exports = function (boardsAPI) { componentDidMount () { boardsAPI.use(boards => { if (boards.isInit) this.setState({ api: true, userid: boards.getMyID() }) - boards.getEventEmitter().on('init', err => { + boards.getEventEmitter().on('init', (err, limited) => { if (!this.isMounted()) return if (err) { - this.setState({ loading: false, api: false }) + this.setState({ loading: false, api: false, limited }) } else { - this.setState({ api: true, userid: boards.getMyID() }) + this.setState({ api: true, userid: boards.getMyID(), limited }) } }) }) @@ -28,6 +28,8 @@ module.exports = function (boardsAPI) { } else if (this.state.loading) { return + } else if (this.state.limited) { + return } else { return } diff --git a/webapp/components/post.jsx b/webapp/components/post.jsx index 70e0514..c887c4a 100644 --- a/webapp/components/post.jsx +++ b/webapp/components/post.jsx @@ -5,6 +5,7 @@ var Link = require('react-router').Link var Clock = require('clock.jsx') var UserID = require('userID.jsx') var { Error } = require('status-components.jsx') +var Comments = require('comment.jsx').Comments module.exports = React.createClass({ getInitialState () { @@ -12,10 +13,10 @@ module.exports = React.createClass({ }, componentDidMount () { if (this.props.api) { - this.props.api.getEventEmitter().on('init', err => { - if (!err) this.init(this.props) + this.props.api.getEventEmitter().on('init', (err, limited) => { + if (!err || limited) this.init(this.props) }) - if (this.props.api.isInit) this.init(this.props) + if (this.props.api.isInit || this.props.api.limited) this.init(this.props) } }, componentWillReceiveProps (props) { @@ -80,6 +81,9 @@ module.exports = React.createClass({ } }, render () { - return
{this.getContent()}
+ return
+
{this.getContent()}
+ +
} }) diff --git a/webapp/components/postlist.jsx b/webapp/components/postlist.jsx index 604f6aa..3f95880 100644 --- a/webapp/components/postlist.jsx +++ b/webapp/components/postlist.jsx @@ -5,7 +5,7 @@ var Post = require('post.jsx') module.exports = React.createClass({ getInitialState () { - return { posts: [], api: false } + return { posts: [] } }, sortFn (a, b) { return (b.date || 0) - (a.date || 0) @@ -16,9 +16,6 @@ module.exports = React.createClass({ } }, init (boards, props) { - if (this.state.init) return - props = props || this.props - this.setState({ api: true }) var onPost = (hash, date, post) => { if (!this.isMounted()) return true var now = (new Date()).getTime() @@ -33,18 +30,29 @@ module.exports = React.createClass({ }*/ this.setState({ posts }) } + props = props || this.props boards.getEventEmitter().on('post in ' + props.board + (props.admin ? '@' + props.admin : ''), onPost) - boards.getPostsInBoard(props.admin, props.board) - this.setState({ init: true }) + this.setState({ api: boards, limited: boards.limited }) + if (boards.isInit) { + boards.getPostsInBoard(props.admin, props.board) + } else { + boards.getEventEmitter().on('init', (err, limited) => { + if (!err) { + boards.getPostsInBoard(props.admin, props.board) + } else { + this.setState({ limited }) + } + }) + } }, componentDidMount () { var boards = this.props.api if (boards) { - if (boards.isInit) { + if (boards.isInit || boards.limited) { this.init(boards) } else { - boards.getEventEmitter().on('init', err => { - if (!err && this.isMounted()) this.init(boards) + boards.getEventEmitter().on('init', (err, limited) => { + if ((!err || limited) && this.isMounted()) this.init(boards) }) } } @@ -61,10 +69,15 @@ module.exports = React.createClass({ } }, render () { - return ( -
+ if (this.state.limited) { + return
+
+

Posts in a board can't be shown in limited mode. Sorry!

+
+ } else { + return
{this.getPosts()}
- ) + } } }) diff --git a/webapp/components/userID.jsx b/webapp/components/userID.jsx index d16a065..1c678ed 100644 --- a/webapp/components/userID.jsx +++ b/webapp/components/userID.jsx @@ -16,10 +16,12 @@ module.exports = React.createClass({ props = props || this.props var boards = props.api if (boards) { - boards.getEventEmitter().on('init', err => { - if (!err && this.isMounted()) this.getProfile(boards) + boards.getEventEmitter().on('init', (err, limited) => { + if ((!err || limited) && this.isMounted()) { + this.getProfile(boards) + } }) - if (boards.isInit) { + if (boards.isInit || boards.limited) { this.getProfile(boards) } } diff --git a/webapp/pages/board.jsx b/webapp/pages/board.jsx index 6b36f1f..4761ae4 100644 --- a/webapp/pages/board.jsx +++ b/webapp/pages/board.jsx @@ -16,8 +16,8 @@ module.exports = function (boardsAPI) { boardsAPI.use(boards => { if (!this.isMounted()) return var ee = boards.getEventEmitter() - ee.on('init', err => { - if (!err && this.isMounted()) { + ee.on('init', (err, limited) => { + if ((!err || limited) && this.isMounted()) { this.init(boards) } }) @@ -46,13 +46,17 @@ module.exports = function (boardsAPI) { var props = newProps || this.props if (!props.params.userid) return boards.getBoardSettings(props.params.userid, props.params.boardname) - this.setState({ loading: true, init: true, api: true, userid: boards.getMyID(), boards: boards }) + this.setState({ loading: true, init: true, api: boards, userid: boards.getMyID(), limited: boards.limited }) }, toolbox () { - return
- - -
+ if (this.state.limited) { + return
Toolbox not available in limited mode
+ } else { + return
+ + +
+ } }, render () { if (this.state.api) { @@ -66,16 +70,16 @@ module.exports = function (boardsAPI) { return (

{this.state.name}

- {this.props.params.userid ?
:

} + {this.props.params.userid ?
:

}
- {this.state.whitelist.map(i => )} + {this.state.whitelist.map(i => )}

{this.toolbox()} - +
) } - } else return + } else return } }) } diff --git a/webapp/pages/postpage.jsx b/webapp/pages/postpage.jsx index f23c569..182c852 100644 --- a/webapp/pages/postpage.jsx +++ b/webapp/pages/postpage.jsx @@ -3,7 +3,6 @@ var Link = require('react-router').Link var UserID = require('userID.jsx') var GetIPFS = require('getipfs.jsx') var Post = require('post.jsx') -var Comments = require('comment.jsx').Comments module.exports = function (boardsAPI) { return React.createClass({ @@ -31,7 +30,6 @@ module.exports = function (boardsAPI) { {this.getContext()}
- } else { return diff --git a/webapp/pages/profile.jsx b/webapp/pages/profile.jsx index a8e1298..fe14124 100644 --- a/webapp/pages/profile.jsx +++ b/webapp/pages/profile.jsx @@ -12,12 +12,12 @@ module.exports = function (boardsAPI) { }, componentDidMount () { boardsAPI.use(boards => { - if (boards.isInit) { + if (boards.isInit || boards.limited) { this.init(boards) } var ee = boards.getEventEmitter() - ee.on('init', err => { - if (!err && this.isMounted()) { + ee.on('init', (err, limited) => { + if ((!err || limited) && this.isMounted()) { this.init(boards) } }) @@ -30,10 +30,8 @@ module.exports = function (boardsAPI) { downloadProfile (boards, props) { var ee = boards.getEventEmitter() var uid = props.params.userid - if (uid === 'me') uid = boards.id ee.on('boards for ' + uid, l => { var u2id = props.params.userid - if (u2id === 'me') u2id = boards.id if (!this.isMounted() || u2id !== uid) return true this.setState({ boards: l }) }) @@ -48,12 +46,11 @@ module.exports = function (boardsAPI) { }, init (boards) { if (this.state.init) return - this.setState({ init: true, api: boards, id: boards.id }) + this.setState({ init: true, api: boards, id: boards.id, limited: boards.limited }) this.downloadProfile(boards, this.props) }, linkToEditor () { var uid = this.props.params.userid - if (uid === 'me' && this.state.id) uid = this.state.id if (uid === this.state.id) { return
This is your profile
@@ -76,7 +73,6 @@ module.exports = function (boardsAPI) { return {this.getEditButton()} } else { var uid = this.props.params.userid - if (uid === 'me') uid = this.state.id return (
{this.linkToEditor()}

{this.state.name}