2015-12-31 06:24:26 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
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');
|
2018-07-07 21:20:45 +00:00
|
|
|
const { valueAsArray } = require('./misc_util.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
2015-12-31 06:24:26 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'New Scan',
|
|
|
|
desc : 'Performs a new scan against various areas of the system',
|
|
|
|
author : 'NuSkooler',
|
2015-12-31 06:24:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* :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)
|
2018-01-15 19:22:11 +00:00
|
|
|
* *
|
|
|
|
|
2015-12-31 06:24:26 +00:00
|
|
|
*/
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const MciCodeIds = {
|
2018-06-23 03:26:46 +00:00
|
|
|
ScanStatusLabel : 1, // TL1
|
|
|
|
ScanStatusList : 2, // VM2 (appends)
|
2016-01-04 02:40:34 +00:00
|
|
|
};
|
2015-12-31 06:24:26 +00:00
|
|
|
|
2017-07-18 01:46:38 +00:00
|
|
|
const Steps = {
|
2018-06-23 03:26:46 +00:00
|
|
|
MessageConfs : 'messageConferences',
|
|
|
|
FileBase : 'fileBase',
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
Finished : 'finished',
|
2017-07-18 01:46:38 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class NewScanModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.newScanFullExit = _.get(options, 'lastMenuResult.fullExit', false);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.currentStep = Steps.MessageConfs;
|
|
|
|
this.currentScanAux = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
// :TODO: Make this conf/area specific:
|
2018-07-07 21:20:45 +00:00
|
|
|
// :TODO: Use newer custom info format - TL10+
|
2018-06-23 03:26:46 +00:00
|
|
|
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';
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateScanStatus(statusText) {
|
|
|
|
this.setViewText('allViews', MciCodeIds.ScanStatusLabel, statusText);
|
|
|
|
}
|
|
|
|
|
|
|
|
newScanMessageConference(cb) {
|
|
|
|
// lazy init
|
|
|
|
if(!this.sortedMessageConfs) {
|
|
|
|
const getAvailOpts = { includeSystemInternal : true }; // find new private messages, bulletins, etc.
|
|
|
|
|
|
|
|
this.sortedMessageConfs = _.map(msgArea.getAvailableMessageConferences(this.client, getAvailOpts), (v, k) => {
|
|
|
|
return {
|
|
|
|
confTag : k,
|
|
|
|
conf : v,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +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
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
this.sortedMessageConfs.sort((a, b) => {
|
|
|
|
if('system_internal' === a.confTag) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return a.conf.name.localeCompare(b.conf.name, { sensitivity : false, numeric : true } );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.currentScanAux.conf = this.currentScanAux.conf || 0;
|
|
|
|
this.currentScanAux.area = this.currentScanAux.area || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateScanStatus(this.scanCompleteMsg);
|
|
|
|
return cb(Errors.DoesNotExist('No more conferences'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
newScanMessageArea(conf, cb) {
|
|
|
|
// :TODO: it would be nice to cache this - must be done by conf!
|
2018-07-07 21:20:45 +00:00
|
|
|
const omitMessageAreaTags = valueAsArray(_.get(this, 'menuConfig.config.omitMessageAreaTags', []));
|
|
|
|
const sortedAreas = _.omitBy(
|
|
|
|
msgArea.getSortedAvailMessageAreasByConfTag(conf.confTag, { client : this.client } ),
|
|
|
|
area => omitMessageAreaTags.includes(area.areaTag)
|
|
|
|
);
|
|
|
|
const currentArea = sortedAreas[this.currentScanAux.area];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Scan and update index until we find something. If results are found,
|
|
|
|
// we'll goto the list module & show them.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const self = this;
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function checkAndUpdateIndex(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// Advance to next area if possible
|
2018-06-22 05:15:04 +00:00
|
|
|
if(sortedAreas.length >= self.currentScanAux.area + 1) {
|
|
|
|
self.currentScanAux.area += 1;
|
|
|
|
return callback(null);
|
|
|
|
} else {
|
|
|
|
self.updateScanStatus(self.scanCompleteMsg);
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(Errors.DoesNotExist('No more areas')); // this will stop our scan
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
function updateStatusScanStarted(callback) {
|
|
|
|
self.updateScanStatus(stringFormat(self.scanStartFmt, {
|
|
|
|
confName : conf.conf.name,
|
|
|
|
confDesc : conf.conf.desc,
|
|
|
|
areaName : currentArea.area.name,
|
|
|
|
areaDesc : currentArea.area.desc
|
|
|
|
}));
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function getNewMessagesCountInArea(callback) {
|
|
|
|
msgArea.getNewMessageCountInAreaForUser(
|
|
|
|
self.client.user.userId, currentArea.areaTag, (err, newMessageCount) => {
|
|
|
|
callback(err, newMessageCount);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function displayMessageList(newMessageCount) {
|
|
|
|
if(newMessageCount <= 0) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return self.newScanMessageArea(conf, cb); // next area, if any
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const nextModuleOpts = {
|
|
|
|
extraArgs: {
|
|
|
|
messageAreaTag : currentArea.areaTag,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return self.gotoMenu(self.menuConfig.config.newScanMessageList || 'newScanMessageList', nextModuleOpts);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
newScanFileBase(cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: add in steps
|
2018-07-07 21:20:45 +00:00
|
|
|
const omitFileAreaTags = valueAsArray(_.get(this, 'menuConfig.config.omitFileAreaTags', []));
|
2018-06-22 05:15:04 +00:00
|
|
|
const filterCriteria = {
|
|
|
|
newerThanFileId : FileBaseFilters.getFileBaseLastViewedFileIdByUser(this.client.user),
|
2018-07-07 21:20:45 +00:00
|
|
|
areaTag : getAvailableFileAreaTags(this.client).filter(ft => !omitFileAreaTags.includes(ft)),
|
2018-06-23 03:26:46 +00:00
|
|
|
order : 'ascending', // oldest first
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FileEntry.findFiles(
|
|
|
|
filterCriteria,
|
|
|
|
(err, fileIds) => {
|
|
|
|
if(err || 0 === fileIds.length) {
|
|
|
|
return cb(err ? err : Errors.DoesNotExist('No more new files'));
|
|
|
|
}
|
|
|
|
|
|
|
|
FileBaseFilters.setFileBaseLastViewedFileIdForUser( this.client.user, fileIds[fileIds.length - 1] );
|
|
|
|
|
|
|
|
const menuOpts = {
|
|
|
|
extraArgs : {
|
|
|
|
fileList : fileIds,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.gotoMenu(this.menuConfig.config.newScanFileBaseList || 'newScanFileBaseList', menuOpts);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSaveState() {
|
|
|
|
return {
|
2018-06-23 03:26:46 +00:00
|
|
|
currentStep : this.currentStep,
|
|
|
|
currentScanAux : this.currentScanAux,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreSavedState(savedState) {
|
2018-06-23 03:26:46 +00:00
|
|
|
this.currentStep = savedState.currentStep;
|
|
|
|
this.currentScanAux = savedState.currentScanAux;
|
2018-06-22 05:15:04 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
|
|
|
if(this.newScanFullExit) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// user has canceled the entire scan @ message list view
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: display scan step/etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadFromConfig(callback) {
|
|
|
|
const loadOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu,
|
|
|
|
noInput : true,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
},
|
|
|
|
function performCurrentStepScan(callback) {
|
|
|
|
return self.performScanCurrentStep(callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
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
|
|
|
};
|