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) {
|
||||
|
|
@ -106,8 +108,8 @@ class Helper {
|
|||
*/
|
||||
|
||||
shuffleFirstItem(array) {
|
||||
let first = array[Math.floor(Math.random()*array.length)]
|
||||
array = array.filter((value)=>value!=first);
|
||||
let first = array[Math.floor(Math.random() * array.length)]
|
||||
array = array.filter((value) => value != first);
|
||||
array = [first].concat(array)
|
||||
return array
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
const EventEmitter = require('events')
|
||||
const imaps = require('imap-simple')
|
||||
const {simpleParser} = require('mailparser')
|
||||
const { simpleParser } = require('mailparser')
|
||||
const addressparser = require('nodemailer/lib/addressparser')
|
||||
const pSeries = require('p-series')
|
||||
const retry = require('async-retry')
|
||||
|
|
@ -21,20 +21,20 @@ const helper = new(Helper)
|
|||
* @returns {undefined|Promise} Returns a promise when no callback is specified, resolving when the action succeeds.
|
||||
* @memberof ImapSimple
|
||||
*/
|
||||
imaps.ImapSimple.prototype.deleteMessage = function (uid, callback) {
|
||||
imaps.ImapSimple.prototype.deleteMessage = function(uid, callback) {
|
||||
var self = this;
|
||||
|
||||
if (callback) {
|
||||
return nodeify(self.deleteMessage(uid), callback);
|
||||
}
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
self.imap.addFlags(uid, '\\Deleted', function (err) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
self.imap.addFlags(uid, '\\Deleted', function(err) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
self.imap.expunge( function (err) {
|
||||
self.imap.expunge(function(err) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
|
|
@ -53,10 +53,10 @@ imaps.ImapSimple.prototype.deleteMessage = function (uid, callback) {
|
|||
* @returns {undefined|Promise} Returns a promise when no callback is specified, resolving to `boxName`
|
||||
* @memberof ImapSimple
|
||||
*/
|
||||
imaps.ImapSimple.prototype.closeBox = function (autoExpunge=true, callback) {
|
||||
imaps.ImapSimple.prototype.closeBox = function(autoExpunge = true, callback) {
|
||||
var self = this;
|
||||
|
||||
if (typeof(autoExpunge) == 'function'){
|
||||
if (typeof(autoExpunge) == 'function') {
|
||||
callback = autoExpunge;
|
||||
autoExpunge = true;
|
||||
}
|
||||
|
|
@ -65,9 +65,9 @@ imaps.ImapSimple.prototype.closeBox = function (autoExpunge=true, callback) {
|
|||
return nodeify(this.closeBox(autoExpunge), callback);
|
||||
}
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
||||
self.imap.closeBox(autoExpunge, function (err, result) {
|
||||
self.imap.closeBox(autoExpunge, function(err, result) {
|
||||
|
||||
if (err) {
|
||||
reject(err);
|
||||
|
|
@ -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.`)
|
||||
}
|
||||
|
||||
|
|
@ -288,7 +295,7 @@ class ImapService extends EventEmitter {
|
|||
// Do nothing
|
||||
}
|
||||
const date = headerPart.date[0]
|
||||
const {uid} = message.attributes
|
||||
const { uid } = message.attributes
|
||||
|
||||
return Mail.create(to, from, date, subject, uid)
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -312,7 +322,7 @@ class ImapService extends EventEmitter {
|
|||
if (messages.length === 0) {
|
||||
return false
|
||||
} else if (!raw) {
|
||||
const fullBody = await _.find(messages[0].parts, {which: ''})
|
||||
const fullBody = await _.find(messages[0].parts, { which: '' })
|
||||
return simpleParser(fullBody.body)
|
||||
} else {
|
||||
return messages[0].parts[1].body
|
||||
|
|
@ -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