Fix last_callers collapsing & DRY code

This commit is contained in:
Bryan Ashby 2018-07-21 21:38:06 -06:00
parent 48f7456d4a
commit f601fd256b
1 changed files with 19 additions and 29 deletions

View File

@ -124,41 +124,31 @@ exports.getModule = class LastCallersModule extends MenuModule {
const hideSysOp = _.get(this, 'menuConfig.config.sysop.hide'); const hideSysOp = _.get(this, 'menuConfig.config.sysop.hide');
const sysOpCollapse = this.getCollapse('menuConfig.config.sysop.collapse'); const sysOpCollapse = this.getCollapse('menuConfig.config.sysop.collapse');
if(hideSysOp) { const collapseList = (withUserId, minAge) => {
loginHistory = loginHistory.filter(item => false === User.isRootUserId(item.userId));
} else if(sysOpCollapse) {
// :TODO: DRY op & user collapse code
const maxAge = sysOpCollapse.asSeconds();
let lastUserId; let lastUserId;
let lastTimestamp; let lastTimestamp;
loginHistory = loginHistory.filter(item => { loginHistory = loginHistory.filter(item => {
const op = User.isRootUserId(item.userId); const secApart = lastTimestamp ? moment.duration(lastTimestamp.diff(item.timestamp)).asSeconds() : 0;
const repeat = lastUserId === item.userId; const collapse = (null === withUserId ? true : withUserId === item.userId) &&
const recent = lastTimestamp ? moment.duration(lastTimestamp.diff(item.timestamp)).seconds() < maxAge : false; (lastUserId === item.userId) &&
(secApart < minAge);
lastUserId = item.userId; lastUserId = item.userId;
lastTimestamp = item.timestamp; lastTimestamp = item.timestamp;
return !op || !repeat || !recent; return !collapse;
}); });
};
if(hideSysOp) {
loginHistory = loginHistory.filter(item => false === User.isRootUserId(item.userId));
} else if(sysOpCollapse) {
collapseList(User.RootUserID, sysOpCollapse.asSeconds());
} }
const userCollapse = this.getCollapse('menuConfig.config.user.collapse'); const userCollapse = this.getCollapse('menuConfig.config.user.collapse');
if(userCollapse) { if(userCollapse) {
const maxAge = userCollapse.asSeconds(); collapseList(null, userCollapse.asSeconds());
let lastUserId;
let lastTimestamp;
loginHistory = loginHistory.filter(item => {
const repeat = lastUserId === item.userId;
const recent = lastTimestamp ? moment.duration(lastTimestamp.diff(item.timestamp)).seconds() < maxAge : false;
lastUserId = item.userId;
lastTimestamp = item.timestamp;
return !repeat || !recent;
});
} }
return cb( return cb(