mirror of
https://github.com/NuSkooler/enigma-bbs.git
synced 2025-06-04 19:57:20 +02:00
Major changes around events, event log, etc.
* User event log is now functional & attached to various events * Add additional missing system events * Completely re-write last_callers to have new functionality, etc. * Events.addListenerMultipleEvents() * New 'moduleInitialize' export for module init vs Event specific registerEvents * Add docs on last_callers mod
This commit is contained in:
parent
c1ae3d88ba
commit
52585c78f0
16 changed files with 392 additions and 171 deletions
|
@ -2,27 +2,16 @@
|
|||
'use strict';
|
||||
|
||||
// ENiGMA½
|
||||
const MenuModule = require('./menu_module.js').MenuModule;
|
||||
const ViewController = require('./view_controller.js').ViewController;
|
||||
const StatLog = require('./stat_log.js');
|
||||
const User = require('./user.js');
|
||||
const stringFormat = require('./string_format.js');
|
||||
const { MenuModule } = require('./menu_module.js');
|
||||
const StatLog = require('./stat_log.js');
|
||||
const User = require('./user.js');
|
||||
const sysDb = require('./database.js').dbs.system;
|
||||
|
||||
// deps
|
||||
const moment = require('moment');
|
||||
const async = require('async');
|
||||
const _ = require('lodash');
|
||||
|
||||
/*
|
||||
Available listFormat object members:
|
||||
userId
|
||||
userName
|
||||
location
|
||||
affiliation
|
||||
ts
|
||||
|
||||
*/
|
||||
|
||||
exports.moduleInfo = {
|
||||
name : 'Last Callers',
|
||||
desc : 'Last callers to the system',
|
||||
|
@ -37,6 +26,9 @@ const MciCodeIds = {
|
|||
exports.getModule = class LastCallersModule extends MenuModule {
|
||||
constructor(options) {
|
||||
super(options);
|
||||
|
||||
this.actionIndicators = _.get(options, 'menuConfig.config.actionIndicators', {});
|
||||
this.actionIndicatorDefault = _.get(options, 'menuConfig.config.actionIndicatorDefault', '-');
|
||||
}
|
||||
|
||||
mciReady(mciData, cb) {
|
||||
|
@ -45,107 +37,191 @@ exports.getModule = class LastCallersModule extends MenuModule {
|
|||
return cb(err);
|
||||
}
|
||||
|
||||
const self = this;
|
||||
const vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
|
||||
|
||||
let loginHistory;
|
||||
let callersView;
|
||||
|
||||
async.series(
|
||||
async.waterfall(
|
||||
[
|
||||
function loadFromConfig(callback) {
|
||||
const loadOpts = {
|
||||
callingMenu : self,
|
||||
mciMap : mciData.menu,
|
||||
noInput : true,
|
||||
};
|
||||
|
||||
vc.loadFromMenuConfig(loadOpts, callback);
|
||||
},
|
||||
function fetchHistory(callback) {
|
||||
callersView = vc.getView(MciCodeIds.CallerList);
|
||||
|
||||
// fetch up
|
||||
StatLog.getSystemLogEntries('user_login_history', StatLog.Order.TimestampDesc, 200, (err, lh) => {
|
||||
loginHistory = lh;
|
||||
|
||||
if(self.menuConfig.config.hideSysOpLogin) {
|
||||
const noOpLoginHistory = loginHistory.filter(lh => {
|
||||
return false === User.isRootUserId(parseInt(lh.log_value)); // log_value=userId
|
||||
});
|
||||
|
||||
//
|
||||
// If we have enough items to display, or hideSysOpLogin is set to 'always',
|
||||
// then set loginHistory to our filtered list. Else, we'll leave it be.
|
||||
//
|
||||
if(noOpLoginHistory.length >= callersView.dimens.height || 'always' === self.menuConfig.config.hideSysOpLogin) {
|
||||
loginHistory = noOpLoginHistory;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Finally, we need to trim up the list to the needed size
|
||||
//
|
||||
loginHistory = loginHistory.slice(0, callersView.dimens.height);
|
||||
|
||||
return callback(err);
|
||||
(next) => {
|
||||
this.prepViewController('callers', 0, mciData.menu, err => {
|
||||
return next(err);
|
||||
});
|
||||
},
|
||||
function getUserNamesAndProperties(callback) {
|
||||
const getPropOpts = {
|
||||
names : [ 'location', 'affiliation' ]
|
||||
};
|
||||
|
||||
const dateTimeFormat = self.menuConfig.config.dateTimeFormat || 'ddd MMM DD';
|
||||
|
||||
async.each(
|
||||
loginHistory,
|
||||
(item, next) => {
|
||||
item.userId = parseInt(item.log_value);
|
||||
item.ts = moment(item.timestamp).format(dateTimeFormat);
|
||||
|
||||
User.getUserName(item.userId, (err, userName) => {
|
||||
if(err) {
|
||||
item.deleted = true;
|
||||
return next(null);
|
||||
} else {
|
||||
item.userName = userName || 'N/A';
|
||||
|
||||
User.loadProperties(item.userId, getPropOpts, (err, props) => {
|
||||
if(!err && props) {
|
||||
item.location = props.location || 'N/A';
|
||||
item.affiliation = item.affils = (props.affiliation || 'N/A');
|
||||
} else {
|
||||
item.location = 'N/A';
|
||||
item.affiliation = item.affils = 'N/A';
|
||||
}
|
||||
return next(null);
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
err => {
|
||||
loginHistory = loginHistory.filter(lh => true !== lh.deleted);
|
||||
return callback(err);
|
||||
}
|
||||
);
|
||||
(next) => {
|
||||
this.fetchHistory( (err, loginHistory) => {
|
||||
return next(err, loginHistory);
|
||||
});
|
||||
},
|
||||
function populateList(callback) {
|
||||
const listFormat = self.menuConfig.config.listFormat || '{userName} - {location} - {affiliation} - {ts}';
|
||||
|
||||
callersView.setItems(_.map(loginHistory, ce => stringFormat(listFormat, ce) ) );
|
||||
|
||||
(loginHistory, next) => {
|
||||
this.loadUserForHistoryItems(loginHistory, (err, updatedHistory) => {
|
||||
return next(err, updatedHistory);
|
||||
});
|
||||
},
|
||||
(loginHistory, next) => {
|
||||
const callersView = this.viewControllers.callers.getView(MciCodeIds.CallerList);
|
||||
callersView.setItems(loginHistory);
|
||||
callersView.redraw();
|
||||
return callback(null);
|
||||
return next(null);
|
||||
}
|
||||
],
|
||||
(err) => {
|
||||
err => {
|
||||
if(err) {
|
||||
self.client.log.error( { error : err.toString() }, 'Error loading last callers');
|
||||
this.client.log.warn( { error : err.message }, 'Error loading last callers');
|
||||
}
|
||||
cb(err);
|
||||
return cb(err);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
getCollapse(conf) {
|
||||
let collapse = _.get(this, conf);
|
||||
collapse = collapse && collapse.match(/^([0-9]+)\s*(minutes|seconds|hours|days|months)$/);
|
||||
if(collapse) {
|
||||
return moment.duration(parseInt(collapse[1]), collapse[2]);
|
||||
}
|
||||
}
|
||||
|
||||
fetchHistory(cb) {
|
||||
const callersView = this.viewControllers.callers.getView(MciCodeIds.CallerList);
|
||||
if(!callersView || 0 === callersView.dimens.height) {
|
||||
return cb(null);
|
||||
}
|
||||
|
||||
StatLog.getSystemLogEntries(
|
||||
'user_login_history',
|
||||
StatLog.Order.TimestampDesc,
|
||||
200, // max items to fetch - we need more than max displayed for filtering/etc.
|
||||
(err, loginHistory) => {
|
||||
if(err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
const dateTimeFormat = _.get(
|
||||
this, 'menuConfig.config.dateTimeFormat', this.client.currentTheme.helpers.getDateFormat('short'));
|
||||
|
||||
loginHistory = loginHistory.map(item => {
|
||||
try {
|
||||
const historyItem = JSON.parse(item.log_value);
|
||||
if(_.isObject(historyItem)) {
|
||||
item.userId = historyItem.userId;
|
||||
item.sessionId = historyItem.sessionId;
|
||||
} else {
|
||||
item.userId = historyItem; // older format
|
||||
item.sessionId = '-none-';
|
||||
}
|
||||
} catch(e) {
|
||||
return null; // we'll filter this out
|
||||
}
|
||||
|
||||
item.timestamp = moment(item.timestamp);
|
||||
|
||||
return Object.assign(
|
||||
item,
|
||||
{
|
||||
ts : moment(item.timestamp).format(dateTimeFormat)
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
const hideSysOp = _.get(this, 'menuConfig.config.sysop.hide');
|
||||
const sysOpCollapse = this.getCollapse('menuConfig.config.sysop.collapse');
|
||||
|
||||
if(hideSysOp) {
|
||||
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 lastTimestamp;
|
||||
|
||||
loginHistory = loginHistory.filter(item => {
|
||||
const op = User.isRootUserId(item.userId);
|
||||
const repeat = lastUserId === item.userId;
|
||||
const recent = lastTimestamp ? moment.duration(lastTimestamp.diff(item.timestamp)).seconds() < maxAge : false;
|
||||
|
||||
lastUserId = item.userId;
|
||||
lastTimestamp = item.timestamp;
|
||||
|
||||
return !op || !repeat || !recent;
|
||||
});
|
||||
}
|
||||
|
||||
const userCollapse = this.getCollapse('menuConfig.config.user.collapse');
|
||||
if(userCollapse) {
|
||||
const maxAge = 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(
|
||||
null,
|
||||
loginHistory.slice(0, callersView.dimens.height) // trim the fat
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
loadUserForHistoryItems(loginHistory, cb) {
|
||||
const getPropOpts = {
|
||||
names : [ 'real_name', 'location', 'affiliation' ]
|
||||
};
|
||||
|
||||
const actionIndicatorNames = _.map(this.actionIndicators, (v, k) => k);
|
||||
let indicatorSumsSql;
|
||||
if(actionIndicatorNames.length > 0) {
|
||||
indicatorSumsSql = actionIndicatorNames.map(i => {
|
||||
return `SUM(CASE WHEN log_value='${_.snakeCase(i)}' THEN 1 ELSE 0 END) AS ${i}`;
|
||||
});
|
||||
}
|
||||
|
||||
async.map(loginHistory, (item, next) => {
|
||||
User.getUserName(item.userId, (err, userName) => {
|
||||
if(err) {
|
||||
return cb(null, null);
|
||||
}
|
||||
|
||||
item.userName = item.text = (userName || 'N/A');
|
||||
|
||||
User.loadProperties(item.userId, getPropOpts, (err, props) => {
|
||||
item.location = (props && props.location) || 'N/A';
|
||||
item.affiliation = item.affils = (props && props.affiliation) || 'N/A';
|
||||
item.realName = (props && props.real_name) || 'N/A';
|
||||
|
||||
if(!indicatorSumsSql) {
|
||||
return next(null, item);
|
||||
}
|
||||
|
||||
sysDb.get(
|
||||
`SELECT ${indicatorSumsSql.join(', ')}
|
||||
FROM user_event_log
|
||||
WHERE user_id=? AND session_id=?
|
||||
LIMIT 1;`,
|
||||
[ item.userId, item.sessionId ],
|
||||
(err, results) => {
|
||||
if(_.isObject(results)) {
|
||||
item.actions = '';
|
||||
Object.keys(results).forEach(n => {
|
||||
const indicator = results[n] > 0 ? this.actionIndicators[n] || this.actionIndicatorDefault : this.actionIndicatorDefault;
|
||||
item[n] = indicator;
|
||||
item.actions += indicator;
|
||||
});
|
||||
}
|
||||
return next(null, item);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
(err, mapped) => {
|
||||
return cb(err, mapped.filter(item => item)); // remove deleted
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue