From 4027b5658948d288822c0648f020833e48ff03b3 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sun, 3 Aug 2014 20:17:47 -0700 Subject: [PATCH] unread: return unread as a list with URL param ?format=list --- cmd/unread/unread.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) 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)