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')
|
|
|
|
|
|
|
|
class MailRepository {
|
|
|
|
constructor() {
|
|
|
|
// MultiMap docs: https://yomguithereal.github.io/mnemonist/multi-map
|
|
|
|
this.mailSummaries = new MultiMap()
|
|
|
|
}
|
|
|
|
|
|
|
|
getForRecipient(address) {
|
|
|
|
const mails = this.mailSummaries.get(address) || []
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-11-02 06:27:28 +01:00
|
|
|
UserRemoveUid(address, 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) & to == address)
|
|
|
|
.forEach(mail => {
|
|
|
|
this.mailSummaries.remove(to, mail)
|
|
|
|
debug('removed ', mail.date, to, mail.subject)
|
|
|
|
deleted = true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return deleted
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:48:19 +01:00
|
|
|
removeUid(uid) {
|
|
|
|
// TODO: make this more efficient, looping through each email is not cool.
|
|
|
|
this.mailSummaries.forEachAssociation((mails, to) => {
|
|
|
|
mails
|
2023-11-02 06:25:22 +01:00
|
|
|
.filter(mail => mail.uid === parseInt(uid))
|
2023-11-01 11:48:19 +01:00
|
|
|
.forEach(mail => {
|
|
|
|
this.mailSummaries.remove(to, mail)
|
|
|
|
debug('removed ', mail.date, to, mail.subject)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
mailCount() {
|
|
|
|
return this.mailSummaries.size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MailRepository
|