Use proper filtering function, remove helper array

pull/16/head
ClaraCrazy 2024-10-01 23:51:18 +02:00
parent dc7d7e6676
commit 1f0d57bad0
No known key found for this signature in database
GPG Key ID: EBBC896ACB497011
1 changed files with 9 additions and 11 deletions

View File

@ -199,7 +199,7 @@ 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) {
const uids = await this._searchWithoutFetch([ let uids = await this._searchWithoutFetch([
['!DELETED'], ['!DELETED'],
['BEFORE', deleteMailsBefore] ['BEFORE', deleteMailsBefore]
]) ])
@ -207,28 +207,26 @@ class ImapService extends EventEmitter {
return return
} }
debug(`deleting mails ${uids}`)
const DeleteOlderThan = moment() const DeleteOlderThan = moment()
.subtract(this.config.email.deleteMailsOlderThanDays, 'days') .subtract(this.config.email.deleteMailsOlderThanDays, 'days')
.toDate() .toDate()
let toDelete = [] const uidsWithHeaders = await this._getMailHeaders(uids)
const uidwithHeaders = await this._getMailHeaders(uids) uidsWithHeaders.forEach(mail => {
uidwithHeaders.forEach(mail => { if (mail['attributes'].date > DeleteOlderThan) {
if (mail['attributes'].date < DeleteOlderThan) { uids.filter(uid => uid !== mail['attributes'].uid)
toDelete.push(mail['attributes'].uid)
} }
}) })
if (toDelete.length === 0) { if (uids.length === 0) {
debug('no mails to delete.') debug('no mails to delete.')
return return
} }
await this.connection.deleteMessage(toDelete) debug(`deleting mails ${uids}`)
await this.connection.deleteMessage(uids)
toDelete.forEach(uid => this.emit(ImapService.EVENT_DELETED_MAIL, uid)) toDelete.forEach(uid => this.emit(ImapService.EVENT_DELETED_MAIL, uid))
console.log(`deleted ${toDelete.length} old messages.`) console.log(`deleted ${uids.length} old messages.`)
} }
/** /**