2015-12-31 06:24:26 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +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');
|
|
|
|
const { valueAsArray } = require('./misc_util.js');
|
2023-02-21 03:09:58 +00:00
|
|
|
const { SystemInternalConfTags } = require('./message_const');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
2015-12-31 06:24:26 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +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 = {
|
2022-06-05 20:04:25 +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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
MessageConfs: 'messageConferences',
|
|
|
|
FileBase: 'fileBase',
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +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);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.newScanFullExit = _.get(options, 'lastMenuResult.fullExit', false);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +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+
|
2022-06-05 20:04:25 +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
|
2022-06-05 20:04:25 +00:00
|
|
|
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-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2023-02-21 03:09:58 +00:00
|
|
|
// Sort conferences by name, other than "System Internal" which should
|
2018-06-23 03:26:46 +00:00
|
|
|
// 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) => {
|
2023-02-21 03:09:58 +00:00
|
|
|
if (SystemInternalConfTags.includes(a.confTag)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return -1;
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
return a.conf.name.localeCompare(b.conf.name, {
|
|
|
|
sensitivity: false,
|
|
|
|
numeric: true,
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.currentScanAux.conf = this.currentScanAux.conf || 0;
|
|
|
|
this.currentScanAux.area = this.currentScanAux.area || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentConf = this.sortedMessageConfs[this.currentScanAux.conf];
|
|
|
|
|
|
|
|
this.newScanMessageArea(currentConf, () => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.sortedMessageConfs.length > this.currentScanAux.conf + 1) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.currentScanAux.conf += 1;
|
|
|
|
this.currentScanAux.area = 0;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.newScanMessageConference(cb); // recursive to next conf
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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!
|
2022-06-05 20:04:25 +00:00
|
|
|
const omitMessageAreaTags = valueAsArray(
|
|
|
|
_.get(this, 'menuConfig.config.omitMessageAreaTags', [])
|
|
|
|
);
|
|
|
|
const sortedAreas = msgArea
|
|
|
|
.getSortedAvailMessageAreasByConfTag(conf.confTag, { client: this.client })
|
|
|
|
.filter(area => {
|
|
|
|
return !omitMessageAreaTags.includes(area.areaTag);
|
|
|
|
});
|
2018-07-07 21:20:45 +00:00
|
|
|
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
|
2022-06-05 20:04:25 +00:00
|
|
|
if (sortedAreas.length >= self.currentScanAux.area + 1) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.currentScanAux.area += 1;
|
|
|
|
return callback(null);
|
|
|
|
} else {
|
|
|
|
self.updateScanStatus(self.scanCompleteMsg);
|
2022-06-05 20:04:25 +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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.updateScanStatus(
|
|
|
|
stringFormat(self.scanStartFmt, {
|
|
|
|
confName: conf.conf.name,
|
|
|
|
confDesc: conf.conf.desc,
|
|
|
|
areaName: currentArea.area.name,
|
|
|
|
areaDesc: currentArea.area.desc,
|
|
|
|
})
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function getNewMessagesCountInArea(callback) {
|
|
|
|
msgArea.getNewMessageCountInAreaForUser(
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.user.userId,
|
|
|
|
currentArea.areaTag,
|
|
|
|
(err, newMessageCount) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
callback(err, newMessageCount);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function displayMessageList(newMessageCount) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (newMessageCount <= 0) {
|
|
|
|
return self.newScanMessageArea(conf, cb); // next area, if any
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const nextModuleOpts = {
|
|
|
|
extraArgs: {
|
2022-06-05 20:04:25 +00:00
|
|
|
messageAreaTag: currentArea.areaTag,
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return self.gotoMenu(
|
|
|
|
self.menuConfig.config.newScanMessageList || 'newScanMessageList',
|
|
|
|
nextModuleOpts
|
|
|
|
);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
newScanFileBase(cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: add in steps
|
2022-06-05 20:04:25 +00:00
|
|
|
const omitFileAreaTags = valueAsArray(
|
|
|
|
_.get(this, 'menuConfig.config.omitFileAreaTags', [])
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
const filterCriteria = {
|
2022-06-05 20:04:25 +00:00
|
|
|
newerThanFileId: FileBaseFilters.getFileBaseLastViewedFileIdByUser(
|
|
|
|
this.client.user
|
|
|
|
),
|
|
|
|
areaTag: getAvailableFileAreaTags(this.client).filter(
|
|
|
|
ft => !omitFileAreaTags.includes(ft)
|
|
|
|
),
|
|
|
|
order: 'ascending', // oldest first
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
FileEntry.findFiles(filterCriteria, (err, fileIds) => {
|
|
|
|
if (err || 0 === fileIds.length) {
|
|
|
|
return cb(err ? err : Errors.DoesNotExist('No more new files'));
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
FileBaseFilters.setFileBaseLastViewedFileIdForUser(
|
|
|
|
this.client.user,
|
|
|
|
fileIds[fileIds.length - 1]
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const menuOpts = {
|
|
|
|
extraArgs: {
|
|
|
|
fileList: fileIds,
|
|
|
|
},
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.gotoMenu(
|
|
|
|
this.menuConfig.config.newScanFileBaseList || 'newScanFileBaseList',
|
|
|
|
menuOpts
|
|
|
|
);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getSaveState() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
currentStep: this.currentStep,
|
|
|
|
currentScanAux: this.currentScanAux,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreSavedState(savedState) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.currentStep = savedState.currentStep;
|
2018-06-23 03:26:46 +00:00
|
|
|
this.currentScanAux = savedState.currentScanAux;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
performScanCurrentStep(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (this.currentStep) {
|
|
|
|
case Steps.MessageConfs:
|
|
|
|
this.newScanMessageConference(() => {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.currentStep = Steps.FileBase;
|
|
|
|
return this.performScanCurrentStep(cb);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case Steps.FileBase:
|
|
|
|
this.newScanFileBase(() => {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.currentStep = Steps.Finished;
|
|
|
|
return this.performScanCurrentStep(cb);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
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 => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +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 = {
|
2022-06-05 20:04:25 +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);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.client.log.error(
|
|
|
|
{ error: err.toString() },
|
|
|
|
'Error during new scan'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2015-12-31 06:24:26 +00:00
|
|
|
};
|