Standardization work on built in user list module plus docs & code cleanup

* More docs, fix some info
* Code cleanup
This commit is contained in:
Bryan Ashby 2018-07-22 12:56:56 -06:00
parent 2e275600b1
commit e6a812cf34
9 changed files with 102 additions and 80 deletions

View file

@ -1,11 +1,12 @@
/* jslint node: true */
'use strict';
const MenuModule = require('./menu_module.js').MenuModule;
const User = require('./user.js');
const ViewController = require('./view_controller.js').ViewController;
const stringFormat = require('./string_format.js');
// ENiGMA½
const { MenuModule } = require('./menu_module.js');
const { getUserList } = require('./user.js');
const { Errors } = require('./enig_error.js');
// deps
const moment = require('moment');
const async = require('async');
const _ = require('lodash');
@ -29,7 +30,7 @@ exports.moduleInfo = {
};
const MciViewIds = {
UserList : 1,
userList : 1,
};
exports.getModule = class UserListModule extends MenuModule {
@ -43,68 +44,51 @@ exports.getModule = class UserListModule extends MenuModule {
return cb(err);
}
const self = this;
const vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
let userList = [];
const USER_LIST_OPTS = {
properties : [ 'location', 'affiliation', 'last_login_timestamp' ],
};
async.series(
[
function loadFromConfig(callback) {
var loadOpts = {
callingMenu : self,
mciMap : mciData.menu,
};
vc.loadFromMenuConfig(loadOpts, callback);
(next) => {
return this.prepViewController('userList', 0, mciData.menu, next);
},
function fetchUserList(callback) {
// :TODO: Currently fetching all users - probably always OK, but this could be paged
User.getUserList(USER_LIST_OPTS, function got(err, ul) {
userList = ul;
callback(err);
});
},
function populateList(callback) {
var userListView = vc.getView(MciViewIds.UserList);
var listFormat = self.menuConfig.config.listFormat || '{userName} - {affils}';
var focusListFormat = self.menuConfig.config.focusListFormat || listFormat; // :TODO: default changed color!
var dateTimeFormat = self.menuConfig.config.dateTimeFormat || 'ddd MMM DD';
function getUserFmtObj(ue) {
return {
userId : ue.userId,
userName : ue.userName,
affils : ue.affiliation,
location : ue.location,
// :TODO: the rest!
note : ue.note || '',
lastLoginTs : moment(ue.last_login_timestamp).format(dateTimeFormat),
};
(next) => {
const userListView = this.viewControllers.userList.getView(MciViewIds.userList);
if(!userListView) {
return cb(Errors.MissingMci(`Missing user list MCI ${MciViewIds.userList}`));
}
userListView.setItems(_.map(userList, function formatUserEntry(ue) {
return stringFormat(listFormat, getUserFmtObj(ue));
}));
const fetchOpts = {
properties : [ 'real_name', 'location', 'affiliation', 'last_login_timestamp' ],
propsCamelCase : true, // e.g. real_name -> realName
};
getUserList(fetchOpts, (err, userList) => {
if(err) {
return next(err);
}
userListView.setFocusItems(_.map(userList, function formatUserEntry(ue) {
return stringFormat(focusListFormat, getUserFmtObj(ue));
}));
const dateTimeFormat = _.get(
this, 'menuConfig.config.dateTimeFormat', this.client.currentTheme.helpers.getDateTimeFormat('short'));
userListView.redraw();
callback(null);
userList = userList.map(entry => {
return Object.assign(
entry,
{
text : entry.userName,
affils : entry.affiliation,
lastLoginTs : moment(entry.lastLoginTimestamp).format(dateTimeFormat),
}
);
});
userListView.setItems(userList);
userListView.redraw();
return next(null);
});
}
],
function complete(err) {
err => {
if(err) {
self.client.log.error( { error : err.toString() }, 'Error loading user list');
this.client.log.error( { error : err.message }, 'Error loading user list');
}
cb(err);
return cb(err);
}
);
});