2015-07-11 22:39:42 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const msgDb = require('./database.js').dbs.message;
|
|
|
|
const wordWrapText = require('./word_wrap.js').wordWrapText;
|
|
|
|
const createNamedUUID = require('./uuid_util.js').createNamedUUID;
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const ANSI = require('./ansi_term.js');
|
|
|
|
const { sanitizeString, getISOTimestampString } = require('./database.js');
|
2020-05-02 19:34:28 +00:00
|
|
|
const { isCP437Encodable } = require('./cp437util');
|
|
|
|
const { containsNonLatinCodepoints } = require('./string_util');
|
2023-01-26 22:42:11 +00:00
|
|
|
const MessageConst = require('./message_const');
|
|
|
|
const { getQuotePrefixFromName } = require('./mail_util');
|
2020-05-02 19:34:28 +00:00
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
const {
|
2022-06-05 20:04:25 +00:00
|
|
|
isAnsi,
|
|
|
|
isFormattedLine,
|
2018-06-22 05:15:04 +00:00
|
|
|
splitTextAtTerms,
|
2022-06-05 20:04:25 +00:00
|
|
|
renderSubstr,
|
|
|
|
} = require('./string_util.js');
|
2016-10-07 03:03:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const ansiPrep = require('./ansi_prep.js');
|
2017-08-29 05:17:35 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const uuidParse = require('uuid-parse');
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
|
|
|
const moment = require('moment');
|
|
|
|
const iconvEncode = require('iconv-lite').encode;
|
2015-07-13 04:56:33 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const ENIGMA_MESSAGE_UUID_NAMESPACE = uuidParse.parse(
|
|
|
|
'154506df-1df8-46b9-98f8-ebb5815baaf8'
|
|
|
|
);
|
2016-07-06 04:18:43 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: this is a ugly hack due to bad variable names - clean it up & just _.camelCase(k)!
|
2018-02-01 05:45:03 +00:00
|
|
|
const MESSAGE_ROW_MAP = {
|
2022-06-05 20:04:25 +00:00
|
|
|
reply_to_message_id: 'replyToMsgId',
|
|
|
|
modified_timestamp: 'modTimestamp',
|
2018-02-01 05:45:03 +00:00
|
|
|
};
|
|
|
|
|
2018-01-27 04:36:16 +00:00
|
|
|
module.exports = class Message {
|
2022-06-05 20:04:25 +00:00
|
|
|
constructor({
|
|
|
|
messageId = 0,
|
|
|
|
areaTag = Message.WellKnownAreaTags.Invalid,
|
|
|
|
uuid,
|
|
|
|
replyToMsgId = 0,
|
|
|
|
toUserName = '',
|
|
|
|
fromUserName = '',
|
|
|
|
subject = '',
|
|
|
|
message = '',
|
|
|
|
modTimestamp = moment(),
|
|
|
|
meta,
|
|
|
|
hashTags = [],
|
|
|
|
} = {}) {
|
|
|
|
this.messageId = messageId;
|
|
|
|
this.areaTag = areaTag;
|
|
|
|
this.messageUuid = uuid;
|
|
|
|
this.replyToMsgId = replyToMsgId;
|
|
|
|
this.toUserName = toUserName;
|
|
|
|
this.fromUserName = fromUserName;
|
|
|
|
this.subject = subject;
|
|
|
|
this.message = message;
|
|
|
|
|
|
|
|
if (_.isDate(modTimestamp) || _.isString(modTimestamp)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
modTimestamp = moment(modTimestamp);
|
|
|
|
}
|
|
|
|
|
2020-11-18 02:06:54 +00:00
|
|
|
this.modTimestamp = modTimestamp || moment();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
this.meta = {};
|
2022-06-05 20:04:25 +00:00
|
|
|
_.defaultsDeep(this.meta, { System: {} }, meta);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.hashTags = hashTags;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
get uuid() {
|
|
|
|
// deprecated, will be removed in the near future
|
2020-05-15 01:46:00 +00:00
|
|
|
return this.messageUuid;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
isValid() {
|
|
|
|
return true;
|
|
|
|
} // :TODO: obviously useless; look into this or remove it
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
static isPrivateAreaTag(areaTag) {
|
|
|
|
return areaTag.toLowerCase() === Message.WellKnownAreaTags.Private;
|
|
|
|
}
|
|
|
|
|
|
|
|
isPrivate() {
|
|
|
|
return Message.isPrivateAreaTag(this.areaTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
isFromRemoteUser() {
|
2023-01-23 21:45:56 +00:00
|
|
|
return null !== this.getRemoteFromUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
setRemoteFromUser(remoteFrom) {
|
|
|
|
this.meta[Message.WellKnownMetaCategories.System][
|
|
|
|
Message.SystemMetaNames.RemoteFromUser
|
|
|
|
] = remoteFrom;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRemoteFromUser() {
|
|
|
|
return _.get(
|
|
|
|
this,
|
|
|
|
[
|
|
|
|
'meta',
|
|
|
|
Message.WellKnownMetaCategories.System,
|
|
|
|
Message.SystemMetaNames.RemoteFromUser,
|
|
|
|
],
|
|
|
|
null
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 19:34:28 +00:00
|
|
|
isCP437Encodable() {
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
|
|
|
isCP437Encodable(this.toUserName) &&
|
2020-05-02 19:34:28 +00:00
|
|
|
isCP437Encodable(this.fromUserName) &&
|
|
|
|
isCP437Encodable(this.subject) &&
|
2022-06-05 20:04:25 +00:00
|
|
|
isCP437Encodable(this.message)
|
|
|
|
);
|
2020-05-02 19:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
containsNonLatinCodepoints() {
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
|
|
|
containsNonLatinCodepoints(this.toUserName) ||
|
2020-05-02 19:34:28 +00:00
|
|
|
containsNonLatinCodepoints(this.fromUserName) ||
|
|
|
|
containsNonLatinCodepoints(this.subject) ||
|
2022-06-05 20:04:25 +00:00
|
|
|
containsNonLatinCodepoints(this.message)
|
|
|
|
);
|
2020-05-02 19:34:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 00:47:58 +00:00
|
|
|
/*
|
|
|
|
:TODO: finish me
|
|
|
|
static checkUserHasDeleteRights(user, messageIdOrUuid, cb) {
|
|
|
|
const isMessageId = _.isNumber(messageIdOrUuid);
|
|
|
|
const getMetaName = isMessageId ? 'getMetaValuesByMessageId' : 'getMetaValuesByMessageUuid';
|
|
|
|
|
|
|
|
Message[getMetaName](messageIdOrUuid, 'System', Message.SystemMetaNames.LocalToUserID, (err, localUserId) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// expect single value
|
|
|
|
if(!_.isString(localUserId)) {
|
|
|
|
return cb(Errors.Invalid(`Invalid ${Message.SystemMetaNames.LocalToUserID} value: ${localUserId}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
localUserId = parseInt(localUserId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
userHasDeleteRights(user) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const messageLocalUserId = parseInt(
|
|
|
|
this.meta.System[Message.SystemMetaNames.LocalToUserID]
|
|
|
|
);
|
2018-07-05 00:47:58 +00:00
|
|
|
return (this.isPrivate() && user.userId === messageLocalUserId) || user.isSysOp();
|
|
|
|
}
|
|
|
|
|
2023-01-13 01:26:44 +00:00
|
|
|
static get WellKnownMetaCategories() {
|
2023-01-26 22:42:11 +00:00
|
|
|
return MessageConst.WellKnownMetaCategories;
|
2023-01-13 01:26:44 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
static get WellKnownAreaTags() {
|
2023-01-26 22:42:11 +00:00
|
|
|
return MessageConst.WellKnownAreaTags;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static get SystemMetaNames() {
|
2023-01-26 22:42:11 +00:00
|
|
|
return MessageConst.SystemMetaNames;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static get AddressFlavor() {
|
2023-01-26 22:42:11 +00:00
|
|
|
return MessageConst.AddressFlavor;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static get StateFlags0() {
|
2023-01-26 22:42:11 +00:00
|
|
|
return MessageConst.StateFlags0;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static get FtnPropertyNames() {
|
2023-01-26 22:42:11 +00:00
|
|
|
return MessageConst.FtnPropertyNames;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 01:50:04 +00:00
|
|
|
static get QWKPropertyNames() {
|
2023-01-26 22:42:11 +00:00
|
|
|
return MessageConst.QWKPropertyNames;
|
2020-04-22 01:50:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 01:26:44 +00:00
|
|
|
static get ActivityPubPropertyNames() {
|
2023-01-26 22:42:11 +00:00
|
|
|
return MessageConst.ActivityPubPropertyNames;
|
2023-01-13 01:26:44 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
setLocalToUserId(userId) {
|
|
|
|
this.meta.System = this.meta.System || {};
|
|
|
|
this.meta.System[Message.SystemMetaNames.LocalToUserID] = userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLocalFromUserId(userId) {
|
|
|
|
this.meta.System = this.meta.System || {};
|
|
|
|
this.meta.System[Message.SystemMetaNames.LocalFromUserID] = userId;
|
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
getLocalFromUserId() {
|
|
|
|
let id = _.get(this, 'meta.System.local_from_user_id', 0);
|
|
|
|
return parseInt(id);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
setRemoteToUser(remoteTo) {
|
|
|
|
this.meta.System = this.meta.System || {};
|
|
|
|
this.meta.System[Message.SystemMetaNames.RemoteToUser] = remoteTo;
|
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
getRemoteToUser() {
|
|
|
|
return _.get(this, 'meta.System.remote_to_user');
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
setExternalFlavor(flavor) {
|
|
|
|
this.meta.System = this.meta.System || {};
|
|
|
|
this.meta.System[Message.SystemMetaNames.ExternalFlavor] = flavor;
|
|
|
|
}
|
|
|
|
|
2023-01-12 05:37:09 +00:00
|
|
|
getAddressFlavor() {
|
|
|
|
return _.get(this, 'meta.System.external_flavor', Message.AddressFlavor.Local);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
static createMessageUUID(areaTag, modTimestamp, subject, body) {
|
|
|
|
assert(_.isString(areaTag));
|
|
|
|
assert(_.isDate(modTimestamp) || moment.isMoment(modTimestamp));
|
|
|
|
assert(_.isString(subject));
|
|
|
|
assert(_.isString(body));
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!moment.isMoment(modTimestamp)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
modTimestamp = moment(modTimestamp);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
areaTag = iconvEncode(areaTag.toUpperCase(), 'CP437');
|
|
|
|
modTimestamp = iconvEncode(modTimestamp.format('DD MMM YY HH:mm:ss'), 'CP437');
|
|
|
|
subject = iconvEncode(subject.toUpperCase().trim(), 'CP437');
|
|
|
|
body = iconvEncode(
|
|
|
|
body.replace(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g, '').trim(),
|
|
|
|
'CP437'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return uuidParse.unparse(
|
|
|
|
createNamedUUID(
|
|
|
|
ENIGMA_MESSAGE_UUID_NAMESPACE,
|
|
|
|
Buffer.concat([areaTag, modTimestamp, subject, body])
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getMessageFromRow(row) {
|
|
|
|
const msg = {};
|
|
|
|
_.each(row, (v, k) => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: see notes around MESSAGE_ROW_MAP -- clean this up so we can just _camelCase()!
|
2018-06-22 05:15:04 +00:00
|
|
|
k = MESSAGE_ROW_MAP[k] || _.camelCase(k);
|
|
|
|
msg[k] = v;
|
|
|
|
});
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Find message IDs or UUIDs by filter. Available filters/options:
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
filter.uuids - use with resultType='id'
|
|
|
|
filter.ids - use with resultType='uuid'
|
2019-02-16 04:53:18 +00:00
|
|
|
filter.toUserName - string|Array(string)
|
|
|
|
filter.fromUserName - string|Array(string)
|
2018-12-15 05:21:57 +00:00
|
|
|
filter.replyToMessageId
|
2018-11-28 04:21:00 +00:00
|
|
|
|
2019-02-16 04:53:18 +00:00
|
|
|
filter.operator = (AND)|OR
|
|
|
|
|
2018-11-28 04:21:00 +00:00
|
|
|
filter.newerThanTimestamp - may not be used with |date|
|
|
|
|
filter.date - moment object - may not be used with |newerThanTimestamp|
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
filter.newerThanMessageId
|
|
|
|
filter.areaTag - note if you want by conf, send in all areas for a conf
|
2019-02-16 04:53:18 +00:00
|
|
|
filter.metaTuples - [ {category, name, value} ]
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
filter.terms - FTS search
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
filter.sort = modTimestamp | messageId
|
|
|
|
filter.order = ascending | (descending)
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
filter.limit
|
2018-12-15 05:21:57 +00:00
|
|
|
filter.resultType = (id) | uuid | count | messageList
|
2018-06-23 03:26:46 +00:00
|
|
|
filter.extraFields = []
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
filter.privateTagUserId = <userId> - if set, only private messages belonging to <userId> are processed
|
2020-05-12 03:54:20 +00:00
|
|
|
- areaTags filter ignored
|
2018-06-23 03:26:46 +00:00
|
|
|
- if NOT present, private areas are skipped
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
*=NYI
|
|
|
|
*/
|
2018-06-22 05:15:04 +00:00
|
|
|
static findMessages(filter, cb) {
|
|
|
|
filter = filter || {};
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
filter.resultType = filter.resultType || 'id';
|
|
|
|
filter.extraFields = filter.extraFields || [];
|
|
|
|
filter.operator = filter.operator || 'AND';
|
|
|
|
|
|
|
|
if ('messageList' === filter.resultType) {
|
|
|
|
filter.extraFields = _.uniq(
|
|
|
|
filter.extraFields.concat([
|
|
|
|
'area_tag',
|
|
|
|
'message_uuid',
|
|
|
|
'reply_to_message_id',
|
|
|
|
'to_user_name',
|
|
|
|
'from_user_name',
|
|
|
|
'subject',
|
|
|
|
'modified_timestamp',
|
|
|
|
])
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-02-01 05:45:03 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const field = 'uuid' === filter.resultType ? 'message_uuid' : 'message_id';
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (moment.isMoment(filter.newerThanTimestamp)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
filter.newerThanTimestamp = getISOTimestampString(filter.newerThanTimestamp);
|
|
|
|
}
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
let sql;
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('count' === filter.resultType) {
|
|
|
|
sql = `SELECT COUNT() AS count
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM message m`;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
sql = `SELECT DISTINCT m.${field}${
|
|
|
|
filter.extraFields.length > 0
|
|
|
|
? ', ' + filter.extraFields.map(f => `m.${f}`).join(', ')
|
|
|
|
: ''
|
|
|
|
}
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM message m`;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sqlOrderDir = 'ascending' === filter.order ? 'ASC' : 'DESC';
|
|
|
|
let sqlOrderBy;
|
|
|
|
let sqlWhere = '';
|
|
|
|
|
2019-02-16 04:53:18 +00:00
|
|
|
function appendWhereClause(clause, op) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (sqlWhere) {
|
2019-02-16 04:53:18 +00:00
|
|
|
sqlWhere += ` ${op || filter.operator} `;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
sqlWhere += ' WHERE ';
|
|
|
|
}
|
|
|
|
sqlWhere += clause;
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// currently only avail sort
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('modTimestamp' === filter.sort) {
|
2018-06-22 05:15:04 +00:00
|
|
|
sqlOrderBy = `ORDER BY m.modified_timestamp ${sqlOrderDir}`;
|
|
|
|
} else {
|
|
|
|
sqlOrderBy = `ORDER BY m.message_id ${sqlOrderDir}`;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Array.isArray(filter.ids)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
appendWhereClause(`m.message_id IN (${filter.ids.join(', ')})`);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Array.isArray(filter.uuids)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const uuidList = filter.uuids.map(u => `"${u}"`).join(', ');
|
|
|
|
appendWhereClause(`m.message_id IN (${uuidList})`);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isNumber(filter.privateTagUserId)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
appendWhereClause(`m.area_tag = "${Message.WellKnownAreaTags.Private}"`);
|
|
|
|
appendWhereClause(
|
|
|
|
`m.message_id IN (
|
2019-11-09 02:20:14 +00:00
|
|
|
SELECT message_id
|
|
|
|
FROM message_meta
|
2018-06-23 03:26:46 +00:00
|
|
|
WHERE meta_category = "System" AND meta_name = "${Message.SystemMetaNames.LocalToUserID}" AND meta_value = ${filter.privateTagUserId}
|
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
|
|
|
if (filter.areaTag && filter.areaTag.length > 0) {
|
2020-05-12 03:54:20 +00:00
|
|
|
if (!Array.isArray(filter.areaTag)) {
|
2022-06-05 20:04:25 +00:00
|
|
|
filter.areaTag = [filter.areaTag];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 03:54:20 +00:00
|
|
|
const areaList = filter.areaTag
|
|
|
|
.filter(t => t !== Message.WellKnownAreaTags.Private)
|
2022-06-05 20:04:25 +00:00
|
|
|
.map(t => `"${t}"`)
|
|
|
|
.join(', ');
|
|
|
|
if (areaList.length > 0) {
|
2020-05-12 03:54:20 +00:00
|
|
|
appendWhereClause(`m.area_tag IN(${areaList})`);
|
|
|
|
} else {
|
|
|
|
// nothing to do; no areas remain
|
|
|
|
return cb(null, []);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// explicit exclude of Private
|
2022-06-05 20:04:25 +00:00
|
|
|
appendWhereClause(
|
|
|
|
`m.area_tag != "${Message.WellKnownAreaTags.Private}"`,
|
|
|
|
'AND'
|
|
|
|
);
|
2020-05-12 03:54:20 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isNumber(filter.replyToMessageId)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
appendWhereClause(`m.reply_to_message_id=${filter.replyToMessageId}`);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
['toUserName', 'fromUserName'].forEach(field => {
|
2019-02-16 04:53:18 +00:00
|
|
|
let val = filter[field];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!val) {
|
2019-02-16 04:53:18 +00:00
|
|
|
return; // next item
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(val)) {
|
|
|
|
val = [val];
|
2019-02-16 04:53:18 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Array.isArray(val)) {
|
|
|
|
val =
|
|
|
|
'(' +
|
|
|
|
val
|
|
|
|
.map(v => {
|
|
|
|
return `m.${_.snakeCase(field)} LIKE "${sanitizeString(v)}"`;
|
|
|
|
})
|
|
|
|
.join(' OR ') +
|
|
|
|
')';
|
2019-02-16 04:53:18 +00:00
|
|
|
appendWhereClause(val);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
_.isString(filter.newerThanTimestamp) &&
|
|
|
|
filter.newerThanTimestamp.length > 0
|
|
|
|
) {
|
2018-11-28 04:21:00 +00:00
|
|
|
// :TODO: should be using "localtime" here?
|
2022-06-05 20:04:25 +00:00
|
|
|
appendWhereClause(
|
|
|
|
`DATETIME(m.modified_timestamp) > DATETIME("${filter.newerThanTimestamp}", "+1 seconds")`
|
|
|
|
);
|
|
|
|
} else if (moment.isMoment(filter.date)) {
|
|
|
|
appendWhereClause(
|
|
|
|
`DATE(m.modified_timestamp, "localtime") = DATE("${filter.date.format(
|
|
|
|
'YYYY-MM-DD'
|
|
|
|
)}")`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isNumber(filter.newerThanMessageId)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
appendWhereClause(`m.message_id > ${filter.newerThanMessageId}`);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (filter.terms && filter.terms.length > 0) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// note the ':' in MATCH expr., see https://www.sqlite.org/cvstrac/wiki?p=FullTextIndex
|
2018-06-22 05:15:04 +00:00
|
|
|
appendWhereClause(
|
|
|
|
`m.message_id IN (
|
2018-06-23 03:26:46 +00:00
|
|
|
SELECT rowid
|
|
|
|
FROM message_fts
|
2018-12-16 06:42:19 +00:00
|
|
|
WHERE message_fts MATCH ":${sanitizeString(filter.terms)}"
|
2018-06-23 03:26:46 +00:00
|
|
|
)`
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Array.isArray(filter.metaTuples)) {
|
2019-02-16 04:53:18 +00:00
|
|
|
let sub = [];
|
|
|
|
filter.metaTuples.forEach(mt => {
|
2022-06-05 20:04:25 +00:00
|
|
|
sub.push(
|
|
|
|
`(meta_category = "${mt.category}" AND meta_name = "${
|
|
|
|
mt.name
|
|
|
|
}" AND meta_value = "${sanitizeString(mt.value)}")`
|
|
|
|
);
|
2019-02-16 04:53:18 +00:00
|
|
|
});
|
|
|
|
sub = sub.join(` ${filter.operator} `);
|
|
|
|
appendWhereClause(
|
|
|
|
`m.message_id IN (
|
|
|
|
SELECT message_id
|
|
|
|
FROM message_meta
|
|
|
|
WHERE ${sub}
|
|
|
|
)`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
sql += `${sqlWhere} ${sqlOrderBy}`;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isNumber(filter.limit)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
sql += ` LIMIT ${filter.limit}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
sql += ';';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('count' === filter.resultType) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msgDb.get(sql, (err, row) => {
|
|
|
|
return cb(err, row ? row.count : 0);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const matches = [];
|
|
|
|
const extra = filter.extraFields.length > 0;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const rowConv =
|
|
|
|
'messageList' === filter.resultType
|
|
|
|
? Message.getMessageFromRow
|
|
|
|
: row => row;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
msgDb.each(
|
|
|
|
sql,
|
|
|
|
(err, row) => {
|
|
|
|
if (_.isObject(row)) {
|
|
|
|
matches.push(extra ? rowConv(row) : row[field]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err, matches);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: use findMessages, by uuid, limit=1
|
2018-06-22 05:15:04 +00:00
|
|
|
static getMessageIdByUuid(uuid, cb) {
|
|
|
|
msgDb.get(
|
|
|
|
`SELECT message_id
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM message
|
|
|
|
WHERE message_uuid = ?
|
|
|
|
LIMIT 1;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[uuid],
|
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 cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const success = row && row.message_id;
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(
|
|
|
|
success ? null : Errors.DoesNotExist(`No message for UUID ${uuid}`),
|
|
|
|
success ? row.message_id : null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: use findMessages
|
2018-06-22 05:15:04 +00:00
|
|
|
static getMessageIdsByMetaValue(category, name, value, cb) {
|
|
|
|
msgDb.all(
|
|
|
|
`SELECT message_id
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM message_meta
|
|
|
|
WHERE meta_category = ? AND meta_name = ? AND meta_value = ?;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[category, name, value],
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, rows) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(
|
|
|
|
null,
|
|
|
|
rows.map(r => parseInt(r.message_id))
|
|
|
|
); // return array of ID(s)
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getMetaValuesByMessageId(messageId, category, name, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const sql = `SELECT meta_value
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM message_meta
|
|
|
|
WHERE message_id = ? AND meta_category = ? AND meta_name = ?;`;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
msgDb.all(sql, [messageId, category, name], (err, rows) => {
|
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === rows.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(Errors.DoesNotExist('No value for category/name'));
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// single values are returned without an array
|
2022-06-05 20:04:25 +00:00
|
|
|
if (1 === rows.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null, rows[0].meta_value);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(
|
|
|
|
null,
|
|
|
|
rows.map(r => r.meta_value)
|
|
|
|
); // map to array of values only
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static getMetaValuesByMessageUuid(uuid, category, name, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getMessageId(callback) {
|
|
|
|
Message.getMessageIdByUuid(uuid, (err, messageId) => {
|
|
|
|
return callback(err, messageId);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getMetaValues(messageId, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Message.getMetaValuesByMessageId(
|
|
|
|
messageId,
|
|
|
|
category,
|
|
|
|
name,
|
|
|
|
(err, values) => {
|
|
|
|
return callback(err, values);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
(err, values) => {
|
|
|
|
return cb(err, values);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadMeta(cb) {
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Example of loaded this.meta:
|
|
|
|
|
|
|
|
meta: {
|
|
|
|
System: {
|
|
|
|
local_to_user_id: 1234,
|
|
|
|
},
|
|
|
|
FtnProperty: {
|
|
|
|
ftn_seen_by: [ "1/102 103", "2/42 52 65" ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2022-06-05 20:04:25 +00:00
|
|
|
const sql = `SELECT meta_category, meta_name, meta_value
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM message_meta
|
|
|
|
WHERE message_id = ?;`;
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const self = this; // :TODO: not required - arrow functions below:
|
|
|
|
msgDb.each(
|
|
|
|
sql,
|
|
|
|
[this.messageId],
|
|
|
|
(err, row) => {
|
|
|
|
if (!(row.meta_category in self.meta)) {
|
|
|
|
self.meta[row.meta_category] = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
self.meta[row.meta_category][row.meta_name] = row.meta_value;
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!(row.meta_name in self.meta[row.meta_category])) {
|
|
|
|
self.meta[row.meta_category][row.meta_name] = row.meta_value;
|
|
|
|
} else {
|
|
|
|
if (_.isString(self.meta[row.meta_category][row.meta_name])) {
|
|
|
|
self.meta[row.meta_category][row.meta_name] = [
|
|
|
|
self.meta[row.meta_category][row.meta_name],
|
|
|
|
];
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.meta[row.meta_category][row.meta_name].push(row.meta_value);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 05:21:57 +00:00
|
|
|
load(loadWith, cb) {
|
|
|
|
assert(_.isString(loadWith.uuid) || _.isNumber(loadWith.messageId));
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadMessage(callback) {
|
2018-12-15 05:21:57 +00:00
|
|
|
const whereField = loadWith.uuid ? 'message_uuid' : 'message_id';
|
2018-06-22 05:15:04 +00:00
|
|
|
msgDb.get(
|
|
|
|
`SELECT message_id, area_tag, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject,
|
2018-06-23 03:26:46 +00:00
|
|
|
message, modified_timestamp, view_count
|
|
|
|
FROM message
|
2018-12-15 05:21:57 +00:00
|
|
|
WHERE ${whereField} = ?
|
2018-06-23 03:26:46 +00:00
|
|
|
LIMIT 1;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[loadWith.uuid || loadWith.messageId],
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, msgRow) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!msgRow) {
|
|
|
|
return callback(
|
|
|
|
Errors.DoesNotExist('Message (no longer) available')
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.messageId = msgRow.message_id;
|
|
|
|
self.areaTag = msgRow.area_tag;
|
|
|
|
self.messageUuid = msgRow.message_uuid;
|
|
|
|
self.replyToMsgId = msgRow.reply_to_message_id;
|
|
|
|
self.toUserName = msgRow.to_user_name;
|
|
|
|
self.fromUserName = msgRow.from_user_name;
|
|
|
|
self.subject = msgRow.subject;
|
|
|
|
self.message = msgRow.message;
|
2020-11-25 02:42:02 +00:00
|
|
|
|
|
|
|
// We use parseZone() to *preserve* the time zone information
|
2022-06-05 20:04:25 +00:00
|
|
|
self.modTimestamp = moment.parseZone(
|
|
|
|
msgRow.modified_timestamp
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function loadMessageMeta(callback) {
|
|
|
|
self.loadMeta(err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function loadHashTags(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO:
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
persistMetaValue(category, name, value, transOrDb, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isFunction(cb) && _.isFunction(transOrDb)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = transOrDb;
|
|
|
|
transOrDb = msgDb;
|
|
|
|
}
|
|
|
|
|
|
|
|
const metaStmt = transOrDb.prepare(
|
2019-11-09 02:20:14 +00:00
|
|
|
`INSERT INTO message_meta (message_id, meta_category, meta_name, meta_value)
|
2022-06-05 20:04:25 +00:00
|
|
|
VALUES (?, ?, ?, ?);`
|
|
|
|
);
|
2015-09-17 04:31:09 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isArray(value)) {
|
|
|
|
value = [value];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
value,
|
|
|
|
(v, next) => {
|
|
|
|
metaStmt.run(self.messageId, category, name, v, err => {
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
persist(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.isValid()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(Errors.Invalid('Cannot persist invalid message!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function beginTransaction(callback) {
|
|
|
|
return msgDb.beginTransaction(callback);
|
|
|
|
},
|
|
|
|
function storeMessage(trans, callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// generate a UUID for this message if required (general case)
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!self.messageUuid) {
|
2020-05-15 01:46:00 +00:00
|
|
|
self.messageUuid = Message.createMessageUUID(
|
2018-06-22 05:15:04 +00:00
|
|
|
self.areaTag,
|
2020-11-18 02:06:54 +00:00
|
|
|
self.modTimestamp,
|
2018-06-22 05:15:04 +00:00
|
|
|
self.subject,
|
|
|
|
self.message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
trans.run(
|
|
|
|
`INSERT INTO message (area_tag, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject, message, modified_timestamp)
|
2018-06-23 03:26:46 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`,
|
2020-05-15 01:46:00 +00:00
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
self.areaTag,
|
|
|
|
self.messageUuid,
|
|
|
|
self.replyToMsgId,
|
|
|
|
self.toUserName,
|
|
|
|
self.fromUserName,
|
|
|
|
self.subject,
|
|
|
|
self.message,
|
|
|
|
getISOTimestampString(self.modTimestamp),
|
2020-05-15 01:46:00 +00:00
|
|
|
],
|
2022-06-05 20:04:25 +00:00
|
|
|
function inserted(err) {
|
|
|
|
// use non-arrow function for 'this' scope
|
|
|
|
if (!err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.messageId = this.lastID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(err, trans);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function storeMeta(trans, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!self.meta) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, trans);
|
|
|
|
}
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Example of self.meta:
|
|
|
|
|
|
|
|
meta: {
|
|
|
|
System: {
|
|
|
|
local_to_user_id: 1234,
|
|
|
|
},
|
|
|
|
FtnProperty: {
|
|
|
|
ftn_seen_by: [ "1/102 103", "2/42 52 65" ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
Object.keys(self.meta),
|
|
|
|
(category, nextCat) => {
|
|
|
|
async.each(
|
|
|
|
Object.keys(self.meta[category]),
|
|
|
|
(name, nextName) => {
|
|
|
|
self.persistMetaValue(
|
|
|
|
category,
|
|
|
|
name,
|
|
|
|
self.meta[category][name],
|
|
|
|
trans,
|
|
|
|
err => {
|
|
|
|
return nextName(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return nextCat(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err, trans);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function storeHashTags(trans, callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: hash tag support
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, trans);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
(err, trans) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (trans) {
|
2018-06-22 05:15:04 +00:00
|
|
|
trans[err ? 'rollback' : 'commit'](transErr => {
|
|
|
|
return cb(err ? err : transErr, self.messageId);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:47:58 +00:00
|
|
|
deleteMessage(requestingUser, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.userHasDeleteRights(requestingUser)) {
|
|
|
|
return cb(
|
|
|
|
Errors.AccessDenied('User does not have rights to delete this message')
|
|
|
|
);
|
2018-07-05 00:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msgDb.run(
|
|
|
|
`DELETE FROM message
|
|
|
|
WHERE message_uuid = ?;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[this.messageUuid],
|
2018-07-05 00:47:58 +00:00
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-26 22:42:11 +00:00
|
|
|
_getQuotePrefix(source) {
|
2018-06-22 05:15:04 +00:00
|
|
|
source = source || 'fromUserName';
|
|
|
|
|
2023-01-26 22:42:11 +00:00
|
|
|
// grab out the name member, so we don't try to build
|
|
|
|
// quote prefixes such as "@N" for "@NuSkooler@some.host", etc.
|
|
|
|
const userName = this[source];
|
|
|
|
return getQuotePrefixFromName(userName);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 22:08:37 +00:00
|
|
|
static getTearLinePosition(input) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const m = input.match(/^--- .+$(?![\s\S]*^--- .+$)/m);
|
|
|
|
return m ? m.index : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
getQuoteLines(options, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!options.termWidth || !options.termHeight || !options.cols) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(Errors.MissingParam());
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
options.startCol = options.startCol || 1;
|
|
|
|
options.includePrefix = _.get(options, 'includePrefix', true);
|
|
|
|
options.ansiResetSgr =
|
|
|
|
options.ansiResetSgr ||
|
|
|
|
ANSI.getSGRFromGraphicRendition({ fg: 39, bg: 49 }, true);
|
|
|
|
options.ansiFocusPrefixSgr =
|
|
|
|
options.ansiFocusPrefixSgr ||
|
|
|
|
ANSI.getSGRFromGraphicRendition({ intensity: 'bold', fg: 39, bg: 49 });
|
|
|
|
options.isAnsi = options.isAnsi || isAnsi(this.message); // :TODO: If this.isAnsi, use that setting
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Some long text that needs to be wrapped and quoted should look right after
|
|
|
|
doing so, don't ya think? yeah I think so
|
2017-08-17 03:36:14 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
Nu> Some long text that needs to be wrapped and quoted should look right
|
|
|
|
Nu> after doing so, don't ya think? yeah I think so
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
Ot> Nu> Some long text that needs to be wrapped and quoted should look
|
|
|
|
Ot> Nu> right after doing so, don't ya think? yeah I think so
|
2018-01-27 04:36:16 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
*/
|
2022-06-05 20:04:25 +00:00
|
|
|
const quotePrefix = options.includePrefix
|
2023-01-26 22:42:11 +00:00
|
|
|
? this._getQuotePrefix(options.prefixSource || 'fromUserName')
|
2022-06-05 20:04:25 +00:00
|
|
|
: '';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
function getWrapped(text, extraPrefix) {
|
|
|
|
extraPrefix = extraPrefix ? ` ${extraPrefix}` : '';
|
|
|
|
|
|
|
|
const wrapOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
width: options.cols - (quotePrefix.length + extraPrefix.length),
|
|
|
|
tabHandling: 'expand',
|
|
|
|
tabWidth: 4,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return wordWrapText(text, wrapOpts).wrapped.map((w, i) => {
|
|
|
|
return i === 0
|
|
|
|
? `${quotePrefix}${w}`
|
|
|
|
: `${quotePrefix}${extraPrefix}${w}`;
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFormattedLine(line) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// for pre-formatted text, we just append a line truncated to fit
|
2018-06-22 05:15:04 +00:00
|
|
|
let newLen;
|
|
|
|
const total = line.length + quotePrefix.length;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (total > options.cols) {
|
2018-06-22 05:15:04 +00:00
|
|
|
newLen = options.cols - total;
|
|
|
|
} else {
|
|
|
|
newLen = total;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${quotePrefix}${line.slice(0, newLen)}`;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.isAnsi) {
|
2018-06-22 05:15:04 +00:00
|
|
|
ansiPrep(
|
2018-06-23 03:26:46 +00:00
|
|
|
this.message.replace(/\r?\n/g, '\r\n'), // normalized LF -> CRLF
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
termWidth: options.termWidth,
|
|
|
|
termHeight: options.termHeight,
|
|
|
|
cols: options.cols,
|
|
|
|
rows: 'auto',
|
|
|
|
startCol: options.startCol,
|
|
|
|
forceLineTerm: true,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
(err, prepped) => {
|
|
|
|
prepped = prepped || this.message;
|
|
|
|
|
|
|
|
let lastSgr = '';
|
|
|
|
const split = splitTextAtTerms(prepped);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const quoteLines = [];
|
|
|
|
const focusQuoteLines = [];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Do not include quote prefixes (e.g. XX> ) on ANSI replies (and therefor quote builder)
|
|
|
|
// as while this works in ENiGMA, other boards such as Mystic, WWIV, etc. will try to
|
|
|
|
// strip colors, colorize the lines, etc. If we exclude the prefixes, this seems to do
|
|
|
|
// the trick and allow them to leave them alone!
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
split.forEach(l => {
|
|
|
|
quoteLines.push(`${lastSgr}${l}`);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
focusQuoteLines.push(
|
|
|
|
`${options.ansiFocusPrefixSgr}>${lastSgr}${renderSubstr(
|
|
|
|
l,
|
|
|
|
1,
|
|
|
|
l.length - 1
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
lastSgr =
|
|
|
|
(l.match(
|
|
|
|
/(?:\x1b\x5b)[?=;0-9]*m(?!.*(?:\x1b\x5b)[?=;0-9]*m)/
|
|
|
|
) || [])[0] || ''; // eslint-disable-line no-control-regex
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
quoteLines[quoteLines.length - 1] += options.ansiResetSgr;
|
|
|
|
|
|
|
|
return cb(null, quoteLines, focusQuoteLines, true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
const QUOTE_RE = /^ ((?:[A-Za-z0-9]{1,2}> )+(?:[A-Za-z0-9]{1,2}>)*) */;
|
|
|
|
const quoted = [];
|
|
|
|
const input = _.trimEnd(this.message).replace(/\x08/g, ''); // eslint-disable-line no-control-regex
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// find *last* tearline
|
2020-12-26 22:08:37 +00:00
|
|
|
let tearLinePos = Message.getTearLinePosition(input);
|
2022-06-05 20:04:25 +00:00
|
|
|
tearLinePos = -1 === tearLinePos ? input.length : tearLinePos; // we just want the index or the entire string
|
|
|
|
|
|
|
|
input
|
|
|
|
.slice(0, tearLinePos)
|
|
|
|
.split(/\r\n\r\n|\n\n/)
|
|
|
|
.forEach(paragraph => {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
// For each paragraph, a state machine:
|
|
|
|
// - New line - line
|
|
|
|
// - New (pre)quoted line - quote_line
|
|
|
|
// - Continuation of new/quoted line
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
// Also:
|
|
|
|
// - Detect pre-formatted lines & try to keep them as-is
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
let state;
|
|
|
|
let buf = '';
|
|
|
|
let quoteMatch;
|
|
|
|
|
|
|
|
if (quoted.length > 0) {
|
|
|
|
//
|
|
|
|
// Preserve paragraph separation.
|
|
|
|
//
|
|
|
|
// FSC-0032 states something about leaving blank lines fully blank
|
|
|
|
// (without a prefix) but it seems nicer (and more consistent with other systems)
|
|
|
|
// to put 'em in.
|
|
|
|
//
|
|
|
|
quoted.push(quotePrefix);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
paragraph.split(/\r?\n/).forEach(line => {
|
|
|
|
if (0 === line.trim().length) {
|
|
|
|
// see blank line notes above
|
|
|
|
return quoted.push(quotePrefix);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
quoteMatch = line.match(QUOTE_RE);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 'line':
|
|
|
|
if (quoteMatch) {
|
|
|
|
if (isFormattedLine(line)) {
|
|
|
|
quoted.push(
|
|
|
|
getFormattedLine(line.replace(/\s/, ''))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
quoted.push(...getWrapped(buf, quoteMatch[1]));
|
|
|
|
state = 'quote_line';
|
|
|
|
buf = line;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
buf += ` ${line}`;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'quote_line':
|
|
|
|
if (quoteMatch) {
|
|
|
|
const rem = line.slice(quoteMatch[0].length);
|
|
|
|
if (!buf.startsWith(quoteMatch[0])) {
|
|
|
|
quoted.push(...getWrapped(buf, quoteMatch[1]));
|
|
|
|
buf = rem;
|
|
|
|
} else {
|
|
|
|
buf += ` ${rem}`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quoted.push(...getWrapped(buf));
|
2018-06-22 05:15:04 +00:00
|
|
|
buf = line;
|
2022-06-05 20:04:25 +00:00
|
|
|
state = 'line';
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (isFormattedLine(line)) {
|
|
|
|
quoted.push(getFormattedLine(line));
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
state = quoteMatch ? 'quote_line' : 'line';
|
|
|
|
buf =
|
|
|
|
'line' === state ? line : line.replace(/\s/, ''); // trim *first* leading space, if any
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
quoted.push(...getWrapped(buf, quoteMatch ? quoteMatch[1] : null));
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
input
|
|
|
|
.slice(tearLinePos)
|
|
|
|
.split(/\r?\n/)
|
|
|
|
.forEach(l => {
|
|
|
|
quoted.push(...getWrapped(l));
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return cb(null, quoted, null, false);
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 03:36:14 +00:00
|
|
|
};
|