mirror of
https://github.com/axllent/mailpit.git
synced 2025-03-11 14:59:57 +02:00
Feature: Improved message search - any order & phrase quoting
This commit is contained in:
parent
133b36c34c
commit
1aa58eeaaf
1
go.mod
1
go.mod
@ -9,6 +9,7 @@ require (
|
||||
github.com/jhillyerd/enmime v0.10.0
|
||||
github.com/k3a/html2text v1.0.8
|
||||
github.com/klauspost/compress v1.15.9
|
||||
github.com/mattn/go-shellwords v1.0.12
|
||||
github.com/mhale/smtpd v0.8.0
|
||||
github.com/ostafen/clover/v2 v2.0.0-alpha.2
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
|
2
go.sum
2
go.sum
@ -124,6 +124,8 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
|
||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
|
||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk=
|
||||
github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
|
||||
github.com/mhale/smtpd v0.8.0 h1:5JvdsehCg33PQrZBvFyDMMUDQmvbzVpZgKob7eYBJc0=
|
||||
github.com/mhale/smtpd v0.8.0/go.mod h1:MQl+y2hwIEQCXtNhe5+55n0GZOjSmeqORDIXbqUL3x4=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
|
@ -18,6 +18,7 @@ import (
|
||||
"github.com/axllent/mailpit/server/websockets"
|
||||
"github.com/jhillyerd/enmime"
|
||||
"github.com/klauspost/compress/zstd"
|
||||
"github.com/mattn/go-shellwords"
|
||||
"github.com/ostafen/clover/v2"
|
||||
)
|
||||
|
||||
@ -327,21 +328,58 @@ func List(mailbox string, start, limit int) ([]data.Summary, error) {
|
||||
}
|
||||
|
||||
// Search returns a summary of items mathing a search. It searched the SearchText field.
|
||||
func Search(mailbox, search string, start, limit int) ([]data.Summary, error) {
|
||||
func Search(mailbox, s string, start, limit int) ([]data.Summary, error) {
|
||||
mailbox = sanitizeMailboxName(mailbox)
|
||||
|
||||
sq := fmt.Sprintf("(?i)%s", cleanString(regexp.QuoteMeta(search)))
|
||||
s = strings.ToLower(s)
|
||||
s = strings.Replace(s, "'", `\'`, -1)
|
||||
s = strings.Replace(s, "(", ``, -1)
|
||||
s = strings.Replace(s, ")", ``, -1)
|
||||
// add another quote if quotes are odd
|
||||
quotes := strings.Count(s, `"`)
|
||||
if quotes%2 != 0 {
|
||||
s += `"`
|
||||
}
|
||||
|
||||
p := shellwords.NewParser()
|
||||
args, err := p.Parse(s)
|
||||
if err != nil {
|
||||
return nil, errors.New("Your search contains invalid characters")
|
||||
}
|
||||
|
||||
results := []data.Summary{}
|
||||
include := []string{}
|
||||
|
||||
for _, w := range args {
|
||||
word := cleanString(w)
|
||||
if word != "" {
|
||||
include = append(include, fmt.Sprintf("%s", regexp.QuoteMeta(word)))
|
||||
}
|
||||
}
|
||||
|
||||
if len(include) == 0 {
|
||||
return results, nil
|
||||
}
|
||||
|
||||
var where clover.Criteria
|
||||
|
||||
for i, w := range include {
|
||||
if i == 0 {
|
||||
where = clover.Field("SearchText").Like(w)
|
||||
} else {
|
||||
where = where.And(clover.Field("SearchText").Like(w))
|
||||
}
|
||||
}
|
||||
|
||||
q, err := db.FindAll(clover.NewQuery(mailbox).
|
||||
Skip(start).
|
||||
Limit(limit).
|
||||
Sort(clover.SortOption{Field: "Created", Direction: -1}).
|
||||
Where(clover.Field("SearchText").Like(sq)))
|
||||
Where(where))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results := []data.Summary{}
|
||||
|
||||
for _, d := range q {
|
||||
cs := &data.Summary{}
|
||||
if err := d.Unmarshal(cs); err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user