2015-08-26 05:17:09 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-07-14 04:44:55 +00:00
|
|
|
// ENiGMA½
|
2017-11-24 23:23:15 +00:00
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const messageArea = require('./message_area.js');
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const MessageAreaConfTempSwitcher = require('./mod_mixins.js').MessageAreaConfTempSwitcher;
|
2015-08-27 05:04:04 +00:00
|
|
|
|
2016-07-14 04:44:55 +00:00
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
2015-08-26 05:17:09 +00:00
|
|
|
|
2015-10-19 03:24:13 +00:00
|
|
|
/*
|
|
|
|
Available listFormat/focusListFormat members (VM1):
|
|
|
|
|
|
|
|
msgNum : Message number
|
|
|
|
to : To username/handle
|
|
|
|
from : From username/handle
|
|
|
|
subj : Subject
|
|
|
|
ts : Message mod timestamp (format with config.dateTimeFormat)
|
|
|
|
newIndicator : New mark/indicator (config.newIndicator)
|
|
|
|
|
|
|
|
MCI codes:
|
|
|
|
|
|
|
|
VM1 : Message list
|
2018-01-15 19:22:11 +00:00
|
|
|
TL2 : Message info 1: { msgNumSelected, msgNumTotal }
|
2015-10-19 03:24:13 +00:00
|
|
|
*/
|
|
|
|
|
2015-08-26 05:17:09 +00:00
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'Message List',
|
|
|
|
desc : 'Module for listing/browsing available messages',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
2018-02-01 05:45:03 +00:00
|
|
|
const MciViewIds = {
|
|
|
|
msgList : 1, // VM1
|
2018-02-12 21:53:15 +00:00
|
|
|
msgInfo1 : 2, // TL2
|
2015-10-11 22:05:45 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class MessageListModule extends MessageAreaConfTempSwitcher(MenuModule) {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2015-08-26 05:17:09 +00:00
|
|
|
|
2018-02-01 05:45:03 +00:00
|
|
|
// :TODO: consider this pattern in base MenuModule - clean up code all over
|
|
|
|
this.config = Object.assign({}, _.get(options, 'menuConfig.config'), options.extraArgs);
|
2015-09-04 06:02:28 +00:00
|
|
|
|
2017-08-30 01:38:34 +00:00
|
|
|
this.lastMessageReachedExit = _.get(options, 'lastMenuResult.lastMessageReached', false);
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
this.menuMethods = {
|
2018-02-01 05:45:03 +00:00
|
|
|
selectMessage : (formData, extraArgs, cb) => {
|
|
|
|
if(MciViewIds.msgList === formData.submitId) {
|
|
|
|
this.initialFocusIndex = formData.value.message;
|
2017-01-26 05:18:05 +00:00
|
|
|
|
|
|
|
const modOpts = {
|
|
|
|
extraArgs : {
|
2018-02-01 05:45:03 +00:00
|
|
|
messageAreaTag : this.getSelectedAreaTag(formData.value.message),// this.config.messageAreaTag,
|
|
|
|
messageList : this.config.messageList,
|
2017-01-26 05:18:05 +00:00
|
|
|
messageIndex : formData.value.message,
|
2018-02-01 05:45:03 +00:00
|
|
|
lastMessageNextExit : true,
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
|
|
|
};
|
2016-07-14 04:44:55 +00:00
|
|
|
|
2018-02-01 05:45:03 +00:00
|
|
|
if(_.isBoolean(this.config.noUpdateLastReadId)) {
|
|
|
|
modOpts.extraArgs.noUpdateLastReadId = this.config.noUpdateLastReadId;
|
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
//
|
|
|
|
// Provide a serializer so we don't dump *huge* bits of information to the log
|
|
|
|
// due to the size of |messageList|. See https://github.com/trentm/node-bunyan/issues/189
|
|
|
|
//
|
2018-02-01 05:45:03 +00:00
|
|
|
const self = this;
|
2017-01-26 05:18:05 +00:00
|
|
|
modOpts.extraArgs.toJSON = function() {
|
2018-02-01 05:45:03 +00:00
|
|
|
const logMsgList = (self.config.messageList.length <= 4) ?
|
|
|
|
self.config.messageList :
|
|
|
|
self.config.messageList.slice(0, 2).concat(self.config.messageList.slice(-2));
|
2017-01-26 05:18:05 +00:00
|
|
|
|
|
|
|
return {
|
2018-02-01 05:45:03 +00:00
|
|
|
// note |this| is scope of toJSON()!
|
2017-01-26 05:18:05 +00:00
|
|
|
messageAreaTag : this.messageAreaTag,
|
|
|
|
apprevMessageList : logMsgList,
|
|
|
|
messageCount : this.messageList.length,
|
2018-02-01 05:45:03 +00:00
|
|
|
messageIndex : this.messageIndex,
|
2017-01-26 05:18:05 +00:00
|
|
|
};
|
2016-07-24 17:47:34 +00:00
|
|
|
};
|
|
|
|
|
2018-02-01 05:45:03 +00:00
|
|
|
return this.gotoMenu(this.config.menuViewPost || 'messageAreaViewPost', modOpts, cb);
|
2017-01-26 05:18:05 +00:00
|
|
|
} else {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-01 05:45:03 +00:00
|
|
|
fullExit : (formData, extraArgs, cb) => {
|
|
|
|
this.menuResult = { fullExit : true };
|
|
|
|
return this.prevMenu(cb);
|
2015-08-31 03:42:24 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
};
|
|
|
|
}
|
2016-08-31 03:31:24 +00:00
|
|
|
|
2018-02-01 05:45:03 +00:00
|
|
|
getSelectedAreaTag(listIndex) {
|
|
|
|
return this.config.messageList[listIndex].areaTag || this.config.messageAreaTag;
|
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
enter() {
|
2017-08-30 01:38:34 +00:00
|
|
|
if(this.lastMessageReachedExit) {
|
|
|
|
return this.prevMenu();
|
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
super.enter();
|
2015-10-11 22:05:45 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
//
|
|
|
|
// Config can specify |messageAreaTag| else it comes from
|
2018-02-01 05:45:03 +00:00
|
|
|
// the user's current area. If |messageList| is supplied,
|
|
|
|
// each item is expected to contain |areaTag|, so we use that
|
|
|
|
// instead in those cases.
|
2017-01-26 05:18:05 +00:00
|
|
|
//
|
2018-02-01 05:45:03 +00:00
|
|
|
if(!Array.isArray(this.config.messageList)) {
|
|
|
|
if(this.config.messageAreaTag) {
|
|
|
|
this.tempMessageConfAndAreaSwitch(this.config.messageAreaTag);
|
|
|
|
} else {
|
|
|
|
this.config.messageAreaTag = this.client.user.properties.message_area_tag;
|
|
|
|
}
|
2015-10-11 22:05:45 +00:00
|
|
|
}
|
2015-09-04 06:02:28 +00:00
|
|
|
}
|
2016-07-24 17:47:34 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
leave() {
|
|
|
|
this.tempMessageConfAndAreaRestore();
|
|
|
|
super.leave();
|
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2015-08-27 05:04:04 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const self = this;
|
|
|
|
const vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
|
2018-02-02 02:34:14 +00:00
|
|
|
let configProvidedMessageList = false;
|
2017-01-26 05:18:05 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadFromConfig(callback) {
|
|
|
|
const loadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu
|
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
},
|
|
|
|
function fetchMessagesInArea(callback) {
|
|
|
|
//
|
|
|
|
// Config can supply messages else we'll need to populate the list now
|
|
|
|
//
|
2018-02-01 05:45:03 +00:00
|
|
|
if(_.isArray(self.config.messageList)) {
|
2018-02-02 02:34:14 +00:00
|
|
|
configProvidedMessageList = true;
|
2018-02-01 05:45:03 +00:00
|
|
|
return callback(0 === self.config.messageList.length ? new Error('No messages in area') : null);
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-02-01 05:45:03 +00:00
|
|
|
messageArea.getMessageListForArea(self.client, self.config.messageAreaTag, function msgs(err, msgList) {
|
2017-01-26 05:18:05 +00:00
|
|
|
if(!msgList || 0 === msgList.length) {
|
|
|
|
return callback(new Error('No messages in area'));
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-02-01 05:45:03 +00:00
|
|
|
self.config.messageList = msgList;
|
2018-01-15 19:22:11 +00:00
|
|
|
return callback(err);
|
2017-01-26 05:18:05 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function getLastReadMesageId(callback) {
|
2018-02-01 05:45:03 +00:00
|
|
|
// messageList entries can contain |isNew| if they want to be considered new
|
2018-02-02 02:34:14 +00:00
|
|
|
if(configProvidedMessageList) {
|
2018-02-01 05:45:03 +00:00
|
|
|
self.lastReadId = 0;
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
messageArea.getMessageAreaLastReadId(self.client.user.userId, self.config.messageAreaTag, function lastRead(err, lastReadId) {
|
2017-01-26 05:18:05 +00:00
|
|
|
self.lastReadId = lastReadId || 0;
|
|
|
|
return callback(null); // ignore any errors, e.g. missing value
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function updateMessageListObjects(callback) {
|
2018-02-03 04:22:47 +00:00
|
|
|
const dateTimeFormat = self.menuConfig.config.dateTimeFormat || self.client.currentTheme.helpers.getDateTimeFormat();
|
2017-01-26 05:18:05 +00:00
|
|
|
const newIndicator = self.menuConfig.config.newIndicator || '*';
|
|
|
|
const regIndicator = new Array(newIndicator.length + 1).join(' '); // fill with space to avoid draw issues
|
|
|
|
|
|
|
|
let msgNum = 1;
|
2018-02-03 04:22:47 +00:00
|
|
|
self.config.messageList.forEach( (listItem, index) => {
|
2017-01-26 05:18:05 +00:00
|
|
|
listItem.msgNum = msgNum++;
|
|
|
|
listItem.ts = moment(listItem.modTimestamp).format(dateTimeFormat);
|
2018-02-01 05:45:03 +00:00
|
|
|
const isNew = _.isBoolean(listItem.isNew) ? listItem.isNew : listItem.messageId > self.lastReadId;
|
|
|
|
listItem.newIndicator = isNew ? newIndicator : regIndicator;
|
2017-01-26 05:18:05 +00:00
|
|
|
|
|
|
|
if(_.isUndefined(self.initialFocusIndex) && listItem.messageId > self.lastReadId) {
|
|
|
|
self.initialFocusIndex = index;
|
2018-01-15 19:22:11 +00:00
|
|
|
}
|
2018-02-03 04:22:47 +00:00
|
|
|
|
|
|
|
listItem.text = `${listItem.msgNum} - ${listItem.subject} from ${listItem.fromUserName}`; // default text
|
2017-01-26 05:18:05 +00:00
|
|
|
});
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function populateList(callback) {
|
2018-02-01 05:45:03 +00:00
|
|
|
const msgListView = vc.getView(MciViewIds.msgList);
|
2018-02-03 04:22:47 +00:00
|
|
|
// :TODO: replace with standard custom info MCI - msgNumSelected, msgNumTotal, areaName, areaDesc, confName, confDesc, ...
|
2018-01-15 19:22:11 +00:00
|
|
|
const messageInfo1Format = self.menuConfig.config.messageInfo1Format || '{msgNumSelected} / {msgNumTotal}';
|
2017-01-26 05:18:05 +00:00
|
|
|
|
2018-02-03 04:22:47 +00:00
|
|
|
msgListView.setItems(self.config.messageList);
|
2017-01-26 05:18:05 +00:00
|
|
|
|
|
|
|
msgListView.on('index update', idx => {
|
|
|
|
self.setViewText(
|
|
|
|
'allViews',
|
2018-02-01 05:45:03 +00:00
|
|
|
MciViewIds.msgInfo1,
|
|
|
|
stringFormat(messageInfo1Format, { msgNumSelected : (idx + 1), msgNumTotal : self.config.messageList.length } ));
|
2017-01-26 05:18:05 +00:00
|
|
|
});
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
if(self.initialFocusIndex > 0) {
|
|
|
|
// note: causes redraw()
|
|
|
|
msgListView.setFocusItemIndex(self.initialFocusIndex);
|
|
|
|
} else {
|
|
|
|
msgListView.redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function drawOtherViews(callback) {
|
|
|
|
const messageInfo1Format = self.menuConfig.config.messageInfo1Format || '{msgNumSelected} / {msgNumTotal}';
|
|
|
|
self.setViewText(
|
|
|
|
'allViews',
|
2018-02-01 05:45:03 +00:00
|
|
|
MciViewIds.msgInfo1,
|
|
|
|
stringFormat(messageInfo1Format, { msgNumSelected : self.initialFocusIndex + 1, msgNumTotal : self.config.messageList.length } ));
|
2017-01-26 05:18:05 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
2018-01-15 19:22:11 +00:00
|
|
|
],
|
2017-01-26 05:18:05 +00:00
|
|
|
err => {
|
|
|
|
if(err) {
|
2018-01-15 19:22:11 +00:00
|
|
|
self.client.log.error( { error : err.message }, 'Error loading message list');
|
2016-09-09 03:54:30 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
return cb(err);
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
);
|
2018-01-15 19:22:11 +00:00
|
|
|
});
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
2015-09-01 05:18:46 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
getSaveState() {
|
2018-01-15 19:22:11 +00:00
|
|
|
return { initialFocusIndex : this.initialFocusIndex };
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
2016-07-14 04:44:55 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
restoreSavedState(savedState) {
|
|
|
|
if(savedState) {
|
|
|
|
this.initialFocusIndex = savedState.initialFocusIndex;
|
2018-01-15 19:22:11 +00:00
|
|
|
}
|
2016-07-14 04:44:55 +00:00
|
|
|
}
|
2016-08-31 03:31:24 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
getMenuResult() {
|
|
|
|
return this.menuResult;
|
|
|
|
}
|
2016-08-31 03:31:24 +00:00
|
|
|
};
|