2015-08-18 21:27:14 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-07-19 03:18:55 +00:00
|
|
|
// ENiGMA½
|
2016-06-20 20:10:12 +00:00
|
|
|
const msgDb = require('./database.js').dbs.message;
|
|
|
|
const Config = require('./config.js').config;
|
|
|
|
const Message = require('./message.js');
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
const msgNetRecord = require('./msg_network.js').recordMessage;
|
|
|
|
|
2016-07-19 03:18:55 +00:00
|
|
|
// deps
|
2016-06-20 20:10:12 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
2015-08-18 21:27:14 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
exports.getAvailableMessageConferences = getAvailableMessageConferences;
|
|
|
|
exports.getSortedAvailMessageConferences = getSortedAvailMessageConferences;
|
|
|
|
exports.getAvailableMessageAreasByConfTag = getAvailableMessageAreasByConfTag;
|
|
|
|
exports.getSortedAvailMessageAreasByConfTag = getSortedAvailMessageAreasByConfTag;
|
|
|
|
exports.getDefaultMessageConferenceTag = getDefaultMessageConferenceTag;
|
|
|
|
exports.getDefaultMessageAreaTagByConfTag = getDefaultMessageAreaTagByConfTag;
|
|
|
|
exports.getMessageConferenceByTag = getMessageConferenceByTag;
|
|
|
|
exports.getMessageAreaByTag = getMessageAreaByTag;
|
|
|
|
exports.changeMessageConference = changeMessageConference;
|
2015-08-20 22:35:04 +00:00
|
|
|
exports.changeMessageArea = changeMessageArea;
|
2016-07-24 17:47:34 +00:00
|
|
|
exports.tempChangeMessageConfAndArea = tempChangeMessageConfAndArea;
|
2015-08-28 04:20:24 +00:00
|
|
|
exports.getMessageListForArea = getMessageListForArea;
|
2016-07-24 17:47:34 +00:00
|
|
|
exports.getNewMessageCountInAreaForUser = getNewMessageCountInAreaForUser;
|
2015-12-31 05:29:10 +00:00
|
|
|
exports.getNewMessagesInAreaForUser = getNewMessagesInAreaForUser;
|
2015-10-22 21:44:44 +00:00
|
|
|
exports.getMessageAreaLastReadId = getMessageAreaLastReadId;
|
|
|
|
exports.updateMessageAreaLastReadId = updateMessageAreaLastReadId;
|
2016-02-21 00:57:38 +00:00
|
|
|
exports.persistMessage = persistMessage;
|
2016-06-20 03:09:45 +00:00
|
|
|
exports.trimMessageAreasScheduledEvent = trimMessageAreasScheduledEvent;
|
2015-08-18 21:27:14 +00:00
|
|
|
|
2016-07-19 03:18:55 +00:00
|
|
|
//
|
|
|
|
// Method for sorting Message areas and conferences
|
|
|
|
// If the sort key is present and is a number, sort in numerical order;
|
|
|
|
// Otherwise, use a locale comparison on the sort key or name as a fallback
|
|
|
|
//
|
|
|
|
function sortAreasOrConfs(areasOrConfs, type) {
|
|
|
|
let entryA;
|
|
|
|
let entryB;
|
|
|
|
|
|
|
|
areasOrConfs.sort((a, b) => {
|
|
|
|
entryA = a[type];
|
|
|
|
entryB = b[type];
|
|
|
|
|
|
|
|
if(_.isNumber(entryA.sort) && _.isNumber(entryB.sort)) {
|
2016-07-19 14:45:18 +00:00
|
|
|
return entryA.sort - entryB.sort;
|
2016-07-19 03:18:55 +00:00
|
|
|
} else {
|
|
|
|
const keyA = entryA.sort ? entryA.sort.toString() : entryA.name;
|
|
|
|
const keyB = entryB.sort ? entryB.sort.toString() : entryB.name;
|
|
|
|
return keyA.localeCompare(keyB);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getAvailableMessageConferences(client, options) {
|
2016-04-13 04:38:32 +00:00
|
|
|
options = options || { includeSystemInternal : false };
|
2016-02-03 04:35:59 +00:00
|
|
|
|
|
|
|
// perform ACS check per conf & omit system_internal if desired
|
2016-07-24 17:47:34 +00:00
|
|
|
return _.omit(Config.messageConferences, (conf, confTag) => {
|
|
|
|
if(!options.includeSystemInternal && 'system_internal' === confTag) {
|
2016-04-13 04:38:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
return !client.acs.hasMessageConfRead(conf);
|
2016-04-13 04:38:32 +00:00
|
|
|
});
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSortedAvailMessageConferences(client, options) {
|
2016-07-19 03:18:55 +00:00
|
|
|
const confs = _.map(getAvailableMessageConferences(client, options), (v, k) => {
|
2016-02-03 04:35:59 +00:00
|
|
|
return {
|
|
|
|
confTag : k,
|
|
|
|
conf : v,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2016-07-19 03:18:55 +00:00
|
|
|
sortAreasOrConfs(confs, 'conf');
|
|
|
|
|
|
|
|
return confs;
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an *object* of available areas within |confTag|
|
|
|
|
function getAvailableMessageAreasByConfTag(confTag, options) {
|
2015-10-18 03:39:54 +00:00
|
|
|
options = options || {};
|
2016-03-23 04:24:00 +00:00
|
|
|
|
|
|
|
// :TODO: confTag === "" then find default
|
2015-10-18 03:39:54 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
if(_.has(Config.messageConferences, [ confTag, 'areas' ])) {
|
2016-04-13 04:38:32 +00:00
|
|
|
const areas = Config.messageConferences[confTag].areas;
|
|
|
|
|
|
|
|
if(!options.client || true === options.noAcsCheck) {
|
|
|
|
// everything - no ACS checks
|
|
|
|
return areas;
|
|
|
|
} else {
|
|
|
|
// perform ACS check per area
|
2016-07-24 17:47:34 +00:00
|
|
|
return _.omit(areas, area => {
|
|
|
|
return !options.client.acs.hasMessageAreaRead(area);
|
2016-04-13 04:38:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2015-10-18 03:39:54 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getSortedAvailMessageAreasByConfTag(confTag, options) {
|
2016-03-25 05:10:08 +00:00
|
|
|
const areas = _.map(getAvailableMessageAreasByConfTag(confTag, options), (v, k) => {
|
|
|
|
return {
|
|
|
|
areaTag : k,
|
|
|
|
area : v,
|
2016-04-13 04:38:32 +00:00
|
|
|
};
|
2016-03-25 05:10:08 +00:00
|
|
|
});
|
|
|
|
|
2016-07-19 03:18:55 +00:00
|
|
|
sortAreasOrConfs(areas, 'area');
|
2016-03-25 05:10:08 +00:00
|
|
|
|
|
|
|
return areas;
|
2015-08-20 22:35:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getDefaultMessageConferenceTag(client, disableAcsCheck) {
|
2015-10-18 02:03:51 +00:00
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
// Find the first conference marked 'default'. If found,
|
|
|
|
// inspect |client| against *read* ACS using defaults if not
|
|
|
|
// specified.
|
|
|
|
//
|
|
|
|
// If the above fails, just go down the list until we get one
|
|
|
|
// that passes.
|
2015-10-18 02:03:51 +00:00
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
// It's possible that we end up with nothing here!
|
|
|
|
//
|
|
|
|
// Note that built in 'system_internal' is always ommited here
|
|
|
|
//
|
2016-06-20 03:09:45 +00:00
|
|
|
let defaultConf = _.findKey(Config.messageConferences, o => o.default);
|
|
|
|
if(defaultConf) {
|
2016-07-24 17:47:34 +00:00
|
|
|
const conf = Config.messageConferences[defaultConf];
|
|
|
|
if(true === disableAcsCheck || client.acs.hasMessageConfRead(conf)) {
|
2016-06-20 03:09:45 +00:00
|
|
|
return defaultConf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// just use anything we can
|
2016-07-24 17:47:34 +00:00
|
|
|
defaultConf = _.findKey(Config.messageConferences, (conf, confTag) => {
|
|
|
|
return 'system_internal' !== confTag && (true === disableAcsCheck || client.acs.hasMessageConfRead(conf));
|
2016-06-20 03:09:45 +00:00
|
|
|
});
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2016-06-20 03:09:45 +00:00
|
|
|
return defaultConf;
|
2015-09-26 06:20:17 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getDefaultMessageAreaTagByConfTag(client, confTag, disableAcsCheck) {
|
2016-06-20 03:09:45 +00:00
|
|
|
//
|
|
|
|
// Similar to finding the default conference:
|
|
|
|
// Find the first entry marked 'default', if any. If found, check | client| against
|
|
|
|
// *read* ACS. If this fails, just find the first one we can that passes checks.
|
|
|
|
//
|
|
|
|
// It's possible that we end up with nothing!
|
|
|
|
//
|
|
|
|
confTag = confTag || getDefaultMessageConferenceTag(client);
|
|
|
|
|
|
|
|
if(confTag && _.has(Config.messageConferences, [ confTag, 'areas' ])) {
|
|
|
|
const areaPool = Config.messageConferences[confTag].areas;
|
|
|
|
let defaultArea = _.findKey(areaPool, o => o.default);
|
|
|
|
if(defaultArea) {
|
2016-07-24 17:47:34 +00:00
|
|
|
const area = areaPool[defaultArea];
|
|
|
|
if(true === disableAcsCheck || client.acs.hasMessageAreaRead(area)) {
|
2016-06-20 03:09:45 +00:00
|
|
|
return defaultArea;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
defaultArea = _.findKey(areaPool, (area) => {
|
|
|
|
return (true === disableAcsCheck || client.acs.hasMessageAreaRead(area));
|
2016-06-20 03:09:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return defaultArea;
|
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2015-08-21 04:51:00 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getMessageConferenceByTag(confTag) {
|
|
|
|
return Config.messageConferences[confTag];
|
|
|
|
}
|
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
function getMessageConfByAreaTag(areaTag) {
|
|
|
|
const confs = Config.messageConferences;
|
|
|
|
let conf;
|
|
|
|
_.forEach(confs, (v) => {
|
|
|
|
if(_.has(v, [ 'areas', areaTag ])) {
|
|
|
|
conf = v;
|
|
|
|
return false; // stop iteration
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMessageConfTagByAreaTag(areaTag) {
|
|
|
|
const confs = Config.messageConferences;
|
|
|
|
return Object.keys(confs).find( (confTag) => {
|
|
|
|
return _.has(confs, [ confTag, 'areas', areaTag]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getMessageAreaByTag(areaTag, optionalConfTag) {
|
2016-06-20 03:09:45 +00:00
|
|
|
const confs = Config.messageConferences;
|
|
|
|
|
|
|
|
if(_.isString(optionalConfTag)) {
|
|
|
|
if(_.has(confs, [ optionalConfTag, 'areas', areaTag ])) {
|
|
|
|
return confs[optionalConfTag].areas[areaTag];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// No confTag to work with - we'll have to search through them all
|
|
|
|
//
|
2016-07-19 03:18:55 +00:00
|
|
|
let area;
|
2016-06-20 03:09:45 +00:00
|
|
|
_.forEach(confs, (v) => {
|
|
|
|
if(_.has(v, [ 'areas', areaTag ])) {
|
|
|
|
area = v.areas[areaTag];
|
|
|
|
return false; // stop iteration
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return area;
|
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2015-08-21 04:51:00 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function changeMessageConference(client, confTag, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getConf(callback) {
|
|
|
|
const conf = getMessageConferenceByTag(confTag);
|
|
|
|
|
|
|
|
if(conf) {
|
|
|
|
callback(null, conf);
|
|
|
|
} else {
|
|
|
|
callback(new Error('Invalid message conference tag'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function getDefaultAreaInConf(conf, callback) {
|
|
|
|
const areaTag = getDefaultMessageAreaTagByConfTag(client, confTag);
|
|
|
|
const area = getMessageAreaByTag(areaTag, confTag);
|
|
|
|
|
|
|
|
if(area) {
|
|
|
|
callback(null, conf, { areaTag : areaTag, area : area } );
|
|
|
|
} else {
|
|
|
|
callback(new Error('No available areas for this user in conference'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function validateAccess(conf, areaInfo, callback) {
|
2016-07-24 17:47:34 +00:00
|
|
|
if(!client.acs.hasMessageConfRead(conf) || !client.acs.hasMessageAreaRead(areaInfo.area)) {
|
|
|
|
return callback(new Error('Access denied to message area and/or conference'));
|
2016-02-03 04:35:59 +00:00
|
|
|
} else {
|
2016-07-24 17:47:34 +00:00
|
|
|
return callback(null, conf, areaInfo);
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
function changeConferenceAndArea(conf, areaInfo, callback) {
|
|
|
|
const newProps = {
|
|
|
|
message_conf_tag : confTag,
|
|
|
|
message_area_tag : areaInfo.areaTag,
|
|
|
|
};
|
|
|
|
client.user.persistProperties(newProps, err => {
|
|
|
|
callback(err, conf, areaInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
],
|
|
|
|
function complete(err, conf, areaInfo) {
|
|
|
|
if(!err) {
|
|
|
|
client.log.info( { confTag : confTag, confName : conf.name, areaTag : areaInfo.areaTag }, 'Current message conference changed');
|
|
|
|
} else {
|
|
|
|
client.log.warn( { confTag : confTag, error : err.message }, 'Could not change message conference');
|
|
|
|
}
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
2015-08-21 04:51:00 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
function changeMessageAreaWithOptions(client, areaTag, options, cb) {
|
|
|
|
options = options || {};
|
|
|
|
|
2015-08-20 22:35:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getArea(callback) {
|
2016-02-03 04:35:59 +00:00
|
|
|
const area = getMessageAreaByTag(areaTag);
|
2016-07-24 17:47:34 +00:00
|
|
|
return callback(area ? null : new Error('Invalid message areaTag'), area);
|
2015-08-20 22:35:04 +00:00
|
|
|
},
|
|
|
|
function validateAccess(area, callback) {
|
2016-02-03 04:35:59 +00:00
|
|
|
//
|
|
|
|
// Need at least *read* to access the area
|
|
|
|
//
|
2016-07-24 17:47:34 +00:00
|
|
|
if(!client.acs.hasMessageAreaRead(area)) {
|
|
|
|
return callback(new Error('Access denied to message area'));
|
2015-11-05 06:04:55 +00:00
|
|
|
} else {
|
2016-07-24 17:47:34 +00:00
|
|
|
return callback(null, area);
|
2015-11-05 06:04:55 +00:00
|
|
|
}
|
2015-08-20 22:35:04 +00:00
|
|
|
},
|
|
|
|
function changeArea(area, callback) {
|
2016-07-24 17:47:34 +00:00
|
|
|
if(true === options.persist) {
|
|
|
|
client.user.persistProperty('message_area_tag', areaTag, function persisted(err) {
|
|
|
|
return callback(err, area);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
client.user.properties['message_area_tag'] = areaTag;
|
|
|
|
return callback(null, area);
|
|
|
|
}
|
2015-08-20 22:35:04 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err, area) {
|
|
|
|
if(!err) {
|
2016-02-03 04:35:59 +00:00
|
|
|
client.log.info( { areaTag : areaTag, area : area }, 'Current message area changed');
|
2015-08-20 22:35:04 +00:00
|
|
|
} else {
|
2016-02-03 04:35:59 +00:00
|
|
|
client.log.warn( { areaTag : areaTag, area : area, error : err.message }, 'Could not change message area');
|
2015-08-20 22:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-08-27 05:25:49 +00:00
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
//
|
|
|
|
// Temporairly -- e.g. non-persisted -- change to an area and it's
|
|
|
|
// associated underlying conference. ACS is checked for both.
|
|
|
|
//
|
|
|
|
// This is useful for example when doing a new scan
|
|
|
|
//
|
|
|
|
function tempChangeMessageConfAndArea(client, areaTag) {
|
|
|
|
const area = getMessageAreaByTag(areaTag);
|
|
|
|
const confTag = getMessageConfTagByAreaTag(areaTag);
|
|
|
|
|
|
|
|
if(!area || !confTag) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const conf = getMessageConferenceByTag(confTag);
|
|
|
|
|
|
|
|
if(!client.acs.hasMessageConfRead(conf) || !client.acs.hasMessageAreaRead(area)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.user.properties.message_conf_tag = confTag;
|
|
|
|
client.user.properties.message_area_tag = areaTag;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function changeMessageArea(client, areaTag, cb) {
|
|
|
|
changeMessageAreaWithOptions(client, areaTag, { persist : true }, cb);
|
|
|
|
}
|
|
|
|
|
2016-01-04 00:47:39 +00:00
|
|
|
function getMessageFromRow(row) {
|
|
|
|
return {
|
|
|
|
messageId : row.message_id,
|
|
|
|
messageUuid : row.message_uuid,
|
|
|
|
replyToMsgId : row.reply_to_message_id,
|
|
|
|
toUserName : row.to_user_name,
|
|
|
|
fromUserName : row.from_user_name,
|
|
|
|
subject : row.subject,
|
|
|
|
modTimestamp : row.modified_timestamp,
|
|
|
|
viewCount : row.view_count,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
function getNewMessageDataInAreaForUserSql(userId, areaTag, lastMessageId, what) {
|
|
|
|
//
|
|
|
|
// Helper for building SQL to fetch either a full message list or simply
|
|
|
|
// a count of new messages based on |what|.
|
|
|
|
//
|
|
|
|
// * If |areaTag| is Message.WellKnownAreaTags.Private,
|
|
|
|
// only messages addressed to |userId| should be returned/counted.
|
|
|
|
//
|
|
|
|
// * Only messages > |lastMessageId| should be returned/counted
|
|
|
|
//
|
|
|
|
const selectWhat = ('count' === what) ?
|
|
|
|
'COUNT() AS count' :
|
|
|
|
'message_id, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject, modified_timestamp, view_count';
|
|
|
|
|
|
|
|
let sql =
|
|
|
|
`SELECT ${selectWhat}
|
|
|
|
FROM message
|
|
|
|
WHERE area_tag = "${areaTag}" AND message_id > ${lastMessageId}`;
|
|
|
|
|
2016-07-25 07:00:35 +00:00
|
|
|
if(Message.isPrivateAreaTag(areaTag)) {
|
2016-07-24 17:47:34 +00:00
|
|
|
sql +=
|
|
|
|
` AND message_id in (
|
|
|
|
SELECT message_id
|
|
|
|
FROM message_meta
|
|
|
|
WHERE meta_category = "System" AND meta_name = "${Message.SystemMetaNames.LocalToUserID}" AND meta_value = ${userId}
|
|
|
|
)`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if('count' === what) {
|
|
|
|
sql += ';';
|
|
|
|
} else {
|
|
|
|
sql += ' ORDER BY message_id;';
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNewMessageCountInAreaForUser(userId, areaTag, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getLastMessageId(callback) {
|
|
|
|
getMessageAreaLastReadId(userId, areaTag, function fetched(err, lastMessageId) {
|
|
|
|
callback(null, lastMessageId || 0); // note: willingly ignoring any errors here!
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getCount(lastMessageId, callback) {
|
|
|
|
const sql = getNewMessageDataInAreaForUserSql(userId, areaTag, lastMessageId, 'count');
|
|
|
|
msgDb.get(sql, (err, row) => {
|
|
|
|
return callback(err, row ? row.count : 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getNewMessagesInAreaForUser(userId, areaTag, cb) {
|
2015-12-31 05:29:10 +00:00
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
// If |areaTag| is Message.WellKnownAreaTags.Private,
|
2015-12-31 05:29:10 +00:00
|
|
|
// only messages addressed to |userId| should be returned.
|
|
|
|
//
|
|
|
|
// Only messages > lastMessageId should be returned
|
|
|
|
//
|
2016-07-19 03:18:55 +00:00
|
|
|
let msgList = [];
|
2015-12-31 05:29:10 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getLastMessageId(callback) {
|
2016-02-03 04:35:59 +00:00
|
|
|
getMessageAreaLastReadId(userId, areaTag, function fetched(err, lastMessageId) {
|
2015-12-31 05:29:10 +00:00
|
|
|
callback(null, lastMessageId || 0); // note: willingly ignoring any errors here!
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getMessages(lastMessageId, callback) {
|
2016-07-24 17:47:34 +00:00
|
|
|
const sql = getNewMessageDataInAreaForUserSql(userId, areaTag, lastMessageId, 'messages');
|
2016-01-04 00:47:39 +00:00
|
|
|
|
2015-12-31 05:29:10 +00:00
|
|
|
msgDb.each(sql, function msgRow(err, row) {
|
|
|
|
if(!err) {
|
2016-01-04 00:47:39 +00:00
|
|
|
msgList.push(getMessageFromRow(row));
|
2015-12-31 05:29:10 +00:00
|
|
|
}
|
|
|
|
}, callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
cb(err, msgList);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getMessageListForArea(options, areaTag, cb) {
|
2015-08-27 05:25:49 +00:00
|
|
|
//
|
|
|
|
// options.client (required)
|
|
|
|
//
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
options.client.log.debug( { areaTag : areaTag }, 'Fetching available messages');
|
2015-09-04 22:17:41 +00:00
|
|
|
|
2015-08-27 05:25:49 +00:00
|
|
|
assert(_.isObject(options.client));
|
|
|
|
|
|
|
|
/*
|
|
|
|
[
|
|
|
|
{
|
|
|
|
messageId, messageUuid, replyToId, toUserName, fromUserName, subject, modTimestamp,
|
|
|
|
status(new|old),
|
|
|
|
viewCount
|
|
|
|
}
|
|
|
|
]
|
|
|
|
*/
|
|
|
|
|
2016-07-18 04:17:24 +00:00
|
|
|
let msgList = [];
|
2015-08-27 05:25:49 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function fetchMessages(callback) {
|
2016-07-25 07:00:35 +00:00
|
|
|
let sql =
|
2016-07-18 04:17:24 +00:00
|
|
|
`SELECT message_id, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject, modified_timestamp, view_count
|
|
|
|
FROM message
|
2016-07-25 07:00:35 +00:00
|
|
|
WHERE area_tag = ?`;
|
|
|
|
|
|
|
|
if(Message.isPrivateAreaTag(areaTag)) {
|
|
|
|
sql +=
|
|
|
|
` AND message_id IN (
|
|
|
|
SELECT message_id
|
|
|
|
FROM message_meta
|
|
|
|
WHERE meta_category = "System" AND meta_name = "${Message.SystemMetaNames.LocalToUserID}" AND meta_value = ${options.client.user.userId}
|
|
|
|
)`;
|
|
|
|
}
|
|
|
|
|
|
|
|
sql += ' ORDER BY message_id;';
|
|
|
|
|
|
|
|
msgDb.each(
|
|
|
|
sql,
|
2016-02-03 04:35:59 +00:00
|
|
|
[ areaTag.toLowerCase() ],
|
2016-07-18 04:17:24 +00:00
|
|
|
(err, row) => {
|
2015-08-27 05:25:49 +00:00
|
|
|
if(!err) {
|
2016-01-04 00:47:39 +00:00
|
|
|
msgList.push(getMessageFromRow(row));
|
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
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getMessageAreaLastReadId(userId, areaTag, cb) {
|
2015-10-22 21:44:44 +00:00
|
|
|
msgDb.get(
|
|
|
|
'SELECT message_id ' +
|
|
|
|
'FROM user_message_area_last_read ' +
|
2016-02-03 04:35:59 +00:00
|
|
|
'WHERE user_id = ? AND area_tag = ?;',
|
2016-07-25 20:36:57 +00:00
|
|
|
[ userId, areaTag.toLowerCase() ],
|
2016-01-04 00:47:39 +00:00
|
|
|
function complete(err, row) {
|
|
|
|
cb(err, row ? row.message_id : 0);
|
|
|
|
}
|
2015-10-22 21:44:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function updateMessageAreaLastReadId(userId, areaTag, messageId, cb) {
|
2015-10-22 21:44:44 +00:00
|
|
|
// :TODO: likely a better way to do this...
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getCurrent(callback) {
|
2016-02-03 04:35:59 +00:00
|
|
|
getMessageAreaLastReadId(userId, areaTag, function result(err, lastId) {
|
2015-10-22 21:44:44 +00:00
|
|
|
lastId = lastId || 0;
|
|
|
|
callback(null, lastId); // ignore errors as we default to 0
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function update(lastId, callback) {
|
|
|
|
if(messageId > lastId) {
|
|
|
|
msgDb.run(
|
2016-02-03 04:35:59 +00:00
|
|
|
'REPLACE INTO user_message_area_last_read (user_id, area_tag, message_id) ' +
|
2015-10-22 21:44:44 +00:00
|
|
|
'VALUES (?, ?, ?);',
|
2016-02-03 04:35:59 +00:00
|
|
|
[ userId, areaTag, messageId ],
|
|
|
|
function written(err) {
|
2016-06-20 20:10:12 +00:00
|
|
|
callback(err, true); // true=didUpdate
|
|
|
|
}
|
2015-10-22 21:44:44 +00:00
|
|
|
);
|
2016-01-04 00:47:39 +00:00
|
|
|
} else {
|
|
|
|
callback(null);
|
2015-10-22 21:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-04 00:47:39 +00:00
|
|
|
],
|
2016-02-03 04:35:59 +00:00
|
|
|
function complete(err, didUpdate) {
|
2016-01-04 00:47:39 +00:00
|
|
|
if(err) {
|
|
|
|
Log.debug(
|
2016-02-03 04:35:59 +00:00
|
|
|
{ error : err.toString(), userId : userId, areaTag : areaTag, messageId : messageId },
|
2016-01-04 00:47:39 +00:00
|
|
|
'Failed updating area last read ID');
|
|
|
|
} else {
|
2016-06-20 20:10:12 +00:00
|
|
|
if(true === didUpdate) {
|
|
|
|
Log.trace(
|
|
|
|
{ userId : userId, areaTag : areaTag, messageId : messageId },
|
|
|
|
'Area last read ID updated');
|
|
|
|
}
|
2016-01-04 00:47:39 +00:00
|
|
|
}
|
|
|
|
cb(err);
|
|
|
|
}
|
2015-10-22 21:44:44 +00:00
|
|
|
);
|
|
|
|
}
|
2016-02-17 05:11:55 +00:00
|
|
|
|
|
|
|
function persistMessage(message, cb) {
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function persistMessageToDisc(callback) {
|
|
|
|
message.persist(callback);
|
|
|
|
},
|
|
|
|
function recordToMessageNetworks(callback) {
|
|
|
|
msgNetRecord(message, callback);
|
2016-08-04 04:43:06 +00:00
|
|
|
}
|
2016-02-17 05:11:55 +00:00
|
|
|
],
|
|
|
|
cb
|
|
|
|
);
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
// method exposed for event scheduler
|
|
|
|
function trimMessageAreasScheduledEvent(args, cb) {
|
|
|
|
|
|
|
|
function trimMessageAreaByMaxMessages(areaInfo, cb) {
|
|
|
|
if(0 === areaInfo.maxMessages) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
msgDb.run(
|
|
|
|
`DELETE FROM message
|
2016-07-18 04:17:24 +00:00
|
|
|
WHERE message_id IN(
|
|
|
|
SELECT message_id
|
2016-06-20 20:10:12 +00:00
|
|
|
FROM message
|
|
|
|
WHERE area_tag = ?
|
2016-07-18 04:17:24 +00:00
|
|
|
ORDER BY message_id DESC
|
|
|
|
LIMIT -1 OFFSET ${areaInfo.maxMessages}
|
|
|
|
);`,
|
2016-07-25 20:36:57 +00:00
|
|
|
[ areaInfo.areaTag.toLowerCase() ],
|
2016-06-20 20:10:12 +00:00
|
|
|
err => {
|
|
|
|
if(err) {
|
2016-07-18 04:17:24 +00:00
|
|
|
Log.error( { areaInfo : areaInfo, err : err, type : 'maxMessages' }, 'Error trimming message area');
|
2016-06-20 20:10:12 +00:00
|
|
|
} else {
|
|
|
|
Log.debug( { areaInfo : areaInfo, type : 'maxMessages' }, 'Area trimmed successfully');
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
function trimMessageAreaByMaxAgeDays(areaInfo, cb) {
|
|
|
|
if(0 === areaInfo.maxAgeDays) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
msgDb.run(
|
|
|
|
`DELETE FROM message
|
|
|
|
WHERE area_tag = ? AND modified_timestamp < date('now', '-${areaInfo.maxAgeDays} days');`,
|
|
|
|
[ areaInfo.areaTag ],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
2016-07-18 04:17:24 +00:00
|
|
|
Log.warn( { areaInfo : areaInfo, err : err, type : 'maxAgeDays' }, 'Error trimming message area');
|
2016-06-20 03:09:45 +00:00
|
|
|
} else {
|
2016-06-20 20:10:12 +00:00
|
|
|
Log.debug( { areaInfo : areaInfo, type : 'maxAgeDays' }, 'Area trimmed successfully');
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
return cb(err);
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
);
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async.waterfall(
|
2016-06-20 20:10:12 +00:00
|
|
|
[
|
2016-06-20 03:09:45 +00:00
|
|
|
function getAreaTags(callback) {
|
|
|
|
let areaTags = [];
|
|
|
|
msgDb.each(
|
|
|
|
`SELECT DISTINCT area_tag
|
|
|
|
FROM message;`,
|
|
|
|
(err, row) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
areaTags.push(row.area_tag);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err, areaTags);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2016-06-20 20:10:12 +00:00
|
|
|
function prepareAreaInfo(areaTags, callback) {
|
|
|
|
let areaInfos = [];
|
|
|
|
|
|
|
|
// determine maxMessages & maxAgeDays per area
|
2016-06-20 03:09:45 +00:00
|
|
|
areaTags.forEach(areaTag => {
|
|
|
|
|
|
|
|
let maxMessages = Config.messageAreaDefaults.maxMessages;
|
|
|
|
let maxAgeDays = Config.messageAreaDefaults.maxAgeDays;
|
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
const area = getMessageAreaByTag(areaTag); // note: we don't know the conf here
|
2016-06-20 03:09:45 +00:00
|
|
|
if(area) {
|
|
|
|
if(area.maxMessages) {
|
|
|
|
maxMessages = area.maxMessages;
|
|
|
|
}
|
|
|
|
if(area.maxAgeDays) {
|
|
|
|
maxAgeDays = area.maxAgeDays;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
areaInfos.push( {
|
|
|
|
areaTag : areaTag,
|
|
|
|
maxMessages : maxMessages,
|
|
|
|
maxAgeDays : maxAgeDays,
|
|
|
|
} );
|
2016-06-20 03:09:45 +00:00
|
|
|
});
|
2016-06-20 20:10:12 +00:00
|
|
|
|
|
|
|
return callback(null, areaInfos);
|
|
|
|
},
|
|
|
|
function trimAreas(areaInfos, callback) {
|
|
|
|
async.each(
|
|
|
|
areaInfos,
|
|
|
|
(areaInfo, next) => {
|
|
|
|
trimMessageAreaByMaxMessages(areaInfo, err => {
|
|
|
|
if(err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
trimMessageAreaByMaxAgeDays(areaInfo, err => {
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
);
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
}
|