2015-12-31 06:24:26 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2016-04-13 04:38:32 +00:00
|
|
|
const msgArea = require('./message_area.js');
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('../core/view_controller.js').ViewController;
|
2015-12-31 06:24:26 +00:00
|
|
|
|
2016-04-13 04:38:32 +00:00
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
2015-12-31 06:24:26 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'New Scan',
|
|
|
|
desc : 'Performs a new scan against various areas of the system',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = NewScanModule;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* :TODO:
|
2016-01-04 02:40:34 +00:00
|
|
|
* * User configurable new scan: Area selection (avail from messages area) (sep module)
|
2016-01-03 01:35:13 +00:00
|
|
|
* * Add status TL/VM (either/both should update if present)
|
|
|
|
* *
|
2015-12-31 06:24:26 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-01-04 02:40:34 +00:00
|
|
|
var MciCodeIds = {
|
|
|
|
ScanStatusLabel : 1, // TL1
|
|
|
|
ScanStatusList : 2, // VM2 (appends)
|
|
|
|
};
|
2015-12-31 06:24:26 +00:00
|
|
|
|
|
|
|
function NewScanModule(options) {
|
|
|
|
MenuModule.call(this, options);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var config = this.menuConfig.config;
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
this.currentStep = 'messageConferences';
|
|
|
|
this.currentScanAux = {};
|
2016-01-04 02:40:34 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
// :TODO: Make this conf/area specific:
|
|
|
|
this.scanStartFmt = config.scanStartFmt || 'Scanning {confName} - {areaName}...';
|
2016-01-04 02:40:34 +00:00
|
|
|
this.scanFinishNoneFmt = config.scanFinishNoneFmt || 'Nothing new';
|
|
|
|
this.scanFinishNewFmt = config.scanFinishNewFmt || '{count} entries found';
|
|
|
|
this.scanCompleteMsg = config.scanCompleteMsg || 'Finished newscan';
|
|
|
|
|
|
|
|
this.updateScanStatus = function(statusText) {
|
|
|
|
var vc = self.viewControllers.allViews;
|
|
|
|
|
|
|
|
var view = vc.getView(MciCodeIds.ScanStatusLabel);
|
|
|
|
if(view) {
|
|
|
|
view.setText(statusText);
|
|
|
|
}
|
|
|
|
|
|
|
|
view = vc.getView(MciCodeIds.ScanStatusList);
|
|
|
|
// :TODO: MenuView needs appendItem()
|
|
|
|
if(view) {
|
|
|
|
}
|
|
|
|
};
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2016-04-13 04:38:32 +00:00
|
|
|
this.newScanMessageConference = function(cb) {
|
2016-02-03 04:35:59 +00:00
|
|
|
// lazy init
|
2016-04-13 04:38:32 +00:00
|
|
|
if(!self.sortedMessageConfs) {
|
|
|
|
const getAvailOpts = { includeSystemInternal : true }; // find new private messages, bulletins, etc.
|
2016-02-03 04:35:59 +00:00
|
|
|
|
|
|
|
self.sortedMessageConfs = _.map(msgArea.getAvailableMessageConferences(self.client, getAvailOpts), (v, k) => {
|
2016-04-13 04:38:32 +00:00
|
|
|
return {
|
|
|
|
confTag : k,
|
|
|
|
conf : v,
|
|
|
|
};
|
|
|
|
});
|
2016-02-03 04:35:59 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Sort conferences by name, other than 'system_internal' which should
|
|
|
|
// always come first such that we display private mails/etc. before
|
|
|
|
// other conferences & areas
|
|
|
|
//
|
2016-04-13 04:38:32 +00:00
|
|
|
self.sortedMessageConfs.sort((a, b) => {
|
2016-02-03 04:35:59 +00:00
|
|
|
if('system_internal' === a.confTag) {
|
2016-04-13 04:38:32 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return a.conf.name.localeCompare(b.conf.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.currentScanAux.conf = self.currentScanAux.conf || 0;
|
|
|
|
self.currentScanAux.area = self.currentScanAux.area || 0;
|
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2016-04-13 04:38:32 +00:00
|
|
|
const currentConf = self.sortedMessageConfs[self.currentScanAux.conf];
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2016-04-13 04:38:32 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function scanArea(callback) {
|
|
|
|
//self.currentScanAux.area = self.currentScanAux.area || 0;
|
|
|
|
|
|
|
|
self.newScanMessageArea(currentConf, () => {
|
|
|
|
if(self.sortedMessageConfs.length > self.currentScanAux.conf + 1) {
|
|
|
|
self.currentScanAux.conf += 1;
|
|
|
|
self.currentScanAux.area = 0;
|
|
|
|
|
|
|
|
self.newScanMessageConference(cb); // recursive to next conf
|
|
|
|
//callback(null);
|
|
|
|
} else {
|
|
|
|
self.updateScanStatus(self.scanCompleteMsg);
|
|
|
|
callback(new Error('No more conferences'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
cb
|
2016-02-03 04:35:59 +00:00
|
|
|
);
|
2016-04-13 04:38:32 +00:00
|
|
|
};
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
this.newScanMessageArea = function(conf, cb) {
|
|
|
|
// :TODO: it would be nice to cache this - must be done by conf!
|
2016-04-13 04:38:32 +00:00
|
|
|
const sortedAreas = msgArea.getSortedAvailMessageAreasByConfTag(conf.confTag, { client : self.client } );
|
2016-02-03 04:35:59 +00:00
|
|
|
const currentArea = sortedAreas[self.currentScanAux.area];
|
2016-01-03 01:35:13 +00:00
|
|
|
|
2016-04-13 04:38:32 +00:00
|
|
|
function getFormatObj() {
|
|
|
|
return {
|
|
|
|
confName : conf.conf.name,
|
|
|
|
confDesc : conf.conf.desc,
|
|
|
|
areaName : currentArea.area.name,
|
|
|
|
areaDesc : currentArea.area.desc
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-01-03 01:35:13 +00:00
|
|
|
//
|
|
|
|
// Scan and update index until we find something. If results are found,
|
|
|
|
// we'll goto the list module & show them.
|
|
|
|
//
|
2015-12-31 06:24:26 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
2016-01-03 01:35:13 +00:00
|
|
|
function checkAndUpdateIndex(callback) {
|
|
|
|
// Advance to next area if possible
|
2016-02-03 04:35:59 +00:00
|
|
|
if(sortedAreas.length >= self.currentScanAux.area + 1) {
|
|
|
|
self.currentScanAux.area += 1;
|
2016-01-03 01:35:13 +00:00
|
|
|
callback(null);
|
|
|
|
} else {
|
2016-01-04 02:40:34 +00:00
|
|
|
self.updateScanStatus(self.scanCompleteMsg);
|
2016-01-03 01:35:13 +00:00
|
|
|
callback(new Error('No more areas'));
|
|
|
|
}
|
|
|
|
},
|
2016-01-04 02:40:34 +00:00
|
|
|
function updateStatusScanStarted(callback) {
|
2016-04-13 04:38:32 +00:00
|
|
|
self.updateScanStatus(self.scanStartFmt.format(getFormatObj()));
|
2016-01-03 01:35:13 +00:00
|
|
|
callback(null);
|
|
|
|
},
|
2015-12-31 06:24:26 +00:00
|
|
|
function newScanAreaAndGetMessages(callback) {
|
|
|
|
msgArea.getNewMessagesInAreaForUser(
|
2016-02-03 04:35:59 +00:00
|
|
|
self.client.user.userId, currentArea.areaTag, function msgs(err, msgList) {
|
2016-01-04 02:40:34 +00:00
|
|
|
if(!err) {
|
|
|
|
if(0 === msgList.length) {
|
2016-04-13 04:38:32 +00:00
|
|
|
self.updateScanStatus(self.scanFinishNoneFmt.format(getFormatObj()));
|
2016-01-04 02:40:34 +00:00
|
|
|
} else {
|
2016-04-13 04:38:32 +00:00
|
|
|
const formatObj = Object.assign(getFormatObj(), { count : msgList.length } );
|
|
|
|
self.updateScanStatus(self.scanFinishNewFmt.format(formatObj));
|
2016-01-04 02:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-31 06:24:26 +00:00
|
|
|
callback(err, msgList);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2016-04-13 04:38:32 +00:00
|
|
|
function displayMessageList(msgList) {
|
2015-12-31 06:24:26 +00:00
|
|
|
if(msgList && msgList.length > 0) {
|
|
|
|
var nextModuleOpts = {
|
|
|
|
extraArgs: {
|
2016-02-03 04:35:59 +00:00
|
|
|
messageAreaTag : currentArea.areaTag,
|
2015-12-31 06:24:26 +00:00
|
|
|
messageList : msgList,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.gotoMenu(config.newScanMessageList || 'newScanMessageList', nextModuleOpts);
|
|
|
|
} else {
|
2016-02-03 04:35:59 +00:00
|
|
|
self.newScanMessageArea(conf, cb);
|
2015-12-31 06:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2016-04-13 04:38:32 +00:00
|
|
|
cb // no more areas
|
2015-12-31 06:24:26 +00:00
|
|
|
);
|
|
|
|
};
|
2016-01-03 01:35:13 +00:00
|
|
|
|
2015-12-31 06:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(NewScanModule, MenuModule);
|
|
|
|
|
|
|
|
NewScanModule.prototype.getSaveState = function() {
|
|
|
|
return {
|
2016-01-03 01:35:13 +00:00
|
|
|
currentStep : this.currentStep,
|
|
|
|
currentScanAux : this.currentScanAux,
|
2015-12-31 06:24:26 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
NewScanModule.prototype.restoreSavedState = function(savedState) {
|
2016-01-03 01:35:13 +00:00
|
|
|
this.currentStep = savedState.currentStep;
|
|
|
|
this.currentScanAux = savedState.currentScanAux;
|
2015-12-31 06:24:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NewScanModule.prototype.mciReady = function(mciData, cb) {
|
|
|
|
|
2016-01-03 01:35:13 +00:00
|
|
|
var self = this;
|
|
|
|
var vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
|
2015-12-31 06:24:26 +00:00
|
|
|
|
|
|
|
// :TODO: display scan step/etc.
|
|
|
|
|
2016-01-03 01:35:13 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function callParentMciReady(callback) {
|
|
|
|
NewScanModule.super_.prototype.mciReady.call(self, mciData, callback);
|
|
|
|
},
|
|
|
|
function loadFromConfig(callback) {
|
2016-04-13 04:38:32 +00:00
|
|
|
const loadOpts = {
|
2016-01-03 01:35:13 +00:00
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu,
|
|
|
|
noInput : true,
|
|
|
|
};
|
|
|
|
|
|
|
|
vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
},
|
|
|
|
function performCurrentStepScan(callback) {
|
|
|
|
switch(self.currentStep) {
|
2016-02-03 04:35:59 +00:00
|
|
|
case 'messageConferences' :
|
2016-04-13 04:38:32 +00:00
|
|
|
self.newScanMessageConference( () => {
|
|
|
|
callback(null); // finished
|
|
|
|
});
|
|
|
|
break;
|
2016-01-03 01:35:13 +00:00
|
|
|
|
2016-04-13 04:38:32 +00:00
|
|
|
default : return callback(null);
|
2016-01-03 01:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.error( { error : err.toString() }, 'Error during new scan');
|
|
|
|
}
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
2015-12-31 06:24:26 +00:00
|
|
|
};
|