diff --git a/public/src/client/category.js b/public/src/client/category.js index 7f64fd4f7f..4592c551a1 100644 --- a/public/src/client/category.js +++ b/public/src/client/category.js @@ -139,7 +139,7 @@ define('forum/category', [ $('[component="category"]').empty(); - loadTopicsAfter(Math.max(0, bookmarkIndex - 1), 1, function () { + loadTopicsAfter(Math.max(0, bookmarkIndex - 1) + 1, 1, function () { Category.scrollToTopic(bookmarkIndex, clickedIndex, 0); }); } @@ -261,7 +261,7 @@ define('forum/category', [ var topics = $('[component="category/topic"]'); var afterEl = direction > 0 ? topics.last() : topics.first(); - var after = parseInt(afterEl.attr('data-index'), 10) || 0; + var after = (parseInt(afterEl.attr('data-index'), 10) || 0) + 1; loadTopicsAfter(after, direction); }; diff --git a/src/categories.js b/src/categories.js index 65c60bb58c..28d99a7d7f 100644 --- a/src/categories.js +++ b/src/categories.js @@ -67,16 +67,7 @@ var privileges = require('./privileges'); }; Categories.isIgnored = function (cids, uid, callback) { - user.getIgnoredCategories(uid, function (err, ignoredCids) { - if (err) { - return callback(err); - } - - cids = cids.map(function (cid) { - return ignoredCids.indexOf(cid.toString()) !== -1; - }); - callback(null, cids); - }); + db.isSortedSetMembers('uid:' + uid + ':ignored:cids', cids, callback); }; Categories.getPageCount = function (cid, uid, callback) { diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index acc8652057..885af43516 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -38,7 +38,7 @@ SocketCategories.get = function (socket, data, callback) { SocketCategories.getWatchedCategories = function (socket, data, callback) { async.parallel({ - categories: async.apply(categories.getCategoriesByPrivilege, socket.uid, 'find'), + categories: async.apply(categories.getCategoriesByPrivilege, 'cid:0:children', socket.uid, 'find'), ignoredCids: async.apply(user.getIgnoredCategories, socket.uid) }, function (err, results) { if (err) { @@ -47,6 +47,7 @@ SocketCategories.getWatchedCategories = function (socket, data, callback) { var watchedCategories = results.categories.filter(function (category) { return category && results.ignoredCids.indexOf(category.cid.toString()) === -1; }); + callback(null, watchedCategories); }); }; @@ -90,7 +91,7 @@ SocketCategories.loadMore = function (socket, data, callback) { set = 'cid:' + data.cid + ':tids:posts'; } - var start = Math.max(0, parseInt(data.after, 10)) + 1; + var start = Math.max(0, parseInt(data.after, 10)); if (data.direction === -1) { start = start - (reverse ? infScrollTopicsPerPage : -infScrollTopicsPerPage); @@ -156,6 +157,9 @@ SocketCategories.getMoveCategories = function (socket, data, callback) { function (next) { db.getSortedSetRange('cid:0:children', 0, -1, next); }, + function (cids, next) { + privileges.categories.filterCids('read', cids, socket.uid, next); + }, function (cids, next) { categories.getCategories(cids, socket.uid, next); } diff --git a/test/categories.js b/test/categories.js index 1cc57a9723..876b4cb2bf 100644 --- a/test/categories.js +++ b/test/categories.js @@ -182,6 +182,130 @@ describe('Categories', function () { }); }); + describe('socket methods', function () { + var socketCategories = require('../src/socket.io/categories'); + + before(function (done) { + Topics.post({ + uid: posterUid, + cid: categoryObj.cid, + title: 'Test Topic Title', + content: 'The content of test topic', + tags: ['nodebb'] + }, done); + }); + + it('should get recent replies in category', function (done) { + socketCategories.getRecentReplies({uid: posterUid}, categoryObj.cid, function (err, data) { + assert.ifError(err); + assert(Array.isArray(data)); + done(); + }); + }); + + it('should get categories', function (done) { + socketCategories.get({uid: posterUid}, {}, function (err, data) { + assert.ifError(err); + assert(Array.isArray(data)); + done(); + }); + }); + + it('should get watched categories', function (done) { + socketCategories.getWatchedCategories({uid: posterUid}, {}, function (err, data) { + assert.ifError(err); + assert(Array.isArray(data)); + done(); + }); + }); + + it('should load more topics', function (done) { + socketCategories.loadMore({uid: posterUid}, {cid: categoryObj.cid, after: 0, author: 'poster', tag: 'nodebb'}, function (err, data) { + assert.ifError(err); + assert(Array.isArray(data.topics)); + assert.equal(data.topics[0].user.username, 'poster'); + assert.equal(data.topics[0].tags[0].value, 'nodebb'); + assert.equal(data.topics[0].category.cid, categoryObj.cid); + done(); + }); + }); + + it('should load page count', function (done) { + socketCategories.getPageCount({uid: posterUid}, categoryObj.cid, function (err, pageCount) { + assert.ifError(err); + assert.equal(pageCount, 1); + done(); + }); + }); + + it('should load page count', function (done) { + socketCategories.getTopicCount({uid: posterUid}, categoryObj.cid, function (err, topicCount) { + assert.ifError(err); + assert.equal(topicCount, 2); + done(); + }); + }); + + it('should load category by privilege', function (done) { + socketCategories.getCategoriesByPrivilege({uid: posterUid}, 'find', function (err, data) { + assert.ifError(err); + assert(Array.isArray(data)); + done(); + }); + }); + + it('should get move categories', function (done) { + socketCategories.getMoveCategories({uid: posterUid}, {}, function (err, data) { + assert.ifError(err); + assert(Array.isArray(data)); + done(); + }); + }); + + it('should ignore category', function (done) { + socketCategories.ignore({uid: posterUid}, categoryObj.cid, function (err) { + assert.ifError(err); + Categories.isIgnored([categoryObj.cid], posterUid, function (err, isIgnored) { + assert.ifError(err); + assert.equal(isIgnored[0], true); + done(); + }); + }); + }); + + it('should watch category', function (done) { + socketCategories.watch({uid: posterUid}, categoryObj.cid, function (err) { + assert.ifError(err); + Categories.isIgnored([categoryObj.cid], posterUid, function (err, isIgnored) { + assert.ifError(err); + assert.equal(isIgnored[0], false); + done(); + }); + }); + }); + + it('should check if user is moderator', function (done) { + socketCategories.isModerator({uid: posterUid}, {}, function (err, isModerator) { + assert.ifError(err); + assert(!isModerator); + done(); + }); + }); + + it('should get category data' , function (done) { + socketCategories.getCategory({uid: posterUid}, categoryObj.cid, function (err, data) { + assert.ifError(err); + assert.equal(categoryObj.cid, data.cid); + done(); + }); + }); + + + }); + + + + after(function (done) { db.emptydb(done); }); diff --git a/test/mocks/databasemock.js b/test/mocks/databasemock.js index 61ab8b2bbe..b5b384a679 100644 --- a/test/mocks/databasemock.js +++ b/test/mocks/databasemock.js @@ -129,7 +129,7 @@ nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates')); nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path')); nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json')); - nconf.set('bcrypt_rounds', 6); + nconf.set('bcrypt_rounds', 4); require('../../build').buildTargets(['js', 'clientCSS', 'acpCSS', 'tpl'], next); },