2015-08-18 21:27:14 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-07-19 03:18:55 +00:00
|
|
|
// ENiGMA½
|
2018-01-12 04:17:59 +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;
|
|
|
|
const sortAreasOrConfs = require('./conf_area_util.js').sortAreasOrConfs;
|
|
|
|
const { getISOTimestampString } = require('./database.js');
|
2016-06-20 20:10:12 +00:00
|
|
|
|
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');
|
2018-01-12 04:17:59 +00:00
|
|
|
const moment = require('moment');
|
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;
|
2018-01-12 04:17:59 +00:00
|
|
|
exports.getMessageIdNewerThanTimestampByArea = getMessageIdNewerThanTimestampByArea;
|
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-02-03 04:35:59 +00:00
|
|
|
function getAvailableMessageConferences(client, options) {
|
2016-04-13 04:38:32 +00:00
|
|
|
options = options || { includeSystemInternal : false };
|
2017-02-20 18:31:24 +00:00
|
|
|
|
|
|
|
assert(client || true === options.noClient);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
|
|
|
// perform ACS check per conf & omit system_internal if desired
|
|
|
|
return _.omitBy(Config.messageConferences, (conf, confTag) => {
|
2016-07-24 17:47:34 +00:00
|
|
|
if(!options.includeSystemInternal && 'system_internal' === confTag) {
|
2016-04-13 04:38:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-20 18:31:24 +00:00
|
|
|
return client && !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');
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-07-19 03:18:55 +00:00
|
|
|
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 || {};
|
2018-01-15 19:22:11 +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
|
2017-01-28 19:33:06 +00:00
|
|
|
return _.omitBy(areas, area => {
|
2016-07-24 17:47:34 +00:00
|
|
|
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
|
|
|
});
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-07-19 03:18:55 +00:00
|
|
|
sortAreasOrConfs(areas, 'area');
|
2018-01-15 19:22:11 +00:00
|
|
|
|
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.
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
// 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;
|
2018-01-15 19:22:11 +00:00
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
});
|
2018-01-15 19:22:11 +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' ])) {
|
2018-01-15 19:22:11 +00:00
|
|
|
const areaPool = Config.messageConferences[confTag].areas;
|
2016-06-20 03:09:45 +00:00
|
|
|
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;
|
2018-01-15 19:22:11 +00:00
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
defaultArea = _.findKey(areaPool, (area) => {
|
2018-01-15 19:22:11 +00:00
|
|
|
return (true === disableAcsCheck || client.acs.hasMessageAreaRead(area));
|
2016-06-20 03:09:45 +00:00
|
|
|
});
|
2018-01-15 19:22:11 +00:00
|
|
|
|
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 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
|
2018-01-15 19:22:11 +00:00
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
});
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-06-20 03:09:45 +00:00
|
|
|
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);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
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);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
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
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
},
|
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) {
|
2016-10-01 19:25:32 +00:00
|
|
|
options = options || {}; // :TODO: this is currently pointless... cb is required...
|
2016-07-24 17:47:34 +00:00
|
|
|
|
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);
|
2018-01-15 19:22:11 +00:00
|
|
|
return callback(area ? null : new Error('Invalid message areaTag'), area);
|
2015-08-20 22:35:04 +00:00
|
|
|
},
|
|
|
|
function validateAccess(area, callback) {
|
2018-01-15 19:22:11 +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
|
|
|
}
|
|
|
|
|
2016-10-01 19:25:32 +00:00
|
|
|
return cb(err);
|
2015-08-20 22:35:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-08-27 05:25:49 +00:00
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
//
|
2018-01-15 19:22:11 +00:00
|
|
|
// Temporairly -- e.g. non-persisted -- change to an area and it's
|
2016-07-24 17:47:34 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
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) {
|
2018-01-15 19:22:11 +00:00
|
|
|
return {
|
2016-01-04 00:47:39 +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,
|
|
|
|
subject : row.subject,
|
|
|
|
modTimestamp : row.modified_timestamp,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-27 04:42:43 +00:00
|
|
|
function getNewMessageCountInAreaForUser(userId, areaTag, cb) {
|
|
|
|
getMessageAreaLastReadId(userId, areaTag, (err, lastMessageId) => {
|
|
|
|
lastMessageId = lastMessageId || 0;
|
2016-07-24 17:47:34 +00:00
|
|
|
|
2018-01-27 04:42:43 +00:00
|
|
|
const filter = {
|
|
|
|
areaTag,
|
|
|
|
newerThanMessageId : lastMessageId,
|
|
|
|
resultType : 'count',
|
|
|
|
};
|
2016-07-24 17:47:34 +00:00
|
|
|
|
2018-01-27 04:42:43 +00:00
|
|
|
if(Message.isPrivateAreaTag(areaTag)) {
|
|
|
|
filter.privateTagUserId = userId;
|
|
|
|
}
|
2016-07-24 17:47:34 +00:00
|
|
|
|
2018-01-27 04:42:43 +00:00
|
|
|
Message.findMessages(filter, (err, count) => {
|
|
|
|
return cb(err, count);
|
|
|
|
});
|
|
|
|
});
|
2016-07-24 17:47:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getNewMessagesInAreaForUser(userId, areaTag, cb) {
|
2018-01-27 04:42:43 +00:00
|
|
|
getMessageAreaLastReadId(userId, areaTag, (err, lastMessageId) => {
|
|
|
|
lastMessageId = lastMessageId || 0;
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
areaTag,
|
|
|
|
newerThanMessageId : lastMessageId,
|
|
|
|
sort : 'messageId',
|
|
|
|
order : 'ascending',
|
|
|
|
extraFields : [ 'message_uuid', 'reply_to_message_id', 'to_user_name', 'from_user_name', 'subject', 'modified_timestamp' ],
|
|
|
|
};
|
2015-12-31 05:29:10 +00:00
|
|
|
|
2018-01-27 04:42:43 +00:00
|
|
|
if(Message.isPrivateAreaTag(areaTag)) {
|
|
|
|
filter.privateTagUserId = userId;
|
2015-12-31 05:29:10 +00:00
|
|
|
}
|
2015-09-04 22:17:41 +00:00
|
|
|
|
2018-01-27 04:42:43 +00:00
|
|
|
Message.findMessages(filter, (err, messages) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
2015-08-27 05:25:49 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 04:42:43 +00:00
|
|
|
return cb(null, messages.map(msg => getMessageFromRow(msg)));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-08-27 05:25:49 +00:00
|
|
|
|
2018-01-27 04:42:43 +00:00
|
|
|
function getMessageListForArea(client, areaTag, cb) {
|
|
|
|
const filter = {
|
|
|
|
areaTag,
|
|
|
|
sort : 'messageId',
|
|
|
|
order : 'ascending',
|
|
|
|
extraFields : [ 'message_uuid', 'reply_to_message_id', 'to_user_name', 'from_user_name', 'subject', 'modified_timestamp' ],
|
|
|
|
};
|
2016-07-25 07:00:35 +00:00
|
|
|
|
2018-01-27 04:42:43 +00:00
|
|
|
if(Message.isPrivateAreaTag(areaTag)) {
|
|
|
|
filter.privateTagUserId = client.user.userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
Message.findMessages(filter, (err, messages) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
2015-08-28 04:20:24 +00:00
|
|
|
}
|
2018-01-27 04:42:43 +00:00
|
|
|
|
|
|
|
return cb(null, messages.map(msg => getMessageFromRow(msg)));
|
|
|
|
});
|
2015-08-27 05:25:49 +00:00
|
|
|
}
|
2015-09-06 21:58:58 +00:00
|
|
|
|
2018-01-12 04:17:59 +00:00
|
|
|
function getMessageIdNewerThanTimestampByArea(areaTag, newerThanTimestamp, cb) {
|
2018-01-27 04:42:43 +00:00
|
|
|
Message.findMessages(
|
|
|
|
{
|
|
|
|
areaTag,
|
|
|
|
newerThanTimestamp,
|
|
|
|
sort : 'modTimestamp',
|
|
|
|
order : 'ascending',
|
|
|
|
limit : 1,
|
|
|
|
},
|
|
|
|
(err, id) => {
|
2018-01-12 04:17:59 +00:00
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-01-27 04:42:43 +00:00
|
|
|
return cb(null, id ? id[0] : null);
|
2018-01-12 04:17:59 +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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-12 04:17:59 +00:00
|
|
|
function updateMessageAreaLastReadId(userId, areaTag, messageId, allowOlder, cb) {
|
|
|
|
if(!cb && _.isFunction(allowOlder)) {
|
|
|
|
cb = allowOlder;
|
|
|
|
allowOlder = false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-01-12 04:17:59 +00:00
|
|
|
if(allowOlder || messageId > lastId) {
|
2015-10-22 21:44:44 +00:00
|
|
|
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) {
|
2018-01-15 19:22:11 +00:00
|
|
|
Log.debug(
|
|
|
|
{ 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) {
|
2018-01-15 19:22:11 +00:00
|
|
|
Log.trace(
|
2016-06-20 20:10:12 +00:00
|
|
|
{ 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) {
|
2016-09-15 03:08:59 +00:00
|
|
|
return message.persist(callback);
|
2016-02-17 05:11:55 +00:00
|
|
|
},
|
|
|
|
function recordToMessageNetworks(callback) {
|
2016-09-15 03:08:59 +00:00
|
|
|
return msgNetRecord(message, callback);
|
2018-01-15 19:22:11 +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) {
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
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() ],
|
2018-01-14 20:52:40 +00:00
|
|
|
function result(err) { // no arrow func; need this
|
2016-06-20 20:10:12 +00:00
|
|
|
if(err) {
|
2018-01-14 20:52:40 +00:00
|
|
|
Log.error( { areaInfo : areaInfo, error : err.message, type : 'maxMessages' }, 'Error trimming message area');
|
2016-06-20 20:10:12 +00:00
|
|
|
} else {
|
2018-01-14 20:52:40 +00:00
|
|
|
Log.debug( { areaInfo : areaInfo, type : 'maxMessages', count : this.changes }, 'Area trimmed successfully');
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
return cb(err);
|
2018-01-15 19:22:11 +00:00
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
);
|
|
|
|
}
|
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 ],
|
2018-01-14 20:52:40 +00:00
|
|
|
function result(err) { // no arrow func; need this
|
2016-06-20 20:10:12 +00:00
|
|
|
if(err) {
|
2018-01-14 20:52:40 +00:00
|
|
|
Log.warn( { areaInfo : areaInfo, error : err.message, type : 'maxAgeDays' }, 'Error trimming message area');
|
2016-06-20 03:09:45 +00:00
|
|
|
} else {
|
2018-01-14 20:52:40 +00:00
|
|
|
Log.debug( { areaInfo : areaInfo, type : 'maxAgeDays', count : this.changes }, '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
|
|
|
}
|
2018-01-14 20:52:40 +00:00
|
|
|
|
2016-06-20 03:09:45 +00:00
|
|
|
async.waterfall(
|
2018-01-14 20:52:40 +00:00
|
|
|
[
|
2016-06-20 03:09:45 +00:00
|
|
|
function getAreaTags(callback) {
|
2018-01-14 20:52:40 +00:00
|
|
|
const areaTags = [];
|
|
|
|
|
|
|
|
//
|
|
|
|
// We use SQL here vs API such that no-longer-used tags are picked up
|
|
|
|
//
|
2016-06-20 03:09:45 +00:00
|
|
|
msgDb.each(
|
|
|
|
`SELECT DISTINCT area_tag
|
|
|
|
FROM message;`,
|
|
|
|
(err, row) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-01-14 20:52:40 +00:00
|
|
|
|
|
|
|
// We treat private mail special
|
|
|
|
if(!Message.isPrivateAreaTag(row.area_tag)) {
|
|
|
|
areaTags.push(row.area_tag);
|
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
},
|
|
|
|
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 => {
|
2018-01-14 20:52:40 +00:00
|
|
|
|
2016-06-20 03:09:45 +00:00
|
|
|
let maxMessages = Config.messageAreaDefaults.maxMessages;
|
|
|
|
let maxAgeDays = Config.messageAreaDefaults.maxAgeDays;
|
2018-01-14 20:52:40 +00:00
|
|
|
|
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) {
|
2018-01-14 20:52:40 +00:00
|
|
|
maxMessages = area.maxMessages || maxMessages;
|
|
|
|
maxAgeDays = area.maxAgeDays || maxAgeDays;
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 20:10:12 +00:00
|
|
|
areaInfos.push( {
|
|
|
|
areaTag : areaTag,
|
|
|
|
maxMessages : maxMessages,
|
|
|
|
maxAgeDays : maxAgeDays,
|
2018-01-14 20:52:40 +00:00
|
|
|
} );
|
2016-06-20 03:09:45 +00:00
|
|
|
});
|
2016-06-20 20:10:12 +00:00
|
|
|
|
|
|
|
return callback(null, areaInfos);
|
|
|
|
},
|
2018-01-14 20:52:40 +00:00
|
|
|
function trimGeneralAreas(areaInfos, callback) {
|
2016-06-20 20:10:12 +00:00
|
|
|
async.each(
|
|
|
|
areaInfos,
|
|
|
|
(areaInfo, next) => {
|
|
|
|
trimMessageAreaByMaxMessages(areaInfo, err => {
|
|
|
|
if(err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
trimMessageAreaByMaxAgeDays(areaInfo, err => {
|
|
|
|
return next(err);
|
2018-01-15 19:22:11 +00:00
|
|
|
});
|
2016-06-20 20:10:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
2018-01-14 20:52:40 +00:00
|
|
|
},
|
|
|
|
function trimExternalPrivateSentMail(callback) {
|
|
|
|
//
|
|
|
|
// *External* (FTN, email, ...) outgoing is cleaned up *after export*
|
|
|
|
// if it is older than the configured |maxExternalSentAgeDays| days
|
|
|
|
//
|
|
|
|
// Outgoing externally exported private mail is:
|
|
|
|
// - In the 'private_mail' area
|
|
|
|
// - Marked exported (state_flags0 exported bit set)
|
|
|
|
// - Marked with any external flavor (we don't mark local)
|
|
|
|
//
|
|
|
|
const maxExternalSentAgeDays = _.get(
|
|
|
|
Config,
|
|
|
|
'messageConferences.system_internal.areas.private_mail.maxExternalSentAgeDays',
|
|
|
|
30
|
|
|
|
);
|
|
|
|
|
|
|
|
msgDb.run(
|
|
|
|
`DELETE FROM message
|
|
|
|
WHERE message_id IN (
|
|
|
|
SELECT m.message_id
|
|
|
|
FROM message m
|
|
|
|
JOIN message_meta mms
|
|
|
|
ON m.message_id = mms.message_id AND
|
|
|
|
(mms.meta_category='System' AND mms.meta_name='${Message.SystemMetaNames.StateFlags0}' AND (mms.meta_value & ${Message.StateFlags0.Exported} = ${Message.StateFlags0.Exported}))
|
|
|
|
JOIN message_meta mmf
|
|
|
|
ON m.message_id = mmf.message_id AND
|
|
|
|
(mmf.meta_category='System' AND mmf.meta_name='${Message.SystemMetaNames.ExternalFlavor}')
|
|
|
|
WHERE m.area_tag='${Message.WellKnownAreaTags.Private}' AND DATETIME('now') > DATETIME(m.modified_timestamp, '+${maxExternalSentAgeDays} days')
|
|
|
|
);`,
|
|
|
|
function results(err) { // no arrow func; need this
|
|
|
|
if(err) {
|
|
|
|
Log.warn( { error : err.message }, 'Error trimming private externally sent messages');
|
|
|
|
} else {
|
|
|
|
Log.debug( { count : this.changes }, 'Private externally sent messages trimmed successfully');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2016-06-20 03:09:45 +00:00
|
|
|
);
|
2016-02-17 05:11:55 +00:00
|
|
|
}
|