feat: flags list sorting, closes #8569

This commit is contained in:
Julian Lam
2020-08-18 21:03:59 -04:00
parent cabe62a06c
commit 346db0d84d
4 changed files with 68 additions and 16 deletions

View File

@@ -112,8 +112,8 @@ Flags.get = async function (flagId) {
return data.flag;
};
Flags.list = async function (filters, uid) {
filters = filters || {};
Flags.list = async function (data) {
const filters = data.filters || {};
let sets = [];
const orSets = [];
@@ -125,7 +125,7 @@ Flags.list = async function (filters, uid) {
for (var type in filters) {
if (filters.hasOwnProperty(type)) {
if (Flags._filters.hasOwnProperty(type)) {
Flags._filters[type](sets, orSets, filters[type], uid);
Flags._filters[type](sets, orSets, filters[type], data.uid);
} else {
winston.warn('[flags/list] No flag filter type found: ' + type);
}
@@ -151,6 +151,8 @@ Flags.list = async function (filters, uid) {
}
}
flagIds = await Flags.sort(flagIds, data.sort);
// Create subset for parsing based on page number (n=20)
const flagsPerPage = Math.abs(parseInt(filters.perPage, 10) || 1);
const pageCount = Math.ceil(flagIds.length / flagsPerPage);
@@ -174,19 +176,42 @@ Flags.list = async function (filters, uid) {
});
}));
const data = await plugins.fireHook('filter:flags.list', {
const payload = await plugins.fireHook('filter:flags.list', {
flags: flags,
page: filters.page,
uid: uid,
uid: data.uid,
});
return {
flags: data.flags,
page: data.page,
flags: payload.flags,
page: payload.page,
pageCount: pageCount,
};
};
Flags.sort = async function (flagIds, sort) {
switch (sort) {
// 'newest' is not handled because that is default
case 'oldest':
flagIds = flagIds.reverse();
break;
case 'reports': {
const keys = flagIds.map(id => `flag:${id}:reports`);
const heat = await db.sortedSetsCard(keys);
const mapped = heat.map((el, i) => ({
index: i, heat: el,
}));
mapped.sort(function (a, b) {
return b.heat - a.heat;
});
flagIds = mapped.map(obj => flagIds[obj.index]);
break;
}
}
return flagIds;
};
Flags.validate = async function (payload) {
const [target, reporter] = await Promise.all([
Flags.getTarget(payload.type, payload.id, payload.uid),