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

@ -10,113 +10,113 @@ 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')
function asObj(str,done){ function asObj (str, done) {
if(str.toString) str = str.toString() if (str.toString) str = str.toString()
if(typeof str === 'string'){ if (typeof str === 'string') {
var obj var obj
try { try {
obj = JSON.parse(str) obj = JSON.parse(str)
} catch (e) { } catch (e) {
console.log('error parsing:',str,'Error:',e) console.log('error parsing:', str, 'Error:', e)
return done(e,undefined) return done(e, undefined)
} }
done(null,obj) done(null, obj)
} else { } else {
console.log('not string:',str) console.log('not string:', str)
done('not string: '+str,undefined) done('not string: ' + str, undefined)
} }
} }
function replyAsObj(res,isJson,done){ function replyAsObj (res, isJson, done) {
if(res.readable){ if (res.readable) {
// Is a stream // Is a stream
console.log('got stream') console.log('got stream')
res.setEncoding('utf8') res.setEncoding('utf8')
var data = '' var data = ''
res.on('data',d => { res.on('data', d => {
console.log('got stream data:',d) console.log('got stream data:', d)
data += d data += d
}) })
res.on('end',() => { res.on('end', () => {
if(isJson) { if (isJson) {
asObj(data,done) asObj(data, done)
} else { } else {
done(null,data) done(null, data)
} }
}) })
} else if(res.split || res.toString){ } else if (res.split || res.toString) {
//console.log('got string or buffer:',res) // console.log('got string or buffer:',res)
if(res.toString) res = res.toString() if (res.toString) res = res.toString()
// Is a string // Is a string
if(isJson){ if (isJson) {
asObj(res,done) asObj(res, done)
} else { } else {
done(null,res) done(null, res)
} }
} }
} }
function BoardsAPI(ipfs){ function BoardsAPI (ipfs) {
this.ipfs = ipfs this.ipfs = ipfs
this.version = 'ipfs:boards:version:dev' this.version = 'ipfs:boards:version:dev'
this.baseurl = '/ipfs-boards-profile/' this.baseurl = '/ipfs-boards-profile/'
this.users = [] // list of IPNS names this.users = [] // list of IPNS names
this.resolvingIPNS = {} this.resolvingIPNS = {}
this.ee = new EventEmitter() this.ee = new EventEmitter()
if(localStorage !== undefined){ if (window && window.localStorage !== undefined) {
// Use localStorage to store the IPNS cache // 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 { try {
this.users = JSON.parse(stored) this.users = JSON.parse(stored)
if(this.users === null || this.users === undefined || !this.users.indexOf || !this.users.push){ if (this.users === null || this.users === undefined || !this.users.indexOf || !this.users.push) {
this.users = [] this.users = []
} }
} catch(e){ } catch (e) {
this.users = [] this.users = []
} }
} }
} }
BoardsAPI.prototype.backupCache = function(){ BoardsAPI.prototype.backupCache = function () {
if(localStorage !== undefined){ if (window && window.localStorage !== undefined) {
// Use localStorage to store the IPNS cache // 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))
} }
} }
// Rewrote this to use event emitters. Should also add periodic rechecking // Rewrote this to use event emitters. Should also add periodic rechecking
BoardsAPI.prototype.resolveIPNS = function(n,handler){ BoardsAPI.prototype.resolveIPNS = function (n, handler) {
if(handler && handler.apply) this.ee.on(n,handler) if (handler && handler.apply) this.ee.on(n, handler)
if(!this.resolvingIPNS[n]){ if (!this.resolvingIPNS[n]) {
this.resolvingIPNS[n] = true this.resolvingIPNS[n] = true
this.ipfs.name.resolve(n,(err,r) => { this.ipfs.name.resolve(n, (err, r) => {
delete this.resolvingIPNS[n] delete this.resolvingIPNS[n]
if(err){ if (err) {
// Communicate error // Communicate error
this.ee.emit('error',err) this.ee.emit('error', err)
} else { } else {
var url = r.Path var url = r.Path
if(url === undefined){ if (url === undefined) {
console.log('Could not resolve',n) console.log('Could not resolve', n)
this.ee.emit('error',r.Message) this.ee.emit('error', r.Message)
} else if(this.users.indexOf(n) < 0){ // new find } else if (this.users.indexOf(n) < 0) { // new find
this.isUserProfile(url,(isit,err) => { this.isUserProfile(url, (isit, err) => {
if(isit){ if (isit) {
console.log(n,'is a user') console.log(n, 'is a user')
this.ee.emit(n,url) this.ee.emit(n, url)
if(this.users.indexOf(n) < 0){ if (this.users.indexOf(n) < 0) {
this.ee.emit('user',n,url) this.ee.emit('user', n, url)
this.users.push(n) this.users.push(n)
this.backupCache() this.backupCache()
} }
} else { } else {
console.log(n,'not a valid profile:',err) console.log(n, 'not a valid profile:', err)
this.ee.emit(n,undefined,'not a valid profile: '+err) this.ee.emit(n, undefined, 'not a valid profile: ' + err)
} }
return true // Remove from listeners return true // Remove from listeners
}) })
} else { // Already known } else { // Already known
this.ee.emit(n,url) this.ee.emit(n, url)
} }
} }
}) })
@ -124,44 +124,44 @@ BoardsAPI.prototype.resolveIPNS = function(n,handler){
return this.ee return this.ee
} }
BoardsAPI.prototype.isUserProfile = function(addr,done){ BoardsAPI.prototype.isUserProfile = function (addr, done) {
if(addr === undefined) return console.log('Asked to check if undefined is a profile') 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.ipfs.cat(addr + this.baseurl + 'ipfs-boards-version.txt', (err, r) => {
if(err) return done(false,err) if (err) return done(false, err)
replyAsObj(r,false,(_,res) => { replyAsObj(r, false, (_, res) => {
if(!res || !res.trim){ if (!res || !res.trim) {
console.log('Could not read version from',addr) console.log('Could not read version from', addr)
} else { } else {
var v = res.trim() var v = res.trim()
console.log('Version in profile snapshot',addr,'is',v) console.log('Version in profile snapshot', addr, 'is', v)
if(v === this.version){ if (v === this.version) {
done(true) done(true)
} else { } else {
done(false,'version mismatch: is "'+v+'" but should be "'+this.version+'"') done(false, 'version mismatch: is "' + v + '" but should be "' + this.version + '"')
} }
} }
}) })
}) })
} }
BoardsAPI.prototype.searchUsers = function(){ BoardsAPI.prototype.searchUsers = function () {
// Look at our peers // Look at our peers
this.ipfs.swarm.peers((err,r) => { this.ipfs.swarm.peers((err, r) => {
if(err) return console.log(err) if (err) return console.log(err)
replyAsObj(r,true,(e,reply) => { replyAsObj(r, true, (e, reply) => {
if(e){ if (e) {
this.ee.emit('error',e) this.ee.emit('error', e)
return console.log('There was an error while getting swarm peers:',e) return console.log('There was an error while getting swarm peers:', e)
} }
console.log('Checking',reply.Strings.length,'peers') console.log('Checking', reply.Strings.length, 'peers')
//reply.Strings.forEach(item => { // reply.Strings.forEach(item => {
var f = (item, done) => { var f = (item, done) => {
var ss = item.split('/') var ss = item.split('/')
var n = ss[ss.length-1] var n = ss[ss.length - 1]
this.ee.once(n,(res,err) => done(err)) this.ee.once(n, (res, err) => done(err))
this.resolveIPNS(n) this.resolveIPNS(n)
} }
asyncjs.eachSeries(reply.Strings,f.bind(this)) asyncjs.eachSeries(reply.Strings, f.bind(this))
}) })
}) })
// Look for who has the correct version file, they probably have a profile // Look for who has the correct version file, they probably have a profile
@ -178,33 +178,33 @@ BoardsAPI.prototype.searchUsers = function(){
return this.ee return this.ee
} }
BoardsAPI.prototype.getProfile = function(userID,done){ BoardsAPI.prototype.getProfile = function (userID, done) {
this.resolveIPNS(userID,(url,err) => { this.resolveIPNS(userID, (url, err) => {
if(err){ if (err) {
this.ee.emit('error',err) this.ee.emit('error', err)
done(err,null) done(err, null)
} else { } else {
// Download actual profile // Download actual profile
this.ipfs.cat(url+this.baseurl+'profile.json',(err2,res) => { this.ipfs.cat(url + this.baseurl + 'profile.json', (err2, res) => {
if(err2){ if (err2) {
this.ee.emit('error',err2) this.ee.emit('error', err2)
done(err2,null) done(err2, null)
} else { } else {
// TODO: JSON parse error handling // TODO: JSON parse error handling
var p = JSON.parse(res.toString()) var p = JSON.parse(res.toString())
this.ee.emit('profile for '+userID,p) this.ee.emit('profile for ' + userID, p)
done(null,p) done(null, p)
} }
}) })
// Get other info // Get other info
this.ipfs.ls(url+this.baseurl+'boards/',(err2,res) => { this.ipfs.ls(url + this.baseurl + 'boards/', (err2, res) => {
if(!err2){ if (!err2) {
var l = res.Objects[0].Links.map(i => { var l = res.Objects[0].Links.map(i => {
return { name: i.Name, hash: i.Hash } return { name: i.Name, hash: i.Hash }
}) })
this.ee.emit('boards for '+userID,l) this.ee.emit('boards for ' + userID, l)
} else { } else {
this.ee.emit('error',err2) this.ee.emit('error', err2)
} }
}) })
} }
@ -213,51 +213,58 @@ BoardsAPI.prototype.getProfile = function(userID,done){
return this.ee return this.ee
} }
BoardsAPI.prototype.getBoardSettings = function(userID,board){ BoardsAPI.prototype.getBoardSettings = function (userID, board) {
if(!userID){ if (!userID) {
return console.log('Invalid USERID',userID) return console.log('Invalid USERID', userID)
} }
if(!board){ if (!board) {
return console.log('Invalid BOARD',board) return console.log('Invalid BOARD', board)
} }
this.resolveIPNS(userID,(r,e) => { this.resolveIPNS(userID, (r, e) => {
if(e){ if (e) {
this.ee.emit('error',e) this.ee.emit('error', e)
} else { } else {
var url = r+this.baseurl+'boards/'+board+'/settings.json' var url = r + this.baseurl + 'boards/' + board + '/settings.json'
this.ipfs.cat(url,(err,resp) => { this.ipfs.cat(url, (err, resp) => {
// TODO: error handling json conversion // TODO: error handling json conversion
var settings = JSON.parse(resp.toString()) var settings = JSON.parse(resp.toString())
if(err){ if (err) {
this.ee.emit('error',err) this.ee.emit('error', err)
} else { } else {
// SETTINGS file is here, need to parse it a little bit // SETTINGS file is here, need to parse it a little bit
this.ee.emit('settings for '+board+'@'+userID,settings,r) this.ee.emit('settings for ' + board + '@' + userID, settings, r)
if(settings.whitelist == true){ if (settings.whitelist === true) {
// Get the whitelist // Get the whitelist
var url = r+this.baseurl+'boards/'+board+'/whitelist' var url = r + this.baseurl + 'boards/' + board + '/whitelist'
this.ipfs.cat(url,(err,res) => { this.ipfs.cat(url, (err, res) => {
if(err){ if (err) {
this.ee.emit('error',err) this.ee.emit('error', err)
// Emit an empty whitelist. // Emit an empty whitelist.
this.ee.emit('whitelist for '+board+'@'+userID,[]) this.ee.emit('whitelist for ' + board + '@' + userID, [])
} else replyAsObj(res,false,(err,whitelist) => { } else {
// Send whitelist replyAsObj(res, false, (err, whitelist) => {
var w = whitelist.split(' ').map(x => x.trim()) if (err) {
this.ee.emit('whitelist for '+board+'@'+userID,w) // 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 // Get the blacklist
var u = r+this.baseurl+'boards/'+board+'/blacklist' var u = r + this.baseurl + 'boards/' + board + '/blacklist'
this.ipfs.cat(u,(err,blacklist) => { this.ipfs.cat(u, (err, blacklist) => {
if(err){ if (err) {
this.ee.emit('error',err) this.ee.emit('error', err)
} else { } else {
// Send blacklist // Send blacklist
var w = blacklist.split(' ') var w = blacklist.split(' ')
this.emit('blacklist for '+board+'@'+userID,w) this.emit('blacklist for ' + board + '@' + userID, w)
} }
}) })
} }
@ -269,211 +276,218 @@ BoardsAPI.prototype.getBoardSettings = function(userID,board){
return this.ee return this.ee
} }
BoardsAPI.prototype.downloadPost = function(hash,adminID,board,op,done){ BoardsAPI.prototype.downloadPost = function (hash, adminID, board, op, done) {
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) {
this.ee.emit('error',err2) this.ee.emit('error', err2)
console.log('Could not download post',hash,'of',board+'@'+adminID) console.log('Could not download post', hash, 'of', board + '@' + adminID)
if(done && done.apply) done(err2) if (done && done.apply) done(err2)
} else { } else {
replyAsObj(r,true,(err,post) => { replyAsObj(r, true, (err, post) => {
if (err) return
// TODO: add JSON parsing error handling // TODO: add JSON parsing error handling
post.hash = hash post.hash = hash
if(op) post.op = op // Inject op if (op) post.op = op // Inject op
if(board){ if (board) {
if(adminID) this.ee.emit('post in '+board+'@'+adminID,post,hash) if (adminID) this.ee.emit('post in ' + board + '@' + adminID, post, hash)
else this.ee.emit('post in '+board,post,hash) else this.ee.emit('post in ' + board, post, hash)
} }
this.ee.emit(hash,post,adminID,board) this.ee.emit(hash, post, adminID, board)
if(done && done.apply) done(null,post) if (done && done.apply) done(null, post)
}) })
} }
}) })
return this.ee return this.ee
} }
BoardsAPI.prototype.retrieveListOfApproved = function(what,addr,adminID,board){ BoardsAPI.prototype.retrieveListOfApproved = function (what, addr, adminID, board) {
var a = addr+this.baseurl+'boards/'+board+'/approved/'+what+'/' var a = addr + this.baseurl + 'boards/' + board + '/approved/' + what + '/'
this.ipfs.ls(a,(err,res) => { this.ipfs.ls(a, (err, res) => {
if(err){ if (err) {
this.ee.emit('error',err) this.ee.emit('error', err)
} else { } else {
// Send approved posts list // Send approved posts list
var ret = res.Objects[0].Links.map(item => { var ret = res.Objects[0].Links.map(item => {
return { date: item.Name, hash: item.Hash } return { date: item.Name, hash: item.Hash }
}) })
this.emit('approved '+what+' for '+board+'@'+adminID,ret) this.emit('approved ' + what + ' for ' + board + '@' + adminID, ret)
} }
}) })
} }
BoardsAPI.prototype.getAllowedContentProducers = function(adminID,board,options){ BoardsAPI.prototype.getAllowedContentProducers = function (adminID, board, options) {
if(!options) return if (!options) return
this.ee.on('settings for '+board+'@'+adminID,function(settings,addr){ this.ee.on('settings for ' + board + '@' + adminID, function (settings, addr) {
// Get stuff based on settings // Get stuff based on settings
if(settings.approval_required == true){ if (settings.approval_required === true) {
// Get approved posts list // Get approved posts list
if(options.posts) this.retrieveListOfApproved('posts',addr,adminID,board) if (options.posts) this.retrieveListOfApproved('posts', addr, adminID, board)
// Get approved comments list // Get approved comments list
if(options.comments) this.retrieveListOfApproved('comments',addr,adminID,board) 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 // TODO: emit all whitelisted users
} else if(settings.blacklist == true){ } else if (settings.blacklist === true) {
// TODO: emit all users not in the blacklist // TODO: emit all users not in the blacklist
} }
}) })
this.getBoardSettings(adminID,board) this.getBoardSettings(adminID, board)
return this.ee return this.ee
} }
BoardsAPI.prototype.getPostsInBoard = function(adminID,board){ BoardsAPI.prototype.getPostsInBoard = function (adminID, board) {
if(adminID){ if (adminID) {
this.ee.on('approved posts for '+board+'@'+adminID,ret => { this.ee.on('approved posts for ' + board + '@' + adminID, ret => {
// Automatically download approved posts // Automatically download approved posts
ret.forEach(item => this.downloadPost(item.hash,adminID,board)) ret.forEach(item => this.downloadPost(item.hash, adminID, board))
}) })
this.ee.on('whitelist for '+board+'@'+adminID, whitelist => { this.ee.on('whitelist for ' + board + '@' + adminID, whitelist => {
// download posts for each user in whitelist // download posts for each user in whitelist
whitelist.forEach(item => { whitelist.forEach(item => {
this.getUserPostListInBoard(item,board,(err,postList) => { this.getUserPostListInBoard(item, board, (err, postList) => {
postList.forEach( i => this.downloadPost(i.hash,adminID,board,item)) if (err) return
postList.forEach(i => this.downloadPost(i.hash, adminID, board, item))
}) })
}) })
}) })
// Get allowed content and content producers // Get allowed content and content producers
this.getAllowedContentProducers(adminID,board,{ posts: true }) this.getAllowedContentProducers(adminID, board, { posts: true })
// Get the admin's posts // Get the admin's posts
this.getUserPostListInBoard(adminID,board,(err,res) => { this.getUserPostListInBoard(adminID, board, (err, res) => {
if(err){ if (err) {
console.log(err) console.log(err)
} else res.forEach(item => this.downloadPost(item.hash,adminID,board,adminID)) } else res.forEach(item => this.downloadPost(item.hash, adminID, board, adminID))
}) })
} else { } else {
// TODO: Download all posts in board from everyone // TODO: Download all posts in board from everyone
// Download my posts // Download my posts
this.getUserPostListInBoard(this.id,board,(err,res) => { this.getUserPostListInBoard(this.id, board, (err, res) => {
if(err){ if (err) {
console.log(err) console.log(err)
} else res.forEach(item => this.downloadPost(item.hash,undefined,board,this.id)) } else res.forEach(item => this.downloadPost(item.hash, undefined, board, this.id))
}) })
} }
return this.ee return this.ee
} }
BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){ BoardsAPI.prototype.getUserPostListInBoard = function (user, board, done) {
this.resolveIPNS(user,(url,err) => { this.resolveIPNS(user, (url, err) => {
if(err){ if (err) {
this.ee.emit('error',err) this.ee.emit('error', err)
done(err) done(err)
} else this.ipfs.ls(url+this.baseurl+'posts/'+board,(e,r) => { } else {
if(e){ this.ipfs.ls(url + this.baseurl + 'posts/' + board, (e, r) => {
this.ee.emit('error',e) if (e) {
done(e) this.ee.emit('error', e)
} else if(r && !r.split){ done(e)
console.log('Found',r.Objects[0].Links.length,'posts in',board,'at',user) } else if (r && !r.split) {
this.ee.emit('post count',board,user,r.Objects[0].Links.length) console.log('Found', r.Objects[0].Links.length, 'posts in', board, 'at', user)
var l = r.Objects[0].Links.map(i => { this.ee.emit('post count', board, user, r.Objects[0].Links.length)
return { date: i.Name, hash: i.Hash } var l = r.Objects[0].Links.map(i => {
}) return { date: i.Name, hash: i.Hash }
done(null,l) })
} done(null, l)
}) }
})
}
return true // remove myself from listeners return true // remove myself from listeners
}) })
return this.ee return this.ee
} }
BoardsAPI.prototype.downloadComment = function(hash,adminID,board,target,done){ BoardsAPI.prototype.downloadComment = function (hash, adminID, board, target, done) {
if(!done && typeof target == 'function'){ if (!done && typeof target === 'function') {
done = target done = target
target = undefined target = undefined
} }
console.log('target',target) console.log('target', target)
this.ipfs.cat(hash,(err2,r) => { this.ipfs.cat(hash, (err2, r) => {
if(err2){ if (err2) {
this.ee.emit('error',err2) this.ee.emit('error', err2)
console.log('Could not download comment',hash,'of',board+'@'+adminID) console.log('Could not download comment', hash, 'of', board + '@' + adminID)
if(done) done(err2) if (done) done(err2)
} else { } else {
// TODO: add JSON parsing error handling // TODO: add JSON parsing error handling
var cmnt = JSON.parse(r.toString()) var cmnt = JSON.parse(r.toString())
cmnt.hash = hash cmnt.hash = hash
if(target){ if (target) {
cmnt.original_parent = cmnt.parent cmnt.original_parent = cmnt.parent
cmnt.parent = target cmnt.parent = target
} }
this.ee.emit(hash,cmnt,adminID,board) this.ee.emit(hash, cmnt, adminID, board)
this.ee.emit('comment for '+(target || cmnt.parent),cmnt) this.ee.emit('comment for ' + (target || cmnt.parent), cmnt)
if(done) done(null,cmnt) if (done) done(null, cmnt)
} }
}) })
return this.ee return this.ee
} }
BoardsAPI.prototype.getCommentsFor = function(parent,board,adminID,target){ BoardsAPI.prototype.getCommentsFor = function (parent, board, adminID, target) {
if(!parent || !board || !adminID){ if (!parent || !board || !adminID) {
return console.log('malformed arguments:',parent,board,adminID) return console.log('malformed arguments:', parent, board, adminID)
} }
// figure out if there's a previous version of the item // figure out if there's a previous version of the item
this.ipfs.cat(parent, (err,res) => { this.ipfs.cat(parent, (err, res) => {
if(err){ if (err) {
this.ee.emit('error',err) this.ee.emit('error', err)
} else { } else {
replyAsObj(res,true,(err2,obj) => { replyAsObj(res, true, (err2, obj) => {
if(err2){ if (err2) {
this.ee.emit('error',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! // Also get comments for the previous version of the parent!
this.getCommentsFor(obj.previous,board,adminID,parent) this.getCommentsFor(obj.previous, board, adminID, parent)
} }
}) })
} }
}) })
// get the admin's comments // get the admin's comments
this.getUserCommentList(parent,adminID,(err,res) => { this.getUserCommentList(parent, adminID, (err, res) => {
if(!err){ if (!err) {
res.forEach(item => this.downloadComment(item.hash,adminID,board,target)) res.forEach(item => this.downloadComment(item.hash, adminID, board, target))
} }
}) })
// Download comments from whitelisted // Download comments from whitelisted
this.ee.on('whitelist for '+board+'@'+adminID, whitelist => { this.ee.on('whitelist for ' + board + '@' + adminID, whitelist => {
// download posts for each user in whitelist // download posts for each user in whitelist
whitelist.forEach(item => { whitelist.forEach(item => {
this.getUserCommentList(parent,item,(err,res) => { this.getUserCommentList(parent, item, (err, res) => {
res.forEach(i => this.downloadComment(i.hash,adminID,board,target)) if (err) return
res.forEach(i => this.downloadComment(i.hash, adminID, board, target))
}) })
}) })
}) })
// Handle approved comments // Handle approved comments
this.ee.on('approved comments for '+board+'@'+adminID,ret => { this.ee.on('approved comments for ' + board + '@' + adminID, ret => {
ret.forEach(item => this.downloadComment(item.hash,adminID,board,target)) ret.forEach(item => this.downloadComment(item.hash, adminID, board, target))
}) })
this.getAllowedContentProducers(adminID,board,{ comments: true }) this.getAllowedContentProducers(adminID, board, { comments: true })
} }
BoardsAPI.prototype.getUserCommentList = function(parent,user,done){ BoardsAPI.prototype.getUserCommentList = function (parent, user, done) {
if(!parent || !user){ if (!parent || !user) {
return console.log('Malformed arguments:',parent,user) return console.log('Malformed arguments:', parent, user)
} }
this.resolveIPNS(user,(url,err) => { this.resolveIPNS(user, (url, err) => {
if(err){ if (err) {
this.ee.emit('error',err) this.ee.emit('error', err)
done(err) done(err)
} else this.ipfs.ls(url+this.baseurl+'comments/'+parent,(e,r) => { } else {
if(e){ this.ipfs.ls(url + this.baseurl + 'comments/' + parent, (e, r) => {
this.ee.emit('error',e) if (e) {
done(e) this.ee.emit('error', e)
} else if(r && !r.split){ done(e)
if(r.Objects && r.Objects[0]){ // If this is not true, then there are no comments } else if (r && !r.split) {
console.log('Found',r.Objects[0].Links.length,'comments for',parent,'at',user) if (r.Objects && r.Objects[0]) { // If this is not true, then there are no comments
var l = r.Objects[0].Links.map(i => { console.log('Found', r.Objects[0].Links.length, 'comments for', parent, 'at', user)
return { date: i.Name, hash: i.Hash } var l = r.Objects[0].Links.map(i => {
}) return { date: i.Name, hash: i.Hash }
done(null,l) })
done(null, l)
}
} }
} })
}) }
return true // remove myself from listeners return true // remove myself from listeners
}) })
return this.ee return this.ee
@ -482,41 +496,41 @@ BoardsAPI.prototype.getUserCommentList = function(parent,user,done){
// API for publishing content and managing to be done later... // API for publishing content and managing to be done later...
// Initialize API // Initialize API
BoardsAPI.prototype.init = function(done){ BoardsAPI.prototype.init = function (done) {
if(this.isInit) return if (this.isInit) return
this.ipfs.id( (err, res) => { this.ipfs.id((err, res) => {
if(err){ if (err) {
console.log('Error while getting OWN ID:',err) console.log('Error while getting OWN ID:', err)
this.ee.emit('error',err) this.ee.emit('error', err)
this.ee.emit('init',err) this.ee.emit('init', err)
if(done && done.apply) done(err) if (done && done.apply) done(err)
} else if(res.ID){ } else if (res.ID) {
console.log('I am',res.ID) console.log('I am', res.ID)
this.id = res.ID this.id = res.ID
this.resolveIPNS(res.ID) this.resolveIPNS(res.ID)
console.log('Version is',this.version) console.log('Version is', this.version)
this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),{n: true},(err2,r) => { this.ipfs.add(new Buffer('ipfs:boards:version:' + this.version), {n: true}, (err2, r) => {
if(err2){ if (err2) {
this.ee.emit('error',err2) this.ee.emit('error', err2)
console.log('Error while calculating version hash:',err2) console.log('Error while calculating version hash:', err2)
this.ee.emit('init',err2) this.ee.emit('init', err2)
if(done && done.apply) done(err2) if (done && done.apply) done(err2)
} else { } else {
if(r && r.Hash) this.version_hash = r.Hash if (r && r.Hash) this.version_hash = r.Hash
if(r && r[0] && r[0].Hash) this.version_hash = r[0].Hash if (r && r[0] && r[0].Hash) this.version_hash = r[0].Hash
console.log('Version hash is',this.version_hash) console.log('Version hash is', this.version_hash)
this.ipfs.version((err,res) => { this.ipfs.version((err, res) => {
if(err){ if (err) {
this.ee.emit('error',err) this.ee.emit('error', err)
this.ee.emit('init',err) this.ee.emit('init', err)
console.log('Error while getting ipfs version:',err) console.log('Error while getting ipfs version:', err)
if(done && done.apply) done(err) if (done && done.apply) done(err)
} else { } else {
this.ipfs_version = res.Version this.ipfs_version = res.Version
console.log('IPFS Version is',res.Version) console.log('IPFS Version is', res.Version)
this.ee.emit('init',undefined) this.ee.emit('init', undefined)
this.isInit = true this.isInit = true
if(done && done.apply) done(null) if (done && done.apply) done(null)
} }
}) })
} }
@ -525,15 +539,15 @@ BoardsAPI.prototype.init = function(done){
}) })
} }
BoardsAPI.prototype.getEventEmitter = function(){ BoardsAPI.prototype.getEventEmitter = function () {
return this.ee return this.ee
} }
BoardsAPI.prototype.getUsers = function(){ BoardsAPI.prototype.getUsers = function () {
return this.users return this.users
} }
BoardsAPI.prototype.getMyID = function(){ BoardsAPI.prototype.getMyID = function () {
return this.id return this.id
} }

View File

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

View File

@ -1,11 +1,11 @@
var BoardsAPI = function(){ var BoardsAPI = function () {
this.done = false this.done = false
this.fa = [] this.fa = []
this.boards this.boards
require.ensure(['options.jsx','ipfs-api','boards-api.js'], _ => { require.ensure(['options.jsx', 'ipfs-api', 'boards-api.js'], _ => {
var opt = require('options.jsx').get() var opt = require('options.jsx').get()
var BoardsAPI = require('boards-api.js') var BoardsAPI = require('boards-api.js')
var ipfs = require('ipfs-api')(opt.addr || 'localhost',opt.port || 5001) var ipfs = require('ipfs-api')(opt.addr || 'localhost', opt.port || 5001)
this.boards = new BoardsAPI(ipfs) this.boards = new BoardsAPI(ipfs)
this.boards.init() this.boards.init()
this.done = true this.done = true
@ -14,9 +14,9 @@ var BoardsAPI = function(){
}) })
} }
BoardsAPI.prototype.use = function(f){ BoardsAPI.prototype.use = function (f) {
if(!f || !f.apply || !f.call) return console.log('Non-function tried to use API:',f) if (!f || !f.apply || !f.call) return console.log('Non-function tried to use API:', f)
if(this.done){ if (this.done) {
f(this.boards) f(this.boards)
} else { } else {
this.fa.push(f) this.fa.push(f)

View File

@ -2,34 +2,36 @@ var React = require('react')
var Icon = require('icon.jsx') var Icon = require('icon.jsx')
module.exports = React.createClass({ module.exports = React.createClass({
getInitialState: function(){ getInitialState: function () {
return { moment: false, text: '...' } return { moment: false, text: '...' }
}, },
componentDidMount: function(){ componentDidMount: function () {
require.ensure(['moment'],_ => { require.ensure(['moment'], _ => {
if(this.isMounted()){ if (this.isMounted()) {
var moment = require('moment') var moment = require('moment')
this.setState({ this.setState({
moment: moment, moment: moment,
interval: setInterval(this.upDate,60*1000), interval: setInterval(this.upDate, 60 * 1000),
text: moment.unix(this.props.date).fromNow() text: moment.unix(this.props.date).fromNow()
}) })
} }
}) })
}, },
upDate: function(){ upDate: function () {
if(this.isMounted()) if (this.isMounted()) {
this.setState({ text: this.state.moment.unix(this.props.date).fromNow() }) this.setState({ text: this.state.moment.unix(this.props.date).fromNow() })
else } else {
clearInterval(this.state.interval) clearInterval(this.state.interval)
}
}, },
getDate: function(){ getDate: function () {
if(this.state.moment) if (this.state.moment) {
return this.state.text return this.state.text
else } else {
return <Icon name="refresh" className="fa-spin" /> return <Icon name="refresh" className="fa-spin" />
}
}, },
render: function(){ render: function () {
return <div className="inline"><Icon name="clock-o" className={this.props.className} /> {this.getDate()}</div> return <div className="inline"><Icon name="clock-o" className={this.props.className} /> {this.getDate()}</div>
} }
}) })

View File

@ -6,33 +6,33 @@ var Link = require('react-router').Link
var UserID = require('userID.jsx') var UserID = require('userID.jsx')
var Comment = React.createClass({ var Comment = React.createClass({
getInitialState: function(){ getInitialState: function () {
return { moment: false } return { moment: false }
}, },
componentDidMount: function(){ componentDidMount: function () {
require.ensure(['moment'],_ => { require.ensure(['moment'], _ => {
if(this.isMounted()) this.setState({ moment: require('moment') }) if (this.isMounted()) this.setState({ moment: require('moment') })
}) })
}, },
getPermalink: function(){ getPermalink: function () {
if(this.props.adminID && this.props.board && this.props.post && this.props.comment.hash){ if (this.props.adminID && this.props.board && this.props.post && this.props.comment.hash) {
return <div className="inline not-first"> return <div className="inline not-first">
<Icon name="code" /> <Link to={'/@'+this.props.adminID+'/'+this.props.board+'/'+this.props.post+'/'+this.props.comment.hash}>Permalink</Link> <Icon name="code" /> <Link to={'/@' + this.props.adminID + '/' + this.props.board + '/' + this.props.post + '/' + this.props.comment.hash}>Permalink</Link>
</div> </div>
} }
}, },
getParentlink: function(){ getParentlink: function () {
if(this.props.showParent && this.props.comment.parent && this.props.comment.parent !== this.props.post){ if (this.props.showParent && this.props.comment.parent && this.props.comment.parent !== this.props.post) {
return <div className="inline not-first"> return <div className="inline not-first">
<Icon name="level-up" /> <Link to={'/@'+this.props.adminID+'/'+this.props.board+'/'+this.props.post+'/'+this.props.comment.parent}>Parent</Link> <Icon name="level-up" /> <Link to={'/@' + this.props.adminID + '/' + this.props.board + '/' + this.props.post + '/' + this.props.comment.parent}>Parent</Link>
</div> </div>
} }
}, },
getComments: function(){ getComments: function () {
return <Comments className="shifted" parent={this.props.comment.hash} post={this.props.post} adminID={this.props.adminID} board={this.props.board} api={this.props.api} /> return <Comments className="shifted" parent={this.props.comment.hash} post={this.props.post} adminID={this.props.adminID} board={this.props.board} api={this.props.api} />
}, },
render: function(){ render: function () {
if(this.props.comment){ if (this.props.comment) {
return <div className="comment"><hr/> return <div className="comment"><hr/>
<div className="icons"> <div className="icons">
<UserID id={this.props.comment.op} api={this.props.api} /> <UserID id={this.props.comment.op} api={this.props.api} />
@ -49,32 +49,33 @@ var Comment = React.createClass({
}) })
var Comments = React.createClass({ var Comments = React.createClass({
getInitialState: function(){ getInitialState: function () {
return { comments: [] } return { comments: [] }
}, },
componentDidMount: function(){ componentDidMount: function () {
var boards = this.props.api var boards = this.props.api
if(boards){ if (boards) {
boards.getEventEmitter().on('comment for '+this.props.parent,cmnt => { boards.getEventEmitter().on('comment for ' + this.props.parent, cmnt => {
if(this.isMounted()) this.setState({ comments: this.state.comments.concat(cmnt) }) if (this.isMounted()) this.setState({ comments: this.state.comments.concat(cmnt) })
}) })
if(boards.isInit && this.isMounted()){ if (boards.isInit && this.isMounted()) {
boards.getCommentsFor(this.props.parent,this.props.board,this.props.adminID) boards.getCommentsFor(this.props.parent, this.props.board, this.props.adminID)
} }
boards.getEventEmitter().on('init', err => { boards.getEventEmitter().on('init', err => {
if(!err && this.isMounted()) if (!err && this.isMounted()) {
boards.getCommentsFor(this.props.parent,this.props.board,this.props.adminID) boards.getCommentsFor(this.props.parent, this.props.board, this.props.adminID)
}
}) })
} }
}, },
getComments: function(){ getComments: function () {
if(this.state.comments.length > 0){ if (this.state.comments.length > 0) {
return this.state.comments.map(cmnt => (<Comment key={cmnt.hash} comment={cmnt} post={this.props.post} adminID={this.props.adminID} board={this.props.board} api={this.props.api} />) ) return this.state.comments.map(cmnt => (<Comment key={cmnt.hash} comment={cmnt} post={this.props.post} adminID={this.props.adminID} board={this.props.board} api={this.props.api} />))
} }
else return <div></div> else return <div></div>
}, },
render: function(){ render: function () {
return <div className={this.props.className+' comments'} >{this.getComments()}</div> return <div className={this.props.className + ' comments'} >{this.getComments()}</div>
} }
}) })

View File

@ -2,10 +2,10 @@ var React = require('react')
require('font-awesome.min.css') require('font-awesome.min.css')
module.exports = React.createClass({ module.exports = React.createClass({
class: function(){ class: function () {
return 'fa fa-'+this.props.name+' '+this.props.className return 'fa fa-' + this.props.name + ' ' + this.props.className
}, },
render: function(){ render: function () {
return ( <i className={this.class()}></i> ) return (<i className={this.class()}></i>)
} }
}) })

View File

@ -1,17 +1,17 @@
var React = require('react') var React = require('react')
module.exports = React.createClass({ module.exports = React.createClass({
getInitialState: function(){ getInitialState: function () {
return { lib: false } return { lib: false }
}, },
componentDidMount: function(){ componentDidMount: function () {
require.ensure(['react-markdown'],_ => { require.ensure(['react-markdown'], _ => {
if(this.isMounted()) this.setState({ MarkdownLib: require('react-markdown') }) if (this.isMounted()) this.setState({ MarkdownLib: require('react-markdown') })
}) })
}, },
renderIfApplicable: function(){ renderIfApplicable: function () {
if(this.props.source){ if (this.props.source) {
if(this.state.MarkdownLib){ if (this.state.MarkdownLib) {
var MarkdownLib = this.state.MarkdownLib var MarkdownLib = this.state.MarkdownLib
return <MarkdownLib source={this.props.source} skipHtml={true} /> return <MarkdownLib source={this.props.source} skipHtml={true} />
} else { } else {
@ -19,7 +19,7 @@ module.exports = React.createClass({
} }
} else return <p>...</p> } else return <p>...</p>
}, },
render: function(){ render: function () {
return this.renderIfApplicable() return this.renderIfApplicable()
} }
}) })

View File

@ -2,17 +2,17 @@ var React = require('react')
var Icon = require('icon.jsx') var Icon = require('icon.jsx')
var Link = require('react-router').Link var Link = require('react-router').Link
module.exports = function(boardsAPI){ module.exports = function (boardsAPI) {
return React.createClass({ return React.createClass({
getInitialState: function(){ getInitialState: function () {
return { api: false, loading: true } return { api: false, loading: true }
}, },
componentDidMount(){ componentDidMount () {
boardsAPI.use(boards => { boardsAPI.use(boards => {
if(boards.isInit) this.setState({ api: true }) if (boards.isInit) this.setState({ api: true })
boards.getEventEmitter().on('init',err => { boards.getEventEmitter().on('init', err => {
if(!this.isMounted()) return if (!this.isMounted()) return
if(err){ if (err) {
this.setState({ loading: false, api: false }) this.setState({ loading: false, api: false })
} else { } else {
this.setState({ api: true }) this.setState({ api: true })
@ -20,19 +20,19 @@ module.exports = function(boardsAPI){
}) })
}) })
}, },
extraButtons: function(){ extraButtons: function () {
if(this.state.api){ if (this.state.api) {
return <span> return <span>
<Link className="nounderline" to="/@me"><Icon name="user" className="fa-2x light"/></Link> <Link className="nounderline" to="/@me"><Icon name="user" className="fa-2x light"/></Link>
<Link className="nounderline" to="/users"><Icon name="globe" className="fa-2x light"/></Link> <Link className="nounderline" to="/users"><Icon name="globe" className="fa-2x light"/></Link>
</span> </span>
} else if(this.state.loading){ } else if (this.state.loading) {
return <Icon name="refresh" className="fa-2x fa-spin light"/> return <Icon name="refresh" className="fa-2x fa-spin light"/>
} else { } else {
return <Link className="nounderline" to="/users"><Icon name="ban" className="fa-2x light"/></Link> return <Link className="nounderline" to="/users"><Icon name="ban" className="fa-2x light"/></Link>
} }
}, },
render: function(){ render: function () {
return ( return (
<div className="navbar"> <div className="navbar">
<div className="container"> <div className="container">

View File

@ -1,12 +1,13 @@
module.exports = { module.exports = {
get: function(){ get: function () {
var opt, s = localStorage.getItem('ipfs-boards-settings') var opt
var s = window.localStorage.getItem('ipfs-boards-settings')
try { try {
opt = JSON.parse(s) opt = JSON.parse(s)
} catch(e){ } catch (e) {
// Do nothing // Do nothing
} }
if(opt === null || opt === undefined) opt = { addr: 'localhost', port: 5001 } if (opt === null || opt === undefined) opt = { addr: 'localhost', port: 5001 }
return opt return opt
} }
} }

View File

@ -6,26 +6,26 @@ var Clock = require('clock.jsx')
var UserID = require('userID.jsx') var UserID = require('userID.jsx')
module.exports = React.createClass({ module.exports = React.createClass({
getInitialState: function(){ getInitialState: function () {
return { moment: false } return { moment: false }
}, },
componentDidMount: function(){ componentDidMount: function () {
require.ensure(['moment'],_ => { require.ensure(['moment'], _ => {
if(this.isMounted()) this.setState({ moment: require('moment') }) if (this.isMounted()) this.setState({ moment: require('moment') })
}) })
}, },
postLink: function(){ postLink: function () {
if(this.props.post.op){ if (this.props.post.op) {
if(this.props.board){ if (this.props.board) {
return '/@'+this.props.post.op+'/'+this.props.board+'/'+this.props.post.hash return '/@' + this.props.post.op + '/' + this.props.board + '/' + this.props.post.hash
} else { } else {
return '/@'+this.props.post.op+'/post/'+this.props.post.hash return '/@' + this.props.post.op + '/post/' + this.props.post.hash
} }
} else { } else {
return '/post/'+this.props.post.hash return '/post/' + this.props.post.hash
} }
}, },
render: function(){ render: function () {
return <div key={this.props.post.title} className="post"> return <div key={this.props.post.title} className="post">
<div className="content"> <div className="content">
<h5>{this.props.post.title}</h5><hr/> <h5>{this.props.post.title}</h5><hr/>

View File

@ -4,51 +4,57 @@ var Icon = require('icon.jsx')
var Post = require('post.jsx') var Post = require('post.jsx')
module.exports = React.createClass({ module.exports = React.createClass({
getInitialState: function(){ getInitialState: function () {
return { posts: [], api: false } return { posts: [], api: false }
}, },
sortFn: function(a,b){ sortFn: function (a, b) {
return (b.date || 0) - (a.date || 0) return (b.date || 0) - (a.date || 0)
}, },
init: function(boards){ init: function (boards) {
if(this.state.init) return if (this.state.init) return
this.setState({ api: true }) this.setState({ api: true })
boards.getPostsInBoard(this.props.admin,this.props.board) var onPost = (post, hash) => {
.on('post in '+this.props.board+(this.props.admin?'@'+this.props.admin:''),(post,hash) => { if (!this.isMounted()) return true
if(!this.isMounted()) return true
var now = (new Date()).getTime() var now = (new Date()).getTime()
var posts = this.state.posts var posts = this.state.posts
if(post.date === undefined || post.date <= 0){ if (post.date === undefined || post.date <= 0) {
posts.push(post) posts.push(post)
} else if(post.date <= now){ } else if (post.date <= now) {
var i = sortedIndex(posts,post,(p) => now-p.date || now) var i = sortedIndex(posts, post, (p) => now - p.date || now)
posts.splice(i,0,post) posts.splice(i, 0, post)
} else { } else {
console.log('Post discarded cause date in the future:',post) console.log('Post discarded cause date in the future:', post)
} }
this.setState({ posts }) 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 }) this.setState({ init: true })
}, },
componentDidMount: function(){ componentDidMount: function () {
var boards = this.props.api var boards = this.props.api
if(boards){ if (boards) {
if(boards.isInit) this.init(boards) if (boards.isInit) {
else boards.getEventEmitter().on('init',err => { this.init(boards)
if(!err && this.isMounted()) this.init(boards) } else {
}) boards.getEventEmitter().on('init', err => {
if (!err && this.isMounted()) this.init(boards)
})
}
} }
}, },
getPosts: function(){ getPosts: function () {
if(this.state.posts.length > 0 || this.state.api){ if (this.state.posts.length > 0 || this.state.api) {
return this.state.posts.map(post => { 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} /> 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 {
<Icon name="refresh" className="fa-3x center-block light fa-spin" /> return <div className="center-block text-center">
</div> <Icon name="refresh" className="fa-3x center-block light fa-spin" />
</div>
}
}, },
render: function(){ render: function () {
return ( return (
<div className="postList"> <div className="postList">
{this.getPosts()} {this.getPosts()}

View File

@ -3,49 +3,50 @@ var Icon = require('icon.jsx')
var Link = require('react-router').Link var Link = require('react-router').Link
module.exports = React.createClass({ module.exports = React.createClass({
getInitialState: function(){ getInitialState: function () {
return { } return { }
}, },
componentDidMount: function(){ componentDidMount: function () {
var boards = this.props.api var boards = this.props.api
if(boards){ if (boards) {
if(boards.isInit){ if (boards.isInit) {
this.getProfile(boards) this.getProfile(boards)
} }
boards.getEventEmitter().on('init',err => { boards.getEventEmitter().on('init', err => {
if(!err && this.isMounted()) this.getProfile(boards) if (!err && this.isMounted()) this.getProfile(boards)
else console.log('ERR INIT',err) else console.log('ERR INIT', err)
}) })
} }
}, },
getProfile: function(boards){ getProfile: function (boards) {
if(this.props.id === undefined) return if (this.props.id === undefined) return
boards.getProfile(this.props.id, (err,res) => { boards.getProfile(this.props.id, (err, res) => {
if(!this.isMounted()) return true if (!this.isMounted()) return true
if(err){ if (err) {
console.log('Error while resolving user badge:',err) console.log('Error while resolving user badge:', err)
} else { } else {
this.setState({ name: res.name || 'Unknown Name' }) this.setState({ name: res.name || 'Unknown Name' })
} }
}) })
}, },
getContent: function(){ getContent: function () {
if(this.state.name){ if (this.state.name) {
return (<Icon name="user" />) return (<Icon name="user" />)
} else { } else {
return '@' return '@'
} }
}, },
render: function(){ 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"> return <div className="user-id">
<Icon name="ban" /> Unknown User <Icon name="ban" /> Unknown User
</div> </div>
else } else {
return (<div className="user-id"> return (<div className="user-id">
<Link className="light nounderline" to={'/@'+this.props.id}> <Link className="light nounderline" to={'/@' + this.props.id}>
{this.getContent()}{this.state.name || this.props.id} {this.getContent()}{this.state.name || this.props.id}
</Link> </Link>
</div>) </div>)
}
} }
}) })

View File

@ -1,17 +1,15 @@
var React = require('react') var React = require('react')
var Markdown = require('markdown.jsx') var Markdown = require('markdown.jsx')
var Link = require('react-router').Link
var Icon = require('icon.jsx')
var UserID = require('userID.jsx') var UserID = require('userID.jsx')
var PostList = require('postlist.jsx') var PostList = require('postlist.jsx')
var GetIPFS = require('getipfs.jsx') var GetIPFS = require('getipfs.jsx')
module.exports = function(boardsAPI){ module.exports = function (boardsAPI) {
return React.createClass({ return React.createClass({
getInitialState: function(){ getInitialState: function () {
return { name: this.props.params.boardname, api: false, whitelist: [] } return { name: this.props.params.boardname, api: false, whitelist: [] }
}, },
componentDidMount: function(){ componentDidMount: function () {
boardsAPI.use(boards => { boardsAPI.use(boards => {
/* /*
When a component inside the component being rendered by the router also needs When a component inside the component being rendered by the router also needs
@ -20,44 +18,45 @@ module.exports = function(boardsAPI){
when the root component mounts) works as a cheap, horrible workaround when the root component mounts) works as a cheap, horrible workaround
*/ */
boards.init() boards.init()
if(!this.isMounted()) return if (!this.isMounted()) return
var ee = boards.getEventEmitter() var ee = boards.getEventEmitter()
ee.on('init',err => { ee.on('init', err => {
if(!err && this.isMounted()){ if (!err && this.isMounted()) {
this.init(boards) this.init(boards)
} }
}) })
if(this.props.params.userid){ if (this.props.params.userid) {
ee.on('whitelist for '+this.props.params.boardname+'@'+this.props.params.userid,(whitelist) => { ee.on('whitelist for ' + this.props.params.boardname + '@' + this.props.params.userid, (whitelist) => {
if(this.isMounted()) if (this.isMounted()) {
this.setState({ whitelist }) this.setState({ whitelist })
else return true } else return true
}) })
ee.on('settings for '+this.props.params.boardname+'@'+this.props.params.userid, (res) => { ee.on('settings for ' + this.props.params.boardname + '@' + this.props.params.userid, (res) => {
if(!this.isMounted()) return true if (!this.isMounted()) return true
if(res) this.setState({ name: res.fullname, description: res.description }) if (res) this.setState({ name: res.fullname, description: res.description })
}) })
} else { } else {
this.setState({ description: 'All the messages posted in __#'+this.props.params.boardname+'__' }) this.setState({ description: 'All the messages posted in __#' + this.props.params.boardname + '__' })
} }
if(boards.isInit || this.state.api){ if (boards.isInit || this.state.api) {
this.init(boards) this.init(boards)
} }
}) })
}, },
init: function(boards){ init: function (boards) {
if(!this.state.init){ if (!this.state.init) {
if(this.props.params.userid) if (this.props.params.userid) {
boards.getBoardSettings(this.props.params.userid,this.props.params.boardname) boards.getBoardSettings(this.props.params.userid, this.props.params.boardname)
}
this.setState({ init: true, api: true, boards: boards }) this.setState({ init: true, api: true, boards: boards })
} }
}, },
render: function(){ render: function () {
if(this.state.api){ if (this.state.api) {
return (<div className="board"> return (<div className="board">
<h2>{this.state.name}</h2> <h2>{this.state.name}</h2>
<Markdown source={this.state.description} skipHtml={true} /> <Markdown source={this.state.description} skipHtml={true} />
{this.props.params.userid?<h5><UserID id={this.props.params.userid} api={this.state.boards} /></h5>:<p></p>} {this.props.params.userid ? <h5><UserID id={this.props.params.userid} api={this.state.boards} /></h5> : <p></p>}
<div className="whitelist"> <div className="whitelist">
{this.state.whitelist.map(i => <UserID id={i} key={i} api={this.state.boards} />)} {this.state.whitelist.map(i => <UserID id={i} key={i} api={this.state.boards} />)}
</div> </div>

View File

@ -3,55 +3,57 @@ var Link = require('react-router').Link
var Icon = require('icon.jsx') var Icon = require('icon.jsx')
var UserID = require('userID.jsx') var UserID = require('userID.jsx')
var GetIPFS = require('getipfs.jsx') var GetIPFS = require('getipfs.jsx')
var Post = require('post.jsx')
var Comment = require('comment.jsx').Comment var Comment = require('comment.jsx').Comment
module.exports = function(boardsAPI){ module.exports = function (boardsAPI) {
return React.createClass({ return React.createClass({
getInitialState: function(){ getInitialState: function () {
return { parent: false, api: false } return { parent: false, api: false }
}, },
componentDidMount: function(){ componentDidMount: function () {
boardsAPI.use(boards => { boardsAPI.use(boards => {
boards.init() boards.init()
boards.getEventEmitter().on('init', err => { boards.getEventEmitter().on('init', err => {
if(!err && this.isMounted()){ if (!err && this.isMounted()) {
this.init(boards) this.init(boards)
} }
}) })
if(this.isMounted() && boards.isInit){ if (this.isMounted() && boards.isInit) {
this.init(boards) this.init(boards)
} }
}) })
}, },
componentWillReceiveProps: function(nextProps) { componentWillReceiveProps: function (nextProps) {
boardsAPI.use(boards => this.downloadComment(boards,nextProps)) boardsAPI.use(boards => this.downloadComment(boards, nextProps))
}, },
downloadComment: function(boards,props){ downloadComment: function (boards, props) {
this.setState({ comment: false }) this.setState({ comment: false })
boards.downloadComment(props.params.commenthash,props.params.userid,props.params.boardname, (err,comment) => { boards.downloadComment(props.params.commenthash, props.params.userid, props.params.boardname, (err, comment) => {
if(err){ if (err) {
this.setState({ comment: { title: 'Error', text: err.Message || err.Error }}) this.setState({
comment: { title: 'Error', text: err.Message || err.Error }
})
} else { } else {
this.setState({ comment }) this.setState({ comment })
} }
}) })
}, },
init: function(boards){ init: function (boards) {
if(this.state.init) return if (this.state.init) return
this.setState({ api: true, boards: boards }) this.setState({ api: true, boards: boards })
this.downloadComment(boards,this.props) this.downloadComment(boards, this.props)
}, },
getContext: function(){ getContext: function () {
if(this.props.params.userid){ 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> 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> 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> } else return <div><h6 className="light">You are viewing a single comment</h6></div>
}, },
showComment: function(){ showComment: function () {
if(this.state.comment){ if (this.state.comment) {
return <Comment comment={this.state.comment} post={this.props.params.posthash} adminID={this.props.params.userid} board={this.props.params.boardname} showParent={true} api={this.state.boards} /> return <Comment comment={this.state.comment} post={this.props.params.posthash} adminID={this.props.params.userid} board={this.props.params.boardname} showParent={true} api={this.state.boards} />
} else { } else {
return <div className="center-block text-center find-content"> return <div className="center-block text-center find-content">
@ -60,15 +62,17 @@ module.exports = function(boardsAPI){
</div> </div>
} }
}, },
render: function(){ render: function () {
if(this.state.api) if (this.state.api) {
return <div className="comment-page"> return <div className="comment-page">
<div className="text-center"> <div className="text-center">
{this.getContext()} {this.getContext()}
</div> </div>
{this.showComment()} {this.showComment()}
</div> </div>
else return <GetIPFS api={this.state.boards} /> } else {
return <GetIPFS api={this.state.boards} />
}
} }
}) })
} }

View File

@ -3,19 +3,19 @@ var Link = require('react-router').Link
var Icon = require('icon.jsx') var Icon = require('icon.jsx')
module.exports = React.createClass({ module.exports = React.createClass({
getInitialState: function(){ getInitialState: function () {
return { connected: false, error: false, long: false } return { connected: false, error: false, long: false }
}, },
componentDidMount: function(){ componentDidMount: function () {
var boards = this.props.api var boards = this.props.api
if(boards){ if (boards) {
if(!this.isMounted()) return if (!this.isMounted()) return
if(boards.isInit){ if (boards.isInit) {
this.setState({ connected: true }) this.setState({ connected: true })
} else { } else {
boards.getEventEmitter().on('init', err => { boards.getEventEmitter().on('init', err => {
if(!this.isMounted()) return if (!this.isMounted()) return
if(err){ if (err) {
this.setState({ error: true }) this.setState({ error: true })
} else { } else {
this.setState({ connected: true }) this.setState({ connected: true })
@ -24,15 +24,15 @@ module.exports = React.createClass({
} }
} else this.startTimer() } else this.startTimer()
}, },
startTimer: function(){ startTimer: function () {
setTimeout(_ => { setTimeout(_ => {
console.log('Connection to go-ipfs has timed out (probably due to CORS)') console.log('Connection to go-ipfs has timed out (probably due to CORS)')
if(this.isMounted()) this.setState({ long: true }) if (this.isMounted()) this.setState({ long: true })
}, 5000) }, 5000)
}, },
render: function(){ render: function () {
var opt = require('options.jsx').get() var opt = require('options.jsx').get()
if(this.state.error || this.state.long){ if (this.state.error || this.state.long) {
return ( return (
<div className=""> <div className="">
<h1><Icon name="ban"/> Connection to IPFS not available</h1> <h1><Icon name="ban"/> Connection to IPFS not available</h1>
@ -51,7 +51,7 @@ module.exports = React.createClass({
</ul> </ul>
<p>Still can't fix it? <a href="https://github.com/fazo96/ipfs-board/issues">File a issue on GitHub</a>, we'll be happy to help!</p> <p>Still can't fix it? <a href="https://github.com/fazo96/ipfs-board/issues">File a issue on GitHub</a>, we'll be happy to help!</p>
</div> </div>
)} else if(this.state.connected){ ) } else if (this.state.connected) {
return <div class="text-center"> return <div class="text-center">
<h1><Icon name="check" /></h1> <h1><Icon name="check" /></h1>
<h5 class="light">You're connected!</h5> <h5 class="light">You're connected!</h5>

View File

@ -5,51 +5,54 @@ var GetIPFS = require('getipfs.jsx')
var Post = require('post.jsx') var Post = require('post.jsx')
var Comments = require('comment.jsx').Comments var Comments = require('comment.jsx').Comments
module.exports = function(boardsAPI){ module.exports = function (boardsAPI) {
return React.createClass({ return React.createClass({
getInitialState: function(){ getInitialState: function () {
return { post: { title: '...', text: '...' }, api: false } return { post: { title: '...', text: '...' }, api: false }
}, },
componentDidMount: function(){ componentDidMount: function () {
boardsAPI.use(boards => { boardsAPI.use(boards => {
boards.init() boards.init()
boards.getEventEmitter().on('init', err => { boards.getEventEmitter().on('init', err => {
if(!err && this.isMounted()){ if (!err && this.isMounted()) {
this.init(boards) this.init(boards)
} }
}) })
if(this.isMounted() && boards.isInit){ if (this.isMounted() && boards.isInit) {
this.init(boards) this.init(boards)
} }
}) })
}, },
componentWillReceiveProps: function(nextProps) { componentWillReceiveProps: function (nextProps) {
boardsAPI.use(boards => this.downloadPost(boards,nextProps)) boardsAPI.use(boards => this.downloadPost(boards, nextProps))
}, },
downloadPost: function(boards,props){ downloadPost: function (boards, props) {
boards.downloadPost(props.params.posthash,props.params.userid,props.params.boardname,props.params.userid,(err,post) => { boards.downloadPost(props.params.posthash, props.params.userid, props.params.boardname, props.params.userid, (err, post) => {
if(err){ if (err) {
this.setState({ post: { title: 'Error', text: err.Message || err.Error }}) this.setState({
post: { title: 'Error', text: err.Message || err.Error }
})
} else { } else {
this.setState({ post }) this.setState({ post })
} }
}) })
}, },
init: function(boards){ init: function (boards) {
if(this.state.init) return if (this.state.init) return
this.setState({ api: true, boards: boards }) this.setState({ api: true, boards: boards })
this.downloadPost(boards,this.props) this.downloadPost(boards, this.props)
}, },
getContext: function(){ getContext: function () {
if(this.props.params.userid){ 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> 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> 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> } else return <div><h6 className="light">You are viewing a single post</h6></div>
}, },
render: function(){ render: function () {
if(this.state.api) if (this.state.api) {
return <div className="post-page"> return <div className="post-page">
<div className="text-center"> <div className="text-center">
{this.getContext()} {this.getContext()}
@ -57,7 +60,9 @@ module.exports = function(boardsAPI){
<Post post={this.state.post} board={this.props.params.boardname} api={this.state.boards} /> <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} /> <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> </div>
else return <GetIPFS api={this.state.boards} /> } else {
return <GetIPFS api={this.state.boards} />
}
} }
}) })
} }

View File

@ -4,42 +4,42 @@ var Link = require('react-router').Link
var Icon = require('icon.jsx') var Icon = require('icon.jsx')
var GetIPFS = require('getipfs.jsx') var GetIPFS = require('getipfs.jsx')
module.exports = function(boardsAPI){ module.exports = function (boardsAPI) {
return React.createClass({ return React.createClass({
getInitialState: function(){ getInitialState: function () {
return { name: '...', boards: [], api: false } return { name: '...', boards: [], api: false }
}, },
componentDidMount: function(){ componentDidMount: function () {
boardsAPI.use(boards => { boardsAPI.use(boards => {
if(boards.isInit){ if (boards.isInit) {
this.setState({ api: boards, id: boards.id }) this.setState({ api: boards, id: boards.id })
this.init(boards) this.init(boards)
} }
var ee = boards.getEventEmitter() var ee = boards.getEventEmitter()
ee.on('init',err => { ee.on('init', err => {
if(!err && this.isMounted()){ if (!err && this.isMounted()) {
this.setState({ api: boards, id: boards.id }) this.setState({ api: boards, id: boards.id })
this.init(boards) this.init(boards)
} }
}) })
}) })
}, },
componentWillReceiveProps: function(nextProps) { componentWillReceiveProps: function (nextProps) {
boardsAPI.use(boards => this.downloadProfile(boards,nextProps)) boardsAPI.use(boards => this.downloadProfile(boards, nextProps))
}, },
downloadProfile: function(boards,props){ downloadProfile: function (boards, props) {
var ee = boards.getEventEmitter() var ee = boards.getEventEmitter()
var uid = props.params.userid var uid = props.params.userid
if(uid === 'me') uid = boards.id if (uid === 'me') uid = boards.id
ee.on('boards for '+uid,l => { ee.on('boards for ' + uid, l => {
var u2id = props.params.userid var u2id = props.params.userid
if(u2id === 'me') u2id = boards.id if (u2id === 'me') u2id = boards.id
if(!this.isMounted() || u2id !== uid) return true if (!this.isMounted() || u2id !== uid) return true
this.setState({ boards: l }) this.setState({ boards: l })
}) })
boards.getProfile(uid,(err,res) => { boards.getProfile(uid, (err, res) => {
if(!this.isMounted()) return true if (!this.isMounted()) return true
if(err){ if (err) {
this.setState({ this.setState({
name: <Icon name="ban" />, name: <Icon name="ban" />,
description: err description: err
@ -49,19 +49,17 @@ module.exports = function(boardsAPI){
} }
}) })
}, },
init: function(boards){ init: function (boards) {
if(this.state.init) return if (this.state.init) return
var ee = boards.getEventEmitter() if (boards.isInit || this.state.api) {
if(boards.isInit || this.state.api){ this.downloadProfile(boards, this.props)
var uid = this.props.params.userid
this.downloadProfile(boards,this.props)
this.setState({ init: true }) this.setState({ init: true })
} }
}, },
linkToEditor: function(){ linkToEditor: function () {
var uid = this.props.params.userid var uid = this.props.params.userid
if(uid === 'me' && this.state.id) uid = this.state.id if (uid === 'me' && this.state.id) uid = this.state.id
if(uid === this.state.id){ if (uid === this.state.id) {
return <div> return <div>
<h6>This is your profile</h6> <h6>This is your profile</h6>
<hr/> <hr/>
@ -69,10 +67,10 @@ module.exports = function(boardsAPI){
} }
return '' return ''
}, },
render: function(){ render: function () {
if(this.state.api){ if (this.state.api) {
var uid = this.props.params.userid var uid = this.props.params.userid
if(uid === 'me') uid = this.state.id if (uid === 'me') uid = this.state.id
return (<div className="profile"> return (<div className="profile">
{this.linkToEditor()} {this.linkToEditor()}
<h1>{this.state.name}</h1> <h1>{this.state.name}</h1>
@ -80,8 +78,8 @@ module.exports = function(boardsAPI){
<hr/> <hr/>
<div className="light breaker">@{uid}</div> <div className="light breaker">@{uid}</div>
{this.state.boards.map(n => { {this.state.boards.map(n => {
return <h6 className="light" key={uid+'/'+n.name}> return <h6 className="light" key={uid + '/' + n.name}>
<Link to={'/@'+uid+'/'+n.name}># {n.name}</Link> <Link to={'/@' + uid + '/' + n.name}># {n.name}</Link>
</h6> </h6>
})} })}
</div>) </div>)

View File

@ -1,57 +1,57 @@
var React = require('react') var React = require('react')
var Icon = require('icon.jsx') var Icon = require('icon.jsx')
module.exports = function(boardsAPI){ module.exports = function (boardsAPI) {
return React.createClass({ return React.createClass({
getDefaults: function(){ getDefaults: function () {
return { addr: 'localhost', port: 5001, api: false } return { addr: 'localhost', port: 5001, api: false }
}, },
getInitialState: function(){ getInitialState: function () {
boardsAPI.use(boards => { boardsAPI.use(boards => {
if(boards.isInit && this.isMounted()) this.setState({ api: true }) if (boards.isInit && this.isMounted()) this.setState({ api: true })
boards.getEventEmitter().on('init', err => { boards.getEventEmitter().on('init', err => {
if(!err && this.isMounted()) this.setState({ api: true }) 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() var obj = this.getDefaults()
try { try {
obj = JSON.parse(s) obj = JSON.parse(s)
} catch(e){ } catch (e) {
localStorage.removeItem('ipfs-boards-settings') window.localStorage.removeItem('ipfs-boards-settings')
} }
return obj || this.getDefaults() return obj || this.getDefaults()
}, },
save: function(){ save: function () {
if(isNaN(this.state.port) || parseInt(this.state.port) > 65535 || parseInt(this.state.port) < 1){ if (isNaN(this.state.port) || parseInt(this.state.port, 10) > 65535 || parseInt(this.state.port, 10) < 1) {
alert('Port number invalid') window.alert('Port number invalid')
} else { } else {
localStorage.setItem('ipfs-boards-settings',JSON.stringify({ window.localStorage.setItem('ipfs-boards-settings', JSON.stringify({
addr: this.state.addr, addr: this.state.addr,
port: parseInt(this.state.port) port: parseInt(this.state.port, 10)
})) }))
window.location.reload(false) window.location.reload(false)
} }
}, },
setDefaults: function(){ setDefaults: function () {
this.setState(this.getDefaults()) this.setState(this.getDefaults())
}, },
onChange: function(event){ onChange: function (event) {
if(event.target.id === 'nodeAddress'){ if (event.target.id === 'nodeAddress') {
this.setState({ addr: event.target.value }) this.setState({ addr: event.target.value })
} else { } else {
this.setState({ port: event.target.value }) this.setState({ port: event.target.value })
} }
}, },
isOK: function(){ isOK: function () {
if(this.state.api){ if (this.state.api) {
return <div className="itsok light"> return <div className="itsok light">
<h5><Icon name="check" /> It's OK</h5> <h5><Icon name="check" /> It's OK</h5>
<p>You're connected to IPFS</p> <p>You're connected to IPFS</p>
</div> </div>
} }
}, },
render: function(){ render: function () {
return ( return (
<div className="settings"> <div className="settings">
<h2><Icon name="cog"/> Settings</h2> <h2><Icon name="cog"/> Settings</h2>
@ -77,5 +77,4 @@ module.exports = function(boardsAPI){
) )
} }
}) })
} }

View File

@ -3,41 +3,41 @@ var Icon = require('icon.jsx')
var GetIPFS = require('getipfs.jsx') var GetIPFS = require('getipfs.jsx')
var UserID = require('userID.jsx') var UserID = require('userID.jsx')
module.exports = function(boardsAPI){ module.exports = function (boardsAPI) {
return React.createClass({ return React.createClass({
getInitialState: function(){ getInitialState: function () {
return { users: [], api: false } return { users: [], api: false }
}, },
componentDidMount: function(){ componentDidMount: function () {
boardsAPI.use(boards => { boardsAPI.use(boards => {
boards.init() boards.init()
if(boards.isInit){ if (boards.isInit) {
if(this.isMounted()){ if (this.isMounted()) {
this.init(boards) this.init(boards)
} }
} }
var ee = boards.getEventEmitter() var ee = boards.getEventEmitter()
ee.on('init', e => { ee.on('init', e => {
if(!e && this.isMounted()){ if (!e && this.isMounted()) {
this.init(boards) this.init(boards)
} }
}) })
ee.on('user',(id) => { ee.on('user', (id) => {
if(id === undefined || id === 'undefined') console.log('found undefined user???') if (id === undefined || id === 'undefined') console.log('found undefined user???')
if(this.isMounted() && this.state.users.indexOf(id) < 0){ if (this.isMounted() && this.state.users.indexOf(id) < 0) {
this.setState({ users: this.state.users.concat(id) }) this.setState({ users: this.state.users.concat(id) })
} }
}) })
}) })
}, },
init: function(boards){ init: function (boards) {
if(this.isMounted() && !this.state.init){ if (this.isMounted() && !this.state.init) {
this.setState({ users: boards.getUsers(), api: true, init: true, boards: boards }) this.setState({ users: boards.getUsers(), api: true, init: true, boards: boards })
boards.searchUsers() boards.searchUsers()
} }
}, },
render: function(){ render: function () {
if(this.state.api){ if (this.state.api) {
return <div> return <div>
<h1><Icon name="users" /> Users</h1> <h1><Icon name="users" /> Users</h1>
<p>Found <b>{this.state.users.length}</b> users</p> <p>Found <b>{this.state.users.length}</b> users</p>

View File

@ -5,9 +5,9 @@ var HtmlWebpackPlugin = require('html-webpack-plugin')
// Most of the config was copied from js-ipfs-api's webpack configuration // Most of the config was copied from js-ipfs-api's webpack configuration
var config = { var config = {
entry: path.join(__dirname,'webapp','app.jsx'), entry: path.join(__dirname, 'webapp', 'app.jsx'),
output: { output: {
path: path.join(__dirname,'webapp','dist'), path: path.join(__dirname, 'webapp', 'dist'),
filename: 'app.js' filename: 'app.js'
}, },
resolve: { resolve: {
@ -23,7 +23,8 @@ var config = {
eslint: { eslint: {
configFile: './.eslintrc', configFile: './.eslintrc',
failOnWarning: true, failOnWarning: true,
failOnError: true failOnError: true,
fix: true
}, },
module: { module: {
preLoaders: [ preLoaders: [
@ -35,15 +36,15 @@ var config = {
], ],
loaders: [ loaders: [
{ test: /\.(ttf|eot|svg|woff(2?))(\?v=\d+\.\d+\.\d+)?$/, loader: 'file' }, { test: /\.(ttf|eot|svg|woff(2?))(\?v=\d+\.\d+\.\d+)?$/, loader: 'file' },
{ test: /\.css$/, loaders: ['style','css'] }, { test: /\.css$/, loaders: ['style', 'css'] },
{ test: /\.md$/, loaders: ['html','markdown'] }, { test: /\.md$/, loaders: ['html', 'markdown'] },
{ test: /\.json$/, loader: 'json' }, { test: /\.json$/, loader: 'json' },
{ {
test: /\.jsx?$/, test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/, exclude: /(node_modules|bower_components)/,
loader: 'babel', loader: 'babel',
query: { query: {
presets: ['es2015','react'], presets: ['es2015', 'react'],
plugins: addTransformRuntime([]) plugins: addTransformRuntime([])
} }
}, },
@ -74,11 +75,11 @@ var config = {
// Optimization // Optimization
new webpack.optimize.OccurenceOrderPlugin(), new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.DedupePlugin(), new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({ new webpack.optimize.UglifyJsPlugin({
compress: { compress: {
warnings: false warnings: false
} }
}) })
] ]
} }
@ -95,15 +96,15 @@ config.devServer = {
contentBase: config.output.path contentBase: config.output.path
} }
function addTransformRuntime(l){ function addTransformRuntime (l) {
if(process.env.os != 'Windows_NT'){ if (process.env.os !== 'Windows_NT') {
// Workaround for babel6 bug on windows // Workaround for babel6 bug on windows
// https://phabricator.babeljs.io/T6670 // https://phabricator.babeljs.io/T6670
// https://phabricator.babeljs.io/T2954 // https://phabricator.babeljs.io/T2954
// Disabling uglify on windows does the trick // Disabling uglify on windows does the trick
return l.concat('transform-runtime') return l.concat('transform-runtime')
} }
return l return l
} }
module.exports = config module.exports = config