mirror of
https://github.com/axllent/mailpit.git
synced 2025-07-15 01:25:10 +02:00
API: Allow ID "latest" for message summary, headers, raw version & HTML/link checks
This commit is contained in:
@ -468,6 +468,23 @@ func GetAttachmentPart(id, partID string) (*enmime.Part, error) {
|
|||||||
return nil, errors.New("attachment not found")
|
return nil, errors.New("attachment not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LatestID returns the latest message ID
|
||||||
|
func LatestID() (string, error) {
|
||||||
|
messages := []MessageSummary{}
|
||||||
|
var err error
|
||||||
|
|
||||||
|
messages, err = List(0, 1)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(messages) == 0 {
|
||||||
|
return "", errors.New("Message not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return messages[0].ID, nil
|
||||||
|
}
|
||||||
|
|
||||||
// MarkRead will mark a message as read
|
// MarkRead will mark a message as read
|
||||||
func MarkRead(id string) error {
|
func MarkRead(id string) error {
|
||||||
if !IsUnread(id) {
|
if !IsUnread(id) {
|
||||||
|
@ -189,6 +189,8 @@ func GetMessage(w http.ResponseWriter, r *http.Request) {
|
|||||||
//
|
//
|
||||||
// Returns the summary of a message, marking the message as read.
|
// Returns the summary of a message, marking the message as read.
|
||||||
//
|
//
|
||||||
|
// The ID can be set to `latest` to return the latest message.
|
||||||
|
//
|
||||||
// Produces:
|
// Produces:
|
||||||
// - application/json
|
// - application/json
|
||||||
//
|
//
|
||||||
@ -197,7 +199,7 @@ func GetMessage(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Parameters:
|
// Parameters:
|
||||||
// + name: ID
|
// + name: ID
|
||||||
// in: path
|
// in: path
|
||||||
// description: Message database ID
|
// description: Message database ID or "latest"
|
||||||
// required: true
|
// required: true
|
||||||
// type: string
|
// type: string
|
||||||
//
|
//
|
||||||
@ -209,6 +211,16 @@ func GetMessage(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
id := vars["id"]
|
id := vars["id"]
|
||||||
|
|
||||||
|
if id == "latest" {
|
||||||
|
var err error
|
||||||
|
id, err = storage.LatestID()
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
fmt.Fprint(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
msg, err := storage.GetMessage(id)
|
msg, err := storage.GetMessage(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fourOFour(w)
|
fourOFour(w)
|
||||||
@ -279,6 +291,8 @@ func GetHeaders(w http.ResponseWriter, r *http.Request) {
|
|||||||
//
|
//
|
||||||
// Returns the message headers as an array.
|
// Returns the message headers as an array.
|
||||||
//
|
//
|
||||||
|
// The ID can be set to `latest` to return the latest message headers.
|
||||||
|
//
|
||||||
// Produces:
|
// Produces:
|
||||||
// - application/json
|
// - application/json
|
||||||
//
|
//
|
||||||
@ -287,7 +301,7 @@ func GetHeaders(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Parameters:
|
// Parameters:
|
||||||
// + name: ID
|
// + name: ID
|
||||||
// in: path
|
// in: path
|
||||||
// description: Database ID
|
// description: Message database ID or "latest"
|
||||||
// required: true
|
// required: true
|
||||||
// type: string
|
// type: string
|
||||||
//
|
//
|
||||||
@ -299,6 +313,16 @@ func GetHeaders(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
id := vars["id"]
|
id := vars["id"]
|
||||||
|
|
||||||
|
if id == "latest" {
|
||||||
|
var err error
|
||||||
|
id, err = storage.LatestID()
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
fmt.Fprint(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
data, err := storage.GetMessageRaw(id)
|
data, err := storage.GetMessageRaw(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fourOFour(w)
|
fourOFour(w)
|
||||||
@ -326,6 +350,8 @@ func DownloadRaw(w http.ResponseWriter, r *http.Request) {
|
|||||||
//
|
//
|
||||||
// Returns the full email source as plain text.
|
// Returns the full email source as plain text.
|
||||||
//
|
//
|
||||||
|
// The ID can be set to `latest` to return the latest message source.
|
||||||
|
//
|
||||||
// Produces:
|
// Produces:
|
||||||
// - text/plain
|
// - text/plain
|
||||||
//
|
//
|
||||||
@ -334,7 +360,7 @@ func DownloadRaw(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Parameters:
|
// Parameters:
|
||||||
// + name: ID
|
// + name: ID
|
||||||
// in: path
|
// in: path
|
||||||
// description: Database ID
|
// description: Message database ID or "latest"
|
||||||
// required: true
|
// required: true
|
||||||
// type: string
|
// type: string
|
||||||
//
|
//
|
||||||
@ -345,9 +371,18 @@ func DownloadRaw(w http.ResponseWriter, r *http.Request) {
|
|||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
|
||||||
id := vars["id"]
|
id := vars["id"]
|
||||||
|
|
||||||
dl := r.FormValue("dl")
|
dl := r.FormValue("dl")
|
||||||
|
|
||||||
|
if id == "latest" {
|
||||||
|
var err error
|
||||||
|
id, err = storage.LatestID()
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
fmt.Fprint(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
data, err := storage.GetMessageRaw(id)
|
data, err := storage.GetMessageRaw(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fourOFour(w)
|
fourOFour(w)
|
||||||
@ -554,7 +589,6 @@ func SetTags(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReleaseMessage (method: POST) will release a message via a pre-configured external SMTP server.
|
// ReleaseMessage (method: POST) will release a message via a pre-configured external SMTP server.
|
||||||
// If no IDs are provided then all messages are updated.
|
|
||||||
func ReleaseMessage(w http.ResponseWriter, r *http.Request) {
|
func ReleaseMessage(w http.ResponseWriter, r *http.Request) {
|
||||||
// swagger:route POST /api/v1/message/{ID}/release message ReleaseMessage
|
// swagger:route POST /api/v1/message/{ID}/release message ReleaseMessage
|
||||||
//
|
//
|
||||||
@ -702,6 +736,16 @@ func HTMLCheck(w http.ResponseWriter, r *http.Request) {
|
|||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
id := vars["id"]
|
id := vars["id"]
|
||||||
|
|
||||||
|
if id == "latest" {
|
||||||
|
var err error
|
||||||
|
id, err = storage.LatestID()
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
fmt.Fprint(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
msg, err := storage.GetMessage(id)
|
msg, err := storage.GetMessage(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fourOFour(w)
|
fourOFour(w)
|
||||||
@ -747,6 +791,16 @@ func LinkCheck(w http.ResponseWriter, r *http.Request) {
|
|||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
id := vars["id"]
|
id := vars["id"]
|
||||||
|
|
||||||
|
if id == "latest" {
|
||||||
|
var err error
|
||||||
|
id, err = storage.LatestID()
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
fmt.Fprint(w, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
msg, err := storage.GetMessage(id)
|
msg, err := storage.GetMessage(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fourOFour(w)
|
fourOFour(w)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package apiv1
|
package apiv1
|
||||||
|
|
||||||
// These structs are for the purpose of defining swagger HTTP responses
|
// These structs are for the purpose of defining swagger HTTP parameters & responses
|
||||||
|
|
||||||
// Application information
|
// Application information
|
||||||
// swagger:response InfoResponse
|
// swagger:response InfoResponse
|
||||||
@ -118,20 +118,20 @@ type releaseMessageRequestBody struct {
|
|||||||
|
|
||||||
// swagger:parameters HTMLCheck
|
// swagger:parameters HTMLCheck
|
||||||
type htmlCheckParams struct {
|
type htmlCheckParams struct {
|
||||||
// Message database ID
|
// Message database ID or "latest"
|
||||||
//
|
//
|
||||||
// in: path
|
// in: path
|
||||||
// description: Message database ID
|
// description: Message database ID or "latest"
|
||||||
// required: true
|
// required: true
|
||||||
ID string
|
ID string
|
||||||
}
|
}
|
||||||
|
|
||||||
// swagger:parameters LinkCheck
|
// swagger:parameters LinkCheck
|
||||||
type linkCheckParams struct {
|
type linkCheckParams struct {
|
||||||
// Message database ID
|
// Message database ID or "latest"
|
||||||
//
|
//
|
||||||
// in: path
|
// in: path
|
||||||
// description: Message database ID
|
// description: Message database ID or "latest"
|
||||||
// required: true
|
// required: true
|
||||||
ID string
|
ID string
|
||||||
|
|
||||||
|
@ -78,31 +78,13 @@ func GetMessageHTML(w http.ResponseWriter, r *http.Request) {
|
|||||||
id := vars["id"]
|
id := vars["id"]
|
||||||
|
|
||||||
if id == "latest" {
|
if id == "latest" {
|
||||||
messages := []storage.MessageSummary{}
|
|
||||||
var err error
|
var err error
|
||||||
|
id, err = storage.LatestID()
|
||||||
search := strings.TrimSpace(r.URL.Query().Get("query"))
|
if err != nil {
|
||||||
if search != "" {
|
|
||||||
messages, _, err = storage.Search(search, 0, 1)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
messages, err = storage.List(0, 1)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(messages) == 0 {
|
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
fmt.Fprint(w, "Message not found")
|
fmt.Fprint(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id = messages[0].ID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
msg, err := storage.GetMessage(id)
|
msg, err := storage.GetMessage(id)
|
||||||
@ -153,31 +135,13 @@ func GetMessageText(w http.ResponseWriter, r *http.Request) {
|
|||||||
id := vars["id"]
|
id := vars["id"]
|
||||||
|
|
||||||
if id == "latest" {
|
if id == "latest" {
|
||||||
messages := []storage.MessageSummary{}
|
|
||||||
var err error
|
var err error
|
||||||
|
id, err = storage.LatestID()
|
||||||
search := strings.TrimSpace(r.URL.Query().Get("query"))
|
if err != nil {
|
||||||
if search != "" {
|
|
||||||
messages, _, err = storage.Search(search, 0, 1)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
messages, err = storage.List(0, 1)
|
|
||||||
if err != nil {
|
|
||||||
httpError(w, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(messages) == 0 {
|
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
fmt.Fprint(w, "Message not found")
|
fmt.Fprint(w, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id = messages[0].ID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
msg, err := storage.GetMessage(id)
|
msg, err := storage.GetMessage(id)
|
||||||
|
@ -50,7 +50,7 @@
|
|||||||
},
|
},
|
||||||
"/api/v1/message/{ID}": {
|
"/api/v1/message/{ID}": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "Returns the summary of a message, marking the message as read.",
|
"description": "Returns the summary of a message, marking the message as read.\n\nThe ID can be set to `latest` to return the latest message.",
|
||||||
"produces": [
|
"produces": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
@ -66,7 +66,7 @@
|
|||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Message database ID",
|
"description": "Message database ID or \"latest\"",
|
||||||
"name": "ID",
|
"name": "ID",
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"required": true
|
"required": true
|
||||||
@ -87,7 +87,7 @@
|
|||||||
},
|
},
|
||||||
"/api/v1/message/{ID}/headers": {
|
"/api/v1/message/{ID}/headers": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "Returns the message headers as an array.",
|
"description": "Returns the message headers as an array.\n\nThe ID can be set to `latest` to return the latest message headers.",
|
||||||
"produces": [
|
"produces": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
@ -103,7 +103,7 @@
|
|||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Database ID",
|
"description": "Message database ID or \"latest\"",
|
||||||
"name": "ID",
|
"name": "ID",
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"required": true
|
"required": true
|
||||||
@ -140,7 +140,7 @@
|
|||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Message database ID",
|
"description": "Message database ID or \"latest\"",
|
||||||
"name": "ID",
|
"name": "ID",
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"required": true
|
"required": true
|
||||||
@ -177,7 +177,7 @@
|
|||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Message database ID",
|
"description": "Message database ID or \"latest\"",
|
||||||
"name": "ID",
|
"name": "ID",
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"required": true
|
"required": true
|
||||||
@ -290,7 +290,7 @@
|
|||||||
},
|
},
|
||||||
"/api/v1/message/{ID}/raw": {
|
"/api/v1/message/{ID}/raw": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "Returns the full email source as plain text.",
|
"description": "Returns the full email source as plain text.\n\nThe ID can be set to `latest` to return the latest message source.",
|
||||||
"produces": [
|
"produces": [
|
||||||
"text/plain"
|
"text/plain"
|
||||||
],
|
],
|
||||||
@ -306,7 +306,7 @@
|
|||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Database ID",
|
"description": "Message database ID or \"latest\"",
|
||||||
"name": "ID",
|
"name": "ID",
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"required": true
|
"required": true
|
||||||
|
Reference in New Issue
Block a user