2018-01-15 00:09:23 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const FileEntry = require('./file_entry.js');
|
|
|
|
const FileBaseFilters = require('./file_base_filter.js');
|
|
|
|
const { getAvailableFileAreaTags } = require('./file_base_area.js');
|
2018-01-15 00:09:23 +00:00
|
|
|
const {
|
2018-06-22 05:15:04 +00:00
|
|
|
getSortedAvailMessageConferences,
|
|
|
|
getSortedAvailMessageAreasByConfTag,
|
|
|
|
updateMessageAreaLastReadId,
|
|
|
|
getMessageIdNewerThanTimestampByArea
|
2018-06-23 03:26:46 +00:00
|
|
|
} = require('./message_area.js');
|
|
|
|
const stringFormat = require('./string_format.js');
|
2018-01-15 00:09:23 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
const moment = require('moment');
|
|
|
|
const _ = require('lodash');
|
2018-01-15 00:09:23 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Set New Scan Date',
|
|
|
|
desc : 'Sets new scan date for applicable scans',
|
|
|
|
author : 'NuSkooler',
|
2018-01-15 00:09:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const MciViewIds = {
|
2018-06-22 05:15:04 +00:00
|
|
|
main : {
|
2018-06-23 03:26:46 +00:00
|
|
|
scanDate : 1,
|
|
|
|
targetSelection : 2,
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-01-15 00:09:23 +00:00
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: for messages, we could insert "conf - all areas" into targets, and allow such
|
2018-01-15 00:09:23 +00:00
|
|
|
|
|
|
|
exports.getModule = class SetNewScanDate extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
const config = this.menuConfig.config;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.target = config.target || 'message';
|
|
|
|
this.scanDateFormat = config.scanDateFormat || 'YYYYMMDD';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
this.menuMethods = {
|
|
|
|
scanDateSubmit : (formData, extraArgs, cb) => {
|
|
|
|
let scanDate = _.get(formData, 'value.scanDate');
|
|
|
|
if(!scanDate) {
|
|
|
|
return cb(Errors.MissingParam('"scanDate" missing from form data'));
|
|
|
|
}
|
|
|
|
|
|
|
|
scanDate = moment(scanDate, this.scanDateFormat);
|
|
|
|
if(!scanDate.isValid()) {
|
|
|
|
return cb(Errors.Invalid(`"${_.get(formData, 'value.scanDate')}" is not a valid date`));
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const targetSelection = _.get(formData, 'value.targetSelection'); // may be undefined if N/A
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
this[`setNewScanDateFor${_.capitalize(this.target)}Base`](targetSelection, scanDate, () => {
|
|
|
|
return this.prevMenu(cb);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setNewScanDateForMessageBase(targetSelection, scanDate, cb) {
|
|
|
|
const target = this.targetSelections[targetSelection];
|
|
|
|
if(!target) {
|
|
|
|
return cb(Errors.UnexpectedState('Unable to get target in which to set new scan'));
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// selected area, or all of 'em
|
2018-06-22 05:15:04 +00:00
|
|
|
let updateAreaTags;
|
|
|
|
if('' === target.area.areaTag) {
|
|
|
|
updateAreaTags = this.targetSelections
|
|
|
|
.map( targetSelection => targetSelection.area.areaTag )
|
2018-06-23 03:26:46 +00:00
|
|
|
.filter( areaTag => areaTag ); // remove the blank 'all' entry
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
updateAreaTags = [ target.area.areaTag ];
|
|
|
|
}
|
|
|
|
|
|
|
|
async.each(updateAreaTags, (areaTag, nextAreaTag) => {
|
|
|
|
getMessageIdNewerThanTimestampByArea(areaTag, scanDate, (err, messageId) => {
|
|
|
|
if(err) {
|
|
|
|
return nextAreaTag(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!messageId) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return nextAreaTag(null); // nothing to do
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
messageId = Math.max(messageId - 1, 0);
|
|
|
|
|
|
|
|
return updateMessageAreaLastReadId(
|
|
|
|
this.client.user.userId,
|
|
|
|
areaTag,
|
|
|
|
messageId,
|
2018-06-23 03:26:46 +00:00
|
|
|
true, // allowOlder
|
2018-06-22 05:15:04 +00:00
|
|
|
nextAreaTag
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setNewScanDateForFileBase(targetSelection, scanDate, cb) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA doesn't currently have the concept of per-area
|
|
|
|
// scan pointers for users, so we use all areas avail
|
|
|
|
// to the user.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const filterCriteria = {
|
2018-06-23 03:26:46 +00:00
|
|
|
areaTag : getAvailableFileAreaTags(this.client),
|
|
|
|
newerThanTimestamp : scanDate,
|
|
|
|
limit : 1,
|
|
|
|
orderBy : 'upload_timestamp',
|
|
|
|
order : 'ascending',
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FileEntry.findFiles(filterCriteria, (err, fileIds) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!fileIds || 0 === fileIds.length) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// nothing to do
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pointerFileId = Math.max(fileIds[0] - 1, 0);
|
|
|
|
|
|
|
|
return FileBaseFilters.setFileBaseLastViewedFileIdForUser(
|
|
|
|
this.client.user,
|
|
|
|
pointerFileId,
|
2018-06-23 03:26:46 +00:00
|
|
|
true, // allowOlder
|
2018-06-22 05:15:04 +00:00
|
|
|
cb
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loadAvailMessageBaseSelections(cb) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Create an array of objects with conf/area information per entry,
|
|
|
|
// sorted naturally or via the 'sort' member in config
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const selections = [];
|
|
|
|
getSortedAvailMessageConferences(this.client).forEach(conf => {
|
|
|
|
getSortedAvailMessageAreasByConfTag(conf.confTag, { client : this.client } ).forEach(area => {
|
|
|
|
selections.push({
|
2018-06-23 03:26:46 +00:00
|
|
|
conf : {
|
2018-06-22 05:15:04 +00:00
|
|
|
confTag : conf.confTag,
|
2018-06-23 03:26:46 +00:00
|
|
|
name : conf.conf.name,
|
|
|
|
desc : conf.conf.desc,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2018-06-23 03:26:46 +00:00
|
|
|
area : {
|
|
|
|
areaTag : area.areaTag,
|
|
|
|
name : area.area.name,
|
|
|
|
desc : area.area.desc,
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
selections.unshift({
|
|
|
|
conf : {
|
2018-06-23 03:26:46 +00:00
|
|
|
confTag : '',
|
|
|
|
name : 'All conferences',
|
|
|
|
desc : 'All conferences',
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2018-06-23 03:26:46 +00:00
|
|
|
area : {
|
|
|
|
areaTag : '',
|
|
|
|
name : 'All areas',
|
|
|
|
desc : 'All areas',
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Find current conf/area & move it directly under "All"
|
|
|
|
const currConfTag = this.client.user.properties.message_conf_tag;
|
|
|
|
const currAreaTag = this.client.user.properties.message_area_tag;
|
2018-06-22 05:15:04 +00:00
|
|
|
if(currConfTag && currAreaTag) {
|
|
|
|
const confAreaIndex = selections.findIndex( confArea => {
|
|
|
|
return confArea.conf.confTag === currConfTag && confArea.area.areaTag === currAreaTag;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(confAreaIndex > -1) {
|
|
|
|
selections.splice(1, 0, selections.splice(confAreaIndex, 1)[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.targetSelections = selections;
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const self = this;
|
|
|
|
const vc = self.addViewController( 'main', new ViewController( { client : this.client } ) );
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function validateConfig(callback) {
|
|
|
|
if(![ 'message', 'file' ].includes(self.target)) {
|
|
|
|
return callback(Errors.Invalid(`Invalid "target" in config: ${self.target}`));
|
|
|
|
}
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TOD0: validate scanDateFormat
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function loadFromConfig(callback) {
|
|
|
|
return vc.loadFromMenuConfig( { callingMenu : self, mciMap : mciData.menu }, callback);
|
|
|
|
},
|
|
|
|
function loadAvailSelections(callback) {
|
|
|
|
switch(self.target) {
|
|
|
|
case 'message' :
|
|
|
|
return self.loadAvailMessageBaseSelections(callback);
|
|
|
|
|
|
|
|
default :
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function populateForm(callback) {
|
|
|
|
const today = moment();
|
|
|
|
|
|
|
|
const scanDateView = vc.getView(MciViewIds.main.scanDate);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: MaskTextEditView needs some love: If setText() with input that matches the mask, we should ignore the non-mask chars! Hack in place for now
|
2018-06-22 05:15:04 +00:00
|
|
|
const scanDateFormat = self.scanDateFormat.replace(/[/\-. ]/g, '');
|
|
|
|
scanDateView.setText(today.format(scanDateFormat));
|
|
|
|
|
|
|
|
if('message' === self.target) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const messageSelectionsFormat = self.menuConfig.config.messageSelectionsFormat || '{conf.name} - {area.name}';
|
|
|
|
const messageSelectionFocusFormat = self.menuConfig.config.messageSelectionFocusFormat || messageSelectionsFormat;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const targetSelectionView = vc.getView(MciViewIds.main.targetSelection);
|
|
|
|
|
|
|
|
targetSelectionView.setItems(self.targetSelections.map(targetSelection => stringFormat(messageSelectionFocusFormat, targetSelection)));
|
|
|
|
targetSelectionView.setFocusItems(self.targetSelections.map(targetSelection => stringFormat(messageSelectionFocusFormat, targetSelection)));
|
|
|
|
|
|
|
|
targetSelectionView.setFocusItemIndex(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.viewControllers.main.resetInitialFocus();
|
|
|
|
//vc.switchFocus(MciViewIds.main.scanDate);
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2018-01-15 00:09:23 +00:00
|
|
|
};
|