48hrs means 48hrs, not two days ago...

FUCK the IMAP protocol, honestly. rfc9051 is cooked up by a fag
pull/16/head
ClaraCrazy 2024-10-01 19:28:35 +02:00
parent 5eaaad5a5d
commit cca5ce7915
2 changed files with 26 additions and 5 deletions

View File

@ -6,6 +6,7 @@ const pSeries = require('p-series')
const retry = require('async-retry')
const debug = require('debug')('48hr-email:imap')
const _ = require('lodash')
const moment = require('moment')
const Mail = require('../domain/mail')
@ -198,7 +199,6 @@ class ImapService extends EventEmitter {
* @param {Date} deleteMailsBefore delete mails before this date instance
*/
async deleteOldMails(deleteMailsBefore) {
debug(`deleting mails before ${deleteMailsBefore}`)
const uids = await this._searchWithoutFetch([
['!DELETED'],
['BEFORE', deleteMailsBefore]
@ -208,10 +208,27 @@ class ImapService extends EventEmitter {
}
debug(`deleting mails ${uids}`)
await this.connection.deleteMessage(uids)
console.log(`deleted ${uids.length} old messages.`)
uids.forEach(uid => this.emit(ImapService.EVENT_DELETED_MAIL, uid))
const DeleteOlderThan = moment()
.subtract(this.config.email.deleteMailsOlderThanDays, 'days')
.toDate()
let toDelete = []
const uidwithHeaders = await this._getMailHeaders(uids)
uidwithHeaders.forEach(mail => {
if (mail['attributes'].date < DeleteOlderThan) {
toDelete.push(mail['attributes'].uid)
}
})
if (toDelete.length === 0) {
debug('no mails to delete.')
return
}
await this.connection.deleteMessage(toDelete)
toDelete.forEach(uid => this.emit(ImapService.EVENT_DELETED_MAIL, uid))
console.log(`deleted ${toDelete.length} old messages.`)
}
/**

View File

@ -73,7 +73,11 @@ class MailProcessingService extends EventEmitter {
try {
await this.imapService.deleteOldMails(
moment()
.subtract(this.config.email.deleteMailsOlderThanDays, 'days')
// Because of how we have to handle the times (IMAP isnt time-aware), we need to subtract one day
// to get all mails in their last few hours before technical purge
//
// This is a bit of a hack, but it works. See imap-service.js#deleteOldMails (L211-227) for more info
.subtract(this.config.email.deleteMailsOlderThanDays - 1, 'days')
.toDate()
)
} catch (error) {