From 124b79f6aad79dbae0bd46c468ee892fd5a293cc Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 25 Nov 2015 16:00:57 -0500 Subject: [PATCH] fix user search pagination --- src/socket.io/user/search.js | 9 ++++++++- src/user/search.js | 24 +++++------------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/socket.io/user/search.js b/src/socket.io/user/search.js index 9cce95e180..c59b2798d6 100644 --- a/src/socket.io/user/search.js +++ b/src/socket.io/user/search.js @@ -5,6 +5,7 @@ var async = require('async'); var db = require('../../database'); var user = require('../../user'); var meta = require('../../meta'); +var pagination = require('../../pagination'); module.exports = function(SocketUser) { SocketUser.search = function(socket, data, callback) { @@ -21,7 +22,13 @@ module.exports = function(SocketUser) { sortBy: data.sortBy, onlineOnly: data.onlineOnly, uid: socket.uid - }, callback); + }, function(err, result) { + if (err) { + return callback(err); + } + result.pagination = pagination.create(data.page, result.pageCount); + callback(null, result); + }); }; SocketUser.loadSearchPage = function(socket, data, callback) { diff --git a/src/user/search.js b/src/user/search.js index 322c137738..59a3cd063b 100644 --- a/src/user/search.js +++ b/src/user/search.js @@ -3,7 +3,6 @@ var async = require('async'), meta = require('../meta'), - pagination = require('../pagination'), plugins = require('../plugins'), db = require('../database'); @@ -42,9 +41,11 @@ module.exports = function(User) { searchResult.matchCount = uids.length; if (paginate) { - var pagination = User.paginate(page, uids); - uids = pagination.data; - searchResult.pagination = pagination.pagination; + var resultsPerPage = parseInt(meta.config.userSearchResultsPerPage, 10) || 20; + var start = Math.max(0, page - 1) * resultsPerPage; + var stop = start + resultsPerPage; + searchResult.pageCount = Math.ceil(uids.length / resultsPerPage); + uids = uids.slice(start, stop); } User.getUsers(uids, uid, next); @@ -57,21 +58,6 @@ module.exports = function(User) { ], callback); }; - User.paginate = function(page, data) { - var resultsPerPage = parseInt(meta.config.userSearchResultsPerPage, 10) || 20; - var start = Math.max(0, page - 1) * resultsPerPage; - var stop = start + resultsPerPage; - - var pageCount = Math.ceil(data.length / resultsPerPage); - var currentPage = Math.max(1, Math.ceil((start + 1) / resultsPerPage)); - - return { - pagination: pagination.create(currentPage, pageCount), - pageCount: pageCount, - data: data.slice(start, stop) - }; - }; - function findUids(query, searchBy, callback) { if (!query) { return callback(null, []);