mirror of
https://github.com/axllent/mailpit.git
synced 2025-06-08 23:46:21 +02:00
Fix: Prevent error when two identical tags are added at the exact same time (#283)
This commit is contained in:
parent
d3b048e933
commit
d381389fc9
@ -6,6 +6,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/axllent/mailpit/config"
|
"github.com/axllent/mailpit/config"
|
||||||
"github.com/axllent/mailpit/internal/logger"
|
"github.com/axllent/mailpit/internal/logger"
|
||||||
@ -15,6 +16,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
addressPlusRe = regexp.MustCompile(`(?U)^(.*){1,}\+(.*)@`)
|
addressPlusRe = regexp.MustCompile(`(?U)^(.*){1,}\+(.*)@`)
|
||||||
|
addTagMutex sync.RWMutex
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetMessageTags will set the tags for a given database ID
|
// SetMessageTags will set the tags for a given database ID
|
||||||
@ -58,14 +60,18 @@ func SetMessageTags(id string, tags []string) error {
|
|||||||
|
|
||||||
// AddMessageTag adds a tag to a message
|
// AddMessageTag adds a tag to a message
|
||||||
func AddMessageTag(id, name string) error {
|
func AddMessageTag(id, name string) error {
|
||||||
|
// prevent two identical tags being added at the same time
|
||||||
|
addTagMutex.Lock()
|
||||||
|
|
||||||
var tagID int
|
var tagID int
|
||||||
|
|
||||||
q := sqlf.From(tenant("tags")).
|
q := sqlf.From(tenant("tags")).
|
||||||
Select("ID").To(&tagID).
|
Select("ID").To(&tagID).
|
||||||
Where("Name = ?", name)
|
Where("Name = ?", name)
|
||||||
|
|
||||||
// tag exists - add tag to message
|
// if tag exists - add tag to message
|
||||||
if err := q.QueryRowAndClose(context.TODO(), db); err == nil {
|
if err := q.QueryRowAndClose(context.TODO(), db); err == nil {
|
||||||
|
addTagMutex.Unlock()
|
||||||
// check message does not already have this tag
|
// check message does not already have this tag
|
||||||
var count int
|
var count int
|
||||||
if _, err := sqlf.From(tenant("message_tags")).
|
if _, err := sqlf.From(tenant("message_tags")).
|
||||||
@ -89,15 +95,17 @@ func AddMessageTag(id, name string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log().Debugf("[tags] adding tag \"%s\" to %s", name, id)
|
// new tag, add to the database
|
||||||
|
|
||||||
// tag dos not exist, add new one
|
|
||||||
if _, err := sqlf.InsertInto(tenant("tags")).
|
if _, err := sqlf.InsertInto(tenant("tags")).
|
||||||
Set("Name", name).
|
Set("Name", name).
|
||||||
ExecAndClose(context.TODO(), db); err != nil {
|
ExecAndClose(context.TODO(), db); err != nil {
|
||||||
|
addTagMutex.Unlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addTagMutex.Unlock()
|
||||||
|
|
||||||
|
// add tag to the message
|
||||||
return AddMessageTag(id, name)
|
return AddMessageTag(id, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user