2015-12-31 06:24:26 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2017-09-26 16:43:22 +00:00
|
|
|
const msgArea = require('./message_area.js');
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const FileEntry = require('./file_entry.js');
|
|
|
|
const FileBaseFilters = require('./file_base_filter.js');
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const { getAvailableFileAreaTags } = require('./file_base_area.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',
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* :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
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const MciCodeIds = {
|
2016-01-04 02:40:34 +00:00
|
|
|
ScanStatusLabel : 1, // TL1
|
|
|
|
ScanStatusList : 2, // VM2 (appends)
|
|
|
|
};
|
2015-12-31 06:24:26 +00:00
|
|
|
|
2017-07-18 01:46:38 +00:00
|
|
|
const Steps = {
|
|
|
|
MessageConfs : 'messageConferences',
|
|
|
|
FileBase : 'fileBase',
|
|
|
|
|
|
|
|
Finished : 'finished',
|
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class NewScanModule extends MenuModule {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2015-12-31 06:24:26 +00:00
|
|
|
|
2016-08-31 03:31:24 +00:00
|
|
|
|
2017-07-18 01:46:38 +00:00
|
|
|
this.newScanFullExit = _.get(options, 'lastMenuResult.fullExit', false);
|
|
|
|
|
|
|
|
this.currentStep = Steps.MessageConfs;
|
2017-01-26 05:18:05 +00:00
|
|
|
this.currentScanAux = {};
|
2016-01-04 02:40:34 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
// :TODO: Make this conf/area specific:
|
|
|
|
const config = this.menuConfig.config;
|
|
|
|
this.scanStartFmt = config.scanStartFmt || 'Scanning {confName} - {areaName}...';
|
|
|
|
this.scanFinishNoneFmt = config.scanFinishNoneFmt || 'Nothing new';
|
|
|
|
this.scanFinishNewFmt = config.scanFinishNewFmt || '{count} entries found';
|
|
|
|
this.scanCompleteMsg = config.scanCompleteMsg || 'Finished newscan';
|
|
|
|
}
|
2016-01-04 02:40:34 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
updateScanStatus(statusText) {
|
|
|
|
this.setViewText('allViews', MciCodeIds.ScanStatusLabel, statusText);
|
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
newScanMessageConference(cb) {
|
2016-02-03 04:35:59 +00:00
|
|
|
// lazy init
|
2017-01-26 05:18:05 +00:00
|
|
|
if(!this.sortedMessageConfs) {
|
2016-04-13 04:38:32 +00:00
|
|
|
const getAvailOpts = { includeSystemInternal : true }; // find new private messages, bulletins, etc.
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
this.sortedMessageConfs = _.map(msgArea.getAvailableMessageConferences(this.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
|
|
|
|
//
|
2017-01-26 05:18:05 +00:00
|
|
|
this.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 {
|
2017-02-02 02:42:27 +00:00
|
|
|
return a.conf.name.localeCompare(b.conf.name, { sensitivity : false, numeric : true } );
|
2016-04-13 04:38:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
this.currentScanAux.conf = this.currentScanAux.conf || 0;
|
|
|
|
this.currentScanAux.area = this.currentScanAux.area || 0;
|
2016-04-13 04:38:32 +00:00
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2017-07-18 01:46:38 +00:00
|
|
|
const currentConf = this.sortedMessageConfs[this.currentScanAux.conf];
|
|
|
|
|
|
|
|
this.newScanMessageArea(currentConf, () => {
|
|
|
|
if(this.sortedMessageConfs.length > this.currentScanAux.conf + 1) {
|
|
|
|
this.currentScanAux.conf += 1;
|
|
|
|
this.currentScanAux.area = 0;
|
|
|
|
|
|
|
|
return this.newScanMessageConference(cb); // recursive to next conf
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
2017-07-18 01:46:38 +00:00
|
|
|
|
|
|
|
this.updateScanStatus(this.scanCompleteMsg);
|
|
|
|
return cb(Errors.DoesNotExist('No more conferences'));
|
|
|
|
});
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
2016-04-13 04:38:32 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
newScanMessageArea(conf, cb) {
|
2016-02-03 04:35:59 +00:00
|
|
|
// :TODO: it would be nice to cache this - must be done by conf!
|
2017-01-26 05:18:05 +00:00
|
|
|
const sortedAreas = msgArea.getSortedAvailMessageAreasByConfTag(conf.confTag, { client : this.client } );
|
|
|
|
const currentArea = sortedAreas[this.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.
|
|
|
|
//
|
2017-01-26 05:18:05 +00:00
|
|
|
const self = this;
|
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);
|
2017-07-18 01:46:38 +00:00
|
|
|
return callback(Errors.DoesNotExist('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,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
return self.gotoMenu(self.menuConfig.config.newScanMessageList || 'newScanMessageList', nextModuleOpts);
|
2015-12-31 06:24:26 +00:00
|
|
|
}
|
|
|
|
],
|
2017-01-26 05:18:05 +00:00
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2015-12-31 06:24:26 +00:00
|
|
|
);
|
2016-08-31 03:31:24 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 01:46:38 +00:00
|
|
|
newScanFileBase(cb) {
|
|
|
|
// :TODO: add in steps
|
2017-09-26 16:43:22 +00:00
|
|
|
const filterCriteria = {
|
|
|
|
newerThanFileId : FileBaseFilters.getFileBaseLastViewedFileIdByUser(this.client.user),
|
|
|
|
areaTag : getAvailableFileAreaTags(this.client),
|
2018-01-12 04:39:14 +00:00
|
|
|
order : 'ascending', // oldest first
|
2017-09-26 16:43:22 +00:00
|
|
|
};
|
|
|
|
|
2017-07-18 01:46:38 +00:00
|
|
|
FileEntry.findFiles(
|
2017-09-26 16:43:22 +00:00
|
|
|
filterCriteria,
|
2017-07-18 01:46:38 +00:00
|
|
|
(err, fileIds) => {
|
|
|
|
if(err || 0 === fileIds.length) {
|
|
|
|
return cb(err ? err : Errors.DoesNotExist('No more new files'));
|
|
|
|
}
|
|
|
|
|
2018-01-12 04:39:14 +00:00
|
|
|
FileBaseFilters.setFileBaseLastViewedFileIdForUser( this.client.user, fileIds[fileIds.length - 1] );
|
2017-07-18 01:46:38 +00:00
|
|
|
|
|
|
|
const menuOpts = {
|
|
|
|
extraArgs : {
|
2018-01-12 04:39:14 +00:00
|
|
|
fileList : fileIds,
|
2017-07-18 01:46:38 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.gotoMenu(this.menuConfig.config.newScanFileBaseList || 'newScanFileBaseList', menuOpts);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
getSaveState() {
|
|
|
|
return {
|
|
|
|
currentStep : this.currentStep,
|
|
|
|
currentScanAux : this.currentScanAux,
|
|
|
|
};
|
|
|
|
}
|
2016-08-31 03:31:24 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
restoreSavedState(savedState) {
|
|
|
|
this.currentStep = savedState.currentStep;
|
|
|
|
this.currentScanAux = savedState.currentScanAux;
|
|
|
|
}
|
2015-12-31 06:24:26 +00:00
|
|
|
|
2017-07-18 01:46:38 +00:00
|
|
|
performScanCurrentStep(cb) {
|
|
|
|
switch(this.currentStep) {
|
|
|
|
case Steps.MessageConfs :
|
|
|
|
this.newScanMessageConference( () => {
|
|
|
|
this.currentStep = Steps.FileBase;
|
|
|
|
return this.performScanCurrentStep(cb);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Steps.FileBase :
|
|
|
|
this.newScanFileBase( () => {
|
|
|
|
this.currentStep = Steps.Finished;
|
|
|
|
return this.performScanCurrentStep(cb);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
default : return cb(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
mciReady(mciData, cb) {
|
|
|
|
if(this.newScanFullExit) {
|
|
|
|
// user has canceled the entire scan @ message list view
|
|
|
|
return cb(null);
|
|
|
|
}
|
2016-01-03 01:35:13 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
super.mciReady(mciData, err => {
|
2016-01-03 01:35:13 +00:00
|
|
|
if(err) {
|
2017-01-26 05:18:05 +00:00
|
|
|
return cb(err);
|
2016-01-03 01:35:13 +00:00
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
|
|
|
|
const self = this;
|
|
|
|
const vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
|
|
|
|
|
|
|
|
// :TODO: display scan step/etc.
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadFromConfig(callback) {
|
|
|
|
const loadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu,
|
|
|
|
noInput : true,
|
|
|
|
};
|
|
|
|
|
|
|
|
vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
},
|
|
|
|
function performCurrentStepScan(callback) {
|
2017-07-18 01:46:38 +00:00
|
|
|
return self.performScanCurrentStep(callback);
|
2017-01-26 05:18:05 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.error( { error : err.toString() }, 'Error during new scan');
|
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2015-12-31 06:24:26 +00:00
|
|
|
};
|