* Update QWK area pointers upon export

* Specialize 0 messages exported
This commit is contained in:
Bryan Ashby 2020-05-12 18:53:47 -06:00
parent f63464c501
commit db91d6d6b7
No known key found for this signature in database
GPG Key ID: B49EB437951D2542
2 changed files with 53 additions and 16 deletions

View File

@ -38,6 +38,7 @@ exports.Errors = {
MissingMci : (reason, reasonCode) => new EnigError('Missing required MCI code(s)', -32009, reason, reasonCode), MissingMci : (reason, reasonCode) => new EnigError('Missing required MCI code(s)', -32009, reason, reasonCode),
BadLogin : (reason, reasonCode) => new EnigError('Bad login attempt', -32010, reason, reasonCode), BadLogin : (reason, reasonCode) => new EnigError('Bad login attempt', -32010, reason, reasonCode),
UserInterrupt : (reason, reasonCode) => new EnigError('User interrupted', -32011, reason, reasonCode), UserInterrupt : (reason, reasonCode) => new EnigError('User interrupted', -32011, reason, reasonCode),
NothingToDo : (reason, reasonCode) => new EnigError('Nothing to do', -32012, reason, reasonCode),
}; };
exports.ErrorReasons = { exports.ErrorReasons = {

View File

@ -14,7 +14,7 @@ const { renderSubstr } = require('./string_util');
const Config = require('./config').get; const Config = require('./config').get;
const FileEntry = require('./file_entry'); const FileEntry = require('./file_entry');
const DownloadQueue = require('./download_queue'); const DownloadQueue = require('./download_queue');
const { Log } = require('./logger').log; const { getISOTimestampString } = require('./database');
// deps // deps
const async = require('async'); const async = require('async');
@ -23,6 +23,7 @@ const fse = require('fs-extra');
const temptmp = require('temptmp'); const temptmp = require('temptmp');
const paths = require('path'); const paths = require('path');
const UUIDv4 = require('uuid/v4'); const UUIDv4 = require('uuid/v4');
const moment = require('moment');
const FormIds = { const FormIds = {
main : 0, main : 0,
@ -37,6 +38,11 @@ const MciViewIds = {
} }
}; };
const UserProperties = {
ExportOptions : 'qwk_export_options',
ExportAreas : 'qwk_export_msg_areas',
};
exports.moduleInfo = { exports.moduleInfo = {
name : 'QWK Export', name : 'QWK Export',
desc : 'Exports a QWK Packet for download', desc : 'Exports a QWK Packet for download',
@ -114,7 +120,7 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
} }
_getUserQWKExportOptions() { _getUserQWKExportOptions() {
let qwkOptions = this.client.user.getProperty('qwk_export_options'); let qwkOptions = this.client.user.getProperty(UserProperties.ExportOptions);
try { try {
qwkOptions = JSON.parse(qwkOptions); qwkOptions = JSON.parse(qwkOptions);
} catch(e) { } catch(e) {
@ -129,9 +135,14 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
} }
_getUserQWKExportAreas() { _getUserQWKExportAreas() {
let qwkExportAreas = this.client.user.getProperty('qwk_export_msg_areas'); let qwkExportAreas = this.client.user.getProperty(UserProperties.ExportAreas);
try { try {
qwkExportAreas = JSON.parse(qwkExportAreas); qwkExportAreas = JSON.parse(qwkExportAreas).map(exportArea => {
if (exportArea.newerThanTimestamp) {
exportArea.newerThanTimestamp = moment(exportArea.newerThanTimestamp);
}
return exportArea;
});
} catch(e) { } catch(e) {
// default to all public and private without 'since' // default to all public and private without 'since'
qwkExportAreas = getAllAvailableMessageAreaTags(this.client).map(areaTag => { qwkExportAreas = getAllAvailableMessageAreaTags(this.client).map(areaTag => {
@ -173,13 +184,13 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
case 'next_area' : case 'next_area' :
updateStatus(state.status); updateStatus(state.status);
updateProgressBar(0, 0); updateProgressBar(0, 0);
this.updateCustomViewTextsWithFilter('main', MciViewIds.main.customRangeStart, state.area); this.updateCustomViewTextsWithFilter('main', MciViewIds.main.customRangeStart, state);
break; break;
case 'message' : case 'message' :
updateStatus(state.status); updateStatus(state.status);
updateProgressBar(state.current, state.total); updateProgressBar(state.current, state.total);
this.updateCustomViewTextsWithFilter('main', MciViewIds.main.customRangeStart, state.message); this.updateCustomViewTextsWithFilter('main', MciViewIds.main.customRangeStart, state);
break; break;
default : default :
@ -198,6 +209,7 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
} }
}; };
let totalExported = 0;
const processMessagesWithFilter = (filter, cb) => { const processMessagesWithFilter = (filter, cb) => {
Message.findMessages(filter, (err, messageIds) => { Message.findMessages(filter, (err, messageIds) => {
if (err) { if (err) {
@ -213,11 +225,12 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
} }
const progress = { const progress = {
current,
message, message,
step : 'message', step : 'message',
total : messageIds.length, total : ++totalExported,
status : `${_.truncate(message.subject, { length : 25 })} (${current} / ${messageIds.length})`, areaCurrent : current,
areaCount : messageIds.length,
status : `${_.truncate(message.subject, { length : 25 })} (${current} / ${messageIds.length})`,
}; };
progressHandler(progress, err => { progressHandler(progress, err => {
@ -271,11 +284,13 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
}, },
(callback) => { (callback) => {
// For each public area -> for each message // For each public area -> for each message
const userExportAreas = this._getUserQWKExportAreas() const userExportAreas = this._getUserQWKExportAreas();
const publicExportAreas = userExportAreas
.filter(exportArea => { .filter(exportArea => {
return exportArea.areaTag !== Message.WellKnownAreaTags.Private; return exportArea.areaTag !== Message.WellKnownAreaTags.Private;
}); });
async.eachSeries(userExportAreas, (exportArea, nextExportArea) => { async.eachSeries(publicExportAreas, (exportArea, nextExportArea) => {
const area = getMessageAreaByTag(exportArea.areaTag); const area = getMessageAreaByTag(exportArea.areaTag);
const conf = getMessageConferenceByTag(area.confTag); const conf = getMessageConferenceByTag(area.confTag);
if (!area || !conf) { if (!area || !conf) {
@ -319,15 +334,15 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
(userExportAreas, callback) => { (userExportAreas, callback) => {
// Private messages to current user if the user has // Private messages to current user if the user has
// elected to export private messages // elected to export private messages
if (!(userExportAreas.find(exportArea => exportArea.areaTag === Message.WellKnownAreaTags.Private))) { const privateExportArea = userExportAreas.find(exportArea => exportArea.areaTag === Message.WellKnownAreaTags.Private);
if (!privateExportArea) {
return callback(null); return callback(null);
} }
const filter = { const filter = {
resultType : 'id', resultType : 'id',
privateTagUserId : this.client.user.userId, privateTagUserId : this.client.user.userId,
// :TODO: newerThanTimestamp for private messages newerThanTimestamp : privateExportArea.newerThanTimestamp,
//newerThanTimestamp : exportArea.newerThanTimestamp
}; };
return processMessagesWithFilter(filter, callback); return processMessagesWithFilter(filter, callback);
}, },
@ -344,6 +359,10 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
packetWriter.finish(this.tempPacketDir); packetWriter.finish(this.tempPacketDir);
}, },
(packetInfo, callback) => { (packetInfo, callback) => {
if (0 === totalExported) {
return callback(Errors.NothingToDo('No messages exported'));
}
const sysDownloadPath = paths.join(sysTempDownloadDir, this.tempName); const sysDownloadPath = paths.join(sysTempDownloadDir, this.tempName);
fse.move(packetInfo.path, sysDownloadPath, err => { fse.move(packetInfo.path, sysDownloadPath, err => {
return callback(null, sysDownloadPath, packetInfo); return callback(null, sysDownloadPath, packetInfo);
@ -375,7 +394,21 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
} }
return callback(err); return callback(err);
}); });
} },
(callback) => {
// update user's export area dates; they can always change/reset them again
const updatedUserExportAreas = this._getUserQWKExportAreas().map(exportArea => {
return Object.assign(exportArea, {
newerThanTimestamp : getISOTimestampString(),
});
});
return this.client.user.persistProperty(
UserProperties.ExportAreas,
JSON.stringify(updatedUserExportAreas),
callback
);
},
], ],
err => { err => {
this.client.startIdleMonitor(); // re-enable this.client.startIdleMonitor(); // re-enable
@ -383,6 +416,9 @@ exports.getModule = class MessageBaseQWKExport extends MenuModule {
if (!err) { if (!err) {
updateStatus('A QWK packet has been placed in your download queue'); updateStatus('A QWK packet has been placed in your download queue');
} else if (err.code === Errors.NothingToDo().code) {
updateStatus('No messages to export with current criteria');
err = null;
} }
return cb(err); return cb(err);