1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-01-18 03:22:06 +02:00

Bugfix: Fix total/unread count after failed message inserts

This commit is contained in:
Ralph Slooten 2022-08-05 15:15:27 +12:00
parent 970a534d77
commit 3b65a8852e
3 changed files with 36 additions and 12 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"net"
"net/mail"
"regexp"
"github.com/axllent/mailpit/config"
"github.com/axllent/mailpit/logger"
@ -19,7 +20,15 @@ func mailHandler(origin net.Addr, from string, to []string, data []byte) error {
}
if _, err := storage.Store(storage.DefaultMailbox, data); err != nil {
logger.Log().Errorf("error storing message: %s", err.Error())
// Value with size 4800709 exceeded 1048576 limit
re := regexp.MustCompile(`(Value with size \d+ exceeded \d+ limit)`)
tooLarge := re.FindStringSubmatch(err.Error())
if len(tooLarge) > 0 {
logger.Log().Errorf("[db] error storing message: %s", tooLarge[0])
} else {
logger.Log().Errorf("[db] error storing message")
logger.Log().Errorf(err.Error())
}
return err
}

View File

@ -209,6 +209,8 @@ func Store(mailbox string, b []byte) (string, error) {
return "", err
}
statsAddNewMessage(mailbox)
// save the raw email in a separate collection
raw := clover.NewDocument()
raw.Set("_id", id)
@ -218,12 +220,11 @@ func Store(mailbox string, b []byte) (string, error) {
if err != nil {
// delete the summary because the data insert failed
logger.Log().Debugf("[db] error inserting raw message, rolling back")
_ = DeleteOneMessage(mailbox, id)
DeleteOneMessage(mailbox, id)
return "", err
}
statsAddNewMessage(mailbox)
count++
if count%100 == 0 {
logger.Log().Infof("100 messages added in %s", time.Since(per100start))
@ -399,10 +400,7 @@ func GetMessage(mailbox, id string) (*data.Message, error) {
from = &mail.Address{Name: env.GetHeader("From")}
}
date, err := env.Date()
if err != nil {
// date =
}
date, _ := env.Date()
obj := data.Message{
ID: q.ObjectId(),
@ -522,11 +520,18 @@ func UnreadMessage(mailbox, id string) error {
// DeleteOneMessage will delete a single message from a mailbox
func DeleteOneMessage(mailbox, id string) error {
q, err := db.FindById(mailbox, id)
if err != nil {
return err
}
unreadStatus := !q.Get("Read").(bool)
if err := db.DeleteById(mailbox, id); err != nil {
return err
}
statsDeleteOneMessage(mailbox)
statsDeleteOneMessage(mailbox, unreadStatus)
return db.DeleteById(mailbox+"_data", id)
}

View File

@ -63,13 +63,23 @@ func statsAddNewMessage(mailbox string) {
statsLock.Unlock()
}
// Deleting one will always mean it was read
func statsDeleteOneMessage(mailbox string) {
// Delete one message from the totals. If the message was unread,
// then it will also deduct one from the Unread status.
func statsDeleteOneMessage(mailbox string, unread bool) {
statsLock.Lock()
s, ok := mailboxStats[mailbox]
if ok {
// deduct from the totals
if s.Total > 0 {
s.Total = s.Total - 1
}
// only deduct if the original was unread
if unread && s.Unread > 0 {
s.Unread = s.Unread - 1
}
mailboxStats[mailbox] = data.MailboxStats{
Total: s.Total - 1,
Total: s.Total,
Unread: s.Unread,
}
}