From 72c75bd7813b2bb41dc175ce6f7fe9fa58eed006 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 16 Sep 2016 00:17:43 +0300 Subject: [PATCH] add pagination to flags page --- public/src/admin/manage/flags.js | 36 +--------------- src/controllers/admin/events.js | 2 +- src/controllers/admin/flags.js | 19 ++++++--- src/posts/flags.js | 70 ++++++++++++++++++-------------- src/socket.io/posts/flag.js | 31 -------------- src/user.js | 6 ++- src/views/admin/manage/flags.tpl | 1 + 7 files changed, 61 insertions(+), 104 deletions(-) diff --git a/public/src/admin/manage/flags.js b/public/src/admin/manage/flags.js index ae44e9232c..aa988608e7 100644 --- a/public/src/admin/manage/flags.js +++ b/public/src/admin/manage/flags.js @@ -2,11 +2,10 @@ /*global define, socket, app, utils, bootbox, ajaxify*/ define('admin/manage/flags', [ - 'forum/infinitescroll', 'autocomplete', 'Chart', 'components' -], function(infinitescroll, autocomplete, Chart, components) { +], function(autocomplete, Chart, components) { var Flags = {}; @@ -20,7 +19,6 @@ define('admin/manage/flags', [ handleDismiss(); handleDismissAll(); handleDelete(); - handleInfiniteScroll(); handleGraphs(); updateFlagDetails(ajaxify.data.posts); @@ -79,38 +77,6 @@ define('admin/manage/flags', [ }); } - function handleInfiniteScroll() { - infinitescroll.init(function(direction) { - if (direction < 0 && !$('.flags').length) { - return; - } - var params = utils.params(); - var sortBy = params.sortBy || 'count'; - var byUsername = params.byUsername || ''; - - infinitescroll.loadMore('posts.getMoreFlags', { - byUsername: byUsername, - sortBy: sortBy, - after: $('[data-next]').attr('data-next') - }, function(data, done) { - if (data.posts && data.posts.length) { - app.parseAndTranslate('admin/manage/flags', 'posts', { - posts: data.posts, - assignees: ajaxify.data.assignees - }, function(html) { - $('[data-next]').attr('data-next', data.next); - $('.post-container').append(html); - html.find('img:not(.not-responsive)').addClass('img-responsive'); - updateFlagDetails(data.posts); - done(); - }); - } else { - done(); - } - }); - }); - } - function handleGraphs() { var dailyCanvas = document.getElementById('flags:daily'); var dailyLabels = utils.getDaysArray().map(function(text, idx) { diff --git a/src/controllers/admin/events.js b/src/controllers/admin/events.js index 0e431d3d4e..9f7bd9a2f2 100644 --- a/src/controllers/admin/events.js +++ b/src/controllers/admin/events.js @@ -13,7 +13,7 @@ eventsController.get = function(req, res, next) { var page = parseInt(req.query.page, 10) || 1; var itemsPerPage = 20; - var start = (page - 1) * 20; + var start = (page - 1) * itemsPerPage; var stop = start + itemsPerPage - 1; async.parallel({ diff --git a/src/controllers/admin/flags.js b/src/controllers/admin/flags.js index f21cc773ad..56852a6a65 100644 --- a/src/controllers/admin/flags.js +++ b/src/controllers/admin/flags.js @@ -4,19 +4,23 @@ var async = require('async'); var posts = require('../../posts'); var user = require('../../user'); var analytics = require('../../analytics'); +var pagination = require('../../pagination'); var flagsController = {}; flagsController.get = function(req, res, next) { var sortBy = req.query.sortBy || 'count'; var byUsername = req.query.byUsername || ''; - var start = 0; - var stop = 19; + + var page = parseInt(req.query.page, 10) || 1; + var itemsPerPage = 20; + var start = (page - 1) * itemsPerPage; + var stop = start + itemsPerPage - 1; async.waterfall([ function (next) { async.parallel({ - posts: function(next) { + flagData: function(next) { if (byUsername) { posts.getUserFlags(byUsername, sortBy, req.uid, start, stop, next); } else { @@ -27,7 +31,9 @@ flagsController.get = function(req, res, next) { analytics: function(next) { analytics.getDailyStatsForSet('analytics:flags', Date.now(), 30, next); }, - assignees: async.apply(user.getAdminsandGlobalMods) + assignees: function(next) { + user.getAdminsandGlobalMods(next); + } }, next); } ], function (err, results) { @@ -49,12 +55,15 @@ flagsController.get = function(req, res, next) { return userObj; }); + var pageCount = Math.max(1, Math.ceil(results.flagData.count / itemsPerPage)); + var data = { - posts: results.posts, + posts: results.flagData.posts, assignees: results.assignees, analytics: results.analytics, next: stop + 1, byUsername: byUsername, + pagination: pagination.create(page, pageCount, req.query), title: '[[pages:flagged-posts]]' }; res.render('admin/manage/flags', data); diff --git a/src/posts/flags.js b/src/posts/flags.js index 152414e436..566a03ae21 100644 --- a/src/posts/flags.js +++ b/src/posts/flags.js @@ -154,12 +154,48 @@ module.exports = function(Posts) { }; Posts.getFlags = function(set, uid, start, stop, callback) { + async.parallel({ + count: function(next) { + db.sortedSetCard(set, next); + }, + posts: function(next) { + async.waterfall([ + function (next) { + db.getSortedSetRevRange(set, start, stop, next); + }, + function (pids, next) { + getFlaggedPostsWithReasons(pids, uid, next); + } + ], next); + } + }, callback); + }; + + Posts.getUserFlags = function(byUsername, sortBy, callerUID, start, stop, callback) { + var count = 0; async.waterfall([ - function (next) { - db.getSortedSetRevRange(set, start, stop, next); + function(next) { + user.getUidByUsername(byUsername, next); }, - function (pids, next) { - getFlaggedPostsWithReasons(pids, uid, next); + function(uid, next) { + if (!uid) { + return next(null, []); + } + + db.getSortedSetRevRange('uid:' + uid + ':flag:pids', 0, -1, next); + }, + function(pids, next) { + count = pids.length; + getFlaggedPostsWithReasons(pids, callerUID, next); + }, + function(posts, next) { + if (sortBy === 'count') { + posts.sort(function(a, b) { + return b.flags - a.flags; + }); + } + + next(null, {posts: posts.slice(start, stop === -1 ? undefined : stop + 1), count: count}); } ], callback); }; @@ -242,32 +278,6 @@ module.exports = function(Posts) { ], callback); } - Posts.getUserFlags = function(byUsername, sortBy, callerUID, start, stop, callback) { - async.waterfall([ - function(next) { - user.getUidByUsername(byUsername, next); - }, - function(uid, next) { - if (!uid) { - return next(null, []); - } - db.getSortedSetRevRange('uid:' + uid + ':flag:pids', 0, -1, next); - }, - function(pids, next) { - getFlaggedPostsWithReasons(pids, callerUID, next); - }, - function(posts, next) { - if (sortBy === 'count') { - posts.sort(function(a, b) { - return b.flags - a.flags; - }); - } - - next(null, posts.slice(start, stop)); - } - ], callback); - }; - Posts.updateFlagData = function(uid, pid, flagObj, callback) { // Retrieve existing flag data to compare for history-saving purposes var changes = []; diff --git a/src/socket.io/posts/flag.js b/src/socket.io/posts/flag.js index 97565753d6..583de733fb 100644 --- a/src/socket.io/posts/flag.js +++ b/src/socket.io/posts/flag.js @@ -136,37 +136,6 @@ module.exports = function(SocketPosts) { ], callback); }; - SocketPosts.getMoreFlags = function(socket, data, callback) { - if (!data || !parseInt(data.after, 10)) { - return callback('[[error:invalid-data]]'); - } - var sortBy = data.sortBy || 'count'; - var byUsername = data.byUsername || ''; - var start = parseInt(data.after, 10); - var stop = start + 19; - - async.waterfall([ - function (next) { - user.isAdminOrGlobalMod(socket.uid, next); - }, - function (isAdminOrGlobalModerator, next) { - if (!isAdminOrGlobalModerator) { - return next(new Error('[[no-privileges]]')); - } - - if (byUsername) { - posts.getUserFlags(byUsername, sortBy, socket.uid, start, stop, next); - } else { - var set = sortBy === 'count' ? 'posts:flags:count' : 'posts:flagged'; - posts.getFlags(set, socket.uid, start, stop, next); - } - }, - function (posts, next) { - next(null, {posts: posts, next: stop + 1}); - } - ], callback); - }; - SocketPosts.updateFlag = function(socket, data, callback) { if (!data || !(data.pid && data.data)) { return callback('[[error:invalid-data]]'); diff --git a/src/user.js b/src/user.js index 48abf3f28c..50adb467e4 100644 --- a/src/user.js +++ b/src/user.js @@ -269,8 +269,10 @@ var utils = require('../public/src/utils'); if (err) { return callback(err); } - - User.getUsersData(results.admins.concat(results.mods), callback); + var uids = results.admins.concat(results.mods).filter(function(uid, index, array) { + return uid && array.indexOf(uid) === index; + }); + User.getUsersData(uids, callback); }); }; diff --git a/src/views/admin/manage/flags.tpl b/src/views/admin/manage/flags.tpl index 5a9dca8edf..4a87588467 100644 --- a/src/views/admin/manage/flags.tpl +++ b/src/views/admin/manage/flags.tpl @@ -167,6 +167,7 @@ +