2015-07-11 22:39:42 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-03-15 04:29:41 +00:00
|
|
|
let msgDb = require('./database.js').dbs.message;
|
|
|
|
let wordWrapText = require('./word_wrap.js').wordWrapText;
|
|
|
|
let ftnUtil = require('./ftn_util.js');
|
2015-07-12 06:32:31 +00:00
|
|
|
|
2016-03-15 04:29:41 +00:00
|
|
|
let uuid = require('node-uuid');
|
|
|
|
let async = require('async');
|
|
|
|
let _ = require('lodash');
|
|
|
|
let assert = require('assert');
|
|
|
|
let moment = require('moment');
|
2015-07-13 04:56:33 +00:00
|
|
|
|
2015-07-16 05:51:00 +00:00
|
|
|
module.exports = Message;
|
2015-07-12 06:32:31 +00:00
|
|
|
|
2015-07-11 22:39:42 +00:00
|
|
|
function Message(options) {
|
2015-09-01 05:18:46 +00:00
|
|
|
options = options || {};
|
2015-07-16 05:51:00 +00:00
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
this.messageId = options.messageId || 0; // always generated @ persist
|
2016-02-03 04:35:59 +00:00
|
|
|
this.areaTag = options.areaTag || Message.WellKnownAreaTags.Invalid;
|
2016-02-10 05:30:59 +00:00
|
|
|
this.uuid = options.uuid || uuid.v1();
|
2015-07-13 04:56:33 +00:00
|
|
|
this.replyToMsgId = options.replyToMsgId || 0;
|
|
|
|
this.toUserName = options.toUserName || '';
|
|
|
|
this.fromUserName = options.fromUserName || '';
|
|
|
|
this.subject = options.subject || '';
|
|
|
|
this.message = options.message || '';
|
2015-07-16 23:13:48 +00:00
|
|
|
|
2016-03-15 04:29:41 +00:00
|
|
|
if(_.isDate(options.modTimestamp) || moment.isMoment(options.modTimestamp)) {
|
|
|
|
this.modTimestamp = moment(options.modTimestamp);
|
2015-07-16 23:13:48 +00:00
|
|
|
} else if(_.isString(options.modTimestamp)) {
|
2016-03-15 04:29:41 +00:00
|
|
|
this.modTimestamp = moment(options.modTimestamp);
|
2015-07-16 23:13:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
this.viewCount = options.viewCount || 0;
|
2015-07-17 04:57:08 +00:00
|
|
|
|
|
|
|
this.meta = {
|
2016-01-03 01:34:50 +00:00
|
|
|
System : {}, // we'll always have this one
|
2015-07-17 04:57:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(_.isObject(options.meta)) {
|
|
|
|
_.defaultsDeep(this.meta, options.meta);
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:10:18 +00:00
|
|
|
if(options.meta) {
|
|
|
|
this.meta = options.meta;
|
|
|
|
}
|
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
this.hashTags = options.hashTags || [];
|
2015-07-12 06:32:31 +00:00
|
|
|
|
|
|
|
this.isValid = function() {
|
|
|
|
// :TODO: validate as much as possible
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2015-10-22 21:44:44 +00:00
|
|
|
this.isPrivate = function() {
|
2016-02-03 04:35:59 +00:00
|
|
|
return this.areaTag === Message.WellKnownAreaTags.Private ? true : false;
|
2015-10-22 21:44:44 +00:00
|
|
|
};
|
|
|
|
|
2015-07-16 23:13:48 +00:00
|
|
|
this.getMessageTimestampString = function(ts) {
|
2016-03-15 04:29:41 +00:00
|
|
|
ts = ts || moment();
|
|
|
|
return ts.format('YYYY-MM-DDTHH:mm:ss.SSSZ');
|
2015-07-12 06:32:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
Message.WellKnownAreaTags = {
|
2015-12-31 05:28:25 +00:00
|
|
|
Invalid : '',
|
|
|
|
Private : 'private_mail',
|
|
|
|
Bulletin : 'local_bulletin',
|
2015-07-13 04:56:33 +00:00
|
|
|
};
|
|
|
|
|
2015-07-17 04:57:08 +00:00
|
|
|
Message.SystemMetaNames = {
|
2015-07-13 04:56:33 +00:00
|
|
|
LocalToUserID : 'local_to_user_id',
|
|
|
|
LocalFromUserID : 'local_from_user_id',
|
2016-03-15 04:29:41 +00:00
|
|
|
StateFlags0 : 'state_flags0', // See Message.StateFlags0
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.StateFlags0 = {
|
|
|
|
None : 0x00000000,
|
|
|
|
Imported : 0x00000001, // imported from foreign system
|
|
|
|
Exported : 0x00000002, // exported to foreign system
|
2015-07-17 04:57:08 +00:00
|
|
|
};
|
2015-07-13 04:56:33 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
Message.FtnPropertyNames = {
|
2015-07-17 04:57:08 +00:00
|
|
|
FtnOrigNode : 'ftn_orig_node',
|
|
|
|
FtnDestNode : 'ftn_dest_node',
|
|
|
|
FtnOrigNetwork : 'ftn_orig_network',
|
|
|
|
FtnDestNetwork : 'ftn_dest_network',
|
2016-02-16 00:56:05 +00:00
|
|
|
FtnAttrFlags : 'ftn_attr_flags',
|
2016-02-03 04:35:59 +00:00
|
|
|
FtnCost : 'ftn_cost',
|
2015-07-17 04:57:08 +00:00
|
|
|
FtnOrigZone : 'ftn_orig_zone',
|
|
|
|
FtnDestZone : 'ftn_dest_zone',
|
|
|
|
FtnOrigPoint : 'ftn_orig_point',
|
|
|
|
FtnDestPoint : 'ftn_dest_point',
|
2016-03-09 05:30:04 +00:00
|
|
|
|
2015-07-17 04:57:08 +00:00
|
|
|
FtnAttribute : 'ftn_attribute',
|
|
|
|
|
|
|
|
FtnTearLine : 'ftn_tear_line', // http://ftsc.org/docs/fts-0004.001
|
|
|
|
FtnOrigin : 'ftn_origin', // http://ftsc.org/docs/fts-0004.001
|
|
|
|
FtnArea : 'ftn_area', // http://ftsc.org/docs/fts-0004.001
|
|
|
|
FtnSeenBy : 'ftn_seen_by', // http://ftsc.org/docs/fts-0004.001
|
2015-07-16 23:13:48 +00:00
|
|
|
};
|
|
|
|
|
2015-07-17 14:07:43 +00:00
|
|
|
// Note: kludges are stored with their names as-is
|
2015-07-17 04:57:08 +00:00
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
Message.prototype.setLocalToUserId = function(userId) {
|
2016-01-03 01:34:50 +00:00
|
|
|
this.meta.System.local_to_user_id = userId;
|
2015-07-13 04:56:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Message.prototype.setLocalFromUserId = function(userId) {
|
2016-01-03 01:34:50 +00:00
|
|
|
this.meta.System.local_from_user_id = userId;
|
2015-07-13 04:56:33 +00:00
|
|
|
};
|
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
Message.getMessageIdByUuid = function(uuid, cb) {
|
|
|
|
msgDb.get(
|
|
|
|
`SELECT message_id
|
|
|
|
FROM message
|
|
|
|
WHERE message_uuid = ?
|
|
|
|
LIMIT 1;`,
|
|
|
|
[ uuid ],
|
|
|
|
(err, row) => {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
|
|
|
const success = (row && row.message_id);
|
|
|
|
cb(success ? null : new Error('No match'), success ? row.message_id : null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.getMessageIdsByMetaValue = function(category, name, value, cb) {
|
|
|
|
msgDb.all(
|
|
|
|
`SELECT message_id
|
|
|
|
FROM message_meta
|
|
|
|
WHERE meta_category = ? AND meta_name = ? AND meta_value = ?;`,
|
|
|
|
[ category, name, value ],
|
|
|
|
(err, rows) => {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
|
|
|
cb(null, rows.map(r => parseInt(r.message_id))); // return array of ID(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-03-15 04:29:41 +00:00
|
|
|
Message.getMetaValuesByMessageId = function(messageId, category, name, cb) {
|
|
|
|
const sql =
|
|
|
|
`SELECT meta_value
|
|
|
|
FROM message_meta
|
|
|
|
WHERE message_id = ? AND meta_category = ? AND meta_name = ?;`;
|
|
|
|
|
|
|
|
msgDb.all(sql, [ messageId, category, name ], (err, rows) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(0 === rows.length) {
|
|
|
|
return cb(new Error('No value for category/name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// single values are returned without an array
|
|
|
|
if(1 === rows.length) {
|
|
|
|
return cb(null, rows[0].meta_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, rows.map(r => r.meta_value)); // map to array of values only
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.getMetaValuesByMessageUuid = function(uuid, category, name, cb) {
|
2016-03-09 05:30:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getMessageId(callback) {
|
|
|
|
Message.getMessageIdByUuid(uuid, (err, messageId) => {
|
|
|
|
callback(err, messageId);
|
|
|
|
});
|
|
|
|
},
|
2016-03-15 04:29:41 +00:00
|
|
|
function getMetaValues(messageId, callback) {
|
|
|
|
Message.getMetaValuesByMessageId(messageId, category, name, (err, values) => {
|
|
|
|
callback(err, values);
|
2016-03-09 05:30:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
2016-03-15 04:29:41 +00:00
|
|
|
(err, values) => {
|
|
|
|
cb(err, values);
|
2016-03-09 05:30:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.prototype.loadMeta = function(cb) {
|
|
|
|
/*
|
|
|
|
Example of loaded this.meta:
|
|
|
|
|
|
|
|
meta: {
|
|
|
|
System: {
|
|
|
|
local_to_user_id: 1234,
|
|
|
|
},
|
|
|
|
FtnProperty: {
|
|
|
|
ftn_seen_by: [ "1/102 103", "2/42 52 65" ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
const sql =
|
|
|
|
`SELECT meta_category, meta_name, meta_value
|
|
|
|
FROM message_meta
|
|
|
|
WHERE message_id = ?;`;
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
msgDb.each(sql, [ this.messageId ], (err, row) => {
|
|
|
|
if(!(row.meta_category in self.meta)) {
|
|
|
|
self.meta[row.meta_category] = { };
|
|
|
|
self.meta[row.meta_category][row.meta_name] = row.meta_value;
|
|
|
|
} else {
|
|
|
|
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] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.meta[row.meta_category][row.meta_name].push(row.meta_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, err => {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-09-01 05:18:46 +00:00
|
|
|
Message.prototype.load = function(options, cb) {
|
|
|
|
assert(_.isString(options.uuid));
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadMessage(callback) {
|
|
|
|
msgDb.get(
|
2016-02-03 04:35:59 +00:00
|
|
|
'SELECT message_id, area_tag, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject, ' +
|
2015-09-01 05:18:46 +00:00
|
|
|
'message, modified_timestamp, view_count ' +
|
|
|
|
'FROM message ' +
|
|
|
|
'WHERE message_uuid=? ' +
|
|
|
|
'LIMIT 1;',
|
|
|
|
[ options.uuid ],
|
2016-06-20 20:41:43 +00:00
|
|
|
(err, msgRow) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
if(!msgRow) {
|
|
|
|
return callback(new Error('Message (no longer) available'));
|
|
|
|
}
|
|
|
|
|
2015-09-01 05:18:46 +00:00
|
|
|
self.messageId = msgRow.message_id;
|
2016-02-03 04:35:59 +00:00
|
|
|
self.areaTag = msgRow.area_tag;
|
2015-09-01 05:18:46 +00:00
|
|
|
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;
|
2016-03-15 04:29:41 +00:00
|
|
|
self.modTimestamp = moment(msgRow.modified_timestamp);
|
2015-09-01 05:18:46 +00:00
|
|
|
self.viewCount = msgRow.view_count;
|
|
|
|
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function loadMessageMeta(callback) {
|
2016-03-09 05:30:04 +00:00
|
|
|
self.loadMeta(err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
2015-09-01 05:18:46 +00:00
|
|
|
},
|
|
|
|
function loadHashTags(callback) {
|
|
|
|
// :TODO:
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
Message.prototype.persistMetaValue = function(category, name, value, cb) {
|
|
|
|
const metaStmt = msgDb.prepare(
|
|
|
|
`INSERT INTO message_meta (message_id, meta_category, meta_name, meta_value)
|
|
|
|
VALUES (?, ?, ?, ?);`);
|
|
|
|
|
|
|
|
if(!_.isArray(value)) {
|
|
|
|
value = [ value ];
|
|
|
|
}
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
async.each(value, (v, next) => {
|
|
|
|
metaStmt.run(self.messageId, category, name, v, err => {
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.startTransaction = function(cb) {
|
|
|
|
msgDb.run('BEGIN;', err => {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.endTransaction = function(hadError, cb) {
|
|
|
|
msgDb.run(hadError ? 'ROLLBACK;' : 'COMMIT;', err => {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-07-12 06:32:31 +00:00
|
|
|
Message.prototype.persist = function(cb) {
|
|
|
|
|
|
|
|
if(!this.isValid()) {
|
2016-03-09 05:30:04 +00:00
|
|
|
return cb(new Error('Cannot persist invalid message!'));
|
2015-07-12 06:32:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
let self = this;
|
|
|
|
|
2015-07-12 06:32:31 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function beginTransaction(callback) {
|
2016-03-09 05:30:04 +00:00
|
|
|
Message.startTransaction(err => {
|
2015-07-12 06:32:31 +00:00
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function storeMessage(callback) {
|
|
|
|
msgDb.run(
|
2016-03-09 05:30:04 +00:00
|
|
|
`INSERT INTO message (area_tag, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject, message, modified_timestamp)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`,
|
|
|
|
[ self.areaTag, self.uuid, self.replyToMsgId, self.toUserName, self.fromUserName, self.subject, self.message, self.getMessageTimestampString(self.modTimestamp) ],
|
|
|
|
function inserted(err) { // use for this scope
|
2015-07-12 06:32:31 +00:00
|
|
|
if(!err) {
|
|
|
|
self.messageId = this.lastID;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function storeMeta(callback) {
|
|
|
|
if(!self.meta) {
|
|
|
|
callback(null);
|
|
|
|
} else {
|
2016-03-09 05:30:04 +00:00
|
|
|
/*
|
|
|
|
Example of self.meta:
|
|
|
|
|
|
|
|
meta: {
|
|
|
|
System: {
|
|
|
|
local_to_user_id: 1234,
|
|
|
|
},
|
|
|
|
FtnProperty: {
|
|
|
|
ftn_seen_by: [ "1/102 103", "2/42 52 65" ]
|
2015-07-17 04:57:08 +00:00
|
|
|
}
|
2016-03-09 05:30:04 +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], err => {
|
|
|
|
nextName(err);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
nextCat(err);
|
2015-07-17 04:57:08 +00:00
|
|
|
});
|
2016-03-09 05:30:04 +00:00
|
|
|
|
|
|
|
}, err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
2015-07-12 06:32:31 +00:00
|
|
|
}
|
2015-07-13 04:56:33 +00:00
|
|
|
},
|
|
|
|
function storeHashTags(callback) {
|
|
|
|
// :TODO: hash tag support
|
|
|
|
callback(null);
|
2015-07-12 06:32:31 +00:00
|
|
|
}
|
2015-07-13 04:56:33 +00:00
|
|
|
],
|
2016-03-09 05:30:04 +00:00
|
|
|
err => {
|
|
|
|
Message.endTransaction(err, transErr => {
|
|
|
|
cb(err ? err : transErr, self.messageId);
|
2015-07-13 04:56:33 +00:00
|
|
|
});
|
|
|
|
}
|
2015-07-12 06:32:31 +00:00
|
|
|
);
|
2015-09-16 03:55:10 +00:00
|
|
|
};
|
|
|
|
|
2015-09-17 04:31:09 +00:00
|
|
|
Message.prototype.getFTNQuotePrefix = function(source) {
|
|
|
|
source = source || 'fromUserName';
|
|
|
|
|
2015-09-21 01:10:09 +00:00
|
|
|
return ftnUtil.getQuotePrefix(this[source]);
|
2015-09-17 04:31:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Message.prototype.getQuoteLines = function(width, options) {
|
2015-09-16 04:44:31 +00:00
|
|
|
// :TODO: options.maxBlankLines = 1
|
2015-09-17 04:31:09 +00:00
|
|
|
|
|
|
|
options = options || {};
|
2015-09-16 04:44:31 +00:00
|
|
|
|
2015-09-17 04:31:09 +00:00
|
|
|
//
|
|
|
|
// Include FSC-0032 style quote prefixes?
|
|
|
|
//
|
|
|
|
// See http://ftsc.org/docs/fsc-0032.001
|
|
|
|
//
|
|
|
|
if(!_.isBoolean(options.includePrefix)) {
|
|
|
|
options.includePrefix = true;
|
|
|
|
}
|
|
|
|
|
2015-09-16 03:55:10 +00:00
|
|
|
var quoteLines = [];
|
|
|
|
|
|
|
|
var origLines = this.message
|
2015-09-20 04:55:09 +00:00
|
|
|
.trim()
|
2015-09-16 03:55:10 +00:00
|
|
|
.replace(/\b/g, '')
|
|
|
|
.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g);
|
|
|
|
|
2015-09-20 04:55:09 +00:00
|
|
|
var quotePrefix = ''; // we need this init even if blank
|
|
|
|
if(options.includePrefix) {
|
2015-09-21 01:10:09 +00:00
|
|
|
quotePrefix = this.getFTNQuotePrefix(options.prefixSource || 'fromUserName');
|
2015-09-20 04:55:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 03:55:10 +00:00
|
|
|
var wrapOpts = {
|
2015-09-20 04:55:09 +00:00
|
|
|
width : width - quotePrefix.length,
|
2015-09-16 03:55:10 +00:00
|
|
|
tabHandling : 'expand',
|
|
|
|
tabWidth : 4,
|
|
|
|
};
|
|
|
|
|
2015-09-20 04:55:09 +00:00
|
|
|
function addPrefix(l) {
|
|
|
|
return quotePrefix + l;
|
2015-09-17 04:31:09 +00:00
|
|
|
}
|
2015-09-16 03:55:10 +00:00
|
|
|
|
2015-09-20 04:55:09 +00:00
|
|
|
var wrapped;
|
2015-09-16 03:55:10 +00:00
|
|
|
for(var i = 0; i < origLines.length; ++i) {
|
2015-09-20 04:55:09 +00:00
|
|
|
wrapped = wordWrapText(origLines[i], wrapOpts).wrapped;
|
|
|
|
Array.prototype.push.apply(quoteLines, _.map(wrapped, addPrefix));
|
2015-09-16 03:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return quoteLines;
|
|
|
|
};
|