Node.js based forum software built for the modern web
moved CategoriesAdmin into its own file, hid popular/active, some jslinting + cleanup, renamed some fn calls to be more descriptive
psychobunnya02a74604ce2
diff --git a/app.js b/app.jsindex 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.tplindex 9992ba10ba..287dfff13d 100644--- a/public/templates/header.tpl+++ b/public/templates/header.tpl@@ -36,12 +36,12 @@<li><a href="/latest">Recent <span class="badge badge-inverse">3</span></a></li>- <li>+ <!--<li><a href="/popular">Popular</a></li><li><a href="/active">Active</a>- </li>+ </li>--><li><a href="/users">Users</a></li>diff --git a/src/categories.js b/src/categories.jsindex 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 functionCategories.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<ii; i++) {- categories.push({- 'name' : name[i],- 'cid' : cids[i],- 'slug' : slug[i],- 'description' : description[i],- 'blockclass' : blockclass[i],- 'icon' : icon[i],- 'badgeclass' : (!topic_count[i] || (has_read[i] && current_user !=0)) ? '' : 'badge-important',- 'topic_count' : topic_count[i] || 0- });- }-- callback({'categories': categories});++ 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<ii; i++) {+ categories.push({+ 'name' : name[i],+ 'cid' : cids[i],+ 'slug' : slug[i],+ 'description' : description[i],+ 'blockclass' : blockclass[i],+ 'icon' : icon[i],+ 'badgeclass' : (!topic_count[i] || (has_read[i] && current_user !=0)) ? '' : 'badge-important',+ 'topic_count' : topic_count[i] || 0+ });}- Categories.hasReadCategories(cids, current_user, function(read_data) {- has_read = read_data;- generateCategories();- });-+ callback({'categories': categories});+ }++ Categories.hasReadCategories(cids, current_user, function(read_data) {+ has_read = read_data;+ generateCategories();});- } else callback({'categories' : []});++ });};}(exports));\ No newline at end of filediff --git a/src/postTools.js b/src/postTools.jsindex cc8d703bdf..00adbfc86d 100644--- a/src/postTools.js+++ b/src/postTools.js@@ -25,7 +25,9 @@ marked.setOptions({function isOwnPost(next) {RDB.get('pid:' + pid + ':uid', function(err, author) {- if (author && parseInt(author) > 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.jsindex 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.jsindex 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.jsindex 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.jsindex 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.jsindex 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<a target=\"_blank\" href=\"http://www.nodebb.org\" class=\"btn btn-large\"><i class=\"icon-comment\"></i> Get NodeBB</a> <a target=\"_blank\" href=\"https://github.com/designcreateplay/NodeBB\" class=\"btn btn-large\"><i class=\"icon-github-alt\"></i> Fork us on Github</a> <a target=\"_blank\" href=\"https://twitter.com/dcplabs\" class=\"btn btn-large\"><i class=\"icon-twitter\"></i> @dcplabs</a>");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;