From bc1cde38da8d6133aca750f5f9981141c8d1444c Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 23 Jan 2014 16:34:54 -0500 Subject: [PATCH 1/9] added getObjectKeys for julian :) --- src/database/mongo.js | 14 ++++++++++++++ src/database/redis.js | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/src/database/mongo.js b/src/database/mongo.js index 7a1b71c36d..3b653a1693 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -343,6 +343,20 @@ }); } + module.getObjectKeys = function(key, callback) { + module.getObject(key, function(err, data) { + if(err) { + return callback(err); + } + + if(data) { + callback(null, Object.keys(data)); + } else { + callback(null, []); + } + }); + } + module.getObjectValues = function(key, callback) { module.getObject(key, function(err, data) { if(err) { diff --git a/src/database/redis.js b/src/database/redis.js index e8964b7a36..6af259f9ca 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -275,6 +275,10 @@ }); } + module.getObjectKeys = function(key, callback) { + redisClient.hkeys(key, callback); + } + module.getObjectValues = function(key, callback) { redisClient.hvals(key, callback); } From 6da12d1a39dc8f5d6896e15a1dcf800778c92794 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 23 Jan 2014 16:37:11 -0500 Subject: [PATCH 2/9] and for andrew --- tests/database.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/database.js b/tests/database.js index 071b291957..de22628e2d 100644 --- a/tests/database.js +++ b/tests/database.js @@ -73,6 +73,12 @@ describe('Test database', function() { }); } + function getObjectKeys(callback) { + db.getObjectKeys(objectKey, function(err, data) { + callback(err, {'getObjectKeys':data}); + }); + } + var objectTasks = [ setObject, getObject, @@ -88,7 +94,8 @@ describe('Test database', function() { isObjectField, incrObjectFieldBy, getObject, - getObjects + getObjects, + getObjectKeys ]; async.series(objectTasks, function(err, results) { From f62c82c7975af005d68d52f952cadd51f9704d28 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 23 Jan 2014 17:08:33 -0500 Subject: [PATCH 3/9] send app to action:app.load --- src/webserver.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/webserver.js b/src/webserver.js index 0d8077419b..1dcc5df18b 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -191,7 +191,7 @@ if(nconf.get('ssl')) { app.use(express.csrf()); - // negative boolean with type check here to support a config.json without a 'use_proxy' value, + // negative boolean with type check here to support a config.json without a 'use_proxy' value, // so unless it's specifically set to false, it's true (as long as it's not a dev env) // todo: remove double negative with a minor release, where backward compatibility can be broken // and if dev mode, then it's probably not behind a proxy but it can be forced by setting 'use_proxy' to true @@ -401,7 +401,7 @@ if(nconf.get('ssl')) { }); }); - plugins.fireHook('action:app.load'); + plugins.fireHook('action:app.load', app); translator.translate(templates.logout.toString(), function(parsedTemplate) { templates.logout = parsedTemplate; From b0ef97459031abfb6ebddd472afdbc41e4162134 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 23 Jan 2014 19:01:30 -0500 Subject: [PATCH 4/9] lots of cleanup and refactor --- src/posts.js | 17 ++--- src/topics.js | 201 +++++++++++++++++++++----------------------------- src/user.js | 17 +++++ 3 files changed, 107 insertions(+), 128 deletions(-) diff --git a/src/posts.js b/src/posts.js index 94ed3724fb..650311c979 100644 --- a/src/posts.js +++ b/src/posts.js @@ -439,26 +439,23 @@ var db = require('./database'), Posts.reIndexPids = function(pids, callback) { - function reIndex(pid, callback) { + function reIndex(pid, next) { Posts.getPostField(pid, 'content', function(err, content) { - db.searchRemove('post', pid, function() { + if(err) { + return next(err); + } + db.searchRemove('post', pid, function() { if (content && content.length) { db.searchIndex('post', content, pid); } - callback(null); + next(); }); }); } - async.each(pids, reIndex, function(err) { - if (err) { - callback(err, null); - } else { - callback(null, 'Posts reindexed'); - } - }); + async.each(pids, reIndex, callback); } Posts.getFavourites = function(uid, callback) { diff --git a/src/topics.js b/src/topics.js index 35e83befeb..aeac06f43e 100644 --- a/src/topics.js +++ b/src/topics.js @@ -53,7 +53,6 @@ var async = require('async'), user.addTopicIdToUser(uid, tid, timestamp); - // in future it may be possible to add topics to several categories, so leaving the door open here. db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid); db.incrObjectField('category:' + cid, 'topic_count'); db.incrObjectField('global', 'topicCount'); @@ -66,75 +65,65 @@ var async = require('async'), }; Topics.post = function(uid, title, content, cid, callback) { + if (title) { + title = title.trim(); + } - categoryTools.privileges(cid, uid, function(err, privileges) { - - if(err) { - return callback(err); - } else if(!privileges.write) { - return callback(new Error('no-privileges')); - } else if (!cid) { - return callback(new Error('invalid-cid')); - } - - if (title) { - title = title.trim(); - } - - if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10)) { - return callback(new Error('title-too-short'), null); - } else if(title.length > parseInt(meta.config.maximumTitleLength, 10)) { - return callback(new Error('title-too-long'), null); - } - - if (content) { - content = content.trim(); - } + if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10)) { + return callback(new Error('title-too-short')); + } else if(title.length > parseInt(meta.config.maximumTitleLength, 10)) { + return callback(new Error('title-too-long')); + } - if (!content || content.length < meta.config.miminumPostLength) { - return callback(new Error('content-too-short'), null); - } + if (content) { + content = content.trim(); + } - user.getUserField(uid, 'lastposttime', function(err, lastposttime) { - if (err) { - return callback(err); - } + if (!content || content.length < meta.config.miminumPostLength) { + return callback(new Error('content-too-short')); + } - if(!lastposttime) { - lastposttime = 0; - } + if (!cid) { + return callback(new Error('invalid-cid')); + } - if (Date.now() - lastposttime < meta.config.postDelay * 1000) { - return callback(new Error('too-many-posts'), null); + async.waterfall([ + function(next) { + categoryTools.privileges(cid, uid, next); + }, + function(privileges, next) { + if(!privileges.write) { + return next(new Error('no-privileges')); } - - Topics.create(uid, title, cid, function(err, tid) { + next(); + }, + function(next) { + user.isReadyToPost(uid, next); + }, + function(next) { + Topics.create(uid, title, cid, next); + }, + function(tid, next) { + Topics.reply(tid, uid, content, next); + }, + function(postData, next) { + threadTools.toggleFollow(postData.tid, uid); + next(null, postData); + }, + function(postData, next) { + Topics.getTopicForCategoryView(postData.tid, uid, function(err, topicData) { if(err) { - return callback(err); + return next(err); } - Topics.reply(tid, uid, content, function(err, postData) { - - if(err) { - return callback(err, null); - } else if(!postData) { - return callback(new Error('invalid-post'), null); - } - - threadTools.toggleFollow(tid, uid); - - Topics.getTopicForCategoryView(tid, uid, function(topicData) { - topicData.unreplied = 1; - - callback(null, { - topicData: topicData, - postData: postData - }); - }); + topicData.unreplied = 1; + next(null, { + topicData: topicData, + postData: postData }); }); - }); - }); + } + ], callback); }; Topics.reply = function(tid, uid, content, callback) { @@ -152,18 +141,7 @@ var async = require('async'), next(); }, function(next) { - user.getUserField(uid, 'lastposttime', next); - }, - function(lastposttime, next) { - if(!lastposttime) { - lastposttime = 0; - } - - if (Date.now() - parseInt(lastposttime, 10) < parseInt(meta.config.postDelay, 10) * 1000) { - return next(new Error('too-many-posts'), null); - } - - next(); + user.isReadyToPost(uid, next); }, function(next) { if (content) { @@ -235,41 +213,40 @@ var async = require('async'), pids.sort(); var mainPid = pids[0]; - posts.getPostData(mainPid, function(err, postData) { - if(err) { - return callback(err); + async.parallel({ + postData: function(callback) { + posts.getPostData(mainPid, callback); + }, + cid: function(callback) { + posts.getCidByPid(mainPid, callback); } - - posts.getCidByPid(mainPid, function(err, cid) { + }, function(err, results) { + Topics.create(results.postData.uid, title, results.cid, function(err, tid) { if(err) { return callback(err); } - Topics.create(postData.uid, title, cid, function(err, tid) { + async.eachSeries(pids, move, function(err) { if(err) { return callback(err); } - async.eachSeries(pids, move, function(err) { + Topics.getTopicData(tid, callback); + }); + + function move(pid, next) { + postTools.privileges(pid, uid, function(err, privileges) { if(err) { - return callback(err); + return next(err); } - Topics.getTopicData(tid, function(err, topicData) { - callback(err, topicData); - }); + if(privileges.editable) { + Topics.movePostToTopic(pid, tid, next); + } else { + next(); + } }); - - function move(pid, next) { - postTools.privileges(pid, uid, function(err, privileges) { - if(privileges.editable) { - Topics.movePostToTopic(pid, tid, next); - } else { - next(); - } - }); - } - }); + } }); }); } @@ -418,6 +395,10 @@ var async = require('async'), Topics.getCategoryData = function(tid, callback) { Topics.getTopicField(tid, 'cid', function(err, cid) { + if(err) { + callback(err); + } + categories.getCategoryData(cid, callback); }); } @@ -838,15 +819,12 @@ var async = require('async'), } function getTeaser(next) { - Topics.getTeaser(tid, function(err, teaser) { - if (err) teaser = {}; - next(null, teaser); - }); + Topics.getTeaser(tid, next); } async.parallel([getTopicData, getReadStatus, getTeaser], function(err, results) { if (err) { - throw new Error(err); + return callback(err); } var topicData = results[0], @@ -863,7 +841,7 @@ var async = require('async'), topicData.teaser_timestamp = utils.toISOString(teaser.timestamp); topicData.teaser_userpicture = teaser.picture; - callback(topicData); + callback(null, topicData); }); } @@ -1151,33 +1129,20 @@ var async = require('async'), Topics.reIndexTopic = function(tid, callback) { Topics.getPids(tid, function(err, pids) { if (err) { - callback(err); - } else { - posts.reIndexPids(pids, function(err) { - if (err) { - callback(err); - } else { - callback(null); - } - }); + return callback(err); } + + posts.reIndexPids(pids, callback); }); } Topics.reIndexAll = function(callback) { db.getSetMembers('topics:tid', function(err, tids) { if (err) { - callback(err, null); - } else { - - async.each(tids, Topics.reIndexTopic, function(err) { - if (err) { - callback(err, null); - } else { - callback(null, 'All topics reindexed.'); - } - }); + return callback(err); } + + async.each(tids, Topics.reIndexTopic, callback); }); } diff --git a/src/user.js b/src/user.js index 3fc8dca0da..12ed7b38b7 100644 --- a/src/user.js +++ b/src/user.js @@ -348,6 +348,23 @@ var bcrypt = require('bcrypt'), } }; + User.isReadyToPost = function(uid, callback) { + User.getUserField(uid, 'lastposttime', function(err, lastposttime) { + if(err) { + return callback(err); + } + + if(!lastposttime) { + lastposttime = 0; + } + + if (Date.now() - parseInt(lastposttime, 10) < parseInt(meta.config.postDelay, 10) * 1000) { + return callback(new Error('too-many-posts')); + } + callback(); + }); + } + User.isEmailAvailable = function(email, callback) { db.isObjectField('email:uid', email, function(err, exists) { callback(err, !exists); From dcc6ec797e9d03c5f8d6853fb1ac879604862d55 Mon Sep 17 00:00:00 2001 From: akhoury Date: Thu, 23 Jan 2014 20:35:53 -0500 Subject: [PATCH 5/9] removed use_proxy config, cleaned-up some comments and sneaked in a time log print on initialization, also, related to pull#820 --- app.js | 1 + src/install.js | 7 ------- src/webserver.js | 20 ++++---------------- 3 files changed, 5 insertions(+), 23 deletions(-) diff --git a/app.js b/app.js index 375a74c695..8dec4daa79 100644 --- a/app.js +++ b/app.js @@ -74,6 +74,7 @@ nconf.set('upload_url', path.join(path.sep, nconf.get('relative_path'), 'uploads', path.sep)); nconf.set('base_dir', __dirname); + winston.info('Time: ' + new Date()); winston.info('Initializing NodeBB v' + pkg.version + ', on port ' + nconf.get('port') + ', using ' + nconf.get('database') +' store at ' + nconf.get(nconf.get('database') + ':host') + ':' + nconf.get(nconf.get('database') + ':port') + '.'); winston.info('NodeBB instance bound to: ' + ((nconf.get('bind_address') === "0.0.0.0" || !nconf.get('bind_address')) ? 'Any address (0.0.0.0)' : nconf.get('bind_address'))); diff --git a/src/install.js b/src/install.js index eae4de5dfa..3ce7537831 100644 --- a/src/install.js +++ b/src/install.js @@ -26,12 +26,6 @@ var async = require('async'), 'default': (nconf.get('use_port') !== undefined ? (nconf.get('use_port') ? 'y' : 'n') : 'y'), pattern: /y[es]*|n[o]?/, message: 'Please enter \'yes\' or \'no\'' - }, { - name: 'use_proxy', - description: 'is NodeBB behind a proxy?', - 'default': (nconf.get('use_proxy') !== undefined ? (nconf.get('use_proxy') ? 'y' : 'n') : 'y'), - pattern: /y[es]*|n[o]?/, - message: 'Please enter \'yes\' or \'no\'' }, { name: 'secret', description: 'Please enter a NodeBB secret', @@ -147,7 +141,6 @@ var async = require('async'), config.bcrypt_rounds = 12; config.upload_path = '/public/uploads'; config.use_port = config.use_port.slice(0, 1) === 'y'; - config.use_proxy = config.use_proxy.slice(0, 1) === 'y'; var urlObject = url.parse(config.base_url), relative_path = (urlObject.pathname && urlObject.pathname.length > 1) ? urlObject.pathname : '', diff --git a/src/webserver.js b/src/webserver.js index 1dcc5df18b..ebffd122b2 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -191,23 +191,11 @@ if(nconf.get('ssl')) { app.use(express.csrf()); - // negative boolean with type check here to support a config.json without a 'use_proxy' value, - // so unless it's specifically set to false, it's true (as long as it's not a dev env) - // todo: remove double negative with a minor release, where backward compatibility can be broken - // and if dev mode, then it's probably not behind a proxy but it can be forced by setting 'use_proxy' to true - - if (nconf.get('use_proxy') === false) { - winston.info('\'use_proxy\' is set to false in config file, skipping \'trust proxy\''); - - } else if (!nconf.get('use_proxy') && process.env.NODE_ENV === 'development') { - winston.info('\'use_proxy\' is not set, skipping because you\'re in development env. Set to true to force enabling it.'); - - } else { - winston.info('\'use_proxy\'' - + (nconf.get('use_proxy') === true ? ' is set to true ' : ' is not set ') - + 'in config file, enabling \'trust proxy\', set to false to disable it.'); - + if (nconf.get('port') != 80 && nconf.get('port') != 443 && nconf.get('use_port') === true) { + winston.info('Enabling \'trust proxy\''); app.enable('trust proxy'); + } else if (process.env.NODE_ENV !== 'development') { + winston.info('Using ports 80 and 443 is not recommend; use a proxy instead. See README.md'); } // Local vars, other assorted setup From 3793ddc2e87999b1ab7c56ed26732612e17bff12 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 23 Jan 2014 21:35:25 -0500 Subject: [PATCH 6/9] fixes user tooltips on infinit load --- public/src/forum/recent.js | 1 + public/src/forum/topic.js | 3 ++- public/src/forum/unread.js | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/public/src/forum/recent.js b/public/src/forum/recent.js index 24ad184a6b..8e0eaeb293 100644 --- a/public/src/forum/recent.js +++ b/public/src/forum/recent.js @@ -91,6 +91,7 @@ define(function() { html = $(translatedHTML); container.append(html); $('span.timeago').timeago(); + app.createUserTooltips(); app.makeNumbersHumanReadable(html.find('.human-readable-number')); }); } diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 5b4e10408c..a48d8c82cf 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -1110,6 +1110,7 @@ define(['composer'], function(composer) { infiniteLoaderActive = false; app.populateOnlineUsers(); + app.createUserTooltips(); app.addCommasToNumbers(); $('span.timeago').timeago(); $('.post-content img').addClass('img-responsive'); @@ -1139,7 +1140,7 @@ define(['composer'], function(composer) { $('#topic-post-count').html(Topic.postCount); updateHeader(); } - }) + }); } function loadMorePosts(tid, callback) { diff --git a/public/src/forum/unread.js b/public/src/forum/unread.js index 9ceb522f87..abd19067de 100644 --- a/public/src/forum/unread.js +++ b/public/src/forum/unread.js @@ -81,6 +81,7 @@ define(function() { html = $(translatedHTML); container.append(html); $('span.timeago').timeago(); + app.createUserTooltips(); app.makeNumbersHumanReadable(html.find('.human-readable-number')); }); } From 1d666bf7f0ee85909da7908d3386704d9f41b83c Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 23 Jan 2014 22:45:11 -0500 Subject: [PATCH 7/9] possible fix to #840 --- public/src/ajaxify.js | 13 ++++++++----- public/src/forum/topic.js | 1 + 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index 5401d24ff2..5ce58f34b7 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -34,6 +34,12 @@ var ajaxify = {}; ajaxify.currentPage = null; ajaxify.go = function (url, callback, quiet) { + if ($('#content').hasClass('ajaxifying')) { + return true; + } + + jQuery('#footer, #content').addClass('ajaxifying'); + // start: the following should be set like so: ajaxify.onchange(function(){}); where the code actually belongs $(window).off('scroll'); app.enterRoom('global'); @@ -48,9 +54,6 @@ var ajaxify = {}; window.onscroll = null; // end - if ($('#content').hasClass('ajaxifying')) { - templates.cancelRequest(); - } // Remove trailing slash url = url.replace(/\/$/, ""); @@ -98,11 +101,12 @@ var ajaxify = {}; translator.load(tpl_url); - jQuery('#footer, #content').removeClass('hide').addClass('ajaxifying'); + jQuery('#footer, #content').removeClass('hide'); templates.flush(); templates.load_template(function () { exec_body_scripts(content); + require(['forum/' + tpl_url], function(script) { if (script && script.init) { script.init(); @@ -172,7 +176,6 @@ var ajaxify = {}; var url = this.href.replace(rootUrl + '/', ''); if (ajaxify.go(url)) { - e.preventDefault(); } } else if (window.location.pathname !== '/outgoing') { diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index a48d8c82cf..223cc25ad1 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -366,6 +366,7 @@ define(['composer'], function(composer) { }); } + $('.topic').on('click', '.post_reply', function() { var selectionText = '', selection = window.getSelection() || document.getSelection(); From f3bdc553481940d89c83484a329b495402b30356 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Thu, 23 Jan 2014 23:31:10 -0500 Subject: [PATCH 8/9] little tweak to user search show timing and 10 users, sort after slicing --- public/src/forum/admin/groups.js | 14 +++++++------- public/src/forum/admin/users.js | 8 ++++---- public/src/forum/users.js | 8 +++++--- src/socket.io/admin.js | 4 ++-- src/user.js | 32 +++++++++++++++++--------------- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/public/src/forum/admin/groups.js b/public/src/forum/admin/groups.js index 93de7bdbdc..ad530c12a9 100644 --- a/public/src/forum/admin/groups.js +++ b/public/src/forum/admin/groups.js @@ -127,16 +127,16 @@ define(function() { foundUserLabel = foundUser.getElementsByTagName('span')[0]; socket.emit('admin.user.search', searchText, function(err, results) { - if (!err && results && results.length > 0) { - var numResults = results.length, + if (!err && results && results.users.length > 0) { + var numResults = results.users.length, resultsSlug = document.createDocumentFragment(), x; if (numResults > 4) numResults = 4; for (x = 0; x < numResults; x++) { - foundUserImg.src = results[x].picture; - foundUserLabel.innerHTML = results[x].username; - foundUser.setAttribute('title', results[x].username); - foundUser.setAttribute('data-uid', results[x].uid); + foundUserImg.src = results.users[x].picture; + foundUserLabel.innerHTML = results.users[x].username; + foundUser.setAttribute('title', results.users[x].username); + foundUser.setAttribute('data-uid', results.users[x].uid); resultsSlug.appendChild(foundUser.cloneNode(true)); } @@ -172,7 +172,7 @@ define(function() { groupMembersEl.on('click', 'li[data-uid]', function() { var uid = this.getAttribute('data-uid'), gid = detailsModal.attr('data-gid'); - + socket.emit('admin.groups.get', gid, function(err, groupObj){ if (!err){ if (groupObj.name == 'Administrators' && uid == yourid){ diff --git a/public/src/forum/admin/users.js b/public/src/forum/admin/users.js index e890329118..98dd235957 100644 --- a/public/src/forum/admin/users.js +++ b/public/src/forum/admin/users.js @@ -196,23 +196,23 @@ define(function() { socket.emit('admin.user.search', username, function(err, data) { if(err) { return app.alertError(err.message); - }console.log(data) + } var html = templates.prepare(templates['admin/users'].blocks['users']).parse({ - users: data + users: data.users }), userListEl = document.querySelector('.users'); userListEl.innerHTML = html; jQuery('.fa-spinner').addClass('none'); - if (data && data.length === 0) { + if (data && data.users.length === 0) { $('#user-notfound-notify').html('User not found!') .show() .addClass('label-danger') .removeClass('label-success'); } else { - $('#user-notfound-notify').html(data.length + ' user' + (data.length > 1 ? 's' : '') + ' found!') + $('#user-notfound-notify').html(data.users.length + ' user' + (data.users.length > 1 ? 's' : '') + ' found! Search took ' + data.timing + ' ms.') .show() .addClass('label-success') .removeClass('label-danger'); diff --git a/public/src/forum/users.js b/public/src/forum/users.js index 77e086259b..fcf52e897a 100644 --- a/public/src/forum/users.js +++ b/public/src/forum/users.js @@ -52,6 +52,8 @@ define(function() { return app.alert(err.message); } + console.log(data); + if (!data) { $('#user-notfound-notify').html('You need to be logged in to search!'); $('#user-notfound-notify').parent().addClass('btn-warning label-warning'); @@ -59,18 +61,18 @@ define(function() { } var html = templates.prepare(templates['users'].blocks['users']).parse({ - users: data + users: data.users }), userListEl = $('#users-container'); userListEl.html(html); - if (data && data.length === 0) { + if (data && data.users.length === 0) { $('#user-notfound-notify').html('User not found!'); $('#user-notfound-notify').parent().addClass('btn-warning label-warning'); } else { - $('#user-notfound-notify').html(data.length + ' user' + (data.length > 1 ? 's' : '') + ' found!'); + $('#user-notfound-notify').html(data.users.length + ' user' + (data.users.length > 1 ? 's' : '') + ' found! Search took ' + data.timing + ' ms.'); $('#user-notfound-notify').parent().addClass('btn-success label-success'); } diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 9ad8abe14d..fbaac2b2b8 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -91,7 +91,7 @@ SocketAdmin.user.search = function(socket, username, callback) { }); } - async.each(data, isAdmin, function(err) { + async.each(data.users, isAdmin, function(err) { callback(err, data); }); }); @@ -126,7 +126,7 @@ SocketAdmin.categories.search = function(socket, data, callback) { cid = data.cid; user.search(username, function(err, data) { - async.map(data, function(userObj, next) { + async.map(data.users, function(userObj, next) { CategoryTools.privileges(cid, userObj.uid, function(err, privileges) { if(err) { return next(err); diff --git a/src/user.js b/src/user.js index 12ed7b38b7..2a8cabaaf7 100644 --- a/src/user.js +++ b/src/user.js @@ -490,34 +490,36 @@ var bcrypt = require('bcrypt'), User.search = function(query, callback) { if (!query || query.length === 0) { - return callback(null, []); + return callback(null, {timing:0, users:[]}); } + var start = process.hrtime(); - // TODO: Have this use db.getObjectKeys (doesn't exist yet) db.getObject('username:uid', function(err, usernamesHash) { if (err) { - return callback(null, []); + return callback(null, {timing: 0, users:[]}); } + query = query.toLowerCase(); + var usernames = Object.keys(usernamesHash), results = []; - results = usernames.filter(function(username) { // Remove non-matches - return username.indexOf(query) === 0; - }).sort(function(a, b) { // Sort alphabetically + results = usernames.filter(function(username) { + return username.toLowerCase().indexOf(query) === 0; + }) + .slice(0, 10) + .sort(function(a, b) { return a > b; - }).slice(0, 5) // Limit 5 - .map(function(username) { // Translate to uids + }) + .map(function(username) { return usernamesHash[username]; }); - if (results && results.length) { - User.getDataForUsers(results, function(userdata) { - callback(null, userdata); - }); - } else { - callback(null, []); - } + User.getDataForUsers(results, function(userdata) { + var diff = process.hrtime(start); + var timing = (diff[0] * 1e3 + diff[1] / 1e6).toFixed(1); + callback(null, {timing: timing, users: userdata}); + }); }); }; From c91ae0c4c4ab3193c2b37cd50fadfb1c2585c266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 24 Jan 2014 00:42:34 -0500 Subject: [PATCH 9/9] Update categories.js --- tests/categories.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/categories.js b/tests/categories.js index 009d195e92..6f83d774f8 100644 --- a/tests/categories.js +++ b/tests/categories.js @@ -1,5 +1,4 @@ -// this test currently needs to talk to the redis database. -// get the redis config info from root directory's config.json: + var winston = require('winston'); process.on('uncaughtException', function (err) { @@ -59,4 +58,4 @@ describe('Categories', function() { db.delete('category:' + categoryObj.cid); db.listRemoveLast('categories:cid'); }); -}); \ No newline at end of file +});