some light refactoring, details API (#5232)

v1.18.x
Julian Lam 8 years ago
parent 640df0379e
commit 98a104564b

@ -1,5 +1,6 @@
{ {
"quick-filters": "Quick Filters", "quick-filters": "Quick Filters",
"state": "State",
"reporter": "Reporter", "reporter": "Reporter",
"reported-at": "Reported At", "reported-at": "Reported At",
"no-flags": "Hooray! No flags found." "no-flags": "Hooray! No flags found."

@ -6,15 +6,19 @@ var user = require('../user');
var flags = require('../flags'); var flags = require('../flags');
// var adminFlagsController = require('./admin/flags'); // var adminFlagsController = require('./admin/flags');
var modsController = {}; var modsController = {
flags: {}
};
modsController.flagged = function (req, res, next) { modsController.flags.list = function (req, res, next) {
async.parallel({ async.parallel({
isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid), isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid),
moderatedCids: async.apply(user.getModeratedCids, req.uid) moderatedCids: async.apply(user.getModeratedCids, req.uid)
}, function (err, results) { }, function (err, results) {
if (err || !(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) { if (err) {
return next(err); return next(err);
} else if (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) {
return next(new Error('[[error:no-privileges]]'));
} }
if (!results.isAdminOrGlobalMod && results.moderatedCids.length) { if (!results.isAdminOrGlobalMod && results.moderatedCids.length) {
@ -22,11 +26,27 @@ modsController.flagged = function (req, res, next) {
} }
flags.list({}, function(err, flags) { flags.list({}, function(err, flags) {
res.render('flags', { res.render('flags/list', {
flags: flags flags: flags
}); });
}); });
}); });
}; };
modsController.flags.detail = function (req, res, next) {
async.parallel({
isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid),
moderatedCids: async.apply(user.getModeratedCids, req.uid),
flagData: async.apply(flags.get, req.params.flagId)
}, function (err, results) {
if (err || !results.flagData) {
return next(err || new Error('[[error:invalid-data]]'));
} else if (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) {
return next(new Error('[[error:no-privileges]]'));
}
res.render('flags/detail', results.flagData);
});
};
module.exports = modsController; module.exports = modsController;

@ -18,6 +18,30 @@ var Flags = {
} }
}; };
Flags.get = function (flagId, callback) {
async.waterfall([
async.apply(async.parallel, {
base: async.apply(db.getObject.bind(db), 'flag:' + flagId),
history: async.apply(db.getSortedSetRevRange.bind(db), 'flag:' + flagId + ':history', 0, -1),
notes: async.apply(db.getSortedSetRevRange.bind(db), 'flag:' + flagId + ':notes', 0, -1)
}),
function (data, next) {
user.getUserFields(data.base.uid, ['username', 'picture'], function (err, userObj) {
next(err, Object.assign(data.base, {
history: data.history,
notes: data.notes,
reporter: {
username: userObj.username,
picture: userObj.picture,
'icon:bgColor': userObj['icon:bgColor'],
'icon:text': userObj['icon:text']
}
}));
});
}
], callback);
};
Flags.list = function (filters, callback) { Flags.list = function (filters, callback) {
if (typeof filters === 'function' && !callback) { if (typeof filters === 'function' && !callback) {
callback = filters; callback = filters;
@ -33,7 +57,7 @@ Flags.list = function (filters, callback) {
function (flagObj, next) { function (flagObj, next) {
user.getUserFields(flagObj.uid, ['username', 'picture'], function (err, userObj) { user.getUserFields(flagObj.uid, ['username', 'picture'], function (err, userObj) {
next(err, Object.assign(flagObj, { next(err, Object.assign(flagObj, {
user: { reporter: {
username: userObj.username, username: userObj.username,
picture: userObj.picture, picture: userObj.picture,
'icon:bgColor': userObj['icon:bgColor'], 'icon:bgColor': userObj['icon:bgColor'],
@ -63,7 +87,7 @@ Flags.list = function (filters, callback) {
} }
next(null, Object.assign(flagObj, { next(null, Object.assign(flagObj, {
target_readable: flagObj.type.charAt(0).toUpperCase() + flagObj.type.slice(1) + ' ' + flagObj.id, target_readable: flagObj.type.charAt(0).toUpperCase() + flagObj.type.slice(1) + ' ' + flagObj.targetId,
datetimeISO: new Date(parseInt(flagObj.datetime, 10)).toISOString() datetimeISO: new Date(parseInt(flagObj.datetime, 10)).toISOString()
})); }));
}); });
@ -95,18 +119,19 @@ Flags.create = function (type, id, uid, reason, callback) {
} }
}); });
}, },
function (next) { async.apply(db.incrObjectField, 'global', 'nextFlagId'),
var flagId = utils.generateUUID(); function (flagId, next) {
async.parallel([ async.parallel([
async.apply(db.setObject.bind(db), 'flag:' + flagId, Object.assign({}, Flags._defaults, { async.apply(db.setObject.bind(db), 'flag:' + flagId, Object.assign({}, Flags._defaults, {
flagId: flagId,
type: type, type: type,
id: id, targetId: id,
description: reason, description: reason,
uid: uid, uid: uid,
datetime: Date.now() datetime: Date.now()
})), })),
async.apply(db.sortedSetAdd.bind(db), 'flags:datetime', Date.now(), flagId) async.apply(db.sortedSetAdd.bind(db), 'flags:datetime', Date.now(), flagId),
async.apply(db.setObjectField.bind(db), 'flagHash:flagId', [type, id, uid].join(':'), flagId)
], next); ], next);
} }
], function (err) { ], function (err) {
@ -280,32 +305,33 @@ Flags.dismissByUid = function (uid, callback) {
}); });
}; };
Flags.get = function (set, cid, uid, start, stop, callback) { // This is the old method to get list of flags, supercede by Flags.list();
async.waterfall([ // Flags.get = function (set, cid, uid, start, stop, callback) {
function (next) { // async.waterfall([
if (Array.isArray(set)) { // function (next) {
db.getSortedSetRevIntersect({sets: set, start: start, stop: -1, aggregate: 'MAX'}, next); // if (Array.isArray(set)) {
} else { // db.getSortedSetRevIntersect({sets: set, start: start, stop: -1, aggregate: 'MAX'}, next);
db.getSortedSetRevRange(set, start, -1, next); // } else {
} // db.getSortedSetRevRange(set, start, -1, next);
}, // }
function (pids, next) { // },
if (cid) { // function (pids, next) {
posts.filterPidsByCid(pids, cid, next); // if (cid) {
} else { // posts.filterPidsByCid(pids, cid, next);
process.nextTick(next, null, pids); // } else {
} // process.nextTick(next, null, pids);
}, // }
function (pids, next) { // },
getFlaggedPostsWithReasons(pids, uid, next); // function (pids, next) {
}, // getFlaggedPostsWithReasons(pids, uid, next);
function (posts, next) { // },
var count = posts.length; // function (posts, next) {
var end = stop - start + 1; // var count = posts.length;
next(null, {posts: posts.slice(0, stop === -1 ? undefined : end), count: count}); // var end = stop - start + 1;
} // next(null, {posts: posts.slice(0, stop === -1 ? undefined : end), count: count});
], callback); // }
}; // ], callback);
// };
function getFlaggedPostsWithReasons(pids, uid, callback) { function getFlaggedPostsWithReasons(pids, uid, callback) {
async.waterfall([ async.waterfall([

@ -40,7 +40,8 @@ function mainRoutes(app, middleware, controllers) {
} }
function modRoutes(app, middleware, controllers) { function modRoutes(app, middleware, controllers) {
setupPageRoute(app, '/flags', middleware, [], controllers.mods.flagged); setupPageRoute(app, '/flags', middleware, [], controllers.mods.flags.list);
setupPageRoute(app, '/flags/:flagId', middleware, [], controllers.mods.flags.detail);
} }
function globalModRoutes(app, middleware, controllers) { function globalModRoutes(app, middleware, controllers) {

Loading…
Cancel
Save