From 8e1d441e20d70e61d12a13ec66c48c8c029f3470 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 5 Dec 2016 12:40:25 -0500 Subject: [PATCH] Added some quick filters, #5232 --- public/language/en-GB/flags.json | 5 +++- public/src/client/flags/list.js | 5 ++++ src/controllers/mods.js | 8 ++++--- src/flags.js | 39 ++++++++++++++++++++++++-------- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/public/language/en-GB/flags.json b/public/language/en-GB/flags.json index cdc3c4069e..fbe32a812e 100644 --- a/public/language/en-GB/flags.json +++ b/public/language/en-GB/flags.json @@ -1,5 +1,4 @@ { - "quick-filters": "Quick Filters", "state": "State", "reporter": "Reporter", "reported-at": "Reported At", @@ -9,11 +8,15 @@ "update": "Update", "updated": "Updated", + "quick-filters": "Quick Filters", + "filter-active": "There are one or more filters active in this list of flags", + "filter-reset": "Remove Filters", "filters": "Filter Options", "filter-reporterId": "Reporter UID", "filter-type": "Flag Type", "filter-type-all": "All Content", "filter-type-post": "Post", + "filter-quick-mine": "Assigned to me", "apply-filters": "Apply Filters", "notes": "Flag Notes", diff --git a/public/src/client/flags/list.js b/public/src/client/flags/list.js index 6111372150..2994e7fcf9 100644 --- a/public/src/client/flags/list.js +++ b/public/src/client/flags/list.js @@ -12,6 +12,11 @@ define('forum/flags/list', ['components'], function (components) { Flags.enableFilterForm = function () { var filtersEl = components.get('flags/filters'); + // Parse ajaxify data to set form values to reflect current filters + for(var filter in ajaxify.data.filters) { + filtersEl.find('[name="' + filter + '"]').val(ajaxify.data.filters[filter]); + } + filtersEl.find('button').on('click', function () { var payload = filtersEl.serializeArray(); var qs = payload.map(function (filter) { diff --git a/src/controllers/mods.js b/src/controllers/mods.js index 759de90ef1..f15b1a6b1a 100644 --- a/src/controllers/mods.js +++ b/src/controllers/mods.js @@ -26,7 +26,7 @@ modsController.flags.list = function (req, res, next) { } // Parse query string params for filters - var valid = ['reporterId', 'type']; + var valid = ['reporterId', 'type', 'quick']; var filters = valid.reduce(function (memo, cur) { if (req.query.hasOwnProperty(cur)) { memo[cur] = req.query[cur]; @@ -35,13 +35,15 @@ modsController.flags.list = function (req, res, next) { return memo; }, {}); - flags.list(filters, function (err, flags) { + flags.list(filters, req.uid, function (err, flags) { if (err) { return next(err); } res.render('flags/list', { - flags: flags + flags: flags, + hasFilter: !!Object.keys(filters).length, + filters: filters }); }); }); diff --git a/src/flags.js b/src/flags.js index 6744d8ad59..492e1faa43 100644 --- a/src/flags.js +++ b/src/flags.js @@ -47,7 +47,7 @@ Flags.get = function (flagId, callback) { ], callback); }; -Flags.list = function (filters, callback) { +Flags.list = function (filters, uid, callback) { if (typeof filters === 'function' && !callback) { callback = filters; filters = {}; @@ -64,16 +64,23 @@ Flags.list = function (filters, callback) { case 'reporterId': sets.push('flags:byReporter:' + filters[type]); break; + + case 'quick': + switch (filters.quick) { + case 'mine': + sets.push('flags:byAssignee:' + uid); + break; + } + break; } } - } sets = sets.length ? sets : ['flags:datetime']; // No filter default async.waterfall([ function (next) { if (sets.length === 1) { - db.getSortedSetRevRange(sets[0], 0, 19, next); + db.getSortedSetRevRange(sets[0], 0, -1, next); } else { db.getSortedSetRevIntersect({sets: sets, start: 0, stop: -1, aggregate: 'MAX'}, next); } @@ -316,6 +323,8 @@ Flags.update = function (flagId, uid, changeset, callback) { // Retrieve existing flag data to compare for history-saving purposes var fields = ['state', 'assignee']; var history = []; + var tasks = []; + var now = Date.now(); async.waterfall([ async.apply(db.getObjectFields.bind(db), 'flag:' + flagId, fields), @@ -325,6 +334,18 @@ Flags.update = function (flagId, uid, changeset, callback) { if (current[prop] === changeset[prop]) { delete changeset[prop]; } else { + // Add tasks as necessary + switch (prop) { + case 'state': + tasks.push(async.apply(db.sortedSetAdd.bind(db), 'flags:byState:' + changeset[prop], now, flagId)); + tasks.push(async.apply(db.sortedSetRemove.bind(db), 'flags:byState:' + current[prop], flagId)); + break; + + case 'assignee': + tasks.push(async.apply(db.sortedSetAdd.bind(db), 'flags:byAssignee:' + changeset[prop], now, flagId)); + break; + } + // Append to history payload history.push(prop + ':' + changeset[prop]); } @@ -336,12 +357,12 @@ Flags.update = function (flagId, uid, changeset, callback) { return next(); } - async.parallel([ - // Save new object to db (upsert) - async.apply(db.setObject, 'flag:' + flagId, changeset), - // Append history - async.apply(Flags.appendHistory, flagId, uid, history) - ], function (err, data) { + // Save new object to db (upsert) + tasks.push(async.apply(db.setObject, 'flag:' + flagId, changeset)); + // Append history + tasks.push(async.apply(Flags.appendHistory, flagId, uid, history)) + + async.parallel(tasks, function (err, data) { return next(err); }); }