mirror of
https://github.com/Crazyco-xyz/48hr.email.git
synced 2025-12-14 13:56:32 +01:00
Finally clean up functions
This commit is contained in:
parent
7d956f7d89
commit
559c9bc9e5
2 changed files with 63 additions and 60 deletions
|
|
@ -30,40 +30,35 @@ class Helper {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert time to highest possible unit (minutes, hours, days) where `time > 1` and `Number.isSafeInteger(time)` (whole number)
|
* Convert time to highest possible unit (minutes → hours → days),
|
||||||
* @param {Number} time
|
* rounding if necessary and prefixing "~" when rounded.
|
||||||
* @param {String} unit
|
*
|
||||||
* @returns {String}
|
* @param {number} time
|
||||||
|
* @param {string} unit "minutes" | "hours" | "days"
|
||||||
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
convertAndRound(time, unit) {
|
convertAndRound(time, unit) {
|
||||||
let convertedTime = time;
|
let value = time;
|
||||||
let convertedUnit = unit;
|
let u = unit;
|
||||||
let rounded = false;
|
|
||||||
|
|
||||||
if (convertedUnit === 'minutes') {
|
// upgrade units
|
||||||
if (convertedTime > 60) {
|
const units = [
|
||||||
convertedTime = convertedTime / 60
|
["minutes", 60, "hours"],
|
||||||
convertedUnit = 'hours';
|
["hours", 24, "days"]
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const [from, factor, to] of units) {
|
||||||
|
if (u === from && value > factor) {
|
||||||
|
value = value / factor;
|
||||||
|
u = to;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (convertedUnit === 'hours') {
|
// determine if rounding is needed
|
||||||
if (convertedTime > 24) {
|
const rounded = !Number.isSafeInteger(value);
|
||||||
convertedTime = convertedTime / 24;
|
if (rounded) value = Math.round(value);
|
||||||
convertedUnit = 'days';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!convertedTime == Number.isSafeInteger(convertedTime)) {
|
return `${rounded ? "~" : ""}${value} ${u}`;
|
||||||
convertedTime = Math.round(convertedTime);
|
|
||||||
rounded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rounded) {
|
|
||||||
convertedTime = `~${convertedTime}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return `${convertedTime} ${convertedUnit}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -196,45 +196,53 @@ class ImapService extends EventEmitter {
|
||||||
* @param {Date} deleteMailsBefore delete mails before this date instance
|
* @param {Date} deleteMailsBefore delete mails before this date instance
|
||||||
*/
|
*/
|
||||||
async deleteOldMails(deleteMailsBefore) {
|
async deleteOldMails(deleteMailsBefore) {
|
||||||
let uids = []
|
let uids;
|
||||||
//fetch mails from date +1day (calculated in MS) to avoid wasting resources and to fix imaps missing time-awareness
|
|
||||||
if (helper.moreThanOneDay(moment(), deleteMailsBefore)) {
|
// Only do heavy IMAP date filtering if the cutoff is older than 1 day
|
||||||
uids = await this._searchWithoutFetch([
|
const useDateFilter = helper.moreThanOneDay(moment(), deleteMailsBefore);
|
||||||
['!DELETED'],
|
|
||||||
['BEFORE', deleteMailsBefore]
|
const searchQuery = useDateFilter ? [
|
||||||
])
|
['!DELETED'],
|
||||||
} else {
|
['BEFORE', deleteMailsBefore]
|
||||||
uids = await this._searchWithoutFetch([
|
] : [
|
||||||
['!DELETED'],
|
['!DELETED']
|
||||||
])
|
];
|
||||||
|
|
||||||
|
uids = await this._searchWithoutFetch(searchQuery);
|
||||||
|
|
||||||
|
if (uids.length === 0) return;
|
||||||
|
|
||||||
|
const deleteOlderThan = helper.purgeTimeStamp();
|
||||||
|
const exampleUids = this.config.email.examples.uids.map(x => parseInt(x));
|
||||||
|
const headers = await this._getMailHeaders(uids);
|
||||||
|
|
||||||
|
// Filter out mails that are too new or whitelisted
|
||||||
|
const toDelete = headers
|
||||||
|
.filter(mail => {
|
||||||
|
const date = mail.attributes.date;
|
||||||
|
const uid = parseInt(mail.attributes.uid);
|
||||||
|
|
||||||
|
if (exampleUids.includes(uid)) return false;
|
||||||
|
return date <= deleteOlderThan;
|
||||||
|
})
|
||||||
|
.map(mail => parseInt(mail.attributes.uid));
|
||||||
|
|
||||||
|
if (toDelete.length === 0) {
|
||||||
|
debug('No mails to delete.');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uids.length === 0) {
|
debug(`Deleting mails ${toDelete}`);
|
||||||
return
|
await this.connection.deleteMessage(toDelete);
|
||||||
}
|
|
||||||
|
|
||||||
const DeleteOlderThan = helper.purgeTimeStamp()
|
toDelete.forEach(uid => {
|
||||||
const uidsWithHeaders = await this._getMailHeaders(uids)
|
this.emit(ImapService.EVENT_DELETED_MAIL, uid);
|
||||||
|
});
|
||||||
|
|
||||||
uidsWithHeaders.forEach(mail => {
|
console.log(`Deleted ${toDelete.length} old messages.`);
|
||||||
if (mail['attributes'].date > DeleteOlderThan || this.config.email.examples.uids.includes(parseInt(mail['attributes'].uid))) {
|
|
||||||
uids = uids.filter(uid => uid !== mail['attributes'].uid)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (uids.length === 0) {
|
|
||||||
debug('No mails to delete.')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
debug(`deleting mails ${uids}`)
|
|
||||||
await this.connection.deleteMessage(uids)
|
|
||||||
uids.forEach(uid => {
|
|
||||||
this.emit(ImapService.EVENT_DELETED_MAIL, uid)
|
|
||||||
})
|
|
||||||
console.log(`Deleted ${uids.length} old messages.`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param uid delete specific mail per UID
|
* @param uid delete specific mail per UID
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue