From 33d1ea4ec7c1800c903b67d2c4909aee1d101fde Mon Sep 17 00:00:00 2001 From: Azareal Date: Thu, 9 May 2019 20:15:09 +1000 Subject: [PATCH] Send empty objects when there aren't any alerts to save space. Fixed an alert endpoint error message. --- public/global.js | 1 + routes.go | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/public/global.js b/public/global.js index 8e87ef3b..448ad1f0 100644 --- a/public/global.js +++ b/public/global.js @@ -147,6 +147,7 @@ function loadAlerts(menuAlerts) { } alertList = []; alertMapping = {}; + if(!data.hasOwnProperty("msgs")) data = {"msgs":[],"count":0}; for(var i in data.msgs) addAlert(data.msgs[i]); console.log("data.count:",data.count) alertCount = data.count; diff --git a/routes.go b/routes.go index d0481253..64b7bfb6 100644 --- a/routes.go +++ b/routes.go @@ -89,11 +89,10 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError return nil } - var msglist string var count int err = stmts.getActivityCountByWatcher.QueryRow(user.ID).Scan(&count) if err == ErrNoRows { - return c.PreErrorJS("Couldn't find the parent topic", w, r) + return c.PreErrorJS("Unable to get the activity count", w, r) } else if err != nil { return c.InternalErrorJS(err, w, r) } @@ -127,6 +126,8 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError return c.InternalErrorJS(err, w, r) } + var msglist string + //var sb strings.Builder var ok bool for _, alert := range alerts { alert.Actor, ok = list[alert.ActorID] @@ -139,13 +140,18 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError return c.LocalErrorJS(err.Error(), w, r) } + //sb.Write(res) msglist += res + "," } if len(msglist) != 0 { msglist = msglist[0 : len(msglist)-1] } - _, _ = io.WriteString(w, `{"msgs":[` + msglist + `],"count":` + strconv.Itoa(count) + `}`) + if count == 0 { + _, _ = io.WriteString(w, `{}`) + } else { + _, _ = io.WriteString(w, `{"msgs":[` + msglist + `],"count":` + strconv.Itoa(count) + `}`) + } default: return c.PreErrorJS("Invalid Module", w, r) }