Updates to user interruptions & node-to-node message module

* Can now have header/footer art on node-to-node messages
* 'text' and more advanced 'contents' fields
* format via 'messageFormat'
This commit is contained in:
Bryan Ashby 2018-11-13 21:11:33 -07:00
parent d8f0708310
commit 330e1efa78
2 changed files with 85 additions and 16 deletions

View File

@ -9,10 +9,14 @@ const {
getConnectionByNodeId, getConnectionByNodeId,
} = require('./client_connections.js'); } = require('./client_connections.js');
const UserInterruptQueue = require('./user_interrupt_queue.js'); const UserInterruptQueue = require('./user_interrupt_queue.js');
const { getThemeArt } = require('./theme.js');
const { pipeToAnsi } = require('./color_codes.js');
const stringFormat = require('./string_format.js');
// deps // deps
const series = require('async/series'); const series = require('async/series');
const _ = require('lodash'); const _ = require('lodash');
const async = require('async');
exports.moduleInfo = { exports.moduleInfo = {
name : 'Node Message', name : 'Node Message',
@ -44,18 +48,16 @@ exports.getModule = class NodeMessageModule extends MenuModule {
const nodeId = formData.value.node; const nodeId = formData.value.node;
const message = formData.value.message; const message = formData.value.message;
const interruptItem = { this.createInterruptItem(message, (err, interruptItem) => {
contents : message, if(0 === nodeId) {
} // ALL nodes
UserInterruptQueue.queueGlobalOtherActive(interruptItem, this.client);
} else {
UserInterruptQueue.queueGlobal(interruptItem, [ getConnectionByNodeId(nodeId) ]);
}
if(0 === nodeId) { return this.prevMenu(cb);
// ALL nodes });
UserInterruptQueue.queueGlobalOtherActive(interruptItem, this.client);
} else {
UserInterruptQueue.queueGlobal(interruptItem, [ getConnectionByNodeId(nodeId) ]);
}
return this.prevMenu(cb);
}, },
} }
} }
@ -96,6 +98,64 @@ exports.getModule = class NodeMessageModule extends MenuModule {
}); });
} }
createInterruptItem(message, cb) {
const textFormatObj = {
fromUserName : this.client.user.username,
fromRealName : this.client.user.properties.real_name,
fromNodeId : this.client.node,
message : message,
};
const messageFormat =
this.config.messageFormat ||
'Message from {fromUserName} on node {fromNodeId}:\r\n{message}';
const item = {
text : stringFormat(messageFormat, textFormatObj),
pause : true,
};
const getArt = (name, callback) => {
const spec = _.get(this.config, `art.${name}`);
if(!spec) {
return callback(null);
}
const getArtOpts = {
name : spec,
client : this.client,
random : false,
};
getThemeArt(getArtOpts, (err, artInfo) => {
// ignore errors
return callback(artInfo ? artInfo.data : null);
});
};
async.waterfall(
[
(callback) => {
getArt('header', headerArt => {
return callback(null, headerArt);
});
},
(headerArt, callback) => {
getArt('footer', footerArt => {
return callback(null, headerArt, footerArt);
});
},
(headerArt, footerArt, callback) => {
if(headerArt || footerArt) {
item.contents = `${headerArt || ''}\r\n${pipeToAnsi(item.text)}\r\n${footerArt || ''}`;
}
return callback(null);
}
],
err => {
return cb(err, item);
}
);
}
prepareNodeList() { prepareNodeList() {
// standard node list with {text} field added for compliance // standard node list with {text} field added for compliance
this.nodeList = [{ this.nodeList = [{

View File

@ -7,6 +7,7 @@ const {
getActiveConnections getActiveConnections
} = require('./client_connections.js'); } = require('./client_connections.js');
const ANSI = require('./ansi_term.js'); const ANSI = require('./ansi_term.js');
const { pipeToAnsi } = require('./color_codes.js');
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
@ -31,6 +32,9 @@ module.exports = class UserInterruptQueue
} }
queueItem(interruptItem) { queueItem(interruptItem) {
if(!_.isString(interruptItem.contents) && !_.isString(interruptItem.text)) {
return;
}
interruptItem.pause = _.get(interruptItem, 'pause', true); interruptItem.pause = _.get(interruptItem, 'pause', true);
this.queue.push(interruptItem); this.queue.push(interruptItem);
} }
@ -51,8 +55,13 @@ module.exports = class UserInterruptQueue
this.client.term.rawWrite('\r\n\r\n'); this.client.term.rawWrite('\r\n\r\n');
} }
Art.display(this.client, interruptItem.contents, err => { if(interruptItem.contents) {
return cb(err, interruptItem); Art.display(this.client, interruptItem.contents, err => {
}); this.client.term.rawWrite('\r\n\r\n');
return cb(err, interruptItem);
});
} else {
return this.client.term.write(pipeToAnsi(`${interruptItem.text}\r\n\r\n`, this.client), cb);
}
} }
}; };