2015-07-26 06:20:07 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-07-29 04:31:28 +00:00
|
|
|
var MenuModule = require('../core/menu_module.js').MenuModule;
|
|
|
|
var userDb = require('../core/database.js').dbs.user;
|
|
|
|
var ViewController = require('../core/view_controller.js').ViewController;
|
|
|
|
var TextView = require('../core/text_view.js').TextView;
|
|
|
|
var getUserLoginHistory = require('../core/stats.js').getUserLoginHistory;
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var moment = require('moment');
|
|
|
|
var async = require('async');
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-26 06:20:07 +00:00
|
|
|
exports.moduleInfo = {
|
2015-07-28 04:10:20 +00:00
|
|
|
name : 'Last Callers',
|
|
|
|
desc : 'Last callers to the system',
|
|
|
|
author : 'NuSkooler',
|
2015-07-29 04:31:28 +00:00
|
|
|
packageName : 'codes.l33t.enigma.lastcallers' // :TODO: concept idea for mods
|
2015-07-26 06:20:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = LastCallersModule;
|
|
|
|
|
2015-07-27 04:51:06 +00:00
|
|
|
// :TODO:
|
2015-07-28 04:10:20 +00:00
|
|
|
// * config.evenRowSGR (optional)
|
2015-07-27 04:51:06 +00:00
|
|
|
|
|
|
|
function LastCallersModule(options) {
|
|
|
|
MenuModule.call(this, options);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
this.menuConfig = options.menuConfig;
|
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
this.rows = 10;
|
|
|
|
|
|
|
|
if(this.menuConfig.config) {
|
|
|
|
if(_.isNumber(this.menuConfig.config.rows)) {
|
|
|
|
this.rows = Math.max(1, this.menuConfig.config.rows);
|
|
|
|
}
|
|
|
|
if(_.isString(this.menuConfig.config.dateTimeFormat)) {
|
|
|
|
this.dateTimeFormat = this.menuConfig.config.dateTimeFormat;
|
2015-07-27 04:51:06 +00:00
|
|
|
}
|
2015-07-28 04:10:20 +00:00
|
|
|
}
|
2015-07-26 06:20:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-27 04:51:06 +00:00
|
|
|
util.inherits(LastCallersModule, MenuModule);
|
|
|
|
|
|
|
|
LastCallersModule.prototype.enter = function(client) {
|
|
|
|
LastCallersModule.super_.prototype.enter.call(this, client);
|
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
// we need the client to init this for theming
|
|
|
|
if(!_.isString(this.dateTimeFormat)) {
|
2015-08-04 05:11:17 +00:00
|
|
|
this.dateTimeFormat = this.client.currentTheme.helpers.getDateFormat('short') + ' ' +
|
2015-07-28 04:10:20 +00:00
|
|
|
this.client.currentTheme.helpers.getTimeFormat('short');
|
|
|
|
}
|
2015-07-27 04:51:06 +00:00
|
|
|
};
|
|
|
|
|
2015-07-30 04:09:51 +00:00
|
|
|
LastCallersModule.prototype.mciReady = function(mciData, cb) {
|
2015-07-27 04:51:06 +00:00
|
|
|
var self = this;
|
|
|
|
var vc = self.viewControllers.lastCallers = new ViewController( { client : self.client } );
|
2015-07-29 04:31:28 +00:00
|
|
|
var loginHistory;
|
2015-07-27 04:51:06 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
2015-07-30 04:09:51 +00:00
|
|
|
function callParentMciReady(callback) {
|
|
|
|
LastCallersModule.super_.prototype.mciReady.call(this, mciData, function parentMciReady(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
2015-07-27 04:51:06 +00:00
|
|
|
function loadFromConfig(callback) {
|
|
|
|
var loadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu,
|
|
|
|
noInput : true,
|
|
|
|
};
|
|
|
|
|
|
|
|
vc.loadFromMenuConfig(loadOpts, function startingViewReady(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function fetchHistory(callback) {
|
2015-07-29 04:31:28 +00:00
|
|
|
getUserLoginHistory(self.rows, function historyRetrieved(err, lh) {
|
|
|
|
loginHistory = lh;
|
|
|
|
callback(err);
|
|
|
|
});
|
2015-07-27 04:51:06 +00:00
|
|
|
},
|
|
|
|
function fetchUserProperties(callback) {
|
2015-07-29 04:31:28 +00:00
|
|
|
async.each(loginHistory, function entry(histEntry, next) {
|
2015-07-27 04:51:06 +00:00
|
|
|
userDb.each(
|
|
|
|
'SELECT prop_name, prop_value ' +
|
|
|
|
'FROM user_property ' +
|
|
|
|
'WHERE user_id=? AND (prop_name="location" OR prop_name="affiliation");',
|
2015-07-29 04:31:28 +00:00
|
|
|
[ histEntry.userId ],
|
2015-07-27 04:51:06 +00:00
|
|
|
function propRow(err, propEntry) {
|
2015-07-29 04:31:28 +00:00
|
|
|
histEntry[propEntry.prop_name] = propEntry.prop_value;
|
2015-07-27 04:51:06 +00:00
|
|
|
},
|
|
|
|
function complete(err) {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, function complete(err) {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function createAndPopulateViews(callback) {
|
|
|
|
//
|
2015-07-28 04:10:20 +00:00
|
|
|
// TL1 = who
|
|
|
|
// TL2 = location
|
|
|
|
// TL3 = affiliation
|
|
|
|
// TL4 = when
|
2015-07-27 04:51:06 +00:00
|
|
|
//
|
2015-07-28 04:10:20 +00:00
|
|
|
// These form the order/layout for a row. Additional rows
|
|
|
|
// will use them as a template.
|
2015-07-27 04:51:06 +00:00
|
|
|
//
|
2015-07-28 04:10:20 +00:00
|
|
|
var views = {
|
|
|
|
who : vc.getView(1),
|
|
|
|
location : vc.getView(2),
|
|
|
|
affils : vc.getView(3),
|
|
|
|
when : vc.getView(4),
|
|
|
|
};
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
var row = views.who.position.row;
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
var nextId = 5;
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
function addView(templateView, text) {
|
|
|
|
// :TODO: Is there a better way to clone this when dealing with instances?
|
|
|
|
var v = new TextView( {
|
|
|
|
client : self.client,
|
|
|
|
id : nextId++,
|
|
|
|
position : { row : row, col : templateView.position.col },
|
|
|
|
ansiSGR : templateView.ansiSGR,
|
|
|
|
textStyle : templateView.textStyle,
|
|
|
|
textOverflow : templateView.textOverflow,
|
|
|
|
dimens : templateView.dimens,
|
|
|
|
resizable : templateView.resizable,
|
|
|
|
} );
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
v.id = nextId++;
|
|
|
|
v.position.row = row;
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
v.setPropertyValue('text', text);
|
|
|
|
vc.addView(v);
|
|
|
|
};
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-29 04:31:28 +00:00
|
|
|
loginHistory.forEach(function entry(histEntry) {
|
2015-07-28 04:10:20 +00:00
|
|
|
if(row === views.who.position.row) {
|
2015-07-29 04:31:28 +00:00
|
|
|
views.who.setText(histEntry.userName);
|
|
|
|
views.location.setText(histEntry.location);
|
|
|
|
views.affils.setText(histEntry.affiliation);
|
|
|
|
views.when.setText(moment(histEntry.timestamp).format(self.dateTimeFormat));
|
2015-07-28 04:10:20 +00:00
|
|
|
} else {
|
2015-07-29 04:31:28 +00:00
|
|
|
addView(views.who, histEntry.userName);
|
|
|
|
addView(views.location, histEntry.location);
|
|
|
|
addView(views.affils, histEntry.affiliation);
|
|
|
|
addView(views.when, moment(histEntry.timestamp).format(self.dateTimeFormat));
|
2015-07-28 04:10:20 +00:00
|
|
|
}
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
row++;
|
|
|
|
});
|
2015-07-30 04:09:51 +00:00
|
|
|
|
|
|
|
callback(null);
|
2015-07-27 04:51:06 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
2015-07-30 04:09:51 +00:00
|
|
|
if(err) {
|
|
|
|
self.client.log.error(err);
|
|
|
|
}
|
|
|
|
cb(err);
|
2015-07-26 06:20:07 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|