diff --git a/cmd/unread/unread.go b/cmd/unread/unread.go index 1f2548c..87022e4 100644 --- a/cmd/unread/unread.go +++ b/cmd/unread/unread.go @@ -95,11 +95,33 @@ func newMailCounter() *mailCounter { func (mc *mailCounter) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Type", "application/json") - c := map[string]int{} - for k, v := range mc.counts { - if v != 0 { - c[k] = v + var c interface{} + if q := r.URL.Query(); q.Get("format") == "list" { + type box struct { + Name string `json:"name"` + Count int `json:"count"` } + boxes := []box{} + names := []string{} + for name := range mc.counts { + names = append(names, name) + } + sort.Strings(names) + for _, k := range names { + v := mc.counts[k] + if v != 0 { + boxes = append(boxes, box{Name: k, Count: v}) + } + } + c = boxes + } else { + m := map[string]int{} + for k, v := range mc.counts { + if v != 0 { + m[k] = v + } + } + c = m } enc := json.NewEncoder(w) err := enc.Encode(c)