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;
|
2016-09-05 03:36:26 +00:00
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const stringFormat = require('./string_format.js');
|
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-08-31 03:31:24 +00:00
|
|
|
this.newScanFullExit = _.has(options, 'lastMenuResult.fullExit') ? options.lastMenuResult.fullExit : false;
|
|
|
|
|
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-09-05 03:36:26 +00:00
|
|
|
|
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-07-24 17:47:34 +00:00
|
|
|
return callback(null);
|
2016-01-03 01:35:13 +00:00
|
|
|
} else {
|
2016-01-04 02:40:34 +00:00
|
|
|
self.updateScanStatus(self.scanCompleteMsg);
|
2016-07-24 17:47:34 +00:00
|
|
|
return callback(new Error('No more areas')); // this will stop our scan
|
2016-01-03 01:35:13 +00:00
|
|
|
}
|
|
|
|
},
|
2016-01-04 02:40:34 +00:00
|
|
|
function updateStatusScanStarted(callback) {
|
2016-09-05 03:36:26 +00:00
|
|
|
self.updateScanStatus(stringFormat(self.scanStartFmt, {
|
|
|
|
confName : conf.conf.name,
|
|
|
|
confDesc : conf.conf.desc,
|
|
|
|
areaName : currentArea.area.name,
|
|
|
|
areaDesc : currentArea.area.desc
|
|
|
|
}));
|
2016-07-24 17:47:34 +00:00
|
|
|
return callback(null);
|
2016-01-03 01:35:13 +00:00
|
|
|
},
|
2016-07-24 17:47:34 +00:00
|
|
|
function getNewMessagesCountInArea(callback) {
|
|
|
|
msgArea.getNewMessageCountInAreaForUser(
|
|
|
|
self.client.user.userId, currentArea.areaTag, (err, newMessageCount) => {
|
|
|
|
callback(err, newMessageCount);
|
2015-12-31 06:24:26 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2016-07-24 17:47:34 +00:00
|
|
|
function displayMessageList(newMessageCount) {
|
|
|
|
if(newMessageCount <= 0) {
|
|
|
|
return self.newScanMessageArea(conf, cb); // next area, if any
|
2015-12-31 06:24:26 +00:00
|
|
|
}
|
2016-07-24 17:47:34 +00:00
|
|
|
|
|
|
|
const nextModuleOpts = {
|
|
|
|
extraArgs: {
|
|
|
|
messageAreaTag : currentArea.areaTag,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return self.gotoMenu(config.newScanMessageList || 'newScanMessageList', nextModuleOpts);
|
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-08-31 03:31:24 +00:00
|
|
|
if(this.newScanFullExit) {
|
|
|
|
// user has canceled the entire scan @ message list view
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-08-04 04:43:56 +00:00
|
|
|
case 'messageConferences' :
|
|
|
|
self.newScanMessageConference( () => {
|
|
|
|
callback(null); // finished
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
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
|
|
|
};
|