unread: return unread as a list with URL param ?format=list

This commit is contained in:
Bill Thiede 2014-08-03 20:17:47 -07:00
parent b666e89f2c
commit 4027b56589

View File

@ -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)