From a02a74604ce243e656db15ef2114672598c2a7c3 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 24 May 2013 11:18:28 -0400 Subject: [PATCH 1/6] moved CategoriesAdmin into its own file, hid popular/active, some jslinting + cleanup, renamed some fn calls to be more descriptive --- app.js | 9 +- public/templates/header.tpl | 4 +- src/categories.js | 164 +++++++++++++++--------------------- src/postTools.js | 29 +++++-- src/posts.js | 8 +- src/routes/admin.js | 2 +- src/threadTools.js | 13 ++- src/topics.js | 11 ++- src/webserver.js | 39 +++------ 9 files changed, 129 insertions(+), 150 deletions(-) diff --git a/app.js b/app.js index e7f858bd98..818968e0fb 100644 --- a/app.js +++ b/app.js @@ -2,6 +2,9 @@ var categories = require('./src/categories.js'), templates = require('./public/src/templates.js'), webserver = require('./src/webserver.js'), websockets = require('./src/websockets.js'), + admin = { + 'categories': require('./src/admin/categories.js') + }, fs = require('fs'); DEVELOPMENT = true; @@ -33,11 +36,13 @@ global.templates = {}; default_categories = JSON.parse(default_categories); for (var category in default_categories) { - categories.create(default_categories[category]); + admin.categories.create(default_categories[category]); } }); - } else console.log('Good.'); + } else { + console.log('Good.'); + } }); } diff --git a/public/templates/header.tpl b/public/templates/header.tpl index 9992ba10ba..287dfff13d 100644 --- a/public/templates/header.tpl +++ b/public/templates/header.tpl @@ -36,12 +36,12 @@
  • Recent 3
  • -
  • +
  • Users
  • diff --git a/src/categories.js b/src/categories.js index 3985a8ae2e..d2c9ca80f1 100644 --- a/src/categories.js +++ b/src/categories.js @@ -7,59 +7,7 @@ var RDB = require('./redis.js'), (function(Categories) { - - // START Move into possibly admin/categories.js - // An admin-only function. Seeing how we have no control panel yet ima leave this right here. sit pretty, you - Categories.create = function(data, callback) { - RDB.incr('global:next_category_id', function(err, cid) { - RDB.handle(err); - - var slug = cid + '/' + utils.slugify(data.name); - RDB.rpush('categories:cid', cid); - - // Topic Info - RDB.set('cid:' + cid + ':name', data.name); - RDB.set('cid:' + cid + ':description', data.description); - RDB.set('cid:' + cid + ':icon', data.icon); - RDB.set('cid:' + cid + ':blockclass', data.blockclass); - RDB.set('cid:' + cid + ':slug', slug); - - RDB.set('category:slug:' + slug + ':cid', cid); - - if (callback) callback({'status': 1}); - }); - }; - - Categories.edit = function(data, callback) { - // just a reminder to self that name + slugs are stored into topics data as well. - }; - // END Move into possibly admin/categories.js - - - - Categories.privileges = function(cid, uid, callback) { - function isModerator(next) { - user.isModerator(uid, cid, function(isMod) { - next(null, isMod); - }); - } - - function isAdministrator(next) { - user.isAdministrator(uid, function(isAdmin) { - next(null, isAdmin); - }); - } - - async.parallel([isModerator, isAdministrator], function(err, results) { - callback({ - editable: results.indexOf(true) !== -1 ? true : false, - view_deleted: results.indexOf(true) !== -1 ? true : false - }); - }); - } - - - Categories.get = function(callback, category_id, current_user) { + Categories.getCategoryById = function(category_id, current_user, callback) { RDB.smembers('categories:' + category_id + ':tid', function(err, tids) { RDB.multi() .get('cid:' + category_id + ':name') @@ -112,8 +60,6 @@ var RDB = require('./redis.js'), callback(categoryData); }); } - - }); }); } @@ -140,7 +86,7 @@ var RDB = require('./redis.js'), }); } - + // not the permanent location for this function Categories.getTopicsByTids = function(tids, current_user, callback, category_id /*temporary*/) { var title = [], uid = [], @@ -276,6 +222,29 @@ var RDB = require('./redis.js'), } + Categories.privileges = function(cid, uid, callback) { + function isModerator(next) { + user.isModerator(uid, cid, function(isMod) { + next(null, isMod); + }); + } + + function isAdministrator(next) { + user.isAdministrator(uid, function(isAdmin) { + next(null, isAdmin); + }); + } + + async.parallel([isModerator, isAdministrator], function(err, results) { + callback({ + editable: results.indexOf(true) !== -1 ? true : false, + view_deleted: results.indexOf(true) !== -1 ? true : false + }); + }); + } + + + Categories.hasReadCategories = function(cids, uid, callback) { var batch = RDB.multi(); @@ -291,6 +260,11 @@ var RDB = require('./redis.js'), Categories.getCategories = function(cids, callback, current_user) { + if (cids.length === 0) { + callback({'categories' : []}); + return; + } + var name = [], description = [], icon = [], @@ -307,49 +281,47 @@ var RDB = require('./redis.js'), slug.push('cid:' + cids[i] + ':slug'); topic_count.push('cid:' + cids[i] + ':topiccount'); } - - if (cids.length > 0) { - RDB.multi() - .mget(name) - .mget(description) - .mget(icon) - .mget(blockclass) - .mget(slug) - .mget(topic_count) - .exec(function(err, replies) { - name = replies[0]; - description = replies[1]; - icon = replies[2]; - blockclass = replies[3]; - slug = replies[4]; - topic_count = replies[5]; - - - function generateCategories() { - var categories = []; - for (var i=0, ii=cids.length; i 0) next(null, author === uid); + if (author && parseInt(author) > 0) { + next(null, author === uid); + } }); } @@ -52,12 +54,17 @@ marked.setOptions({ RDB.set('pid:' + pid + ':editor', uid); posts.get_tid_by_pid(pid, function(tid) { - io.sockets.in('topic_' + tid).emit('event:post_edited', { pid: pid, content: marked(content || '') }); + io.sockets.in('topic_' + tid).emit('event:post_edited', { + pid: pid, + content: marked(content || '') + }); }); }; PostTools.privileges(pid, uid, function(privileges) { - if (privileges.editable) success(); + if (privileges.editable) { + success(); + } }); } @@ -66,12 +73,16 @@ marked.setOptions({ RDB.set('pid:' + pid + ':deleted', 1); posts.get_tid_by_pid(pid, function(tid) { - io.sockets.in('topic_' + tid).emit('event:post_deleted', { pid: pid }); + io.sockets.in('topic_' + tid).emit('event:post_deleted', { + pid: pid + }); }); }; PostTools.privileges(pid, uid, function(privileges) { - if (privileges.editable) success(); + if (privileges.editable) { + success(); + } }); } @@ -80,12 +91,16 @@ marked.setOptions({ RDB.del('pid:' + pid + ':deleted'); posts.get_tid_by_pid(pid, function(tid) { - io.sockets.in('topic_' + tid).emit('event:post_restored', { pid: pid }); + io.sockets.in('topic_' + tid).emit('event:post_restored', { + pid: pid + }); }); }; PostTools.privileges(pid, uid, function(privileges) { - if (privileges.editable) success(); + if (privileges.editable) { + success(); + } }); } diff --git a/src/posts.js b/src/posts.js index e8de7135de..aa9a7a7f81 100644 --- a/src/posts.js +++ b/src/posts.js @@ -14,16 +14,12 @@ marked.setOptions({ (function(Posts) { - Posts.get = function(callback, tid, current_user, start, end) { - if (start == null) start = 0; - if (end == null) end = start + 10; - + Posts.getPostsByTid = function(tid, current_user, start, end, callback) { RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) { RDB.handle(err); if (pids.length === 0 ) { - callback(false); - return; + throw new Error('Topic should never have 0 posts. tid: ' + tid); } topics.markAsRead(tid, current_user); diff --git a/src/routes/admin.js b/src/routes/admin.js index bf0536993f..e5b203a8cd 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -50,7 +50,7 @@ var user = require('./../user.js'), } break; case 'topics' : - categories.get(function(data) { + categories.getCategoryById(0, 0, function(data) { res.send(JSON.stringify(data)); }); break; diff --git a/src/threadTools.js b/src/threadTools.js index 6886750dd0..82acdca15c 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -145,10 +145,17 @@ var RDB = require('./redis.js'), RDB.set('tid:' + tid + ':category_name', data.categories[0].name); RDB.set('tid:' + tid + ':category_slug', data.categories[0].slug); }); - socket.emit('api:topic.move', { status: 'ok' }); - io.sockets.in('topic_' + tid).emit('event:topic_moved', { tid: tid }); + + socket.emit('api:topic.move', { + status: 'ok' + }); + io.sockets.in('topic_' + tid).emit('event:topic_moved', { + tid: tid + }); } else { - socket.emit('api:topic.move', { status: 'error' }); + socket.emit('api:topic.move', { + status: 'error' + }); } }); }); diff --git a/src/topics.js b/src/topics.js index 422b1e7a49..bf40fc8931 100644 --- a/src/topics.js +++ b/src/topics.js @@ -14,8 +14,7 @@ marked.setOptions({ }); (function(Topics) { - Topics.get = function(callback, tid, current_user) { - + Topics.getTopicById = function(tid, current_user, callback) { function getTopicData(next) { RDB.multi() .get('tid:' + tid + ':title') @@ -37,9 +36,9 @@ marked.setOptions({ } function getTopicPosts(next) { - posts.get(function(postData) { + posts.getPostsByTid(tid, current_user, 0, 10, function(postData) { next(null, postData); - }, tid, current_user, 0, 10); + }); } function getPrivileges(next) { @@ -68,7 +67,7 @@ marked.setOptions({ pid = postData.pid[i]; - // ############ to be moved into posts.get ############ + // ############ to be moved into posts.getPostsByTid ############ if (postData.deleted[i] === null || (postData.deleted[i] === '1' && privileges.view_deleted) || current_user === uid) { var post_obj = { 'pid' : pid, @@ -95,7 +94,7 @@ marked.setOptions({ retrieved_posts.push(post_obj); } } - // ########## end to be moved into posts.get ############ + // ########## end to be moved into posts.getPostsByTid ############ } callback({ diff --git a/src/webserver.js b/src/webserver.js index d372e29413..f7b8028b4b 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -105,7 +105,8 @@ var express = require('express'), }); // These functions are called via ajax once the initial page is loaded to populate templates with data - function api_method(req, res) { + function api_method(req, res) { + var uid = (req.user) ? req.user.uid : 0; switch(req.params.method) { case 'get_templates_listing' : @@ -118,7 +119,7 @@ var express = require('express'), data.motd_class = (config.show_motd || config.show_motd === undefined) ? '' : 'none'; data.motd = marked(config.motd || "# NodeBB v0.1\nWelcome to NodeBB, the discussion platform of the future.\n\n Get NodeBB Fork us on Github @dcplabs"); res.send(JSON.stringify(data)); - }, (req.user) ? req.user.uid : 0); + }, uid); break; case 'login' : var data = {}, @@ -165,43 +166,27 @@ var express = require('express'), res.send(JSON.stringify(data)); break; case 'topic' : - topics.get(function(data) { - if(!data) { - res.send(false); - return; - } - res.send(JSON.stringify(data)); - }, req.params.id, (req.user) ? req.user.uid : 0); + topics.getTopicById(req.params.id, uid, function(data) { + res.send(data ? JSON.stringify(data) : false); + }); break; case 'category' : - categories.get(function(data) { - if(!data) { - res.send(false); - return; - } - res.send(JSON.stringify(data)); - }, req.params.id, (req.user) ? req.user.uid : 0); + categories.getCategoryById(req.params.id, uid, function(data) { + res.send(data ? JSON.stringify(data) : false); + }, req.params.id, uid); break; case 'latest' : - categories.getLatestTopics((req.user) ? req.user.uid : 0, 0, 9, function(data) { + categories.getLatestTopics(uid, 0, 9, function(data) { res.send(JSON.stringify(data)); }); break; case 'popular' : - categories.get(function(data) { - if(!data) { - res.send(false); - return; - } + categories.getLatestTopics(uid, 0, 9, function(data) { res.send(JSON.stringify(data)); }); break; case 'active' : - categories.get(function(data) { - if(!data) { - res.send(false); - return; - } + categories.getLatestTopics(uid, 0, 9, function(data) { res.send(JSON.stringify(data)); }); break; From 06aa2337d18e2909327c4ad57bcffd9f7090009b Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 24 May 2013 11:18:41 -0400 Subject: [PATCH 2/6] admincategories.js --- src/admin/categories.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/admin/categories.js diff --git a/src/admin/categories.js b/src/admin/categories.js new file mode 100644 index 0000000000..6c681ebb16 --- /dev/null +++ b/src/admin/categories.js @@ -0,0 +1,31 @@ +var RDB = require('./../redis.js'), + utils = require('./../../public/src/utils.js'), + categories = require('./../categories.js'); + +(function(CategoriesAdmin) { + + CategoriesAdmin.create = function(data, callback) { + RDB.incr('global:next_category_id', function(err, cid) { + RDB.handle(err); + + var slug = cid + '/' + utils.slugify(data.name); + RDB.rpush('categories:cid', cid); + + // Topic Info + RDB.set('cid:' + cid + ':name', data.name); + RDB.set('cid:' + cid + ':description', data.description); + RDB.set('cid:' + cid + ':icon', data.icon); + RDB.set('cid:' + cid + ':blockclass', data.blockclass); + RDB.set('cid:' + cid + ':slug', slug); + + RDB.set('category:slug:' + slug + ':cid', cid); + + if (callback) callback({'status': 1}); + }); + }; + + CategoriesAdmin.edit = function(data, callback) { + // just a reminder to self that name + slugs are stored into topics data as well. + }; + +}(exports)); \ No newline at end of file From af7550ca30f50da7688341bfe94bc7d90fb5af41 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 24 May 2013 11:38:16 -0400 Subject: [PATCH 3/6] console.log --- public/templates/footer.tpl | 1 - 1 file changed, 1 deletion(-) diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl index 4e4b9671b0..515f511a30 100644 --- a/public/templates/footer.tpl +++ b/public/templates/footer.tpl @@ -98,7 +98,6 @@ } }) socket.on('api:notifications.get', function(data) { - console.log(data); var notifFrag = document.createDocumentFragment(), notifEl = document.createElement('li'), numRead = data.read.length, From 2d90635dc83d1d4b846a36f47260fb2d81bfc138 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Fri, 24 May 2013 11:39:30 -0400 Subject: [PATCH 4/6] added 500ms delay to disconnect message --- public/src/app.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 0660913cf4..1f9d04c719 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -50,13 +50,15 @@ var socket, }); socket.on('disconnect', function(data){ - app.alert({ - alert_id: 'connection_alert', - title: 'Disconnect', - message: 'You have disconnected from NodeBB, we will try to reconnect!', - type: 'error', - timeout: 5000 - }); + setTimeout(function() { + app.alert({ + alert_id: 'connection_alert', + title: 'Disconnect', + message: 'You have disconnected from NodeBB, we will try to reconnect!', + type: 'error', + timeout: 5000 + }); + }, 500); }); socket.on('reconnecting', function(data) { From 55c704c6f9da26a6cbd8e1499957934e76ce1899 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 24 May 2013 11:44:26 -0400 Subject: [PATCH 5/6] hide category blocks in recent tab --- public/templates/category.tpl | 6 +++--- src/categories.js | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/public/templates/category.tpl b/public/templates/category.tpl index 464aa5d4e4..8064da84d7 100644 --- a/public/templates/category.tpl +++ b/public/templates/category.tpl @@ -11,7 +11,7 @@
    -
    + -
    +