From b82c6400140b515b166c9af84698e63101b33f54 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Fri, 23 Nov 2018 14:47:18 -0700 Subject: [PATCH] Work on using UserProps, fix up ISO timestamps, etc. --- core/database.js | 8 +++++++- core/file_base_area.js | 5 +++-- core/file_base_filter.js | 12 +++++++----- core/message_area.js | 9 +++++---- core/misc_util.js | 4 +++- core/nua.js | 35 ++++++++++++++++++++--------------- core/stat_log.js | 8 ++++++-- core/user_config.js | 24 +++++++++++++++--------- core/user_property.js | 36 +++++++++++++++++++++++++++--------- core/web_password_reset.js | 4 ++-- 10 files changed, 95 insertions(+), 50 deletions(-) diff --git a/core/database.js b/core/database.js index 8bae6a87..017aa949 100644 --- a/core/database.js +++ b/core/database.js @@ -67,7 +67,13 @@ function loadDatabaseForMod(modInfo, cb) { function getISOTimestampString(ts) { ts = ts || moment(); - return ts.format('YYYY-MM-DDTHH:mm:ss.SSSZ'); + if(!moment.isMoment(ts)) { + if(_.isString(ts)) { + ts = ts.replace(/\//g, '-'); + } + ts = moment(ts); + } + return ts.utc().format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'); } function sanatizeString(s) { diff --git a/core/file_base_area.js b/core/file_base_area.js index a684f575..760eb87c 100644 --- a/core/file_base_area.js +++ b/core/file_base_area.js @@ -14,6 +14,7 @@ const resolveMimeType = require('./mime_util.js').resolveMimeType; const stringFormat = require('./string_format.js'); const wordWrapText = require('./word_wrap.js').wordWrapText; const StatLog = require('./stat_log.js'); +const UserProps = require('./user_property.js'); // deps const _ = require('lodash'); @@ -136,11 +137,11 @@ function changeFileAreaWithOptions(client, areaTag, options, cb) { }, function changeArea(area, callback) { if(true === options.persist) { - client.user.persistProperty('file_area_tag', areaTag, err => { + client.user.persistProperty(UserProps.FileAreaTag, areaTag, err => { return callback(err, area); }); } else { - client.user.properties['file_area_tag'] = areaTag; + client.user.properties[UserProps.FileAreaTag] = areaTag; return callback(null, area); } } diff --git a/core/file_base_filter.js b/core/file_base_filter.js index 82a75986..0ff19b94 100644 --- a/core/file_base_filter.js +++ b/core/file_base_filter.js @@ -1,9 +1,11 @@ /* jslint node: true */ 'use strict'; +const UserProps = require('./user_property.js'); + // deps -const _ = require('lodash'); -const uuidV4 = require('uuid/v4'); +const _ = require('lodash'); +const uuidV4 = require('uuid/v4'); module.exports = class FileBaseFilters { constructor(client) { @@ -90,7 +92,7 @@ module.exports = class FileBaseFilters { } persist(cb) { - return this.client.user.persistProperty('file_base_filters', JSON.stringify(this.filters), cb); + return this.client.user.persistProperty(UserProps.FileBaseFilters, JSON.stringify(this.filters), cb); } cleanTags(tags) { @@ -102,7 +104,7 @@ module.exports = class FileBaseFilters { if(activeFilter) { this.activeFilter = activeFilter; - this.client.user.persistProperty('file_base_filter_active_uuid', filterUuid); + this.client.user.persistProperty(UserProps.FileBaseFilterActiveUuid, filterUuid); return true; } @@ -150,6 +152,6 @@ module.exports = class FileBaseFilters { return; } - return user.persistProperty('user_file_base_last_viewed', fileId, cb); + return user.persistProperty(UserProps.FileBaseLastViewedId, fileId, cb); } }; diff --git a/core/message_area.js b/core/message_area.js index 98647cfd..ea6b4a87 100644 --- a/core/message_area.js +++ b/core/message_area.js @@ -8,6 +8,7 @@ const Message = require('./message.js'); const Log = require('./logger.js').log; const msgNetRecord = require('./msg_network.js').recordMessage; const sortAreasOrConfs = require('./conf_area_util.js').sortAreasOrConfs; +const UserProps = require('./user_property.js'); // deps const async = require('async'); @@ -222,8 +223,8 @@ function changeMessageConference(client, confTag, cb) { }, function changeConferenceAndArea(conf, areaInfo, callback) { const newProps = { - message_conf_tag : confTag, - message_area_tag : areaInfo.areaTag, + [ UserProps.MessageConfTag ] : confTag, + [ UserProps.MessageAreaTag ] : areaInfo.areaTag, }; client.user.persistProperties(newProps, err => { callback(err, conf, areaInfo); @@ -262,11 +263,11 @@ function changeMessageAreaWithOptions(client, areaTag, options, cb) { }, function changeArea(area, callback) { if(true === options.persist) { - client.user.persistProperty('message_area_tag', areaTag, function persisted(err) { + client.user.persistProperty(UserProps.MessageAreaTag, areaTag, function persisted(err) { return callback(err, area); }); } else { - client.user.properties['message_area_tag'] = areaTag; + client.user.properties[UserProps.MessageAreaTag] = areaTag; return callback(null, area); } } diff --git a/core/misc_util.js b/core/misc_util.js index 6e477821..62a3967d 100644 --- a/core/misc_util.js +++ b/core/misc_util.js @@ -4,6 +4,8 @@ const paths = require('path'); const os = require('os'); +const moment = require('moment'); + const packageJson = require('../package.json'); exports.isProduction = isProduction; @@ -57,4 +59,4 @@ function valueAsArray(value) { return []; } return Array.isArray(value) ? value : [ value ]; -} \ No newline at end of file +} diff --git a/core/nua.js b/core/nua.js index 2d838fcb..42c63895 100644 --- a/core/nua.js +++ b/core/nua.js @@ -8,9 +8,14 @@ const theme = require('./theme.js'); const login = require('./system_menu_method.js').login; const Config = require('./config.js').get; const messageArea = require('./message_area.js'); +const { + getISOTimestampString +} = require('./database.js'); +const UserProps = require('./user_property.js'); // deps const _ = require('lodash'); +const moment = require('moment'); exports.moduleInfo = { name : 'NUA', @@ -80,20 +85,20 @@ exports.getModule = class NewUserAppModule extends MenuModule { areaTag = areaTag || ''; newUser.properties = { - real_name : formData.value.realName, - birthdate : new Date(Date.parse(formData.value.birthdate)).toISOString(), // :TODO: Use moment & explicit ISO string format - sex : formData.value.sex, - location : formData.value.location, - affiliation : formData.value.affils, - email_address : formData.value.email, - web_address : formData.value.web, - account_created : new Date().toISOString(), // :TODO: Use moment & explicit ISO string format + [ 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.AccountCreated ] : getISOTimestampString(), - message_conf_tag : confTag, - message_area_tag : areaTag, + [ UserProps.MessageConfTag ] : confTag, + [ UserProps.MessageAreaTag ] : areaTag, - term_height : self.client.term.termHeight, - term_width : self.client.term.termWidth, + [ UserProps.TermHeight ] : self.client.term.termHeight, + [ UserProps.TermWidth ] : self.client.term.termWidth, // :TODO: Other defaults // :TODO: should probably have a place to create defaults/etc. @@ -101,9 +106,9 @@ exports.getModule = class NewUserAppModule extends MenuModule { const defaultTheme = _.get(config, 'theme.default'); if('*' === defaultTheme) { - newUser.properties.theme_id = theme.getRandomTheme(); + newUser.properties[UserProps.ThemeId] = theme.getRandomTheme(); } else { - newUser.properties.theme_id = defaultTheme; + newUser.properties[UserProps.ThemeId] = defaultTheme; } // :TODO: User.create() should validate email uniqueness! @@ -133,7 +138,7 @@ exports.getModule = class NewUserAppModule extends MenuModule { }; } - if(User.AccountStatus.inactive === self.client.user.properties.account_status) { + if(User.AccountStatus.inactive === self.client.user.properties[UserProps.AccountStatus]) { return self.gotoMenu(extraArgs.inactive, cb); } else { // diff --git a/core/stat_log.js b/core/stat_log.js index 4ee4c53b..173222b9 100644 --- a/core/stat_log.js +++ b/core/stat_log.js @@ -2,10 +2,12 @@ 'use strict'; const sysDb = require('./database.js').dbs.system; +const { + getISOTimestampString +} = require('./database.js'); // deps const _ = require('lodash'); -const moment = require('moment'); /* System Event Log & Stats @@ -149,7 +151,9 @@ class StatLog { } // the time "now" in the ISO format we use and love :) - get now() { return moment().format('YYYY-MM-DDTHH:mm:ss.SSSZ'); } + get now() { + return getISOTimestampString(); + } appendSystemLogEntry(logName, logValue, keep, keepType, cb) { sysDb.run( diff --git a/core/user_config.js b/core/user_config.js index f9aad6c4..ef72e1ad 100644 --- a/core/user_config.js +++ b/core/user_config.js @@ -1,11 +1,17 @@ /* jslint node: true */ 'use strict'; +// ENiGMA½ 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'); +// deps const async = require('async'); const assert = require('assert'); const _ = require('lodash'); @@ -101,15 +107,15 @@ exports.getModule = class UserConfigModule extends MenuModule { assert(formData.value.password === formData.value.passwordConfirm); const newProperties = { - real_name : formData.value.realName, - birthdate : new Date(Date.parse(formData.value.birthdate)).toISOString(), - sex : formData.value.sex, - location : formData.value.location, - affiliation : formData.value.affils, - email_address : formData.value.email, - web_address : formData.value.web, - term_height : formData.value.termHeight.toString(), - theme_id : self.availThemeInfo[formData.value.theme].themeId, + [ 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, }; // runtime set theme diff --git a/core/user_property.js b/core/user_property.js index 04f8f0c9..bbb36928 100644 --- a/core/user_property.js +++ b/core/user_property.js @@ -8,18 +8,36 @@ // can utilize their own properties as well! // module.exports = { - PassPbkdf2Salt : 'pw_pbkdf2_salt', - PassPbkdf2Dk : 'pw_pbkdf2_dk', + PassPbkdf2Salt : 'pw_pbkdf2_salt', + PassPbkdf2Dk : 'pw_pbkdf2_dk', - AccountStatus : 'account_status', + AccountStatus : 'account_status', - Birthdate : 'birthdate', + RealName : 'real_name', + Sex : 'sex', + Birthdate : 'birthdate', + Location : 'location', + Affiliations : 'affiliation', + EmailAddress : 'email_address', + WebAddress : 'web_address', + TermHeight : 'term_height', + TermWidth : 'term_width', + ThemeId : 'theme_id', + AccountCreated : 'account_created', - FailedLoginAttempts : 'failed_login_attempts', - AccountLockedTs : 'account_locked_timestamp', - AccountLockedPrevStatus : 'account_locked_prev_status', // previous account status + FailedLoginAttempts : 'failed_login_attempts', + AccountLockedTs : 'account_locked_timestamp', + AccountLockedPrevStatus : 'account_locked_prev_status', // previous account status - EmailPwResetToken : 'email_password_reset_token', - EmailPwResetTokenTs : 'email_password_reset_token_ts', + EmailPwResetToken : 'email_password_reset_token', + EmailPwResetTokenTs : 'email_password_reset_token_ts', + + FileAreaTag : 'file_area_tag', + FileBaseFilters : 'file_base_filters', + FileBaseFilterActiveUuid : 'file_base_filter_active_uuid', + FileBaseLastViewedId : 'user_file_base_last_viewed', + + MessageConfTag : 'message_conf_tag', + MessageAreaTag : 'message_area_tag', }; diff --git a/core/web_password_reset.js b/core/web_password_reset.js index ceefe9c5..c76a232c 100644 --- a/core/web_password_reset.js +++ b/core/web_password_reset.js @@ -79,8 +79,8 @@ class WebPasswordReset { token = token.toString('hex'); const newProperties = { - email_password_reset_token : token, - email_password_reset_token_ts : getISOTimestampString(), + [ UserProps.EmailPwResetToken ] : token, + [ UserProps.EmailPwResetTokenTs ] : getISOTimestampString(), }; // we simply place the reset token in the user's properties