48hr.email/domain/mail-repository.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-11-01 11:48:19 +01:00
const debug = require('debug')('48hr-email:mail-summary-store')
const MultiMap = require('mnemonist/multi-map')
const _ = require('lodash')
2024-10-02 02:36:25 +02:00
const config = require('../application/config')
2023-11-01 11:48:19 +01:00
class MailRepository {
constructor() {
// MultiMap docs: https://yomguithereal.github.io/mnemonist/multi-map
this.mailSummaries = new MultiMap()
2024-10-02 02:36:25 +02:00
this.config = config
2023-11-01 11:48:19 +01:00
}
getForRecipient(address) {
2024-10-02 02:36:25 +02:00
let mails = this.mailSummaries.get(address) || []
mails.forEach(mail => {
if (mail.to == this.config.http.examples.email && !this.config.http.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)
2024-10-02 02:36:25 +02:00
}
})
2023-11-01 11:48:19 +01:00
return _.orderBy(mails, mail => Date.parse(mail.date), ['desc'])
}
getAll() {
const mails = [...this.mailSummaries.values()]
return _.orderBy(mails, mail => Date.parse(mail.date), ['desc'])
}
add(to, mailSummary) {
this.mailSummaries.set(to.toLowerCase(), mailSummary)
}
removeUid(uid, address) {
if (!this.config.http.examples.uids.includes(parseInt(uid))) {
var deleted = false
// TODO: make this more efficient, looping through each email is not cool.
this.mailSummaries.forEachAssociation((mails, to) => {
mails
.filter(mail => mail.uid === parseInt(uid) && (address ? to == address : true))
.forEach(mail => {
this.mailSummaries.remove(to, mail)
debug('removed ', mail.date, to, mail.subject)
deleted = true
})
})
return deleted
}
return false
2023-11-01 11:48:19 +01:00
}
mailCount() {
return this.mailSummaries.size
}
}
module.exports = MailRepository