2015-08-18 21:27:14 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const msgDb = require('./database.js').dbs.message;
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
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');
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const SysProps = require('./system_property.js');
|
2023-02-21 03:09:58 +00:00
|
|
|
const {
|
|
|
|
SystemInternalConfTags,
|
|
|
|
WellKnownConfTags,
|
|
|
|
WellKnownAreaTags,
|
|
|
|
} = require('./message_const');
|
2023-03-15 03:58:03 +00:00
|
|
|
const Collection = require('./activitypub/collection');
|
|
|
|
const { Collections } = require('./activitypub/const');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
exports.startup = startup;
|
|
|
|
exports.shutdown = shutdown;
|
|
|
|
exports.getAvailableMessageConferences = getAvailableMessageConferences;
|
|
|
|
exports.getSortedAvailMessageConferences = getSortedAvailMessageConferences;
|
|
|
|
exports.getAvailableMessageAreasByConfTag = getAvailableMessageAreasByConfTag;
|
2016-02-03 04:35:59 +00:00
|
|
|
exports.getSortedAvailMessageAreasByConfTag = getSortedAvailMessageAreasByConfTag;
|
2022-06-12 20:12:03 +00:00
|
|
|
exports.getAllAvailableMessageAreaTags = getAllAvailableMessageAreaTags;
|
|
|
|
exports.getDefaultMessageConferenceTag = getDefaultMessageConferenceTag;
|
|
|
|
exports.getDefaultMessageAreaTagByConfTag = getDefaultMessageAreaTagByConfTag;
|
|
|
|
exports.getSuitableMessageConfAndAreaTags = getSuitableMessageConfAndAreaTags;
|
|
|
|
exports.getMessageConferenceByTag = getMessageConferenceByTag;
|
|
|
|
exports.getMessageAreaByTag = getMessageAreaByTag;
|
|
|
|
exports.getMessageConfTagByAreaTag = getMessageConfTagByAreaTag;
|
|
|
|
exports.changeMessageConference = changeMessageConference;
|
|
|
|
exports.changeMessageArea = changeMessageArea;
|
|
|
|
exports.hasMessageConfAndAreaRead = hasMessageConfAndAreaRead;
|
|
|
|
exports.hasMessageConfAndAreaWrite = hasMessageConfAndAreaWrite;
|
|
|
|
exports.filterMessageAreaTagsByReadACS = filterMessageAreaTagsByReadACS;
|
|
|
|
exports.filterMessageListByReadACS = filterMessageListByReadACS;
|
|
|
|
exports.tempChangeMessageConfAndArea = tempChangeMessageConfAndArea;
|
|
|
|
exports.getMessageListForArea = getMessageListForArea;
|
|
|
|
exports.getNewMessageCountInAreaForUser = getNewMessageCountInAreaForUser;
|
|
|
|
exports.getNewMessageCountAddressedToUser = getNewMessageCountAddressedToUser;
|
|
|
|
exports.getNewMessagesInAreaForUser = getNewMessagesInAreaForUser;
|
|
|
|
exports.getMessageIdNewerThanTimestampByArea = getMessageIdNewerThanTimestampByArea;
|
|
|
|
exports.getMessageAreaLastReadId = getMessageAreaLastReadId;
|
|
|
|
exports.updateMessageAreaLastReadId = updateMessageAreaLastReadId;
|
|
|
|
exports.persistMessage = persistMessage;
|
|
|
|
exports.trimMessageAreasScheduledEvent = trimMessageAreasScheduledEvent;
|
2015-08-18 21:27:14 +00:00
|
|
|
|
2018-11-28 04:21:00 +00:00
|
|
|
function startup(cb) {
|
|
|
|
// by default, private messages are NOT included
|
|
|
|
async.series(
|
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
|
|
|
Message.findMessages({ resultType: 'count' }, (err, count) => {
|
|
|
|
if (count) {
|
|
|
|
StatLog.setNonPersistentSystemStat(
|
|
|
|
SysProps.MessageTotalCount,
|
|
|
|
count
|
|
|
|
);
|
2018-11-28 04:21:00 +00:00
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
|
|
|
Message.findMessages(
|
|
|
|
{ resultType: 'count', date: moment() },
|
|
|
|
(err, count) => {
|
|
|
|
if (count) {
|
|
|
|
StatLog.setNonPersistentSystemStat(
|
|
|
|
SysProps.MessagesToday,
|
|
|
|
count
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return callback(err);
|
2018-11-28 04:21:00 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
|
|
|
},
|
2018-11-28 04:21:00 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function shutdown(cb) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getAvailableMessageConferences(client, options) {
|
2022-06-05 20:04:25 +00:00
|
|
|
options = options || { includeSystemInternal: false };
|
2017-02-20 18:31:24 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(client || true === options.noClient);
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2023-02-21 03:09:58 +00:00
|
|
|
// perform ACS check per conf & omit "System Internal" if desired
|
2018-06-22 05:15:04 +00:00
|
|
|
return _.omitBy(Config().messageConferences, (conf, confTag) => {
|
2023-02-21 03:09:58 +00:00
|
|
|
if (!options.includeSystemInternal && SystemInternalConfTags.includes(confTag)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-13 04:38:32 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return client && !client.acs.hasMessageConfRead(conf);
|
|
|
|
});
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSortedAvailMessageConferences(client, options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const confs = _.map(getAvailableMessageConferences(client, options), (v, k) => {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
confTag: k,
|
|
|
|
conf: v,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
});
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
sortAreasOrConfs(confs, 'conf');
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return confs;
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an *object* of available areas within |confTag|
|
|
|
|
function getAvailableMessageAreasByConfTag(confTag, options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
// :TODO: confTag === "" then find default
|
|
|
|
|
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.has(config.messageConferences, [confTag, 'areas'])) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const areas = config.messageConferences[confTag].areas;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!options.client || true === options.noAcsCheck) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// everything - no ACS checks
|
2018-06-22 05:15:04 +00:00
|
|
|
return areas;
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// perform ACS check per area
|
2018-06-22 05:15:04 +00:00
|
|
|
return _.omitBy(areas, area => {
|
|
|
|
return !options.client.acs.hasMessageAreaRead(area);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const areas = _.map(getAvailableMessageAreasByConfTag(confTag, options), (v, k) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return {
|
|
|
|
areaTag: k,
|
|
|
|
area: v,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
});
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
sortAreasOrConfs(areas, 'area');
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return areas;
|
2015-08-20 22:35:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 16:42:57 +00:00
|
|
|
function getAllAvailableMessageAreaTags(client, options) {
|
|
|
|
const areaTags = [];
|
|
|
|
|
|
|
|
// mask over older messy APIs for now
|
2022-06-05 20:04:25 +00:00
|
|
|
const confOpts = Object.assign({}, options, { noClient: client ? false : true });
|
2020-05-03 16:42:57 +00:00
|
|
|
const areaOpts = Object.assign({}, options, { client });
|
|
|
|
|
|
|
|
Object.keys(getAvailableMessageConferences(client, confOpts)).forEach(confTag => {
|
2022-06-05 20:04:25 +00:00
|
|
|
areaTags.push(
|
|
|
|
...Object.keys(getAvailableMessageAreasByConfTag(confTag, areaOpts))
|
|
|
|
);
|
2020-05-03 16:42:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return areaTags;
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getDefaultMessageConferenceTag(client, disableAcsCheck) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Find the first conference marked 'default'. If found,
|
|
|
|
// inspect |client| against *read* ACS using defaults if not
|
|
|
|
// specified.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// If the above fails, just go down the list until we get one
|
|
|
|
// that passes.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// It's possible that we end up with nothing here!
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2023-02-21 03:09:58 +00:00
|
|
|
// Note that built in "System Internal" are always omitted here
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const config = Config();
|
|
|
|
let defaultConf = _.findKey(config.messageConferences, o => o.default);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (defaultConf) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const conf = config.messageConferences[defaultConf];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === disableAcsCheck || client.acs.hasMessageConfRead(conf)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return defaultConf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// just use anything we can
|
|
|
|
defaultConf = _.findKey(config.messageConferences, (conf, confTag) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
2023-02-21 03:09:58 +00:00
|
|
|
!SystemInternalConfTags.includes(confTag) &&
|
2022-06-05 20:04:25 +00:00
|
|
|
(true === disableAcsCheck || client.acs.hasMessageConfRead(conf))
|
|
|
|
);
|
2018-06-22 05:15:04 +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) {
|
2018-06-22 05:15:04 +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);
|
|
|
|
|
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (confTag && _.has(config.messageConferences, [confTag, 'areas'])) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const areaPool = config.messageConferences[confTag].areas;
|
|
|
|
let defaultArea = _.findKey(areaPool, o => o.default);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (defaultArea) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const area = areaPool[defaultArea];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === disableAcsCheck || client.acs.hasMessageAreaRead(area)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return defaultArea;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 03:21:33 +00:00
|
|
|
defaultArea = _.findKey(areaPool, (area, areaTag) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Message.isPrivateAreaTag(areaTag)) {
|
2019-09-12 03:21:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return true === disableAcsCheck || client.acs.hasMessageAreaRead(area);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return defaultArea;
|
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2015-08-21 04:51:00 +00:00
|
|
|
|
2019-09-12 03:21:33 +00:00
|
|
|
function getSuitableMessageConfAndAreaTags(client) {
|
|
|
|
//
|
|
|
|
// Attempts to get a pair of suitable conf/area tags:
|
|
|
|
// * Where the client/user has proper ACS to both
|
|
|
|
// * Try to use defaults where possible
|
|
|
|
// * If default conf/area is not an option, use any
|
|
|
|
// that pass ACS.
|
|
|
|
// * Returns a tuple [confTag, areaTag]; areaTag
|
|
|
|
// and possibly confTag may both be set to '' if
|
|
|
|
// if we fail to find something.
|
|
|
|
//
|
|
|
|
let confTag = getDefaultMessageConferenceTag(client);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!confTag) {
|
|
|
|
return ['', '']; // can't have an area without a conf
|
2019-09-12 03:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let areaTag = getDefaultMessageAreaTagByConfTag(client, confTag);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!areaTag) {
|
2019-09-12 03:21:33 +00:00
|
|
|
// OK, perhaps *any* area in *any* conf?
|
|
|
|
_.forEach(Config().messageConferences, (conf, ct) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!client.acs.hasMessageConfRead(conf)) {
|
2019-09-12 03:21:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_.forEach(conf.areas, (area, at) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
!_.includes(Message.WellKnownAreaTags, at) &&
|
|
|
|
client.acs.hasMessageAreaRead(area)
|
|
|
|
) {
|
2019-09-12 03:21:33 +00:00
|
|
|
confTag = ct;
|
|
|
|
areaTag = at;
|
2022-06-05 20:04:25 +00:00
|
|
|
return false; // stop inner iteration
|
2019-09-12 03:21:33 +00:00
|
|
|
}
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
if (areaTag) {
|
|
|
|
return false; // stop iteration
|
2019-09-12 03:21:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return [confTag, areaTag || ''];
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getMessageConferenceByTag(confTag) {
|
2019-09-12 03:21:33 +00:00
|
|
|
return Object.assign({ confTag }, Config().messageConferences[confTag]);
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
function getMessageConfTagByAreaTag(areaTag) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const confs = Config().messageConferences;
|
2022-06-05 20:04:25 +00:00
|
|
|
return Object.keys(confs).find(confTag => {
|
|
|
|
return _.has(confs, [confTag, 'areas', areaTag]);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2016-07-24 17:47:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getMessageAreaByTag(areaTag, optionalConfTag) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const confs = Config().messageConferences;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: this could be cached
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(optionalConfTag)) {
|
|
|
|
if (_.has(confs, [optionalConfTag, 'areas', areaTag])) {
|
2019-09-12 03:21:33 +00:00
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
areaTag,
|
2022-06-05 20:04:25 +00:00
|
|
|
confTag: optionalConfTag,
|
2019-09-12 03:21:33 +00:00
|
|
|
},
|
|
|
|
confs[optionalConfTag].areas[areaTag]
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// No confTag to work with - we'll have to search through them all
|
|
|
|
//
|
|
|
|
let area;
|
2019-09-12 03:21:33 +00:00
|
|
|
_.forEach(confs, (conf, confTag) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.has(conf, ['areas', areaTag])) {
|
2019-09-12 03:21:33 +00:00
|
|
|
area = Object.assign({ areaTag, confTag }, conf.areas[areaTag]);
|
2022-06-05 20:04:25 +00:00
|
|
|
return false; // stop iteration
|
2018-06-22 05:15:04 +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) {
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getConf(callback) {
|
|
|
|
const conf = getMessageConferenceByTag(confTag);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (conf) {
|
2018-06-22 05:15:04 +00:00
|
|
|
callback(null, conf);
|
|
|
|
} else {
|
|
|
|
callback(new Error('Invalid message conference tag'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function getDefaultAreaInConf(conf, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const areaTag = getDefaultMessageAreaTagByConfTag(client, confTag);
|
|
|
|
const area = getMessageAreaByTag(areaTag, confTag);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (area) {
|
|
|
|
callback(null, conf, { areaTag: areaTag, area: area });
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
callback(new Error('No available areas for this user in conference'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function validateAccess(conf, areaInfo, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
!client.acs.hasMessageConfRead(conf) ||
|
|
|
|
!client.acs.hasMessageAreaRead(areaInfo.area)
|
|
|
|
) {
|
|
|
|
return callback(
|
|
|
|
new Error('Access denied to message area and/or conference')
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
return callback(null, conf, areaInfo);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function changeConferenceAndArea(conf, areaInfo, callback) {
|
|
|
|
const newProps = {
|
2022-06-05 20:04:25 +00:00
|
|
|
[UserProps.MessageConfTag]: confTag,
|
|
|
|
[UserProps.MessageAreaTag]: areaInfo.areaTag,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
client.user.persistProperties(newProps, err => {
|
|
|
|
callback(err, conf, areaInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
],
|
|
|
|
function complete(err, conf, areaInfo) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err) {
|
|
|
|
client.log.info(
|
|
|
|
{ confTag: confTag, confName: conf.name, areaTag: areaInfo.areaTag },
|
2022-08-13 17:51:11 +00:00
|
|
|
`${client.node} changed message conference to ${areaInfo.areaTag}`
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
client.log.warn(
|
|
|
|
{ confTag: confTag, error: err.message },
|
|
|
|
'Could not change message conference'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
2015-08-21 04:51:00 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 17:47:34 +00:00
|
|
|
function changeMessageAreaWithOptions(client, areaTag, options, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
options = options || {}; // :TODO: this is currently pointless... cb is required...
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getArea(callback) {
|
|
|
|
const area = getMessageAreaByTag(areaTag);
|
|
|
|
return callback(area ? null : new Error('Invalid message areaTag'), area);
|
|
|
|
},
|
|
|
|
function validateAccess(area, callback) {
|
|
|
|
//
|
|
|
|
// Need at least *read* to access the area
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!client.acs.hasMessageAreaRead(area)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(new Error('Access denied to message area'));
|
|
|
|
} else {
|
|
|
|
return callback(null, area);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function changeArea(area, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === options.persist) {
|
|
|
|
client.user.persistProperty(
|
|
|
|
UserProps.MessageAreaTag,
|
|
|
|
areaTag,
|
|
|
|
function persisted(err) {
|
|
|
|
return callback(err, area);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-11-23 21:47:18 +00:00
|
|
|
client.user.properties[UserProps.MessageAreaTag] = areaTag;
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, area);
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
function complete(err, area) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err) {
|
2022-08-13 17:51:11 +00:00
|
|
|
client.log.debug(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ areaTag: areaTag, area: area },
|
2022-08-13 17:51:11 +00:00
|
|
|
`Node ${client.node} changed message area to ${areaTag}`
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
client.log.warn(
|
|
|
|
{ areaTag: areaTag, area: area, error: err.message },
|
|
|
|
'Could not change message area'
|
|
|
|
);
|
2018-06-22 05:15:04 +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-06-23 03:26:46 +00:00
|
|
|
// Temporairly -- e.g. non-persisted -- change to an area and it's
|
|
|
|
// associated underlying conference. ACS is checked for both.
|
2016-07-24 17:47:34 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// This is useful for example when doing a new scan
|
2016-07-24 17:47:34 +00:00
|
|
|
//
|
|
|
|
function tempChangeMessageConfAndArea(client, areaTag) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const area = getMessageAreaByTag(areaTag);
|
|
|
|
const confTag = getMessageConfTagByAreaTag(areaTag);
|
2016-07-24 17:47:34 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!area || !confTag) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-24 17:47:34 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const conf = getMessageConferenceByTag(confTag);
|
2016-07-24 17:47:34 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!client.acs.hasMessageConfRead(conf) || !client.acs.hasMessageAreaRead(area)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-11-24 00:41:16 +00:00
|
|
|
client.user.properties[UserProps.MessageConfTag] = confTag;
|
|
|
|
client.user.properties[UserProps.MessageAreaTag] = areaTag;
|
2016-07-24 17:47:34 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return true;
|
2016-07-24 17:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function changeMessageArea(client, areaTag, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
changeMessageAreaWithOptions(client, areaTag, { persist: true }, cb);
|
2016-07-24 17:47:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 03:56:33 +00:00
|
|
|
function hasMessageConfAndAreaRead(client, areaOrTag) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(areaOrTag)) {
|
2019-09-12 03:56:33 +00:00
|
|
|
areaOrTag = getMessageAreaByTag(areaOrTag) || {};
|
|
|
|
}
|
|
|
|
const conf = getMessageConferenceByTag(areaOrTag.confTag);
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
|
|
|
client.acs.hasMessageConfRead(conf) && client.acs.hasMessageAreaRead(areaOrTag)
|
|
|
|
);
|
2019-09-12 03:21:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 04:05:03 +00:00
|
|
|
function hasMessageConfAndAreaWrite(client, areaOrTag) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(areaOrTag)) {
|
2019-09-17 04:05:03 +00:00
|
|
|
areaOrTag = getMessageAreaByTag(areaOrTag) || {};
|
|
|
|
}
|
|
|
|
const conf = getMessageConferenceByTag(areaOrTag.confTag);
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
|
|
|
client.acs.hasMessageConfWrite(conf) && client.acs.hasMessageAreaWrite(areaOrTag)
|
|
|
|
);
|
2019-09-17 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 03:21:33 +00:00
|
|
|
function filterMessageAreaTagsByReadACS(client, areaTags) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!Array.isArray(areaTags)) {
|
|
|
|
areaTags = [areaTags];
|
2019-09-12 03:21:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return areaTags.filter(areaTag => {
|
2019-09-12 03:21:33 +00:00
|
|
|
const area = getMessageAreaByTag(areaTag);
|
|
|
|
return hasMessageConfAndAreaRead(client, area);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-12 04:03:24 +00:00
|
|
|
function filterMessageListByReadACS(client, messageList) {
|
|
|
|
//
|
|
|
|
// Filter out messages belonging to conf/areas the user
|
|
|
|
// doesn't have access to.
|
|
|
|
//
|
|
|
|
|
|
|
|
// Keep a cache around for quick lookup.
|
2022-06-05 20:04:25 +00:00
|
|
|
const acsCache = new Map(); // areaTag:boolean
|
2019-09-12 04:03:24 +00:00
|
|
|
|
|
|
|
return messageList.filter(msg => {
|
|
|
|
let cached = acsCache.get(msg.areaTag);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (false === cached) {
|
2019-09-12 04:03:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === cached) {
|
2019-09-12 04:03:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
cached = hasMessageConfAndAreaRead(client, msg.areaTag);
|
|
|
|
acsCache.set(msg.areaTag, cached);
|
|
|
|
return cached;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-26 20:59:03 +00:00
|
|
|
function getNewMessageCountInAreaForUser(
|
|
|
|
user,
|
|
|
|
areaTag,
|
|
|
|
options = { addrToOnly: false },
|
|
|
|
cb
|
|
|
|
) {
|
|
|
|
getMessageAreaLastReadId(user.userId, areaTag, (err, lastMessageId) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
lastMessageId = lastMessageId || 0;
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
areaTag,
|
2022-06-05 20:04:25 +00:00
|
|
|
newerThanMessageId: lastMessageId,
|
|
|
|
resultType: 'count',
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Message.isPrivateAreaTag(areaTag)) {
|
2023-03-26 20:59:03 +00:00
|
|
|
filter.privateTagUserId = user.userId;
|
|
|
|
} else if (options.addrToOnly) {
|
|
|
|
filter.toUserName = user.username;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Message.findMessages(filter, (err, count) => {
|
|
|
|
return cb(err, count);
|
|
|
|
});
|
|
|
|
});
|
2016-07-24 17:47:34 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 02:30:25 +00:00
|
|
|
// New message count -- for all areas available to the user
|
|
|
|
// that are addressed to that user (ie: matching username)
|
|
|
|
// Does NOT Include private messages.
|
|
|
|
function getNewMessageCountAddressedToUser(client, cb) {
|
2022-06-12 20:12:03 +00:00
|
|
|
const areaTags = getAllAvailableMessageAreaTags(client).filter(
|
|
|
|
areaTag => areaTag !== Message.WellKnownAreaTags.Private
|
|
|
|
);
|
2022-05-12 02:30:25 +00:00
|
|
|
|
|
|
|
let newMessageCount = 0;
|
2022-06-12 20:12:03 +00:00
|
|
|
async.forEach(
|
|
|
|
areaTags,
|
|
|
|
(areaTag, nextAreaTag) => {
|
|
|
|
getMessageAreaLastReadId(client.user.userId, areaTag, (_, lastMessageId) => {
|
2023-02-21 03:09:58 +00:00
|
|
|
lastMessageId = lastMessageId || 0; // eslint-disable-line no-unused-vars
|
2022-06-12 20:12:03 +00:00
|
|
|
getNewMessageCountInAreaForUser(
|
2023-03-26 20:59:03 +00:00
|
|
|
client.user,
|
2022-06-12 20:12:03 +00:00
|
|
|
areaTag,
|
2023-03-26 20:59:03 +00:00
|
|
|
{ addrToOnly: true },
|
2022-06-12 20:12:03 +00:00
|
|
|
(err, count) => {
|
|
|
|
newMessageCount += count;
|
|
|
|
return nextAreaTag(err);
|
|
|
|
}
|
|
|
|
);
|
2022-05-12 02:30:25 +00:00
|
|
|
});
|
2022-06-12 20:12:03 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return cb(null, newMessageCount);
|
|
|
|
}
|
|
|
|
);
|
2022-05-12 02:30:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getNewMessagesInAreaForUser(userId, areaTag, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
getMessageAreaLastReadId(userId, areaTag, (err, lastMessageId) => {
|
|
|
|
lastMessageId = lastMessageId || 0;
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
areaTag,
|
2022-06-05 20:04:25 +00:00
|
|
|
resultType: 'messageList',
|
|
|
|
newerThanMessageId: lastMessageId,
|
|
|
|
sort: 'messageId',
|
|
|
|
order: 'ascending',
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Message.isPrivateAreaTag(areaTag)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
filter.privateTagUserId = userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Message.findMessages(filter, cb);
|
|
|
|
});
|
2018-01-27 04:42:43 +00:00
|
|
|
}
|
2015-08-27 05:25:49 +00:00
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
function getMessageListForArea(client, areaTag, filter, cb) {
|
|
|
|
if (!cb && _.isFunction(filter)) {
|
2018-11-22 00:55:31 +00:00
|
|
|
cb = filter;
|
|
|
|
filter = {
|
|
|
|
areaTag,
|
2022-06-05 20:04:25 +00:00
|
|
|
resultType: 'messageList',
|
|
|
|
sort: 'messageId',
|
|
|
|
order: 'ascending',
|
2018-11-22 00:55:31 +00:00
|
|
|
};
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
Object.assign(filter, { areaTag });
|
2018-11-22 00:55:31 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (client) {
|
|
|
|
if (!hasMessageConfAndAreaRead(client, areaTag)) {
|
2019-09-10 03:44:03 +00:00
|
|
|
return cb(null, []);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Message.isPrivateAreaTag(areaTag)) {
|
2019-09-10 03:44:03 +00:00
|
|
|
filter.privateTagUserId = client ? client.user.userId : 'INVALID_USER_ID';
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Message.findMessages(filter, cb);
|
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-06-22 05:15:04 +00:00
|
|
|
Message.findMessages(
|
|
|
|
{
|
|
|
|
areaTag,
|
|
|
|
newerThanTimestamp,
|
2022-06-05 20:04:25 +00:00
|
|
|
sort: 'modTimestamp',
|
|
|
|
order: 'ascending',
|
|
|
|
limit: 1,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
(err, id) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
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) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msgDb.get(
|
2022-06-05 20:04:25 +00:00
|
|
|
'SELECT message_id ' +
|
|
|
|
'FROM user_message_area_last_read ' +
|
|
|
|
'WHERE user_id = ? AND area_tag = ?;',
|
|
|
|
[userId, areaTag.toLowerCase()],
|
2018-06-22 05:15:04 +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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!cb && _.isFunction(allowOlder)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = allowOlder;
|
|
|
|
allowOlder = false;
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: likely a better way to do this...
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getCurrent(callback) {
|
|
|
|
getMessageAreaLastReadId(userId, areaTag, function result(err, lastId) {
|
|
|
|
lastId = lastId || 0;
|
2018-06-23 03:26:46 +00:00
|
|
|
callback(null, lastId); // ignore errors as we default to 0
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function update(lastId, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (allowOlder || messageId > lastId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msgDb.run(
|
2018-06-23 03:26:46 +00:00
|
|
|
'REPLACE INTO user_message_area_last_read (user_id, area_tag, message_id) ' +
|
2022-06-05 20:04:25 +00:00
|
|
|
'VALUES (?, ?, ?);',
|
|
|
|
[userId, areaTag, messageId],
|
2018-06-22 05:15:04 +00:00
|
|
|
function written(err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
callback(err, true); // true=didUpdate
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
function complete(err, didUpdate) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
Log.debug(
|
2022-06-05 20:04:25 +00:00
|
|
|
{
|
|
|
|
error: err.toString(),
|
|
|
|
userId: userId,
|
|
|
|
areaTag: areaTag,
|
|
|
|
messageId: messageId,
|
|
|
|
},
|
|
|
|
'Failed updating area last read ID'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === didUpdate) {
|
2018-06-22 05:15:04 +00:00
|
|
|
Log.trace(
|
2022-06-05 20:04:25 +00:00
|
|
|
{ userId: userId, areaTag: areaTag, messageId: messageId },
|
|
|
|
'Area last read ID updated'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
2015-10-22 21:44:44 +00:00
|
|
|
}
|
2016-02-17 05:11:55 +00:00
|
|
|
|
|
|
|
function persistMessage(message, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function persistMessageToDisc(callback) {
|
|
|
|
return message.persist(callback);
|
|
|
|
},
|
|
|
|
function recordToMessageNetworks(callback) {
|
|
|
|
return msgNetRecord(message, callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
cb
|
|
|
|
);
|
2016-06-20 03:09:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// method exposed for event scheduler
|
2016-06-20 20:10:12 +00:00
|
|
|
function trimMessageAreasScheduledEvent(args, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
function trimMessageAreaByMaxMessages(areaInfo, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === areaInfo.maxMessages) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
2016-06-20 20:10:12 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
msgDb.run(
|
|
|
|
`DELETE FROM message
|
2018-06-23 03:26:46 +00:00
|
|
|
WHERE message_id IN(
|
|
|
|
SELECT message_id
|
|
|
|
FROM message
|
|
|
|
WHERE area_tag = ?
|
|
|
|
ORDER BY message_id DESC
|
|
|
|
LIMIT -1 OFFSET ${areaInfo.maxMessages}
|
|
|
|
);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[areaInfo.areaTag.toLowerCase()],
|
|
|
|
function result(err) {
|
|
|
|
// no arrow func; need this
|
|
|
|
if (err) {
|
|
|
|
Log.error(
|
|
|
|
{ areaInfo: areaInfo, error: err.message, type: 'maxMessages' },
|
|
|
|
'Error trimming message area'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.debug(
|
|
|
|
{ areaInfo: areaInfo, type: 'maxMessages', count: this.changes },
|
|
|
|
'Area trimmed successfully'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function trimMessageAreaByMaxAgeDays(areaInfo, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === areaInfo.maxAgeDays) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
msgDb.run(
|
|
|
|
`DELETE FROM message
|
2018-06-23 03:26:46 +00:00
|
|
|
WHERE area_tag = ? AND modified_timestamp < date('now', '-${areaInfo.maxAgeDays} days');`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[areaInfo.areaTag],
|
|
|
|
function result(err) {
|
|
|
|
// no arrow func; need this
|
|
|
|
if (err) {
|
|
|
|
Log.warn(
|
|
|
|
{ areaInfo: areaInfo, error: err.message, type: 'maxAgeDays' },
|
|
|
|
'Error trimming message area'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.debug(
|
|
|
|
{ areaInfo: areaInfo, type: 'maxAgeDays', count: this.changes },
|
|
|
|
'Area trimmed successfully'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getAreaTags(callback) {
|
|
|
|
const areaTags = [];
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We use SQL here vs API such that no-longer-used tags are picked up
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
msgDb.each(
|
|
|
|
`SELECT DISTINCT area_tag
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM message;`,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, row) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// We treat private mail special
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!Message.isPrivateAreaTag(row.area_tag)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
areaTags.push(row.area_tag);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err, areaTags);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function prepareAreaInfo(areaTags, callback) {
|
|
|
|
let areaInfos = [];
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// determine maxMessages & maxAgeDays per area
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
|
|
|
areaTags.forEach(areaTag => {
|
|
|
|
let maxMessages = config.messageAreaDefaults.maxMessages;
|
2022-06-05 20:04:25 +00:00
|
|
|
let maxAgeDays = config.messageAreaDefaults.maxAgeDays;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const area = getMessageAreaByTag(areaTag); // note: we don't know the conf here
|
|
|
|
if (area) {
|
2018-06-22 05:15:04 +00:00
|
|
|
maxMessages = area.maxMessages || maxMessages;
|
2022-06-05 20:04:25 +00:00
|
|
|
maxAgeDays = area.maxAgeDays || maxAgeDays;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
areaInfos.push({
|
|
|
|
areaTag: areaTag,
|
|
|
|
maxMessages: maxMessages,
|
|
|
|
maxAgeDays: maxAgeDays,
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null, areaInfos);
|
|
|
|
},
|
|
|
|
function trimGeneralAreas(areaInfos, callback) {
|
2023-03-15 03:58:03 +00:00
|
|
|
const cbWrap = (e, t, c) => {
|
|
|
|
if (e) {
|
|
|
|
Log.warn({ error: e.message, type: t }, `Failed trimming (${t})`);
|
|
|
|
}
|
|
|
|
return c(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ApSharedAreaTag = Message.WellKnownAreaTags.ActivityPubShared;
|
|
|
|
|
|
|
|
// Clean up messages, and any associated ActivityPub 'SharedInbox'
|
|
|
|
// Notes (ie: the source of said messages)
|
2018-06-22 05:15:04 +00:00
|
|
|
async.each(
|
|
|
|
areaInfos,
|
2023-03-15 03:58:03 +00:00
|
|
|
(areaInfo, nextArea) => {
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
next => {
|
|
|
|
trimMessageAreaByMaxMessages(areaInfo, err => {
|
|
|
|
return cbWrap(err, 'Messages:MaxCount', next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
next => {
|
|
|
|
if (areaInfo.areaTag !== ApSharedAreaTag) {
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
Collection.removeByMaxCount(
|
|
|
|
Collections.SharedInbox,
|
|
|
|
areaInfo.maxMessages,
|
|
|
|
err => {
|
|
|
|
return cbWrap(
|
|
|
|
err,
|
|
|
|
'ActivityPubShared:MaxCount',
|
|
|
|
next
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
next => {
|
|
|
|
trimMessageAreaByMaxAgeDays(areaInfo, err => {
|
|
|
|
return cbWrap(err, 'Messages:MaxAgeDays', next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
next => {
|
|
|
|
if (areaInfo.areaTag !== ApSharedAreaTag) {
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
Collection.removeByMaxAgeDays(
|
|
|
|
Collections.SharedInbox,
|
|
|
|
areaInfo.maxAgeDays,
|
|
|
|
err => {
|
|
|
|
return cbWrap(
|
|
|
|
err,
|
|
|
|
'ActivityPubShared:MaxAgeDays',
|
|
|
|
next
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return nextArea(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2023-03-15 03:58:03 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function trimExternalPrivateSentMail(callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// *External* (FTN, email, ...) outgoing is cleaned up *after export*
|
|
|
|
// if it is older than the configured |maxExternalSentAgeDays| days
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// 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)
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const maxExternalSentAgeDays = _.get(
|
|
|
|
Config,
|
2023-02-21 03:09:58 +00:00
|
|
|
[
|
|
|
|
'messageConferences',
|
|
|
|
WellKnownConfTags.SystemInternal,
|
|
|
|
'areas',
|
|
|
|
WellKnownAreaTags.Private,
|
|
|
|
'maxExternalSentAgeDays',
|
|
|
|
],
|
2018-06-22 05:15:04 +00:00
|
|
|
30
|
|
|
|
);
|
|
|
|
|
|
|
|
msgDb.run(
|
|
|
|
`DELETE FROM message
|
2018-06-23 03:26:46 +00:00
|
|
|
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')
|
|
|
|
);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
function results(err) {
|
|
|
|
// no arrow func; need this
|
|
|
|
if (err) {
|
|
|
|
Log.warn(
|
|
|
|
{ error: err.message },
|
|
|
|
'Error trimming private externally sent messages'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.debug(
|
|
|
|
{ count: this.changes },
|
|
|
|
'Private externally sent messages trimmed successfully'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return callback(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
}
|