1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-25 14:54:19 +01:00

refactored all code to comply with new code style

This commit is contained in:
Enrico Fasoli 2015-12-14 00:29:25 +01:00
parent 307605a404
commit 78c659e123
20 changed files with 594 additions and 567 deletions

View File

@ -63,9 +63,9 @@ function BoardsAPI(ipfs){
this.users = [] // list of IPNS names
this.resolvingIPNS = {}
this.ee = new EventEmitter()
if(localStorage !== undefined){
if (window && window.localStorage !== undefined) {
// Use localStorage to store the IPNS cache
var stored = localStorage.getItem('ipfs-boards-user-cache')
var stored = window.localStorage.getItem('ipfs-boards-user-cache')
try {
this.users = JSON.parse(stored)
if (this.users === null || this.users === undefined || !this.users.indexOf || !this.users.push) {
@ -78,9 +78,9 @@ function BoardsAPI(ipfs){
}
BoardsAPI.prototype.backupCache = function () {
if(localStorage !== undefined){
if (window && window.localStorage !== undefined) {
// Use localStorage to store the IPNS cache
localStorage.setItem('ipfs-boards-user-cache',JSON.stringify(this.users))
window.localStorage.setItem('ipfs-boards-user-cache', JSON.stringify(this.users))
}
}
@ -233,7 +233,7 @@ BoardsAPI.prototype.getBoardSettings = function(userID,board){
} else {
// SETTINGS file is here, need to parse it a little bit
this.ee.emit('settings for ' + board + '@' + userID, settings, r)
if(settings.whitelist == true){
if (settings.whitelist === true) {
// Get the whitelist
var url = r + this.baseurl + 'boards/' + board + '/whitelist'
this.ipfs.cat(url, (err, res) => {
@ -241,14 +241,21 @@ BoardsAPI.prototype.getBoardSettings = function(userID,board){
this.ee.emit('error', err)
// Emit an empty whitelist.
this.ee.emit('whitelist for ' + board + '@' + userID, [])
} else replyAsObj(res,false,(err,whitelist) => {
} else {
replyAsObj(res, false, (err, whitelist) => {
if (err) {
// Emit an empty whitelist.
this.ee.emit('whitelist for ' + board + '@' + userID, [])
} else {
// Send whitelist
var w = whitelist.split(' ').map(x => x.trim())
this.ee.emit('whitelist for ' + board + '@' + userID, w)
})
}
})
}
if(!settings.whitelist_only && !settings.approval_required && settings.blacklist == true){
})
}
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) => {
@ -278,6 +285,7 @@ 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
post.hash = hash
if (op) post.op = op // Inject op
@ -312,14 +320,14 @@ BoardsAPI.prototype.getAllowedContentProducers = function(adminID,board,options)
if (!options) return
this.ee.on('settings for ' + board + '@' + adminID, function (settings, addr) {
// Get stuff based on settings
if(settings.approval_required == true){
if (settings.approval_required === true) {
// Get approved posts list
if (options.posts) this.retrieveListOfApproved('posts', addr, adminID, board)
// Get approved comments list
if (options.comments) this.retrieveListOfApproved('comments', addr, adminID, board)
} else if(settings.whitelist_only == true){
} else if (settings.whitelist_only === true) {
// TODO: emit all whitelisted users
} else if(settings.blacklist == true){
} else if (settings.blacklist === true) {
// TODO: emit all users not in the blacklist
}
})
@ -337,6 +345,7 @@ BoardsAPI.prototype.getPostsInBoard = function(adminID,board){
// download posts for each user in whitelist
whitelist.forEach(item => {
this.getUserPostListInBoard(item, board, (err, postList) => {
if (err) return
postList.forEach(i => this.downloadPost(i.hash, adminID, board, item))
})
})
@ -366,7 +375,8 @@ BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
if (err) {
this.ee.emit('error', err)
done(err)
} else this.ipfs.ls(url+this.baseurl+'posts/'+board,(e,r) => {
} else {
this.ipfs.ls(url + this.baseurl + 'posts/' + board, (e, r) => {
if (e) {
this.ee.emit('error', e)
done(e)
@ -379,13 +389,14 @@ BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){
done(null, l)
}
})
}
return true // remove myself from listeners
})
return this.ee
}
BoardsAPI.prototype.downloadComment = function (hash, adminID, board, target, done) {
if(!done && typeof target == 'function'){
if (!done && typeof target === 'function') {
done = target
target = undefined
}
@ -423,7 +434,7 @@ BoardsAPI.prototype.getCommentsFor = function(parent,board,adminID,target){
replyAsObj(res, true, (err2, obj) => {
if (err2) {
this.ee.emit('error', err2)
} else if(typeof obj.previous == 'string'){
} else if (typeof obj.previous === 'string') {
// Also get comments for the previous version of the parent!
this.getCommentsFor(obj.previous, board, adminID, parent)
}
@ -441,6 +452,7 @@ BoardsAPI.prototype.getCommentsFor = function(parent,board,adminID,target){
// download posts for each user in whitelist
whitelist.forEach(item => {
this.getUserCommentList(parent, item, (err, res) => {
if (err) return
res.forEach(i => this.downloadComment(i.hash, adminID, board, target))
})
})
@ -460,7 +472,8 @@ BoardsAPI.prototype.getUserCommentList = function(parent,user,done){
if (err) {
this.ee.emit('error', err)
done(err)
} else this.ipfs.ls(url+this.baseurl+'comments/'+parent,(e,r) => {
} else {
this.ipfs.ls(url + this.baseurl + 'comments/' + parent, (e, r) => {
if (e) {
this.ee.emit('error', e)
done(e)
@ -474,6 +487,7 @@ BoardsAPI.prototype.getUserCommentList = function(parent,user,done){
}
}
})
}
return true // remove myself from listeners
})
return this.ee

View File

@ -3,8 +3,6 @@ var ReactDOM = require('react-dom')
var Router = require('react-router').Router
var Route = require('react-router').Route
var IndexRoute = require('react-router').IndexRoute
var Redirect = require('react-router').Redirect
var Link = require('react-router').Link
// Load CSS
require('normalize.css')
@ -14,11 +12,9 @@ require('raleway.css')
// Load Components
var opt = require('options.jsx').get()
var boardsWrapper = require('boardsapiwrapper.js')
var boards = new boardsWrapper()
var BoardsWrapper = require('boardsapiwrapper.js')
var boards = new BoardsWrapper()
var Icon = require('icon.jsx')
var GetIPFS = require('getipfs.jsx')
// Load pages
@ -33,13 +29,13 @@ var CommentPage = require('commentpage.jsx')(boards)
// Define Main Components
var Container = React.createClass({
render: function(){
render () {
return (<div className="container app">{this.props.children}</div>)
}
})
var App = React.createClass({
render: function(){
render () {
return (<div><Navbar /><Container>{this.props.children}</Container></div>)
}
})
@ -47,10 +43,10 @@ var App = React.createClass({
// Static pages
var Static = React.createClass({
html: function(){
html () {
return { __html: this.props.content }
},
render: function(){
render () {
if (this.props.content) {
return <div className={this.props.className} dangerouslySetInnerHTML={this.html()} />
} else {
@ -60,13 +56,13 @@ var Static = React.createClass({
})
var Homepage = React.createClass({
render: function(){
render () {
return <Static className="homepage" content={require('landing.md')} />
}
})
var NotFound = React.createClass({
render: function(){
render () {
return (<div className="text-center">
<h1><Icon name="ban"/></h1>
<p>Sorry, there's nothing here!</p>

View File

@ -18,16 +18,18 @@ module.exports = React.createClass({
})
},
upDate: function () {
if(this.isMounted())
if (this.isMounted()) {
this.setState({ text: this.state.moment.unix(this.props.date).fromNow() })
else
} else {
clearInterval(this.state.interval)
}
},
getDate: function () {
if(this.state.moment)
if (this.state.moment) {
return this.state.text
else
} else {
return <Icon name="refresh" className="fa-spin" />
}
},
render: function () {
return <div className="inline"><Icon name="clock-o" className={this.props.className} /> {this.getDate()}</div>

View File

@ -62,8 +62,9 @@ var Comments = React.createClass({
boards.getCommentsFor(this.props.parent, this.props.board, this.props.adminID)
}
boards.getEventEmitter().on('init', err => {
if(!err && this.isMounted())
if (!err && this.isMounted()) {
boards.getCommentsFor(this.props.parent, this.props.board, this.props.adminID)
}
})
}
},

View File

@ -1,6 +1,7 @@
module.exports = {
get: function () {
var opt, s = localStorage.getItem('ipfs-boards-settings')
var opt
var s = window.localStorage.getItem('ipfs-boards-settings')
try {
opt = JSON.parse(s)
} catch (e) {

View File

@ -13,8 +13,7 @@ module.exports = React.createClass({
init: function (boards) {
if (this.state.init) return
this.setState({ api: true })
boards.getPostsInBoard(this.props.admin,this.props.board)
.on('post in '+this.props.board+(this.props.admin?'@'+this.props.admin:''),(post,hash) => {
var onPost = (post, hash) => {
if (!this.isMounted()) return true
var now = (new Date()).getTime()
var posts = this.state.posts
@ -27,26 +26,33 @@ module.exports = React.createClass({
console.log('Post discarded cause date in the future:', post)
}
this.setState({ posts })
})
}
boards.getEventEmitter().on('post in ' + this.props.board + (this.props.admin ? '@' + this.props.admin : ''), onPost)
boards.getPostsInBoard(this.props.admin, this.props.board)
this.setState({ init: true })
},
componentDidMount: function () {
var boards = this.props.api
if (boards) {
if(boards.isInit) this.init(boards)
else boards.getEventEmitter().on('init',err => {
if (boards.isInit) {
this.init(boards)
} else {
boards.getEventEmitter().on('init', err => {
if (!err && this.isMounted()) this.init(boards)
})
}
}
},
getPosts: function () {
if (this.state.posts.length > 0 || this.state.api) {
return this.state.posts.map(post => {
return <Post key={post.hash} board={this.props.board} admin={this.props.admin} post={post} api={this.props.api} />
})
} else return <div className="center-block text-center">
} else {
return <div className="center-block text-center">
<Icon name="refresh" className="fa-3x center-block light fa-spin" />
</div>
}
},
render: function () {
return (

View File

@ -37,15 +37,16 @@ module.exports = React.createClass({
}
},
render: function () {
if(this.props.id === undefined || this.props.id === 'undefined')
if (this.props.id === undefined || this.props.id === 'undefined') {
return <div className="user-id">
<Icon name="ban" /> Unknown User
</div>
else
} else {
return (<div className="user-id">
<Link className="light nounderline" to={'/@' + this.props.id}>
{this.getContent()}{this.state.name || this.props.id}
</Link>
</div>)
}
}
})

View File

@ -1,7 +1,5 @@
var React = require('react')
var Markdown = require('markdown.jsx')
var Link = require('react-router').Link
var Icon = require('icon.jsx')
var UserID = require('userID.jsx')
var PostList = require('postlist.jsx')
var GetIPFS = require('getipfs.jsx')
@ -29,9 +27,9 @@ module.exports = function(boardsAPI){
})
if (this.props.params.userid) {
ee.on('whitelist for ' + this.props.params.boardname + '@' + this.props.params.userid, (whitelist) => {
if(this.isMounted())
if (this.isMounted()) {
this.setState({ whitelist })
else return true
} else return true
})
ee.on('settings for ' + this.props.params.boardname + '@' + this.props.params.userid, (res) => {
if (!this.isMounted()) return true
@ -47,8 +45,9 @@ module.exports = function(boardsAPI){
},
init: function (boards) {
if (!this.state.init) {
if(this.props.params.userid)
if (this.props.params.userid) {
boards.getBoardSettings(this.props.params.userid, this.props.params.boardname)
}
this.setState({ init: true, api: true, boards: boards })
}
},

View File

@ -3,7 +3,6 @@ var Link = require('react-router').Link
var Icon = require('icon.jsx')
var UserID = require('userID.jsx')
var GetIPFS = require('getipfs.jsx')
var Post = require('post.jsx')
var Comment = require('comment.jsx').Comment
module.exports = function (boardsAPI) {
@ -31,7 +30,9 @@ module.exports = function(boardsAPI){
this.setState({ comment: false })
boards.downloadComment(props.params.commenthash, props.params.userid, props.params.boardname, (err, comment) => {
if (err) {
this.setState({ comment: { title: 'Error', text: err.Message || err.Error }})
this.setState({
comment: { title: 'Error', text: err.Message || err.Error }
})
} else {
this.setState({ comment })
}
@ -44,10 +45,11 @@ module.exports = function(boardsAPI){
},
getContext: function () {
if (this.props.params.userid) {
if(this.props.params.boardname)
if (this.props.params.boardname) {
return <div>Comment by <UserID id={this.props.params.userid} api={this.state.boards} /> in <Link to={'@' + this.props.params.userid + '/' + this.props.params.boardname}>#{this.props.params.boardname}</Link> to <Link to={'/@' + this.props.params.userid + '/' + this.props.params.boardname + '/' + this.props.params.posthash }>{this.props.params.posthash}</Link></div>
else
} else {
return <div>Comment by <UserID id={this.props.params.userid} api={this.state.boards} /></div>
}
} else return <div><h6 className="light">You are viewing a single comment</h6></div>
},
showComment: function () {
@ -61,14 +63,16 @@ module.exports = function(boardsAPI){
}
},
render: function () {
if(this.state.api)
if (this.state.api) {
return <div className="comment-page">
<div className="text-center">
{this.getContext()}
</div>
{this.showComment()}
</div>
else return <GetIPFS api={this.state.boards} />
} else {
return <GetIPFS api={this.state.boards} />
}
}
})
}

View File

@ -29,7 +29,9 @@ module.exports = function(boardsAPI){
downloadPost: function (boards, props) {
boards.downloadPost(props.params.posthash, props.params.userid, props.params.boardname, props.params.userid, (err, post) => {
if (err) {
this.setState({ post: { title: 'Error', text: err.Message || err.Error }})
this.setState({
post: { title: 'Error', text: err.Message || err.Error }
})
} else {
this.setState({ post })
}
@ -42,14 +44,15 @@ module.exports = function(boardsAPI){
},
getContext: function () {
if (this.props.params.userid) {
if(this.props.params.boardname)
if (this.props.params.boardname) {
return <div>Posted by <UserID id={this.props.params.userid} api={this.state.boards} /> in <Link to={'@' + this.props.params.userid + '/' + this.props.params.boardname}>#{this.props.params.boardname}</Link></div>
else
} else {
return <div>Posted by <UserID id={this.props.params.userid} api={this.state.boards} /></div>
}
} else return <div><h6 className="light">You are viewing a single post</h6></div>
},
render: function () {
if(this.state.api)
if (this.state.api) {
return <div className="post-page">
<div className="text-center">
{this.getContext()}
@ -57,7 +60,9 @@ module.exports = function(boardsAPI){
<Post post={this.state.post} board={this.props.params.boardname} api={this.state.boards} />
<Comments parent={this.props.params.posthash} board={this.props.params.boardname} adminID={this.props.params.userid} post={this.props.params.posthash} api={this.state.boards} />
</div>
else return <GetIPFS api={this.state.boards} />
} else {
return <GetIPFS api={this.state.boards} />
}
}
})
}

View File

@ -51,9 +51,7 @@ module.exports = function(boardsAPI){
},
init: function (boards) {
if (this.state.init) return
var ee = boards.getEventEmitter()
if (boards.isInit || this.state.api) {
var uid = this.props.params.userid
this.downloadProfile(boards, this.props)
this.setState({ init: true })
}

View File

@ -13,22 +13,22 @@ module.exports = function(boardsAPI){
if (!err && this.isMounted()) this.setState({ api: true })
})
})
var s = localStorage.getItem('ipfs-boards-settings')
var s = window.localStorage.getItem('ipfs-boards-settings')
var obj = this.getDefaults()
try {
obj = JSON.parse(s)
} catch (e) {
localStorage.removeItem('ipfs-boards-settings')
window.localStorage.removeItem('ipfs-boards-settings')
}
return obj || this.getDefaults()
},
save: function () {
if(isNaN(this.state.port) || parseInt(this.state.port) > 65535 || parseInt(this.state.port) < 1){
alert('Port number invalid')
if (isNaN(this.state.port) || parseInt(this.state.port, 10) > 65535 || parseInt(this.state.port, 10) < 1) {
window.alert('Port number invalid')
} else {
localStorage.setItem('ipfs-boards-settings',JSON.stringify({
window.localStorage.setItem('ipfs-boards-settings', JSON.stringify({
addr: this.state.addr,
port: parseInt(this.state.port)
port: parseInt(this.state.port, 10)
}))
window.location.reload(false)
}
@ -77,5 +77,4 @@ module.exports = function(boardsAPI){
)
}
})
}

View File

@ -23,7 +23,8 @@ var config = {
eslint: {
configFile: './.eslintrc',
failOnWarning: true,
failOnError: true
failOnError: true,
fix: true
},
module: {
preLoaders: [
@ -96,7 +97,7 @@ config.devServer = {
}
function addTransformRuntime (l) {
if(process.env.os != 'Windows_NT'){
if (process.env.os !== 'Windows_NT') {
// Workaround for babel6 bug on windows
// https://phabricator.babeljs.io/T6670
// https://phabricator.babeljs.io/T2954