add pagination to flags page

v1.18.x
barisusakli 8 years ago
parent dd78f46980
commit 72c75bd781

@ -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) {

@ -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({

@ -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);

@ -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 = [];

@ -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]]');

@ -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);
});
};

@ -167,6 +167,7 @@
</div>
</div>
<!-- END posts -->
<!-- IMPORT partials/paginator.tpl -->
</div>
</div>
</div>

Loading…
Cancel
Save