From 65d7186a86f6dd2e55da8a5dcedf6e24f41fefe5 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sat, 17 Oct 2015 20:58:07 -0600 Subject: [PATCH] * Add new last_callers.js --- mods/last_callers.js | 121 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 mods/last_callers.js diff --git a/mods/last_callers.js b/mods/last_callers.js new file mode 100644 index 00000000..d0c7d68a --- /dev/null +++ b/mods/last_callers.js @@ -0,0 +1,121 @@ +/* jslint node: true */ +'use strict'; + +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 getUserLoginHistory = require('../core/stats.js').getUserLoginHistory; +var colorCodes = require('../core/color_codes.js'); + +var moment = require('moment'); +var async = require('async'); +var assert = require('assert'); +var _ = require('lodash'); + +/* + Available listFormat object members: + userId + userName + location + affiliation + timestamp + +*/ + +exports.moduleInfo = { + name : 'Last Callers', + desc : 'Last callers to the system', + author : 'NuSkooler', + packageName : 'codes.l33t.enigma.lastcallers' // :TODO: concept idea for mods +}; + +exports.getModule = LastCallersModule; + +var MciCodeIds = { + CallerList : 1, +}; + +function LastCallersModule(options) { + MenuModule.call(this, options); +} + +require('util').inherits(LastCallersModule, MenuModule); + +LastCallersModule.prototype.mciReady = function(mciData, cb) { + var self = this; + var vc = self.viewControllers.allViews = new ViewController( { client : self.client } ); + + var loginHistory; + var callersView; + + async.series( + [ + function callParentMciReady(callback) { + LastCallersModule.super_.prototype.mciReady.call(self, mciData, callback); + }, + function loadFromConfig(callback) { + var loadOpts = { + callingMenu : self, + mciMap : mciData.menu, + noInput : true, + }; + + vc.loadFromMenuConfig(loadOpts, callback); + }, + function fetchHistory(callback) { + callersView = vc.getView(MciCodeIds.CallerList); + + getUserLoginHistory(callersView.dimens.height, function historyRetrieved(err, lh) { + loginHistory = lh; + callback(err); + }); + }, + function fetchUserProperties(callback) { + async.each(loginHistory, function entry(histEntry, next) { + userDb.each( + 'SELECT prop_name, prop_value ' + + 'FROM user_property ' + + 'WHERE user_id=? AND (prop_name="location" OR prop_name="affiliation");', + [ histEntry.userId ], + function propRow(err, propEntry) { + histEntry[propEntry.prop_name] = propEntry.prop_value; + }, + function complete(err) { + next(); + } + ); + }, function complete(err) { + callback(err); + }); + }, + function populateList(callback) { + var callersView = vc.getView(MciCodeIds.CallerList); + + var listFormat = self.menuConfig.config.listFormat || '{userName} - {location} - {affils} - {ts}'; + var dateTimeFormat = self.menuConfig.config.dateTimeFormat || 'ddd MMM DD'; + + callersView.setItems(_.map(loginHistory, function formatCallEntry(ce) { + return listFormat.format({ + userId : ce.userId, + userName : ce.userName, + ts : moment(ce.timestamp).format(dateTimeFormat), + location : ce.location, + affils : ce.affiliation, + }); + })); + + // :TODO: This is a hack until pipe codes are better implemented + callersView.focusItems = callersView.items; + + callersView.redraw(); + callback(null); + } + ], + function complete(err) { + if(err) { + self.client.log.error( { error : err.toString() }, 'Error loading last callers'); + } + cb(err); + } + ); +} \ No newline at end of file