2015-08-18 21:27:14 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var msgDb = require('./database.js').dbs.message;
|
2015-08-20 22:35:04 +00:00
|
|
|
var Config = require('./config.js').config;
|
2015-09-06 21:58:58 +00:00
|
|
|
var Message = require('./message.js');
|
2015-08-18 21:27:14 +00:00
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
var _ = require('lodash');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
exports.getAvailableMessageAreas = getAvailableMessageAreas;
|
2015-09-26 06:20:17 +00:00
|
|
|
exports.getDefaultMessageArea = getDefaultMessageArea;
|
2015-08-21 04:51:00 +00:00
|
|
|
exports.getMessageAreaByName = getMessageAreaByName;
|
2015-08-20 22:35:04 +00:00
|
|
|
exports.changeMessageArea = changeMessageArea;
|
2015-08-28 04:20:24 +00:00
|
|
|
exports.getMessageListForArea = getMessageListForArea;
|
2015-09-06 21:58:58 +00:00
|
|
|
exports.gotoMsgAreaFSEModuleForMessage = gotoMsgAreaFSEModuleForMessage;
|
2015-08-18 21:27:14 +00:00
|
|
|
|
2015-08-20 22:35:04 +00:00
|
|
|
function getAvailableMessageAreas() {
|
2015-09-04 06:02:28 +00:00
|
|
|
// example: [ { "name" : "local_music", "desc" : "Music Discussion", "groups" : ["somegroup"] }, ... ]
|
2015-08-20 22:35:04 +00:00
|
|
|
return Config.messages.areas;
|
|
|
|
}
|
|
|
|
|
2015-09-26 06:20:17 +00:00
|
|
|
function getDefaultMessageArea() {
|
2015-10-18 02:03:51 +00:00
|
|
|
//
|
|
|
|
// Return first non-private/etc. area name. This will be from config.hjson
|
|
|
|
//
|
|
|
|
var avail = getAvailableMessageAreas();
|
|
|
|
for(var i = 0; i < avail.length; ++i) {
|
|
|
|
if(Message.WellKnownAreaNames.Private !== avail[i].name) {
|
|
|
|
return avail[i];
|
|
|
|
}
|
|
|
|
}
|
2015-09-26 06:20:17 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:51:00 +00:00
|
|
|
function getMessageAreaByName(areaName) {
|
|
|
|
areaName = areaName.toLowerCase();
|
|
|
|
|
|
|
|
var availAreas = getAvailableMessageAreas();
|
|
|
|
var index = _.findIndex(availAreas, function pred(an) {
|
|
|
|
return an.name == areaName;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(index > -1) {
|
|
|
|
return availAreas[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:35:04 +00:00
|
|
|
function changeMessageArea(client, areaName, cb) {
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getArea(callback) {
|
2015-08-21 04:51:00 +00:00
|
|
|
var area = getMessageAreaByName(areaName);
|
2015-08-27 05:25:49 +00:00
|
|
|
|
2015-08-21 04:51:00 +00:00
|
|
|
if(area) {
|
|
|
|
callback(null, area);
|
|
|
|
} else {
|
|
|
|
callback(new Error('Invalid message area'));
|
|
|
|
}
|
2015-08-20 22:35:04 +00:00
|
|
|
},
|
|
|
|
function validateAccess(area, callback) {
|
|
|
|
// :TODO: validate user has access to |area| -- must belong to group(s) specified
|
|
|
|
callback(null, area);
|
|
|
|
},
|
|
|
|
function changeArea(area, callback) {
|
2015-08-21 04:51:00 +00:00
|
|
|
client.user.persistProperty('message_area_name', area.name, function persisted(err) {
|
2015-08-20 22:35:04 +00:00
|
|
|
callback(err, area);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err, area) {
|
|
|
|
if(!err) {
|
2015-09-04 22:17:41 +00:00
|
|
|
console.log(JSON.stringify(area))
|
2015-08-20 22:35:04 +00:00
|
|
|
client.log.info( area, 'Current message area changed');
|
|
|
|
} else {
|
|
|
|
client.log.warn( { area : area, error : err.message }, 'Could not change message area');
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-08-27 05:25:49 +00:00
|
|
|
|
|
|
|
function getMessageListForArea(options, areaName, cb) {
|
|
|
|
//
|
|
|
|
// options.client (required)
|
|
|
|
//
|
|
|
|
|
2015-09-04 22:17:41 +00:00
|
|
|
options.client.log.debug( { areaName : areaName }, 'Fetching available messages');
|
|
|
|
|
2015-08-27 05:25:49 +00:00
|
|
|
assert(_.isObject(options.client));
|
|
|
|
|
|
|
|
/*
|
|
|
|
[
|
|
|
|
{
|
|
|
|
messageId, messageUuid, replyToId, toUserName, fromUserName, subject, modTimestamp,
|
|
|
|
status(new|old),
|
|
|
|
viewCount
|
|
|
|
}
|
|
|
|
]
|
|
|
|
*/
|
|
|
|
|
|
|
|
var msgList = [];
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function fetchMessages(callback) {
|
|
|
|
msgDb.each(
|
2015-08-27 22:14:56 +00:00
|
|
|
'SELECT message_id, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject, modified_timestamp, view_count ' +
|
|
|
|
'FROM message ' +
|
|
|
|
'WHERE area_name=? ' +
|
2015-08-27 05:25:49 +00:00
|
|
|
'ORDER BY message_id;',
|
|
|
|
[ areaName.toLowerCase() ],
|
|
|
|
function msgRow(err, row) {
|
|
|
|
if(!err) {
|
|
|
|
msgList.push( {
|
2015-09-01 05:18:46 +00:00
|
|
|
messageId : row.message_id,
|
|
|
|
messageUuid : row.message_uuid,
|
|
|
|
replyToMsgId : row.reply_to_message_id,
|
|
|
|
toUserName : row.to_user_name,
|
|
|
|
fromUserName : row.from_user_name,
|
2015-08-28 04:20:24 +00:00
|
|
|
subject : row.subject,
|
2015-09-01 05:18:46 +00:00
|
|
|
modTimestamp : row.modified_timestamp,
|
2015-08-28 04:20:24 +00:00
|
|
|
viewCount : row.view_count,
|
2015-08-27 05:25:49 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
2015-08-28 04:20:24 +00:00
|
|
|
},
|
|
|
|
function fetchStatus(callback) {
|
|
|
|
callback(null);// :TODO: fixmeh.
|
2015-08-27 05:25:49 +00:00
|
|
|
}
|
2015-08-28 04:20:24 +00:00
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
cb(err, msgList);
|
|
|
|
}
|
2015-08-27 05:25:49 +00:00
|
|
|
);
|
|
|
|
}
|
2015-09-06 21:58:58 +00:00
|
|
|
|
|
|
|
function gotoMsgAreaFSEModuleForMessage(options, cb) {
|
|
|
|
// options.client
|
|
|
|
// options.msgAreaName
|
|
|
|
// options.messageUuid
|
|
|
|
// options.moduleName
|
|
|
|
// options.msgNumber
|
|
|
|
// options.msgTotal
|
|
|
|
|
|
|
|
var msg = new Message();
|
|
|
|
msg.load( { uuid : options.messageUuid, user : options.client.user }, function loaded(err) {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
|
|
|
var modOpts = {
|
|
|
|
name : options.moduleName,
|
|
|
|
extraArgs : {
|
|
|
|
message : msg,
|
|
|
|
messageAreaName : options.msgAreaName,
|
|
|
|
messageNumber : options.msgNumber,
|
|
|
|
messageTotal : options.msgTotal,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
options.client.gotoMenuModule(modOpts, cb);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|