mirror of
https://github.com/axllent/mailpit.git
synced 2025-05-13 22:06:31 +02:00
Bugfix: Fix total/unread count after failed message inserts
This commit is contained in:
parent
970a534d77
commit
3b65a8852e
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"net"
|
"net"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
"github.com/axllent/mailpit/config"
|
"github.com/axllent/mailpit/config"
|
||||||
"github.com/axllent/mailpit/logger"
|
"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 {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,6 +209,8 @@ func Store(mailbox string, b []byte) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
statsAddNewMessage(mailbox)
|
||||||
|
|
||||||
// save the raw email in a separate collection
|
// save the raw email in a separate collection
|
||||||
raw := clover.NewDocument()
|
raw := clover.NewDocument()
|
||||||
raw.Set("_id", id)
|
raw.Set("_id", id)
|
||||||
@ -218,12 +220,11 @@ func Store(mailbox string, b []byte) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
// delete the summary because the data insert failed
|
// delete the summary because the data insert failed
|
||||||
logger.Log().Debugf("[db] error inserting raw message, rolling back")
|
logger.Log().Debugf("[db] error inserting raw message, rolling back")
|
||||||
_ = DeleteOneMessage(mailbox, id)
|
DeleteOneMessage(mailbox, id)
|
||||||
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
statsAddNewMessage(mailbox)
|
|
||||||
|
|
||||||
count++
|
count++
|
||||||
if count%100 == 0 {
|
if count%100 == 0 {
|
||||||
logger.Log().Infof("100 messages added in %s", time.Since(per100start))
|
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")}
|
from = &mail.Address{Name: env.GetHeader("From")}
|
||||||
}
|
}
|
||||||
|
|
||||||
date, err := env.Date()
|
date, _ := env.Date()
|
||||||
if err != nil {
|
|
||||||
// date =
|
|
||||||
}
|
|
||||||
|
|
||||||
obj := data.Message{
|
obj := data.Message{
|
||||||
ID: q.ObjectId(),
|
ID: q.ObjectId(),
|
||||||
@ -522,11 +520,18 @@ func UnreadMessage(mailbox, id string) error {
|
|||||||
|
|
||||||
// DeleteOneMessage will delete a single message from a mailbox
|
// DeleteOneMessage will delete a single message from a mailbox
|
||||||
func DeleteOneMessage(mailbox, id string) error {
|
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 {
|
if err := db.DeleteById(mailbox, id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
statsDeleteOneMessage(mailbox)
|
statsDeleteOneMessage(mailbox, unreadStatus)
|
||||||
|
|
||||||
return db.DeleteById(mailbox+"_data", id)
|
return db.DeleteById(mailbox+"_data", id)
|
||||||
}
|
}
|
||||||
|
@ -63,13 +63,23 @@ func statsAddNewMessage(mailbox string) {
|
|||||||
statsLock.Unlock()
|
statsLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deleting one will always mean it was read
|
// Delete one message from the totals. If the message was unread,
|
||||||
func statsDeleteOneMessage(mailbox string) {
|
// then it will also deduct one from the Unread status.
|
||||||
|
func statsDeleteOneMessage(mailbox string, unread bool) {
|
||||||
statsLock.Lock()
|
statsLock.Lock()
|
||||||
s, ok := mailboxStats[mailbox]
|
s, ok := mailboxStats[mailbox]
|
||||||
if ok {
|
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{
|
mailboxStats[mailbox] = data.MailboxStats{
|
||||||
Total: s.Total - 1,
|
Total: s.Total,
|
||||||
Unread: s.Unread,
|
Unread: s.Unread,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user