unread: return unread as a list with URL param ?format=list
This commit is contained in:
parent
b666e89f2c
commit
4027b56589
@ -95,12 +95,34 @@ func newMailCounter() *mailCounter {
|
|||||||
func (mc *mailCounter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (mc *mailCounter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
c := map[string]int{}
|
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 {
|
for k, v := range mc.counts {
|
||||||
if v != 0 {
|
if v != 0 {
|
||||||
c[k] = v
|
m[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c = m
|
||||||
|
}
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
err := enc.Encode(c)
|
err := enc.Encode(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user