diff --git a/lib/boards-api.js b/lib/boards-api.js index e933c25..6b87d93 100644 --- a/lib/boards-api.js +++ b/lib/boards-api.js @@ -10,113 +10,113 @@ Needs to be browserified to work in the browser var EventEmitter = require('wolfy87-eventemitter') var asyncjs = require('async') -function asObj(str,done){ - if(str.toString) str = str.toString() - if(typeof str === 'string'){ +function asObj (str, done) { + if (str.toString) str = str.toString() + if (typeof str === 'string') { var obj try { obj = JSON.parse(str) } catch (e) { - console.log('error parsing:',str,'Error:',e) - return done(e,undefined) + console.log('error parsing:', str, 'Error:', e) + return done(e, undefined) } - done(null,obj) + done(null, obj) } else { - console.log('not string:',str) - done('not string: '+str,undefined) + console.log('not string:', str) + done('not string: ' + str, undefined) } } -function replyAsObj(res,isJson,done){ - if(res.readable){ +function replyAsObj (res, isJson, done) { + if (res.readable) { // Is a stream console.log('got stream') res.setEncoding('utf8') var data = '' - res.on('data',d => { - console.log('got stream data:',d) + res.on('data', d => { + console.log('got stream data:', d) data += d }) - res.on('end',() => { - if(isJson) { - asObj(data,done) + res.on('end', () => { + if (isJson) { + asObj(data, done) } else { - done(null,data) + done(null, data) } }) - } else if(res.split || res.toString){ - //console.log('got string or buffer:',res) - if(res.toString) res = res.toString() + } else if (res.split || res.toString) { + // console.log('got string or buffer:',res) + if (res.toString) res = res.toString() // Is a string - if(isJson){ - asObj(res,done) + if (isJson) { + asObj(res, done) } else { - done(null,res) + done(null, res) } } } -function BoardsAPI(ipfs){ +function BoardsAPI (ipfs) { this.ipfs = ipfs this.version = 'ipfs:boards:version:dev' this.baseurl = '/ipfs-boards-profile/' 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){ + if (this.users === null || this.users === undefined || !this.users.indexOf || !this.users.push) { this.users = [] } - } catch(e){ + } catch (e) { this.users = [] } } } -BoardsAPI.prototype.backupCache = function(){ - if(localStorage !== undefined){ +BoardsAPI.prototype.backupCache = function () { + 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)) } } // Rewrote this to use event emitters. Should also add periodic rechecking -BoardsAPI.prototype.resolveIPNS = function(n,handler){ - if(handler && handler.apply) this.ee.on(n,handler) - if(!this.resolvingIPNS[n]){ +BoardsAPI.prototype.resolveIPNS = function (n, handler) { + if (handler && handler.apply) this.ee.on(n, handler) + if (!this.resolvingIPNS[n]) { this.resolvingIPNS[n] = true - this.ipfs.name.resolve(n,(err,r) => { + this.ipfs.name.resolve(n, (err, r) => { delete this.resolvingIPNS[n] - if(err){ + if (err) { // Communicate error - this.ee.emit('error',err) + this.ee.emit('error', err) } else { var url = r.Path - if(url === undefined){ - console.log('Could not resolve',n) - this.ee.emit('error',r.Message) - } else if(this.users.indexOf(n) < 0){ // new find - this.isUserProfile(url,(isit,err) => { - if(isit){ - console.log(n,'is a user') - this.ee.emit(n,url) - if(this.users.indexOf(n) < 0){ - this.ee.emit('user',n,url) + if (url === undefined) { + console.log('Could not resolve', n) + this.ee.emit('error', r.Message) + } else if (this.users.indexOf(n) < 0) { // new find + this.isUserProfile(url, (isit, err) => { + if (isit) { + console.log(n, 'is a user') + this.ee.emit(n, url) + if (this.users.indexOf(n) < 0) { + this.ee.emit('user', n, url) this.users.push(n) this.backupCache() } } else { - console.log(n,'not a valid profile:',err) - this.ee.emit(n,undefined,'not a valid profile: '+err) + console.log(n, 'not a valid profile:', err) + this.ee.emit(n, undefined, 'not a valid profile: ' + err) } return true // Remove from listeners }) } 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 } -BoardsAPI.prototype.isUserProfile = function(addr,done){ - if(addr === undefined) return console.log('Asked to check if undefined is a profile') - this.ipfs.cat(addr+this.baseurl+'ipfs-boards-version.txt',(err,r) => { - if(err) return done(false,err) - replyAsObj(r,false,(_,res) => { - if(!res || !res.trim){ - console.log('Could not read version from',addr) +BoardsAPI.prototype.isUserProfile = function (addr, done) { + if (addr === undefined) return console.log('Asked to check if undefined is a profile') + this.ipfs.cat(addr + this.baseurl + 'ipfs-boards-version.txt', (err, r) => { + if (err) return done(false, err) + replyAsObj(r, false, (_, res) => { + if (!res || !res.trim) { + console.log('Could not read version from', addr) } else { var v = res.trim() - console.log('Version in profile snapshot',addr,'is',v) - if(v === this.version){ + console.log('Version in profile snapshot', addr, 'is', v) + if (v === this.version) { done(true) } 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 - this.ipfs.swarm.peers((err,r) => { - if(err) return console.log(err) - replyAsObj(r,true,(e,reply) => { - if(e){ - this.ee.emit('error',e) - return console.log('There was an error while getting swarm peers:',e) + this.ipfs.swarm.peers((err, r) => { + if (err) return console.log(err) + replyAsObj(r, true, (e, reply) => { + if (e) { + this.ee.emit('error', e) + return console.log('There was an error while getting swarm peers:', e) } - console.log('Checking',reply.Strings.length,'peers') - //reply.Strings.forEach(item => { + console.log('Checking', reply.Strings.length, 'peers') + // reply.Strings.forEach(item => { var f = (item, done) => { var ss = item.split('/') - var n = ss[ss.length-1] - this.ee.once(n,(res,err) => done(err)) + var n = ss[ss.length - 1] + this.ee.once(n, (res, err) => done(err)) 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 @@ -178,33 +178,33 @@ BoardsAPI.prototype.searchUsers = function(){ return this.ee } -BoardsAPI.prototype.getProfile = function(userID,done){ - this.resolveIPNS(userID,(url,err) => { - if(err){ - this.ee.emit('error',err) - done(err,null) +BoardsAPI.prototype.getProfile = function (userID, done) { + this.resolveIPNS(userID, (url, err) => { + if (err) { + this.ee.emit('error', err) + done(err, null) } else { // Download actual profile - this.ipfs.cat(url+this.baseurl+'profile.json',(err2,res) => { - if(err2){ - this.ee.emit('error',err2) - done(err2,null) + this.ipfs.cat(url + this.baseurl + 'profile.json', (err2, res) => { + if (err2) { + this.ee.emit('error', err2) + done(err2, null) } else { // TODO: JSON parse error handling var p = JSON.parse(res.toString()) - this.ee.emit('profile for '+userID,p) - done(null,p) + this.ee.emit('profile for ' + userID, p) + done(null, p) } }) // Get other info - this.ipfs.ls(url+this.baseurl+'boards/',(err2,res) => { - if(!err2){ + this.ipfs.ls(url + this.baseurl + 'boards/', (err2, res) => { + if (!err2) { var l = res.Objects[0].Links.map(i => { return { name: i.Name, hash: i.Hash } }) - this.ee.emit('boards for '+userID,l) + this.ee.emit('boards for ' + userID, l) } else { - this.ee.emit('error',err2) + this.ee.emit('error', err2) } }) } @@ -213,51 +213,58 @@ BoardsAPI.prototype.getProfile = function(userID,done){ return this.ee } -BoardsAPI.prototype.getBoardSettings = function(userID,board){ - if(!userID){ - return console.log('Invalid USERID',userID) +BoardsAPI.prototype.getBoardSettings = function (userID, board) { + if (!userID) { + return console.log('Invalid USERID', userID) } - if(!board){ - return console.log('Invalid BOARD',board) + if (!board) { + return console.log('Invalid BOARD', board) } - this.resolveIPNS(userID,(r,e) => { - if(e){ - this.ee.emit('error',e) + this.resolveIPNS(userID, (r, e) => { + if (e) { + this.ee.emit('error', e) } else { - var url = r+this.baseurl+'boards/'+board+'/settings.json' - this.ipfs.cat(url,(err,resp) => { + var url = r + this.baseurl + 'boards/' + board + '/settings.json' + this.ipfs.cat(url, (err, resp) => { // TODO: error handling json conversion var settings = JSON.parse(resp.toString()) - if(err){ - this.ee.emit('error',err) + if (err) { + this.ee.emit('error', err) } 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){ + this.ee.emit('settings for ' + board + '@' + userID, settings, r) + if (settings.whitelist === true) { // Get the whitelist - var url = r+this.baseurl+'boards/'+board+'/whitelist' - this.ipfs.cat(url,(err,res) => { - if(err){ - this.ee.emit('error',err) + var url = r + this.baseurl + 'boards/' + board + '/whitelist' + this.ipfs.cat(url, (err, res) => { + if (err) { + this.ee.emit('error', err) // Emit an empty whitelist. - this.ee.emit('whitelist for '+board+'@'+userID,[]) - } else replyAsObj(res,false,(err,whitelist) => { - // Send whitelist - var w = whitelist.split(' ').map(x => x.trim()) - this.ee.emit('whitelist for '+board+'@'+userID,w) - }) + this.ee.emit('whitelist for ' + board + '@' + userID, []) + } 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) => { - if(err){ - this.ee.emit('error',err) + var u = r + this.baseurl + 'boards/' + board + '/blacklist' + this.ipfs.cat(u, (err, blacklist) => { + if (err) { + this.ee.emit('error', err) } else { // Send blacklist 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 } -BoardsAPI.prototype.downloadPost = function(hash,adminID,board,op,done){ - console.log('Downloading post',hash) - this.ipfs.cat(hash,(err2,r) => { - if(err2){ - this.ee.emit('error',err2) - console.log('Could not download post',hash,'of',board+'@'+adminID) - if(done && done.apply) done(err2) +BoardsAPI.prototype.downloadPost = function (hash, adminID, board, op, done) { + console.log('Downloading post', hash) + this.ipfs.cat(hash, (err2, r) => { + if (err2) { + this.ee.emit('error', err2) + console.log('Could not download post', hash, 'of', board + '@' + adminID) + if (done && done.apply) done(err2) } else { - replyAsObj(r,true,(err,post) => { + replyAsObj(r, true, (err, post) => { + if (err) return // TODO: add JSON parsing error handling post.hash = hash - if(op) post.op = op // Inject op - if(board){ - if(adminID) this.ee.emit('post in '+board+'@'+adminID,post,hash) - else this.ee.emit('post in '+board,post,hash) + if (op) post.op = op // Inject op + if (board) { + if (adminID) this.ee.emit('post in ' + board + '@' + adminID, post, hash) + else this.ee.emit('post in ' + board, post, hash) } - this.ee.emit(hash,post,adminID,board) - if(done && done.apply) done(null,post) + this.ee.emit(hash, post, adminID, board) + if (done && done.apply) done(null, post) }) } }) return this.ee } -BoardsAPI.prototype.retrieveListOfApproved = function(what,addr,adminID,board){ - var a = addr+this.baseurl+'boards/'+board+'/approved/'+what+'/' - this.ipfs.ls(a,(err,res) => { - if(err){ - this.ee.emit('error',err) +BoardsAPI.prototype.retrieveListOfApproved = function (what, addr, adminID, board) { + var a = addr + this.baseurl + 'boards/' + board + '/approved/' + what + '/' + this.ipfs.ls(a, (err, res) => { + if (err) { + this.ee.emit('error', err) } else { // Send approved posts list var ret = res.Objects[0].Links.map(item => { 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){ - if(!options) return - this.ee.on('settings for '+board+'@'+adminID,function(settings,addr){ +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) + 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){ + if (options.comments) this.retrieveListOfApproved('comments', addr, adminID, board) + } 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 } }) - this.getBoardSettings(adminID,board) + this.getBoardSettings(adminID, board) return this.ee } -BoardsAPI.prototype.getPostsInBoard = function(adminID,board){ - if(adminID){ - this.ee.on('approved posts for '+board+'@'+adminID,ret => { +BoardsAPI.prototype.getPostsInBoard = function (adminID, board) { + if (adminID) { + this.ee.on('approved posts for ' + board + '@' + adminID, ret => { // 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 whitelist.forEach(item => { - this.getUserPostListInBoard(item,board,(err,postList) => { - postList.forEach( i => this.downloadPost(i.hash,adminID,board,item)) + this.getUserPostListInBoard(item, board, (err, postList) => { + if (err) return + postList.forEach(i => this.downloadPost(i.hash, adminID, board, item)) }) }) }) // Get allowed content and content producers - this.getAllowedContentProducers(adminID,board,{ posts: true }) + this.getAllowedContentProducers(adminID, board, { posts: true }) // Get the admin's posts - this.getUserPostListInBoard(adminID,board,(err,res) => { - if(err){ + this.getUserPostListInBoard(adminID, board, (err, res) => { + if (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 { // TODO: Download all posts in board from everyone // Download my posts - this.getUserPostListInBoard(this.id,board,(err,res) => { - if(err){ + this.getUserPostListInBoard(this.id, board, (err, res) => { + if (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 } -BoardsAPI.prototype.getUserPostListInBoard = function(user,board,done){ - this.resolveIPNS(user,(url,err) => { - if(err){ - this.ee.emit('error',err) +BoardsAPI.prototype.getUserPostListInBoard = function (user, board, done) { + this.resolveIPNS(user, (url, err) => { + if (err) { + this.ee.emit('error', err) done(err) - } else this.ipfs.ls(url+this.baseurl+'posts/'+board,(e,r) => { - if(e){ - this.ee.emit('error',e) - done(e) - } else if(r && !r.split){ - console.log('Found',r.Objects[0].Links.length,'posts in',board,'at',user) - this.ee.emit('post count',board,user,r.Objects[0].Links.length) - var l = r.Objects[0].Links.map(i => { - return { date: i.Name, hash: i.Hash } - }) - done(null,l) - } - }) + } else { + this.ipfs.ls(url + this.baseurl + 'posts/' + board, (e, r) => { + if (e) { + this.ee.emit('error', e) + done(e) + } else if (r && !r.split) { + console.log('Found', r.Objects[0].Links.length, 'posts in', board, 'at', user) + this.ee.emit('post count', board, user, r.Objects[0].Links.length) + var l = r.Objects[0].Links.map(i => { + return { date: i.Name, hash: i.Hash } + }) + 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'){ +BoardsAPI.prototype.downloadComment = function (hash, adminID, board, target, done) { + if (!done && typeof target === 'function') { done = target target = undefined } - console.log('target',target) - this.ipfs.cat(hash,(err2,r) => { - if(err2){ - this.ee.emit('error',err2) - console.log('Could not download comment',hash,'of',board+'@'+adminID) - if(done) done(err2) + console.log('target', target) + this.ipfs.cat(hash, (err2, r) => { + if (err2) { + this.ee.emit('error', err2) + console.log('Could not download comment', hash, 'of', board + '@' + adminID) + if (done) done(err2) } else { // TODO: add JSON parsing error handling var cmnt = JSON.parse(r.toString()) cmnt.hash = hash - if(target){ + if (target) { cmnt.original_parent = cmnt.parent cmnt.parent = target } - this.ee.emit(hash,cmnt,adminID,board) - this.ee.emit('comment for '+(target || cmnt.parent),cmnt) - if(done) done(null,cmnt) + this.ee.emit(hash, cmnt, adminID, board) + this.ee.emit('comment for ' + (target || cmnt.parent), cmnt) + if (done) done(null, cmnt) } }) return this.ee } -BoardsAPI.prototype.getCommentsFor = function(parent,board,adminID,target){ - if(!parent || !board || !adminID){ - return console.log('malformed arguments:',parent,board,adminID) +BoardsAPI.prototype.getCommentsFor = function (parent, board, adminID, target) { + if (!parent || !board || !adminID) { + return console.log('malformed arguments:', parent, board, adminID) } // figure out if there's a previous version of the item - this.ipfs.cat(parent, (err,res) => { - if(err){ - this.ee.emit('error',err) + this.ipfs.cat(parent, (err, res) => { + if (err) { + this.ee.emit('error', err) } else { - replyAsObj(res,true,(err2,obj) => { - if(err2){ - this.ee.emit('error',err2) - } else if(typeof obj.previous == 'string'){ + replyAsObj(res, true, (err2, obj) => { + if (err2) { + this.ee.emit('error', err2) + } else if (typeof obj.previous === 'string') { // 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 - this.getUserCommentList(parent,adminID,(err,res) => { - if(!err){ - res.forEach(item => this.downloadComment(item.hash,adminID,board,target)) + this.getUserCommentList(parent, adminID, (err, res) => { + if (!err) { + res.forEach(item => this.downloadComment(item.hash, adminID, board, target)) } }) // 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 whitelist.forEach(item => { - this.getUserCommentList(parent,item,(err,res) => { - res.forEach(i => this.downloadComment(i.hash,adminID,board,target)) + this.getUserCommentList(parent, item, (err, res) => { + if (err) return + res.forEach(i => this.downloadComment(i.hash, adminID, board, target)) }) }) }) // Handle approved comments - this.ee.on('approved comments for '+board+'@'+adminID,ret => { - ret.forEach(item => this.downloadComment(item.hash,adminID,board,target)) + this.ee.on('approved comments for ' + board + '@' + adminID, ret => { + 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){ - if(!parent || !user){ - return console.log('Malformed arguments:',parent,user) +BoardsAPI.prototype.getUserCommentList = function (parent, user, done) { + if (!parent || !user) { + return console.log('Malformed arguments:', parent, user) } - this.resolveIPNS(user,(url,err) => { - if(err){ - this.ee.emit('error',err) + this.resolveIPNS(user, (url, err) => { + if (err) { + this.ee.emit('error', err) done(err) - } else this.ipfs.ls(url+this.baseurl+'comments/'+parent,(e,r) => { - if(e){ - this.ee.emit('error',e) - done(e) - } else if(r && !r.split){ - if(r.Objects && r.Objects[0]){ // If this is not true, then there are no comments - console.log('Found',r.Objects[0].Links.length,'comments for',parent,'at',user) - var l = r.Objects[0].Links.map(i => { - return { date: i.Name, hash: i.Hash } - }) - done(null,l) + } else { + this.ipfs.ls(url + this.baseurl + 'comments/' + parent, (e, r) => { + if (e) { + this.ee.emit('error', e) + done(e) + } else if (r && !r.split) { + if (r.Objects && r.Objects[0]) { // If this is not true, then there are no comments + console.log('Found', r.Objects[0].Links.length, 'comments for', parent, 'at', user) + var l = r.Objects[0].Links.map(i => { + return { date: i.Name, hash: i.Hash } + }) + done(null, l) + } } - } - }) + }) + } return true // remove myself from listeners }) return this.ee @@ -482,41 +496,41 @@ BoardsAPI.prototype.getUserCommentList = function(parent,user,done){ // API for publishing content and managing to be done later... // Initialize API -BoardsAPI.prototype.init = function(done){ - if(this.isInit) return - this.ipfs.id( (err, res) => { - if(err){ - console.log('Error while getting OWN ID:',err) - this.ee.emit('error',err) - this.ee.emit('init',err) - if(done && done.apply) done(err) - } else if(res.ID){ - console.log('I am',res.ID) +BoardsAPI.prototype.init = function (done) { + if (this.isInit) return + this.ipfs.id((err, res) => { + if (err) { + console.log('Error while getting OWN ID:', err) + this.ee.emit('error', err) + this.ee.emit('init', err) + if (done && done.apply) done(err) + } else if (res.ID) { + console.log('I am', res.ID) this.id = res.ID this.resolveIPNS(res.ID) - console.log('Version is',this.version) - this.ipfs.add(new Buffer('ipfs:boards:version:'+this.version),{n: true},(err2,r) => { - if(err2){ - this.ee.emit('error',err2) - console.log('Error while calculating version hash:',err2) - this.ee.emit('init',err2) - if(done && done.apply) done(err2) + console.log('Version is', this.version) + this.ipfs.add(new Buffer('ipfs:boards:version:' + this.version), {n: true}, (err2, r) => { + if (err2) { + this.ee.emit('error', err2) + console.log('Error while calculating version hash:', err2) + this.ee.emit('init', err2) + if (done && done.apply) done(err2) } else { - if(r && r.Hash) this.version_hash = r.Hash - if(r && r[0] && r[0].Hash) this.version_hash = r[0].Hash - console.log('Version hash is',this.version_hash) - this.ipfs.version((err,res) => { - if(err){ - this.ee.emit('error',err) - this.ee.emit('init',err) - console.log('Error while getting ipfs version:',err) - if(done && done.apply) done(err) + if (r && r.Hash) this.version_hash = r.Hash + if (r && r[0] && r[0].Hash) this.version_hash = r[0].Hash + console.log('Version hash is', this.version_hash) + this.ipfs.version((err, res) => { + if (err) { + this.ee.emit('error', err) + this.ee.emit('init', err) + console.log('Error while getting ipfs version:', err) + if (done && done.apply) done(err) } else { this.ipfs_version = res.Version - console.log('IPFS Version is',res.Version) - this.ee.emit('init',undefined) + console.log('IPFS Version is', res.Version) + this.ee.emit('init', undefined) 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 } -BoardsAPI.prototype.getUsers = function(){ +BoardsAPI.prototype.getUsers = function () { return this.users } -BoardsAPI.prototype.getMyID = function(){ +BoardsAPI.prototype.getMyID = function () { return this.id } diff --git a/webapp/app.jsx b/webapp/app.jsx index 461eea3..fa821e9 100644 --- a/webapp/app.jsx +++ b/webapp/app.jsx @@ -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,25 +29,25 @@ var CommentPage = require('commentpage.jsx')(boards) // Define Main Components var Container = React.createClass({ - render: function(){ - return (
Sorry, there's nothing here!
diff --git a/webapp/components/boardsapiwrapper.js b/webapp/components/boardsapiwrapper.js index ce4b8b6..69ed2f4 100644 --- a/webapp/components/boardsapiwrapper.js +++ b/webapp/components/boardsapiwrapper.js @@ -1,11 +1,11 @@ -var BoardsAPI = function(){ +var BoardsAPI = function () { this.done = false this.fa = [] 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 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.init() this.done = true @@ -14,9 +14,9 @@ var BoardsAPI = function(){ }) } -BoardsAPI.prototype.use = function(f){ - if(!f || !f.apply || !f.call) return console.log('Non-function tried to use API:',f) - if(this.done){ +BoardsAPI.prototype.use = function (f) { + if (!f || !f.apply || !f.call) return console.log('Non-function tried to use API:', f) + if (this.done) { f(this.boards) } else { this.fa.push(f) diff --git a/webapp/components/clock.jsx b/webapp/components/clock.jsx index 2c241a8..6dfa93c 100644 --- a/webapp/components/clock.jsx +++ b/webapp/components/clock.jsx @@ -2,34 +2,36 @@ var React = require('react') var Icon = require('icon.jsx') module.exports = React.createClass({ - getInitialState: function(){ + getInitialState: function () { return { moment: false, text: '...' } }, - componentDidMount: function(){ - require.ensure(['moment'],_ => { - if(this.isMounted()){ + componentDidMount: function () { + require.ensure(['moment'], _ => { + if (this.isMounted()) { var moment = require('moment') this.setState({ moment: moment, - interval: setInterval(this.upDate,60*1000), + interval: setInterval(this.upDate, 60 * 1000), text: moment.unix(this.props.date).fromNow() }) } }) }, - upDate: function(){ - if(this.isMounted()) + upDate: function () { + 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) + getDate: function () { + if (this.state.moment) { return this.state.text - else + } else { return...
}, - render: function(){ + render: function () { return this.renderIfApplicable() } }) diff --git a/webapp/components/navbar.jsx b/webapp/components/navbar.jsx index e8587a0..ad13288 100644 --- a/webapp/components/navbar.jsx +++ b/webapp/components/navbar.jsx @@ -2,17 +2,17 @@ var React = require('react') var Icon = require('icon.jsx') var Link = require('react-router').Link -module.exports = function(boardsAPI){ +module.exports = function (boardsAPI) { return React.createClass({ - getInitialState: function(){ + getInitialState: function () { return { api: false, loading: true } }, - componentDidMount(){ + componentDidMount () { boardsAPI.use(boards => { - if(boards.isInit) this.setState({ api: true }) - boards.getEventEmitter().on('init',err => { - if(!this.isMounted()) return - if(err){ + if (boards.isInit) this.setState({ api: true }) + boards.getEventEmitter().on('init', err => { + if (!this.isMounted()) return + if (err) { this.setState({ loading: false, api: false }) } else { this.setState({ api: true }) @@ -20,19 +20,19 @@ module.exports = function(boardsAPI){ }) }) }, - extraButtons: function(){ - if(this.state.api){ + extraButtons: function () { + if (this.state.api) { returnStill can't fix it? File a issue on GitHub, we'll be happy to help!
You're connected to IPFS
Found {this.state.users.length} users
diff --git a/webpack.config.js b/webpack.config.js index b8c91b3..45ca565 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -5,9 +5,9 @@ var HtmlWebpackPlugin = require('html-webpack-plugin') // Most of the config was copied from js-ipfs-api's webpack configuration var config = { - entry: path.join(__dirname,'webapp','app.jsx'), + entry: path.join(__dirname, 'webapp', 'app.jsx'), output: { - path: path.join(__dirname,'webapp','dist'), + path: path.join(__dirname, 'webapp', 'dist'), filename: 'app.js' }, resolve: { @@ -23,7 +23,8 @@ var config = { eslint: { configFile: './.eslintrc', failOnWarning: true, - failOnError: true + failOnError: true, + fix: true }, module: { preLoaders: [ @@ -35,15 +36,15 @@ var config = { ], loaders: [ { test: /\.(ttf|eot|svg|woff(2?))(\?v=\d+\.\d+\.\d+)?$/, loader: 'file' }, - { test: /\.css$/, loaders: ['style','css'] }, - { test: /\.md$/, loaders: ['html','markdown'] }, + { test: /\.css$/, loaders: ['style', 'css'] }, + { test: /\.md$/, loaders: ['html', 'markdown'] }, { test: /\.json$/, loader: 'json' }, { test: /\.jsx?$/, exclude: /(node_modules|bower_components)/, loader: 'babel', query: { - presets: ['es2015','react'], + presets: ['es2015', 'react'], plugins: addTransformRuntime([]) } }, @@ -74,11 +75,11 @@ var config = { // Optimization new webpack.optimize.OccurenceOrderPlugin(), new webpack.optimize.DedupePlugin(), - new webpack.optimize.UglifyJsPlugin({ - compress: { - warnings: false - } - }) + new webpack.optimize.UglifyJsPlugin({ + compress: { + warnings: false + } + }) ] } @@ -95,15 +96,15 @@ config.devServer = { contentBase: config.output.path } -function addTransformRuntime(l){ - if(process.env.os != 'Windows_NT'){ - // Workaround for babel6 bug on windows - // https://phabricator.babeljs.io/T6670 - // https://phabricator.babeljs.io/T2954 - // Disabling uglify on windows does the trick - return l.concat('transform-runtime') - } - return l +function addTransformRuntime (l) { + if (process.env.os !== 'Windows_NT') { + // Workaround for babel6 bug on windows + // https://phabricator.babeljs.io/T6670 + // https://phabricator.babeljs.io/T2954 + // Disabling uglify on windows does the trick + return l.concat('transform-runtime') + } + return l } module.exports = config