diff --git a/server/apiv1/api.go b/server/apiv1/api.go index 2914d01..55789ab 100644 --- a/server/apiv1/api.go +++ b/server/apiv1/api.go @@ -451,6 +451,33 @@ func SetReadStatus(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("ok")) } +// GetTags (method: GET) will get all tags currently in use +func GetTags(w http.ResponseWriter, _ *http.Request) { + // swagger:route GET /api/v1/tags tags SetTags + // + // # Get all current tags + // + // Produces: + // - application/json + // + // Schemes: http, https + // + // Responses: + // 200: ArrayResponse + // default: ErrorResponse + + tags := storage.GetAllTags() + + data, err := json.Marshal(tags) + if err != nil { + httpError(w, err.Error()) + return + } + + w.Header().Add("Content-Type", "application/json") + _, _ = w.Write(data) +} + // SetTags (method: PUT) will set the tags for all provided IDs func SetTags(w http.ResponseWriter, r *http.Request) { // swagger:route PUT /api/v1/tags tags SetTags diff --git a/server/apiv1/swagger.go b/server/apiv1/swagger.go index 4c259cd..ee83f01 100644 --- a/server/apiv1/swagger.go +++ b/server/apiv1/swagger.go @@ -96,3 +96,7 @@ type okResponse struct { // in: body Body string } + +// Plain JSON array response +// swagger:response ArrayResponse +type arrayResponse []string diff --git a/server/server.go b/server/server.go index 876a444..4003d63 100644 --- a/server/server.go +++ b/server/server.go @@ -95,6 +95,7 @@ func apiRoutes() *mux.Router { r.HandleFunc(config.Webroot+"api/v1/messages", middleWareFunc(apiv1.GetMessages)).Methods("GET") r.HandleFunc(config.Webroot+"api/v1/messages", middleWareFunc(apiv1.SetReadStatus)).Methods("PUT") r.HandleFunc(config.Webroot+"api/v1/messages", middleWareFunc(apiv1.DeleteMessages)).Methods("DELETE") + r.HandleFunc(config.Webroot+"api/v1/tags", middleWareFunc(apiv1.GetTags)).Methods("GET") r.HandleFunc(config.Webroot+"api/v1/tags", middleWareFunc(apiv1.SetTags)).Methods("PUT") r.HandleFunc(config.Webroot+"api/v1/search", middleWareFunc(apiv1.Search)).Methods("GET") r.HandleFunc(config.Webroot+"api/v1/message/{id}/part/{partID}", middleWareFunc(apiv1.DownloadAttachment)).Methods("GET") diff --git a/storage/database.go b/storage/database.go index 88bd5b2..4a13460 100644 --- a/storage/database.go +++ b/storage/database.go @@ -709,15 +709,8 @@ func DeleteAllMessages() error { return err } -// StatsGet returns the total/unread statistics for a mailbox -func StatsGet() MailboxStats { - var ( - total = CountTotal() - unread = CountUnread() - ) - - dbLastAction = time.Now() - +// GetAllTags returns all used tags +func GetAllTags() []string { q := sqlf.From("mailbox"). Select(`DISTINCT Tags`). Where("Tags != ?", "[]") @@ -750,6 +743,19 @@ func StatsGet() MailboxStats { sort.Strings(tags) + return tags +} + +// StatsGet returns the total/unread statistics for a mailbox +func StatsGet() MailboxStats { + var ( + total = CountTotal() + unread = CountUnread() + tags = GetAllTags() + ) + + dbLastAction = time.Now() + return MailboxStats{ Total: total, Unread: unread,