mirror of
https://github.com/Crazyco-xyz/48hr.email.git
synced 2025-12-14 13:56:32 +01:00
probably fix things?
This commit is contained in:
parent
9d991486ae
commit
f42fbd4e74
4 changed files with 381 additions and 364 deletions
|
|
@ -15,19 +15,20 @@ class Helper {
|
|||
|
||||
/**
|
||||
* Check if time difference between now and purgeTimeStamp is more than one day
|
||||
* @param {Date} now
|
||||
* @param {number|Date} now
|
||||
* @param {Date} past
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
moreThanOneDay(now, past) {
|
||||
const DAY_IN_MS = 24 * 60 * 60 * 1000;
|
||||
if((now - past) / DAY_IN_MS >= 1){
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
const nowMs = now instanceof Date ? now.getTime() : now;
|
||||
const pastMs = past instanceof Date ? past.getTime() : new Date(past).getTime();
|
||||
|
||||
return (nowMs - pastMs) >= DAY_IN_MS;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert time to highest possible unit (minutes, hours, days) where `time > 1` and `Number.isSafeInteger(time)` (whole number)
|
||||
* @param {Number} time
|
||||
|
|
@ -43,7 +44,8 @@ class Helper {
|
|||
if (convertedTime > 60) {
|
||||
convertedTime = convertedTime / 60
|
||||
convertedUnit = 'hours';
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
if (convertedUnit === 'hours') {
|
||||
if (convertedTime > 24) {
|
||||
|
|
|
|||
|
|
@ -136,8 +136,7 @@ class ImapService extends EventEmitter {
|
|||
|
||||
await this.connection.openBox('INBOX')
|
||||
debug('connected to imap')
|
||||
},
|
||||
{
|
||||
}, {
|
||||
retries: 5
|
||||
}
|
||||
)
|
||||
|
|
@ -202,12 +201,14 @@ class ImapService extends EventEmitter {
|
|||
async deleteOldMails(deleteMailsBefore) {
|
||||
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() + 24 * 60 * 60 * 1000, deleteMailsBefore)) {
|
||||
if (helper.moreThanOneDay(moment(), deleteMailsBefore)) {
|
||||
console.log("Deleting mails older than one day");
|
||||
uids = await this._searchWithoutFetch([
|
||||
['!DELETED'],
|
||||
['BEFORE', deleteMailsBefore]
|
||||
])
|
||||
} else {
|
||||
console.log("Deleting mails without date filter");
|
||||
uids = await this._searchWithoutFetch([
|
||||
['!DELETED'],
|
||||
])
|
||||
|
|
@ -219,21 +220,27 @@ class ImapService extends EventEmitter {
|
|||
|
||||
const DeleteOlderThan = helper.purgeTimeStamp()
|
||||
const uidsWithHeaders = await this._getMailHeaders(uids)
|
||||
console.log(`Fetched ${uidsWithHeaders.length} mails for deletion check.`);
|
||||
|
||||
uidsWithHeaders.forEach(mail => {
|
||||
if (mail['attributes'].date > DeleteOlderThan || this.config.email.examples.uids.includes(parseInt(mail['attributes'].uid))) {
|
||||
uids = uids.filter(uid => uid !== mail['attributes'].uid)
|
||||
console.log(mail['attributes'].date > DeleteOlderThan ? `Mail UID: ${mail['attributes'].uid} is newer than purge time.` : `Mail UID: ${mail['attributes'].uid} is an example mail.`);
|
||||
}
|
||||
})
|
||||
|
||||
if (uids.length === 0) {
|
||||
console.log("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))
|
||||
uids.forEach(uid => {
|
||||
this.emit(ImapService.EVENT_DELETED_MAIL, uid)
|
||||
console.log(`UID deleted: ${uid}`);
|
||||
})
|
||||
console.log(`deleted ${uids.length} old messages.`)
|
||||
}
|
||||
|
||||
|
|
@ -302,7 +309,10 @@ class ImapService extends EventEmitter {
|
|||
debug(`fetching full message ${uid}`)
|
||||
|
||||
// For security we also filter TO, so it is harder to just enumerate all messages.
|
||||
const searchCriteria = [['UID', uid], ['TO', to]]
|
||||
const searchCriteria = [
|
||||
['UID', uid],
|
||||
['TO', to]
|
||||
]
|
||||
const fetchOptions = {
|
||||
bodies: ['HEADER', ''], // Empty string means full body
|
||||
markSeen: false
|
||||
|
|
@ -322,7 +332,9 @@ class ImapService extends EventEmitter {
|
|||
|
||||
async _getAllUids() {
|
||||
// We ignore mails that are flagged as DELETED, but have not been removed (expunged) yet.
|
||||
const uids = await this._searchWithoutFetch([['!DELETED']])
|
||||
const uids = await this._searchWithoutFetch([
|
||||
['!DELETED']
|
||||
])
|
||||
// Create copy to not mutate the original array. Sort with newest first (DESC).
|
||||
return [...uids].sort().reverse()
|
||||
}
|
||||
|
|
@ -349,7 +361,9 @@ class ImapService extends EventEmitter {
|
|||
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)'],
|
||||
struct: false
|
||||
}
|
||||
const searchCriteria = [['UID', ...uids]]
|
||||
const searchCriteria = [
|
||||
['UID', ...uids]
|
||||
]
|
||||
return this.connection.search(searchCriteria, fetchOptions)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,8 +17,7 @@ class MailProcessingService extends EventEmitter {
|
|||
|
||||
// Cached methods:
|
||||
this.cachedFetchFullMail = mem(
|
||||
this.imapService.fetchOneFullMail.bind(this.imapService),
|
||||
{maxAge: 10 * 60 * 1000}
|
||||
this.imapService.fetchOneFullMail.bind(this.imapService), { maxAge: 10 * 60 * 1000 }
|
||||
)
|
||||
|
||||
this.initialLoadDone = false
|
||||
|
|
@ -27,7 +26,9 @@ class MailProcessingService extends EventEmitter {
|
|||
this.imapService.once(ImapService.EVENT_INITIAL_LOAD_DONE, () =>
|
||||
this._deleteOldMails()
|
||||
)
|
||||
setInterval(() => this._deleteOldMails(), 10 * 60 * 1000)
|
||||
setInterval(() => {
|
||||
this._deleteOldMails()
|
||||
}, 60 * 1000)
|
||||
}
|
||||
|
||||
getMailSummaries(address) {
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ class MailRepository {
|
|||
mails.forEach(mail => {
|
||||
if (mail.to == this.config.email.examples.account && !this.config.email.examples.uids.includes(parseInt(mail.uid))) {
|
||||
mails = mails.filter(m => m.uid != mail.uid)
|
||||
debug('prevented non-example email from being shown in example inbox', mail.uid)
|
||||
console.log('prevented non-example email from being shown in example inbox', mail.uid)
|
||||
}
|
||||
})
|
||||
return _.orderBy(mails, mail => Date.parse(mail.date), ['desc'])
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue