mirror of
https://github.com/axllent/mailpit.git
synced 2025-02-03 13:12:03 +02:00
parent
53f8d34961
commit
699a534632
@ -55,7 +55,9 @@ func Search(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
messages, err := storage.Search(search)
|
start, limit := getStartLimit(r)
|
||||||
|
|
||||||
|
messages, err := storage.Search(search, start, limit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, err.Error())
|
httpError(w, err.Error())
|
||||||
return
|
return
|
||||||
|
@ -181,7 +181,9 @@ func assertSearchEqual(t *testing.T, uri, query string, count int) {
|
|||||||
t.Logf("Test search: %s", query)
|
t.Logf("Test search: %s", query)
|
||||||
m := apiv1.MessagesResult{}
|
m := apiv1.MessagesResult{}
|
||||||
|
|
||||||
data, err := clientGet(uri + "?query=" + url.QueryEscape(query))
|
limit := fmt.Sprintf("%d", count)
|
||||||
|
|
||||||
|
data, err := clientGet(uri + "?query=" + url.QueryEscape(query) + "&limit=" + limit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf(err.Error())
|
t.Errorf(err.Error())
|
||||||
return
|
return
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// Package storage handles all database actions
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -290,9 +291,9 @@ func List(start, limit int) ([]Summary, error) {
|
|||||||
// The search is broken up by segments (exact phrases can be quoted), and interprits specific terms such as:
|
// The search is broken up by segments (exact phrases can be quoted), and interprits specific terms such as:
|
||||||
// is:read, is:unread, has:attachment, to:<term>, from:<term> & subject:<term>
|
// is:read, is:unread, has:attachment, to:<term>, from:<term> & subject:<term>
|
||||||
// Negative searches also also included by prefixing the search term with a `-` or `!`
|
// Negative searches also also included by prefixing the search term with a `-` or `!`
|
||||||
func Search(search string) ([]Summary, error) {
|
func Search(search string, start, limit int) ([]Summary, error) {
|
||||||
results := []Summary{}
|
results := []Summary{}
|
||||||
start := time.Now()
|
tsStart := time.Now()
|
||||||
|
|
||||||
s := strings.ToLower(search)
|
s := strings.ToLower(search)
|
||||||
// add another quote if missing closing quote
|
// add another quote if missing closing quote
|
||||||
@ -308,7 +309,7 @@ func Search(search string) ([]Summary, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// generate the SQL based on arguments
|
// generate the SQL based on arguments
|
||||||
q := searchParser(args)
|
q := searchParser(args, start, limit)
|
||||||
|
|
||||||
if err := q.QueryAndClose(nil, db, func(row *sql.Rows) {
|
if err := q.QueryAndClose(nil, db, func(row *sql.Rows) {
|
||||||
var id string
|
var id string
|
||||||
@ -336,7 +337,7 @@ func Search(search string) ([]Summary, error) {
|
|||||||
return results, err
|
return results, err
|
||||||
}
|
}
|
||||||
|
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(tsStart)
|
||||||
|
|
||||||
logger.Log().Debugf("[db] search for \"%s\" in %s", search, elapsed)
|
logger.Log().Debugf("[db] search for \"%s\" in %s", search, elapsed)
|
||||||
|
|
||||||
@ -648,13 +649,10 @@ func DeleteAllMessages() error {
|
|||||||
// StatsGet returns the total/unread statistics for a mailbox
|
// StatsGet returns the total/unread statistics for a mailbox
|
||||||
func StatsGet() MailboxStats {
|
func StatsGet() MailboxStats {
|
||||||
var (
|
var (
|
||||||
start = time.Now()
|
|
||||||
total = CountTotal()
|
total = CountTotal()
|
||||||
unread = CountUnread()
|
unread = CountUnread()
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.Log().Debugf("[db] statistics calculated in %s", time.Since(start))
|
|
||||||
|
|
||||||
dbLastAction = time.Now()
|
dbLastAction = time.Now()
|
||||||
|
|
||||||
return MailboxStats{
|
return MailboxStats{
|
||||||
|
@ -180,7 +180,7 @@ func TestSearch(t *testing.T) {
|
|||||||
search = fmt.Sprintf("\"the email body %d jdsauk dwqmdqw\"", i)
|
search = fmt.Sprintf("\"the email body %d jdsauk dwqmdqw\"", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
summaries, err := Search(search)
|
summaries, err := Search(search, 0, 100)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log("error ", err)
|
t.Log("error ", err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
@ -196,7 +196,7 @@ func TestSearch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// search something that will return 200 rsults
|
// search something that will return 200 rsults
|
||||||
summaries, err := Search("This is the email body")
|
summaries, err := Search("This is the email body", 0, testRuns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log("error ", err)
|
t.Log("error ", err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
|
@ -8,7 +8,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// SearchParser returns the SQL syntax for the database search based on the search arguments
|
// SearchParser returns the SQL syntax for the database search based on the search arguments
|
||||||
func searchParser(args []string) *sqlf.Stmt {
|
func searchParser(args []string, start, limit int) *sqlf.Stmt {
|
||||||
|
if limit == 0 {
|
||||||
|
limit = 50
|
||||||
|
}
|
||||||
|
|
||||||
q := sqlf.From("mailbox").
|
q := sqlf.From("mailbox").
|
||||||
Select(`ID, Data, read,
|
Select(`ID, Data, read,
|
||||||
json_extract(Data, '$.To') as ToJSON,
|
json_extract(Data, '$.To') as ToJSON,
|
||||||
@ -17,7 +21,12 @@ func searchParser(args []string) *sqlf.Stmt {
|
|||||||
json_extract(Data, '$.Attachments') as Attachments
|
json_extract(Data, '$.Attachments') as Attachments
|
||||||
`).
|
`).
|
||||||
OrderBy("Sort DESC").
|
OrderBy("Sort DESC").
|
||||||
Limit(200)
|
Limit(limit).
|
||||||
|
Offset(start)
|
||||||
|
|
||||||
|
if limit > 0 {
|
||||||
|
q = q.Limit(limit)
|
||||||
|
}
|
||||||
|
|
||||||
for _, w := range args {
|
for _, w := range args {
|
||||||
if cleanString(w) == "" {
|
if cleanString(w) == "" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user