From 35de0a2487b4143c010e2dc2134331cafc4262c1 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Thu, 20 Aug 2015 16:35:04 -0600 Subject: [PATCH] * Initial work on moving to JSON configured Message Areas (vs ID's in SQLite). This will be applied to user groups, and other types as well such that JSON can simply be edited in config.json * Hopefully better/proper merge of config.js + config.json => config --- core/config.js | 17 +++++++++++++- core/message_area.js | 52 +++++++++++++++++++++++++++++++++++++++---- core/user.js | 3 ++- mods/msg_area_list.js | 41 ++++++++++++++++++++++++++++++---- 4 files changed, 103 insertions(+), 10 deletions(-) diff --git a/core/config.js b/core/config.js index 56db26a3..80dda942 100644 --- a/core/config.js +++ b/core/config.js @@ -31,7 +31,15 @@ function init(configPath, cb) { }); }, function mergeWithDefaultConfig(configJson, callback) { - var mergedConfig = _.defaultsDeep(configJson, getDefaultConfig()); + //var mergedConfig = _.defaultsDeep(configJson, getDefaultConfig()); + var mergedConfig = _.merge(getDefaultConfig(), configJson, function mergeCustomizer(conf1, conf2) { + // Arrays should always concat + if(_.isArray(conf1)) { + // :TODO: look for collisions & override dupes + return conf1.concat(conf2); + } + }); + callback(null, mergedConfig); } ], @@ -67,6 +75,7 @@ function getDefaultConfig() { passwordMax : 128, requireActivation : true, // require SysOp activation? invalidUsernames : [], + defaultGroups : [ 'users' ] }, @@ -113,6 +122,12 @@ function getDefaultConfig() { } }, + messages : { + areas : [ + { name : "private_mail", desc : "Private Email", groups : [ "users" ] } + ] + }, + networks : { /* networkName : { // e.g. fidoNet diff --git a/core/message_area.js b/core/message_area.js index 077c87ad..7f653d75 100644 --- a/core/message_area.js +++ b/core/message_area.js @@ -2,15 +2,59 @@ 'use strict'; var msgDb = require('./database.js').dbs.message; +var Config = require('./config.js').config; var async = require('async'); var _ = require('lodash'); var assert = require('assert'); exports.getAvailableMessageAreas = getAvailableMessageAreas; -exports.changeCurrentArea = changeCurrentArea; +exports.changeMessageArea = changeMessageArea; -// :TODO: need total / new + other stats +function getAvailableMessageAreas() { + return Config.messages.areas; +} + +function changeMessageArea(client, areaName, cb) { + + async.waterfall( + [ + function getArea(callback) { + var availAreas = getAvailableMessageAreas(); + + areaName = areaName.toLowerCase(); // always lookup lowercase + var index = _.findIndex(availAreas, function pred(a) { + return a.name === areaName; + }); + + if(index > -1) { + callback(null, availAreas[index]); + } else { + callback(new Error('Invalid message area')); + } + }, + function validateAccess(area, callback) { + // :TODO: validate user has access to |area| -- must belong to group(s) specified + callback(null, area); + }, + function changeArea(area, callback) { + client.user.persistProperties( { message_area_name : area.name, message_area_desc : area.desc }, function persisted(err) { + callback(err, area); + }); + } + ], + function complete(err, area) { + if(!err) { + client.log.info( area, 'Current message area changed'); + } else { + client.log.warn( { area : area, error : err.message }, 'Could not change message area'); + } + + cb(err); + } + ); +} +/* function getAvailableMessageAreas(cb) { var areas = []; // { areaId, name, groupIds[] } @@ -59,7 +103,6 @@ function getAvailableMessageAreas(cb) { cb(err, areas); } ); - } function changeCurrentArea(client, areaId, cb) { @@ -104,4 +147,5 @@ function changeCurrentArea(client, areaId, cb) { cb(err); } ); -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/core/user.js b/core/user.js index 3e5e7b5c..cfddcb18 100644 --- a/core/user.js +++ b/core/user.js @@ -331,7 +331,8 @@ User.prototype.persistProperty = function(propName, propValue, cb) { User.prototype.persistProperties = function(properties, cb) { var self = this; - // :TODO: should probably add/update live properties + // update live props + _.merge(this.properties, properties); var stmt = userDb.prepare( 'REPLACE INTO user_property (user_id, prop_name, prop_value) ' + diff --git a/mods/msg_area_list.js b/mods/msg_area_list.js index 01ce02a5..08324c6b 100644 --- a/mods/msg_area_list.js +++ b/mods/msg_area_list.js @@ -30,11 +30,26 @@ function MessageAreaListModule(options) { } } - this.entryFormat = this.entryFormat || '( {areaId} ) - {name}'; + this.entryFormat = this.entryFormat || '( {index} ) - {desc}'; this.menuMethods = { changeArea : function(formData, extraArgs) { if(1 === formData.submitId) { + var areaName = self.messageAreas[formData.value.area].name; + + messageArea.changeMessageArea(self.client, areaName, function areaChanged(err) { + if(err) { + self.client.term.pipeWrite('\n|00Cannot change area: ' + err.message + '\n'); + + setTimeout(function timeout() { + self.client.gotoMenuModule( { name : self.menuConfig.fallback } ); + }, 1000); + } else { + self.client.gotoMenuModule( { name : self.menuConfig.fallback } ); + } + }); + + /* var areaId = self.messageAreas[formData.value.area].areaId; messageArea.changeCurrentArea(self.client, areaId, function areaChanged(err) { @@ -48,6 +63,7 @@ function MessageAreaListModule(options) { self.client.gotoMenuModule( { name : self.menuConfig.fallback } ); } }); +*/ } } }; @@ -59,11 +75,19 @@ require('util').inherits(MessageAreaListModule, MenuModule); MessageAreaListModule.prototype.enter = function(client) { var self = this; + self.messageAreas = messageArea.getAvailableMessageAreas(); + + console.log(self.messageAreas) + + MessageAreaListModule.super_.prototype.enter.call(self, client); + + /* + messageArea.getAvailableMessageAreas(function fetched(err, areas) { self.messageAreas = areas; MessageAreaListModule.super_.prototype.enter.call(self, client); - }); + });*/ }; MessageAreaListModule.prototype.mciReady = function(mciData, cb) { @@ -91,14 +115,23 @@ MessageAreaListModule.prototype.mciReady = function(mciData, cb) { function populateAreaListView(callback) { var areaListView = vc.getView(1); + /* var areaList = []; self.messageAreas.forEach(function entry(msgArea) { // :TODO: depending on options, filter out private, local user to user, etc. area IDs // :TODO: dep. on options, filter out areas that current user does not have access to areaList.push(strUtil.format(self.entryFormat, msgArea)); - }); + });*/ - areaListView.setItems(areaList); + var areaListItems = []; + for(var i = 0; i < self.messageAreas.length; ++i) { + areaListItems.push(strUtil.format( + self.entryFormat, + { index : i, name : self.messageAreas[i].name, desc : self.messageAreas[i].desc }) + ); + } + + areaListView.setItems(areaListItems); areaListView.redraw(); } ],