2015-08-26 05:17:09 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-07-14 04:44:55 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('../core/menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('../core/view_controller.js').ViewController;
|
|
|
|
const messageArea = require('../core/message_area.js');
|
2016-09-05 03:36:26 +00:00
|
|
|
const stringFormat = require('../core/string_format.js');
|
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
|
2016-09-09 03:54:30 +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.getModule = MessageListModule;
|
|
|
|
|
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'Message List',
|
|
|
|
desc : 'Module for listing/browsing available messages',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
2016-09-09 03:54:30 +00:00
|
|
|
const MCICodesIDs = {
|
|
|
|
MsgList : 1, // VM1
|
|
|
|
MsgInfo1 : 2, // TL2
|
2015-10-11 22:05:45 +00:00
|
|
|
};
|
|
|
|
|
2015-08-26 05:17:09 +00:00
|
|
|
function MessageListModule(options) {
|
|
|
|
MenuModule.call(this, options);
|
|
|
|
|
2016-07-14 04:44:55 +00:00
|
|
|
const self = this;
|
|
|
|
const config = this.menuConfig.config;
|
2015-09-04 06:02:28 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
this.messageAreaTag = config.messageAreaTag;
|
2016-01-04 00:59:29 +00:00
|
|
|
|
|
|
|
if(options.extraArgs) {
|
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
// |extraArgs| can override |messageAreaTag| provided by config
|
2016-01-04 00:59:29 +00:00
|
|
|
// as well as supply a pre-defined message list
|
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
if(options.extraArgs.messageAreaTag) {
|
|
|
|
this.messageAreaTag = options.extraArgs.messageAreaTag;
|
2016-01-04 00:59:29 +00:00
|
|
|
}
|
2015-09-04 06:02:28 +00:00
|
|
|
|
2016-01-04 00:59:29 +00:00
|
|
|
if(options.extraArgs.messageList) {
|
|
|
|
this.messageList = options.extraArgs.messageList;
|
|
|
|
}
|
|
|
|
}
|
2015-08-31 03:42:24 +00:00
|
|
|
|
|
|
|
this.menuMethods = {
|
2016-07-25 20:35:58 +00:00
|
|
|
selectMessage : function(formData, extraArgs, cb) {
|
2015-08-31 03:42:24 +00:00
|
|
|
if(1 === formData.submitId) {
|
2016-07-14 04:44:55 +00:00
|
|
|
self.initialFocusIndex = formData.value.message;
|
|
|
|
|
|
|
|
const modOpts = {
|
2015-09-06 21:58:58 +00:00
|
|
|
extraArgs : {
|
2016-02-03 04:35:59 +00:00
|
|
|
messageAreaTag : self.messageAreaTag,
|
2015-09-06 21:58:58 +00:00
|
|
|
messageList : self.messageList,
|
2015-09-07 06:12:01 +00:00
|
|
|
messageIndex : formData.value.message,
|
2015-09-06 21:58:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-24 17:47:34 +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
|
|
|
|
//
|
|
|
|
modOpts.extraArgs.toJSON = function() {
|
|
|
|
const logMsgList = (this.messageList.length <= 4) ?
|
|
|
|
this.messageList :
|
|
|
|
this.messageList.slice(0, 2).concat(this.messageList.slice(-2));
|
|
|
|
|
|
|
|
return {
|
|
|
|
messageAreaTag : this.messageAreaTag,
|
|
|
|
apprevMessageList : logMsgList,
|
|
|
|
messageCount : this.messageList.length,
|
|
|
|
messageIndex : formData.value.message,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-07-25 20:35:58 +00:00
|
|
|
return self.gotoMenu(config.menuViewPost || 'messageAreaViewPost', modOpts, cb);
|
|
|
|
} else {
|
|
|
|
return cb(null);
|
2015-08-31 03:42:24 +00:00
|
|
|
}
|
2016-08-31 03:31:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fullExit : function(formData, extraArgs, cb) {
|
|
|
|
self.menuResult = { fullExit : true };
|
|
|
|
return self.prevMenu(cb);
|
2015-08-31 03:42:24 +00:00
|
|
|
}
|
|
|
|
};
|
2015-10-11 22:05:45 +00:00
|
|
|
|
|
|
|
this.setViewText = function(id, text) {
|
2016-07-14 04:44:55 +00:00
|
|
|
const v = self.viewControllers.allViews.getView(id);
|
2015-10-11 22:05:45 +00:00
|
|
|
if(v) {
|
|
|
|
v.setText(text);
|
|
|
|
}
|
|
|
|
};
|
2015-08-26 05:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(MessageListModule, MenuModule);
|
|
|
|
|
2016-08-04 01:48:45 +00:00
|
|
|
require('../core/mod_mixins.js').MessageAreaConfTempSwitcher.call(MessageListModule.prototype);
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
MessageListModule.prototype.enter = function() {
|
|
|
|
MessageListModule.super_.prototype.enter.call(this);
|
2015-09-04 06:02:28 +00:00
|
|
|
|
2016-01-04 00:59:29 +00:00
|
|
|
//
|
2016-02-03 04:35:59 +00:00
|
|
|
// Config can specify |messageAreaTag| else it comes from
|
2016-01-04 00:59:29 +00:00
|
|
|
// the user's current area
|
|
|
|
//
|
2016-07-24 17:47:34 +00:00
|
|
|
if(this.messageAreaTag) {
|
2016-08-04 01:48:45 +00:00
|
|
|
this.tempMessageConfAndAreaSwitch(this.messageAreaTag);
|
2016-07-24 17:47:34 +00:00
|
|
|
} else {
|
2016-09-29 03:54:25 +00:00
|
|
|
this.messageAreaTag = this.client.user.properties.message_area_tag;
|
2015-09-04 06:02:28 +00:00
|
|
|
}
|
|
|
|
};
|
2016-07-24 17:47:34 +00:00
|
|
|
|
|
|
|
MessageListModule.prototype.leave = function() {
|
2016-08-04 01:48:45 +00:00
|
|
|
this.tempMessageConfAndAreaRestore();
|
2016-07-24 17:47:34 +00:00
|
|
|
|
|
|
|
MessageListModule.super_.prototype.leave.call(this);
|
|
|
|
};
|
2015-09-04 06:02:28 +00:00
|
|
|
|
2015-08-27 05:04:04 +00:00
|
|
|
MessageListModule.prototype.mciReady = function(mciData, cb) {
|
2016-07-14 04:44:55 +00:00
|
|
|
const self = this;
|
|
|
|
const vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2015-08-31 03:42:24 +00:00
|
|
|
async.series(
|
2015-08-27 05:04:04 +00:00
|
|
|
[
|
|
|
|
function callParentMciReady(callback) {
|
2015-10-12 05:26:27 +00:00
|
|
|
MessageListModule.super_.prototype.mciReady.call(self, mciData, callback);
|
2015-08-27 05:04:04 +00:00
|
|
|
},
|
|
|
|
function loadFromConfig(callback) {
|
2016-09-09 03:54:30 +00:00
|
|
|
const loadOpts = {
|
2015-08-27 05:04:04 +00:00
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu
|
|
|
|
};
|
|
|
|
|
2016-09-09 03:54:30 +00:00
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
2015-08-27 05:25:49 +00:00
|
|
|
},
|
|
|
|
function fetchMessagesInArea(callback) {
|
2016-01-04 00:59:29 +00:00
|
|
|
//
|
|
|
|
// Config can supply messages else we'll need to populate the list now
|
|
|
|
//
|
|
|
|
if(_.isArray(self.messageList)) {
|
2016-09-09 03:54:30 +00:00
|
|
|
return callback(0 === self.messageList.length ? new Error('No messages in area') : null);
|
2016-01-04 00:59:29 +00:00
|
|
|
}
|
2016-09-09 03:54:30 +00:00
|
|
|
|
|
|
|
messageArea.getMessageListForArea( { client : self.client }, self.messageAreaTag, function msgs(err, msgList) {
|
|
|
|
if(!msgList || 0 === msgList.length) {
|
|
|
|
return callback(new Error('No messages in area'));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.messageList = msgList;
|
|
|
|
return callback(err);
|
|
|
|
});
|
2015-08-28 04:20:24 +00:00
|
|
|
},
|
2015-10-22 21:44:44 +00:00
|
|
|
function getLastReadMesageId(callback) {
|
2016-02-03 04:35:59 +00:00
|
|
|
messageArea.getMessageAreaLastReadId(self.client.user.userId, self.messageAreaTag, function lastRead(err, lastReadId) {
|
2015-10-22 21:44:44 +00:00
|
|
|
self.lastReadId = lastReadId || 0;
|
2016-09-09 03:54:30 +00:00
|
|
|
return callback(null); // ignore any errors, e.g. missing value
|
2015-10-22 21:44:44 +00:00
|
|
|
});
|
|
|
|
},
|
2016-07-14 04:44:55 +00:00
|
|
|
function updateMessageListObjects(callback) {
|
|
|
|
const dateTimeFormat = self.menuConfig.config.dateTimeFormat || 'ddd MMM Do';
|
|
|
|
const newIndicator = self.menuConfig.config.newIndicator || '*';
|
|
|
|
const regIndicator = new Array(newIndicator.length + 1).join(' '); // fill with space to avoid draw issues
|
|
|
|
|
|
|
|
let msgNum = 1;
|
|
|
|
self.messageList.forEach( (listItem, index) => {
|
|
|
|
listItem.msgNum = msgNum++;
|
|
|
|
listItem.ts = moment(listItem.modTimestamp).format(dateTimeFormat);
|
|
|
|
listItem.newIndicator = listItem.messageId > self.lastReadId ? newIndicator : regIndicator;
|
|
|
|
|
|
|
|
if(_.isUndefined(self.initialFocusIndex) && listItem.messageId > self.lastReadId) {
|
|
|
|
self.initialFocusIndex = index;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return callback(null);
|
|
|
|
},
|
2015-08-31 03:42:24 +00:00
|
|
|
function populateList(callback) {
|
2016-09-09 03:54:30 +00:00
|
|
|
const msgListView = vc.getView(MCICodesIDs.MsgList);
|
|
|
|
const listFormat = self.menuConfig.config.listFormat || '{msgNum} - {subject} - {toUserName}';
|
|
|
|
const focusListFormat = self.menuConfig.config.focusListFormat || listFormat; // :TODO: default change color here
|
|
|
|
const messageInfo1Format = self.menuConfig.config.messageInfo1Format || '{msgNumSelected} / {msgNumTotal}';
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2016-07-14 04:44:55 +00:00
|
|
|
// :TODO: This can take a very long time to load large lists. What we need is to implement the "owner draw" concept in
|
|
|
|
// which items are requested (e.g. their format at least) *as-needed* vs trying to get the format for all of them at once
|
2015-10-19 03:24:13 +00:00
|
|
|
|
2016-07-14 04:44:55 +00:00
|
|
|
msgListView.setItems(_.map(self.messageList, listEntry => {
|
2016-09-05 03:36:26 +00:00
|
|
|
return stringFormat(listFormat, listEntry);
|
2015-08-28 04:20:24 +00:00
|
|
|
}));
|
|
|
|
|
2016-07-14 04:44:55 +00:00
|
|
|
msgListView.setFocusItems(_.map(self.messageList, listEntry => {
|
2016-09-05 03:36:26 +00:00
|
|
|
return stringFormat(focusListFormat, listEntry);
|
2015-10-21 04:37:39 +00:00
|
|
|
}));
|
2015-10-10 05:35:40 +00:00
|
|
|
|
2016-09-09 03:54:30 +00:00
|
|
|
msgListView.on('index update', idx => {
|
|
|
|
self.setViewText(
|
|
|
|
MCICodesIDs.MsgInfo1,
|
|
|
|
stringFormat(messageInfo1Format, { msgNumSelected : (idx + 1), msgNumTotal : self.messageList.length } ));
|
2015-10-11 22:05:45 +00:00
|
|
|
});
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2016-07-14 04:44:55 +00:00
|
|
|
if(self.initialFocusIndex > 0) {
|
|
|
|
// note: causes redraw()
|
|
|
|
msgListView.setFocusItemIndex(self.initialFocusIndex);
|
|
|
|
} else {
|
|
|
|
msgListView.redraw();
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2015-09-01 05:18:46 +00:00
|
|
|
|
2016-09-09 03:54:30 +00:00
|
|
|
return callback(null);
|
2015-10-11 22:05:45 +00:00
|
|
|
},
|
2016-09-09 03:54:30 +00:00
|
|
|
function drawOtherViews(callback) {
|
|
|
|
const messageInfo1Format = self.menuConfig.config.messageInfo1Format || '{msgNumSelected} / {msgNumTotal}';
|
|
|
|
self.setViewText(
|
|
|
|
MCICodesIDs.MsgInfo1,
|
|
|
|
stringFormat(messageInfo1Format, { msgNumSelected : self.initialFocusIndex + 1, msgNumTotal : self.messageList.length } ));
|
|
|
|
return callback(null);
|
2015-10-11 22:05:45 +00:00
|
|
|
},
|
2016-09-09 03:54:30 +00:00
|
|
|
],
|
|
|
|
err => {
|
2015-08-28 04:20:24 +00:00
|
|
|
if(err) {
|
2016-07-14 04:44:55 +00:00
|
|
|
self.client.log.error( { error : err.message }, 'Error loading message list');
|
2015-08-28 04:20:24 +00:00
|
|
|
}
|
2016-09-09 03:54:30 +00:00
|
|
|
return cb(err);
|
2015-08-27 05:04:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-07-14 04:44:55 +00:00
|
|
|
MessageListModule.prototype.getSaveState = function() {
|
|
|
|
return { initialFocusIndex : this.initialFocusIndex };
|
|
|
|
};
|
|
|
|
|
|
|
|
MessageListModule.prototype.restoreSavedState = function(savedState) {
|
|
|
|
if(savedState) {
|
|
|
|
this.initialFocusIndex = savedState.initialFocusIndex;
|
|
|
|
}
|
2016-08-31 03:31:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MessageListModule.prototype.getMenuResult = function() {
|
|
|
|
return this.menuResult;
|
|
|
|
};
|