2023-09-27 06:29:03 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-11-02 05:15:45 +02:00
|
|
|
"net/url"
|
2023-09-27 06:29:03 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/axllent/mailpit/config"
|
|
|
|
"github.com/axllent/mailpit/internal/storage"
|
|
|
|
)
|
|
|
|
|
2023-11-02 05:15:45 +02:00
|
|
|
// RedirectToLatestMessage (method: GET) redirects the web UI to the latest message
|
|
|
|
func RedirectToLatestMessage(w http.ResponseWriter, r *http.Request) {
|
2024-01-02 06:29:59 +02:00
|
|
|
var messages []storage.MessageSummary
|
2023-11-11 12:47:20 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
search := strings.TrimSpace(r.URL.Query().Get("query"))
|
|
|
|
if search != "" {
|
2024-08-04 07:04:14 +02:00
|
|
|
messages, _, err = storage.Search(search, "", 0, 0, 1)
|
2023-11-11 12:47:20 +02:00
|
|
|
if err != nil {
|
|
|
|
httpError(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2024-08-04 07:04:14 +02:00
|
|
|
messages, err = storage.List(0, 0, 1)
|
2023-11-11 12:47:20 +02:00
|
|
|
if err != nil {
|
|
|
|
httpError(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2023-11-02 05:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uri := config.Webroot
|
|
|
|
|
|
|
|
if len(messages) == 1 {
|
|
|
|
uri, err = url.JoinPath(uri, "/view/"+messages[0].ID)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, uri, 302)
|
|
|
|
}
|