2015-10-12 05:26:27 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-11-23 21:47:18 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const theme = require('./theme.js');
|
|
|
|
const sysValidate = require('./system_view_validate.js');
|
|
|
|
const UserProps = require('./user_property.js');
|
|
|
|
const { getISOTimestampString } = require('./database.js');
|
2023-04-25 16:24:11 +00:00
|
|
|
const EnigAssert = require('./enigma_assert');
|
2015-10-12 05:26:27 +00:00
|
|
|
|
2018-11-23 21:47:18 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
2015-10-12 05:26:27 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'User Configuration',
|
|
|
|
desc: 'Module for user configuration',
|
|
|
|
author: 'NuSkooler',
|
2015-10-12 05:26:27 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const MciCodeIds = {
|
2022-06-05 20:04:25 +00:00
|
|
|
RealName: 1,
|
|
|
|
BirthDate: 2,
|
|
|
|
Sex: 3,
|
|
|
|
Loc: 4,
|
|
|
|
Affils: 5,
|
|
|
|
Email: 6,
|
|
|
|
Web: 7,
|
|
|
|
TermHeight: 8,
|
|
|
|
Theme: 9,
|
|
|
|
Password: 10,
|
|
|
|
PassConfirm: 11,
|
|
|
|
ThemeInfo: 20,
|
|
|
|
ErrorMsg: 21,
|
|
|
|
|
|
|
|
SaveCancel: 25,
|
2015-10-13 06:35:37 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class UserConfigModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
this.menuMethods = {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Validation support
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
validateEmailAvail: function (data, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// If nothing changed, we know it's OK
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
self.client.user.properties[UserProps.EmailAddress].toLowerCase() ===
|
|
|
|
data.toLowerCase()
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Otherwise we can use the standard system method
|
2018-06-22 05:15:04 +00:00
|
|
|
return sysValidate.validateEmailAvail(data, cb);
|
|
|
|
},
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
validatePassword: function (data, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Blank is OK - this means we won't be changing it
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!data || 0 === data.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Otherwise we can use the standard system method
|
2018-06-22 05:15:04 +00:00
|
|
|
return sysValidate.validatePasswordSpec(data, cb);
|
|
|
|
},
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
validatePassConfirmMatch: function (data, cb) {
|
2019-06-14 04:54:05 +00:00
|
|
|
var passwordView = self.getMenuView(MciCodeIds.Password);
|
2022-06-05 20:04:25 +00:00
|
|
|
cb(
|
|
|
|
passwordView.getData() === data
|
|
|
|
? null
|
|
|
|
: new Error('Passwords do not match')
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
viewValidationListener: function (err, cb) {
|
2019-06-14 04:54:05 +00:00
|
|
|
var errMsgView = self.getMenuView(MciCodeIds.ErrorMsg);
|
2018-06-22 05:15:04 +00:00
|
|
|
var newFocusId;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (errMsgView) {
|
|
|
|
if (err) {
|
2023-02-04 20:44:55 +00:00
|
|
|
errMsgView.setText(err.friendlyText);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err.view.getId() === MciCodeIds.PassConfirm) {
|
2018-06-22 05:15:04 +00:00
|
|
|
newFocusId = MciCodeIds.Password;
|
2019-06-14 04:54:05 +00:00
|
|
|
var passwordView = self.getMenuView(MciCodeIds.Password);
|
2018-06-22 05:15:04 +00:00
|
|
|
passwordView.clearText();
|
|
|
|
err.view.clearText();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errMsgView.clearText();
|
|
|
|
}
|
|
|
|
}
|
2023-02-04 20:44:55 +00:00
|
|
|
|
|
|
|
return cb(err, newFocusId);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Handlers
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
saveChanges: function (formData, extraArgs, cb) {
|
2023-04-25 16:24:11 +00:00
|
|
|
EnigAssert(formData.value.password === formData.value.passwordConfirm);
|
|
|
|
|
|
|
|
// cache a copy of |formData| as changing a theme below can invalidate it
|
|
|
|
formData = _.clone(formData);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const newProperties = {
|
2022-06-05 20:04:25 +00:00
|
|
|
[UserProps.RealName]: formData.value.realName,
|
|
|
|
[UserProps.Birthdate]: getISOTimestampString(
|
|
|
|
formData.value.birthdate
|
|
|
|
),
|
|
|
|
[UserProps.Sex]: formData.value.sex,
|
|
|
|
[UserProps.Location]: formData.value.location,
|
|
|
|
[UserProps.Affiliations]: formData.value.affils,
|
|
|
|
[UserProps.EmailAddress]: formData.value.email,
|
|
|
|
[UserProps.WebAddress]: formData.value.web,
|
|
|
|
[UserProps.TermHeight]: formData.value.termHeight.toString(),
|
|
|
|
[UserProps.ThemeId]:
|
|
|
|
self.availThemeInfo[formData.value.theme].themeId,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2023-04-25 16:24:11 +00:00
|
|
|
// Runtime set theme
|
2018-06-22 05:15:04 +00:00
|
|
|
theme.setClientTheme(self.client, newProperties.theme_id);
|
|
|
|
|
|
|
|
// persist all changes
|
|
|
|
self.client.user.persistProperties(newProperties, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.client.log.warn(
|
|
|
|
{ error: err.toString() },
|
|
|
|
'Failed persisting updated properties'
|
|
|
|
);
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: warn end user!
|
2018-06-22 05:15:04 +00:00
|
|
|
return self.prevMenu(cb);
|
|
|
|
}
|
2022-05-07 17:47:04 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
self.client.log.info(
|
|
|
|
`User "${self.client.user.username}" updated configuration`
|
|
|
|
);
|
2022-05-07 17:47:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// New password if it's not empty
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-12 20:12:03 +00:00
|
|
|
if (formData.value.password.length > 0) {
|
|
|
|
self.client.user.setNewAuthCredentials(
|
|
|
|
formData.value.password,
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
self.client.log.error(
|
|
|
|
{ err: err },
|
|
|
|
'Failed storing new authentication credentials'
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
self.client.log.info(
|
|
|
|
`User "${self.client.user.username}" updated authentication credentials`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return self.prevMenu(cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-12 20:12:03 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
return self.prevMenu(cb);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-14 04:54:05 +00:00
|
|
|
getMenuView(viewId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.viewControllers.menu.getView(viewId);
|
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const self = this;
|
|
|
|
const vc = (self.viewControllers.menu = new ViewController({
|
|
|
|
client: self.client,
|
|
|
|
}));
|
2018-06-22 05:15:04 +00:00
|
|
|
let currentThemeIdIndex = 0;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadFromConfig(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
vc.loadFromMenuConfig(
|
|
|
|
{ callingMenu: self, mciMap: mciData.menu },
|
|
|
|
callback
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function prepareAvailableThemes(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.availThemeInfo = _.sortBy(
|
|
|
|
[...theme.getAvailableThemes()].map(entry => {
|
|
|
|
const theme = entry[1].get();
|
|
|
|
return {
|
|
|
|
themeId: theme.info.themeId,
|
|
|
|
name: theme.info.name,
|
|
|
|
author: theme.info.author,
|
|
|
|
desc: _.isString(theme.info.desc)
|
|
|
|
? theme.info.desc
|
|
|
|
: '',
|
|
|
|
group: _.isString(theme.info.group)
|
|
|
|
? theme.info.group
|
|
|
|
: '',
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
'name'
|
|
|
|
);
|
|
|
|
|
|
|
|
currentThemeIdIndex = Math.max(
|
|
|
|
0,
|
|
|
|
_.findIndex(self.availThemeInfo, function cmp(ti) {
|
|
|
|
return (
|
|
|
|
ti.themeId ===
|
|
|
|
self.client.user.properties[UserProps.ThemeId]
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function populateViews(callback) {
|
2018-11-24 00:41:16 +00:00
|
|
|
const user = self.client.user;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.setViewText(
|
|
|
|
'menu',
|
|
|
|
MciCodeIds.RealName,
|
|
|
|
user.properties[UserProps.RealName]
|
|
|
|
);
|
|
|
|
self.setViewText(
|
|
|
|
'menu',
|
|
|
|
MciCodeIds.BirthDate,
|
|
|
|
moment(user.properties[UserProps.Birthdate]).format(
|
|
|
|
'YYYYMMDD'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
self.setViewText(
|
|
|
|
'menu',
|
|
|
|
MciCodeIds.Sex,
|
|
|
|
user.properties[UserProps.Sex]
|
|
|
|
);
|
|
|
|
self.setViewText(
|
|
|
|
'menu',
|
|
|
|
MciCodeIds.Loc,
|
|
|
|
user.properties[UserProps.Location]
|
|
|
|
);
|
|
|
|
self.setViewText(
|
|
|
|
'menu',
|
|
|
|
MciCodeIds.Affils,
|
|
|
|
user.properties[UserProps.Affiliations]
|
|
|
|
);
|
|
|
|
self.setViewText(
|
|
|
|
'menu',
|
|
|
|
MciCodeIds.Email,
|
|
|
|
user.properties[UserProps.EmailAddress]
|
|
|
|
);
|
|
|
|
self.setViewText(
|
|
|
|
'menu',
|
|
|
|
MciCodeIds.Web,
|
|
|
|
user.properties[UserProps.WebAddress]
|
|
|
|
);
|
|
|
|
self.setViewText(
|
|
|
|
'menu',
|
|
|
|
MciCodeIds.TermHeight,
|
|
|
|
user.properties[UserProps.TermHeight].toString()
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2019-06-14 04:54:05 +00:00
|
|
|
var themeView = self.getMenuView(MciCodeIds.Theme);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (themeView) {
|
2018-06-22 05:15:04 +00:00
|
|
|
themeView.setItems(_.map(self.availThemeInfo, 'name'));
|
|
|
|
themeView.setFocusItemIndex(currentThemeIdIndex);
|
|
|
|
}
|
|
|
|
|
2019-06-14 04:54:05 +00:00
|
|
|
var realNameView = self.getMenuView(MciCodeIds.RealName);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (realNameView) {
|
|
|
|
realNameView.setFocus(true); // :TODO: HACK! menu.hjson sets focus, but manual population above breaks this. Needs a real fix!
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
function complete(err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.client.log.warn(
|
|
|
|
{ error: err.toString() },
|
|
|
|
'User configuration failed to init'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
self.prevMenu();
|
|
|
|
} else {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2015-10-12 05:26:27 +00:00
|
|
|
};
|