2015-08-26 05:17:09 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const messageArea = require('./message_area.js');
|
|
|
|
const MessageAreaConfTempSwitcher = require('./mod_mixins.js').MessageAreaConfTempSwitcher;
|
2018-07-05 00:49:03 +00:00
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const Message = require('./message.js');
|
2018-11-24 00:41:16 +00:00
|
|
|
const UserProps = require('./user_property.js');
|
2018-06-23 03:26:46 +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
|
|
|
/*
|
2018-07-09 01:20:08 +00:00
|
|
|
Available itemFormat/focusItemFormat members for |msgList|
|
2015-10-19 03:24:13 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
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)
|
2015-10-19 03:24:13 +00:00
|
|
|
*/
|
2015-08-26 05:17:09 +00:00
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Message List',
|
|
|
|
desc : 'Module for listing/browsing available messages',
|
|
|
|
author : 'NuSkooler',
|
2015-08-26 05:17:09 +00:00
|
|
|
};
|
|
|
|
|
2018-07-05 00:49:03 +00:00
|
|
|
const FormIds = {
|
|
|
|
allViews : 0,
|
|
|
|
delPrompt : 1,
|
|
|
|
};
|
|
|
|
|
2018-02-01 05:45:03 +00:00
|
|
|
const MciViewIds = {
|
2018-07-05 00:49:03 +00:00
|
|
|
allViews : {
|
|
|
|
msgList : 1, // VM1 - see above
|
|
|
|
delPromptXy : 2, // %XY2, e.g: delete confirmation
|
|
|
|
customRangeStart : 10, // Everything |msgList| has plus { msgNumSelected, msgNumTotal }
|
|
|
|
},
|
|
|
|
|
|
|
|
delPrompt: {
|
|
|
|
prompt : 1,
|
|
|
|
}
|
2015-10-11 22:05:45 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class MessageListModule extends MessageAreaConfTempSwitcher(MenuModule) {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: consider this pattern in base MenuModule - clean up code all over
|
|
|
|
this.config = Object.assign({}, _.get(options, 'menuConfig.config'), options.extraArgs);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
this.lastMessageReachedExit = _.get(options, 'lastMenuResult.lastMessageReached', false);
|
|
|
|
|
|
|
|
this.menuMethods = {
|
|
|
|
selectMessage : (formData, extraArgs, cb) => {
|
2018-07-05 00:49:03 +00:00
|
|
|
if(MciViewIds.allViews.msgList === formData.submitId) {
|
2020-07-05 23:50:48 +00:00
|
|
|
this.initialFocusIndex = formData.value.messageIndex ||
|
|
|
|
formData.value.message; // older deprecated arg name
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const modOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
extraArgs : {
|
2020-07-05 23:50:48 +00:00
|
|
|
messageAreaTag : this.getSelectedAreaTag(this.initialFocusIndex),
|
2018-06-23 03:26:46 +00:00
|
|
|
messageList : this.config.messageList,
|
2020-07-05 23:50:48 +00:00
|
|
|
messageIndex : this.initialFocusIndex,
|
2018-06-23 03:26:46 +00:00
|
|
|
lastMessageNextExit : true,
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(_.isBoolean(this.config.noUpdateLastReadId)) {
|
|
|
|
modOpts.extraArgs.noUpdateLastReadId = this.config.noUpdateLastReadId;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +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-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const self = this;
|
|
|
|
modOpts.extraArgs.toJSON = function() {
|
|
|
|
const logMsgList = (self.config.messageList.length <= 4) ?
|
|
|
|
self.config.messageList :
|
|
|
|
self.config.messageList.slice(0, 2).concat(self.config.messageList.slice(-2));
|
|
|
|
|
|
|
|
return {
|
2018-06-23 03:26:46 +00:00
|
|
|
// note |this| is scope of toJSON()!
|
|
|
|
messageAreaTag : this.messageAreaTag,
|
|
|
|
apprevMessageList : logMsgList,
|
|
|
|
messageCount : this.messageList.length,
|
|
|
|
messageIndex : this.messageIndex,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.gotoMenu(this.config.menuViewPost || 'messageAreaViewPost', modOpts, cb);
|
|
|
|
} else {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
fullExit : (formData, extraArgs, cb) => {
|
|
|
|
this.menuResult = { fullExit : true };
|
|
|
|
return this.prevMenu(cb);
|
2018-07-05 00:49:03 +00:00
|
|
|
},
|
|
|
|
deleteSelected : (formData, extraArgs, cb) => {
|
|
|
|
if(MciViewIds.allViews.msgList != formData.submitId) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
2020-07-05 23:50:48 +00:00
|
|
|
const messageIndex = formData.value.messageIndex ||
|
|
|
|
formData.value.message; // older, deprecated arg name
|
2018-07-05 00:49:03 +00:00
|
|
|
return this.promptDeleteMessageConfirm(messageIndex, cb);
|
|
|
|
},
|
|
|
|
deleteMessageYes : (formData, extraArgs, cb) => {
|
|
|
|
const msgListView = this.viewControllers.allViews.getView(MciViewIds.allViews.msgList);
|
|
|
|
this.enableMessageListIndexUpdates(msgListView);
|
|
|
|
if(this.selectedMessageForDelete) {
|
|
|
|
this.selectedMessageForDelete.deleteMessage(this.client.user, err => {
|
|
|
|
if(err) {
|
|
|
|
this.client.log.error(`Failed to delete message: ${this.selectedMessageForDelete.messageUuid}`);
|
|
|
|
} else {
|
|
|
|
this.client.log.info(`User deleted message: ${this.selectedMessageForDelete.messageUuid}`);
|
|
|
|
this.config.messageList.splice(msgListView.focusedItemIndex, 1);
|
|
|
|
this.updateMessageNumbersAfterDelete(msgListView.focusedItemIndex);
|
|
|
|
msgListView.setItems(this.config.messageList);
|
|
|
|
}
|
|
|
|
this.selectedMessageForDelete = null;
|
|
|
|
msgListView.redraw();
|
2018-07-08 17:33:39 +00:00
|
|
|
this.populateCustomLabelsForSelected(msgListView.focusedItemIndex);
|
2018-07-05 00:49:03 +00:00
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
deleteMessageNo : (formData, extraArgs, cb) => {
|
2018-07-06 02:35:38 +00:00
|
|
|
const msgListView = this.viewControllers.allViews.getView(MciViewIds.allViews.msgList);
|
|
|
|
this.enableMessageListIndexUpdates(msgListView);
|
2018-07-05 00:49:03 +00:00
|
|
|
return cb(null);
|
2018-07-08 02:04:51 +00:00
|
|
|
},
|
|
|
|
markAllRead : (formData, extraArgs, cb) => {
|
|
|
|
if(this.config.noUpdateLastReadId) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.markAllMessagesAsRead(cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getSelectedAreaTag(listIndex) {
|
|
|
|
return this.config.messageList[listIndex].areaTag || this.config.messageAreaTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
enter() {
|
|
|
|
if(this.lastMessageReachedExit) {
|
|
|
|
return this.prevMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
super.enter();
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Config can specify |messageAreaTag| else it comes from
|
|
|
|
// the user's current area. If |messageList| is supplied,
|
|
|
|
// each item is expected to contain |areaTag|, so we use that
|
|
|
|
// instead in those cases.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(!Array.isArray(this.config.messageList)) {
|
|
|
|
if(this.config.messageAreaTag) {
|
|
|
|
this.tempMessageConfAndAreaSwitch(this.config.messageAreaTag);
|
|
|
|
} else {
|
2018-11-24 00:41:16 +00:00
|
|
|
this.config.messageAreaTag = this.client.user.properties[UserProps.MessageAreaTag];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leave() {
|
|
|
|
this.tempMessageConfAndAreaRestore();
|
|
|
|
super.leave();
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:49:03 +00:00
|
|
|
populateCustomLabelsForSelected(selectedIndex) {
|
|
|
|
const formatObj = Object.assign(
|
|
|
|
{
|
|
|
|
msgNumSelected : (selectedIndex + 1),
|
|
|
|
msgNumTotal : this.config.messageList.length,
|
|
|
|
},
|
|
|
|
this.config.messageList[selectedIndex] // plus, all the selected message props
|
|
|
|
);
|
|
|
|
return this.updateCustomViewTextsWithFilter('allViews', MciViewIds.allViews.customRangeStart, formatObj);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const self = this;
|
|
|
|
const vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
|
2018-06-22 05:15:04 +00:00
|
|
|
let configProvidedMessageList = false;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadFromConfig(callback) {
|
|
|
|
const loadOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
},
|
|
|
|
function fetchMessagesInArea(callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Config can supply messages else we'll need to populate the list now
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(_.isArray(self.config.messageList)) {
|
|
|
|
configProvidedMessageList = true;
|
|
|
|
return callback(0 === self.config.messageList.length ? new Error('No messages in area') : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
messageArea.getMessageListForArea(self.client, self.config.messageAreaTag, function msgs(err, msgList) {
|
|
|
|
if(!msgList || 0 === msgList.length) {
|
|
|
|
return callback(new Error('No messages in area'));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.config.messageList = msgList;
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
2019-07-01 01:25:47 +00:00
|
|
|
function getLastReadMessageId(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// messageList entries can contain |isNew| if they want to be considered new
|
2018-06-22 05:15:04 +00:00
|
|
|
if(configProvidedMessageList) {
|
|
|
|
self.lastReadId = 0;
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
messageArea.getMessageAreaLastReadId(self.client.user.userId, self.config.messageAreaTag, function lastRead(err, lastReadId) {
|
|
|
|
self.lastReadId = lastReadId || 0;
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(null); // ignore any errors, e.g. missing value
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function updateMessageListObjects(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const dateTimeFormat = self.menuConfig.config.dateTimeFormat || self.client.currentTheme.helpers.getDateTimeFormat();
|
|
|
|
const newIndicator = self.menuConfig.config.newIndicator || '*';
|
2018-07-05 00:49:03 +00:00
|
|
|
const regIndicator = ' '.repeat(newIndicator.length); // fill with space to avoid draw issues
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
let msgNum = 1;
|
|
|
|
self.config.messageList.forEach( (listItem, index) => {
|
2018-06-23 03:26:46 +00:00
|
|
|
listItem.msgNum = msgNum++;
|
|
|
|
listItem.ts = moment(listItem.modTimestamp).format(dateTimeFormat);
|
|
|
|
const isNew = _.isBoolean(listItem.isNew) ? listItem.isNew : listItem.messageId > self.lastReadId;
|
|
|
|
listItem.newIndicator = isNew ? newIndicator : regIndicator;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(_.isUndefined(self.initialFocusIndex) && listItem.messageId > self.lastReadId) {
|
|
|
|
self.initialFocusIndex = index;
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
listItem.text = `${listItem.msgNum} - ${listItem.subject} from ${listItem.fromUserName}`; // default text
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
return callback(null);
|
|
|
|
},
|
2018-07-05 00:49:03 +00:00
|
|
|
function populateAndDrawViews(callback) {
|
|
|
|
const msgListView = vc.getView(MciViewIds.allViews.msgList);
|
2018-06-22 05:15:04 +00:00
|
|
|
msgListView.setItems(self.config.messageList);
|
2018-07-05 00:49:03 +00:00
|
|
|
self.enableMessageListIndexUpdates(msgListView);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(self.initialFocusIndex > 0) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// note: causes redraw()
|
2018-06-22 05:15:04 +00:00
|
|
|
msgListView.setFocusItemIndex(self.initialFocusIndex);
|
|
|
|
} else {
|
|
|
|
msgListView.redraw();
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:49:03 +00:00
|
|
|
self.populateCustomLabelsForSelected(self.initialFocusIndex || 0);
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.error( { error : err.message }, 'Error loading message list');
|
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getSaveState() {
|
|
|
|
return { initialFocusIndex : this.initialFocusIndex };
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreSavedState(savedState) {
|
|
|
|
if(savedState) {
|
|
|
|
this.initialFocusIndex = savedState.initialFocusIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getMenuResult() {
|
|
|
|
return this.menuResult;
|
|
|
|
}
|
2018-07-05 00:49:03 +00:00
|
|
|
|
|
|
|
enableMessageListIndexUpdates(msgListView) {
|
|
|
|
msgListView.on('index update', idx => this.populateCustomLabelsForSelected(idx) );
|
|
|
|
}
|
|
|
|
|
2018-07-08 02:04:51 +00:00
|
|
|
markAllMessagesAsRead(cb) {
|
|
|
|
if(!this.config.messageList || this.config.messageList.length === 0) {
|
|
|
|
return cb(null); // nothing to do.
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Generally we'll have a message list for a specific area,
|
|
|
|
// but this is not always the case. For a given area, we need
|
|
|
|
// to find the highest message ID in the list to set a
|
|
|
|
// last read pointer.
|
|
|
|
//
|
|
|
|
const areaHighestIds = {};
|
|
|
|
this.config.messageList.forEach(msg => {
|
|
|
|
const highestId = areaHighestIds[msg.areaTag];
|
|
|
|
if(highestId) {
|
|
|
|
if(msg.messageId > highestId) {
|
|
|
|
areaHighestIds[msg.areaTag] = msg.messageId;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
areaHighestIds[msg.areaTag] = msg.messageId;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-08 02:13:24 +00:00
|
|
|
const regIndicator = ' '.repeat( (this.menuConfig.config.newIndicator || '*').length );
|
2018-07-08 02:04:51 +00:00
|
|
|
async.forEachOf(areaHighestIds, (highestId, areaTag, nextArea) => {
|
|
|
|
messageArea.updateMessageAreaLastReadId(
|
|
|
|
this.client.user.userId,
|
|
|
|
areaTag,
|
|
|
|
highestId,
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
this.client.log.warn( { error : err.message }, 'Failed marking area as read');
|
|
|
|
} else {
|
2018-07-08 02:13:24 +00:00
|
|
|
// update newIndicator on messages
|
|
|
|
this.config.messageList.forEach(msg => {
|
|
|
|
if(areaTag === msg.areaTag) {
|
|
|
|
msg.newIndicator = regIndicator;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const msgListView = this.viewControllers.allViews.getView(MciViewIds.allViews.msgList);
|
|
|
|
msgListView.setItems(this.config.messageList);
|
|
|
|
msgListView.redraw();
|
2018-07-08 02:04:51 +00:00
|
|
|
this.client.log.info( { highestId, areaTag }, 'User marked area as read');
|
|
|
|
}
|
|
|
|
return nextArea(null); // always continue
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, () => {
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:49:03 +00:00
|
|
|
updateMessageNumbersAfterDelete(startIndex) {
|
|
|
|
// all index -= 1 from this point on.
|
|
|
|
for(let i = startIndex; i < this.config.messageList.length; ++i) {
|
|
|
|
const msgItem = this.config.messageList[i];
|
|
|
|
msgItem.msgNum -= 1;
|
|
|
|
msgItem.text = `${msgItem.msgNum} - ${msgItem.subject} from ${msgItem.fromUserName}`; // default text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
promptDeleteMessageConfirm(messageIndex, cb) {
|
|
|
|
const messageInfo = this.config.messageList[messageIndex];
|
|
|
|
if(!_.isObject(messageInfo)) {
|
|
|
|
return cb(Errors.Invalid(`Invalid message index: ${messageIndex}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
// :TODO: create static userHasDeleteRights() that takes id || uuid that doesn't require full msg load
|
|
|
|
this.selectedMessageForDelete = new Message();
|
|
|
|
this.selectedMessageForDelete.load( { uuid : messageInfo.messageUuid }, err => {
|
|
|
|
if(err) {
|
|
|
|
this.selectedMessageForDelete = null;
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!this.selectedMessageForDelete.userHasDeleteRights(this.client.user)) {
|
|
|
|
this.selectedMessageForDelete = null;
|
|
|
|
return cb(Errors.AccessDenied('User does not have rights to delete this message'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// user has rights to delete -- prompt/confirm then proceed
|
|
|
|
return this.promptConfirmDelete(cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
promptConfirmDelete(cb) {
|
|
|
|
const promptXyView = this.viewControllers.allViews.getView(MciViewIds.allViews.delPromptXy);
|
|
|
|
if(!promptXyView) {
|
|
|
|
return cb(Errors.MissingMci(`Missing prompt XY${MciViewIds.allViews.delPromptXy} MCI`));
|
|
|
|
}
|
|
|
|
|
|
|
|
const promptOpts = {
|
|
|
|
clearAtSubmit : true,
|
|
|
|
};
|
|
|
|
if(promptXyView.dimens.width) {
|
|
|
|
promptOpts.clearWidth = promptXyView.dimens.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.promptForInput(
|
|
|
|
{
|
|
|
|
formName : 'delPrompt',
|
|
|
|
formId : FormIds.delPrompt,
|
|
|
|
promptName : this.config.deleteMessageFromListPrompt || 'deleteMessageFromListPrompt',
|
|
|
|
prevFormName : 'allViews',
|
|
|
|
position : promptXyView.position,
|
|
|
|
},
|
|
|
|
promptOpts,
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-08-31 03:31:24 +00:00
|
|
|
};
|