mirror of
				https://github.com/Crazyco-xyz/48hr.email.git
				synced 2025-11-04 15:26:33 +01:00 
			
		
		
		
	48hrs means 48hrs, not two days ago...
FUCK the IMAP protocol, honestly. rfc9051 is cooked up by a fag
This commit is contained in:
		
							parent
							
								
									5eaaad5a5d
								
							
						
					
					
						commit
						cca5ce7915
					
				
					 2 changed files with 26 additions and 5 deletions
				
			
		| 
						 | 
					@ -6,6 +6,7 @@ const pSeries = require('p-series')
 | 
				
			||||||
const retry = require('async-retry')
 | 
					const retry = require('async-retry')
 | 
				
			||||||
const debug = require('debug')('48hr-email:imap')
 | 
					const debug = require('debug')('48hr-email:imap')
 | 
				
			||||||
const _ = require('lodash')
 | 
					const _ = require('lodash')
 | 
				
			||||||
 | 
					const moment = require('moment')
 | 
				
			||||||
const Mail = require('../domain/mail')
 | 
					const Mail = require('../domain/mail')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -198,7 +199,6 @@ 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) {
 | 
				
			||||||
		debug(`deleting mails before ${deleteMailsBefore}`)
 | 
					 | 
				
			||||||
		const uids = await this._searchWithoutFetch([
 | 
							const uids = await this._searchWithoutFetch([
 | 
				
			||||||
			['!DELETED'],
 | 
								['!DELETED'],
 | 
				
			||||||
			['BEFORE', deleteMailsBefore]
 | 
								['BEFORE', deleteMailsBefore]
 | 
				
			||||||
| 
						 | 
					@ -208,10 +208,27 @@ class ImapService extends EventEmitter {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		debug(`deleting mails ${uids}`)
 | 
							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.`)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -73,7 +73,11 @@ class MailProcessingService extends EventEmitter {
 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
			await this.imapService.deleteOldMails(
 | 
								await this.imapService.deleteOldMails(
 | 
				
			||||||
				moment()
 | 
									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()
 | 
										.toDate()
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
		} catch (error) {
 | 
							} catch (error) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue