From 09aa0558ab7a57fc0a233db07f2242720e53aba3 Mon Sep 17 00:00:00 2001 From: Azareal Date: Tue, 7 May 2019 12:33:33 +1000 Subject: [PATCH] Reduce the amount of data sent in alert payloads. --- common/alerts.go | 12 ++++++------ public/global.js | 13 +++++++------ routes.go | 8 ++++---- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/common/alerts.go b/common/alerts.go index d4406d6b..2e92d80b 100644 --- a/common/alerts.go +++ b/common/alerts.go @@ -79,7 +79,7 @@ func BuildAlert(alert Alert, user User /* The current user */) (out string, err }*/ if alert.Event == "friend_invite" { - return buildAlertString(phrases.GetTmplPhrase("alerts.new_friend_invite"), []string{alert.Actor.Name}, alert.Actor.Link, alert.Actor.Avatar, alert.ASID), nil + return buildAlertString(".new_friend_invite", []string{alert.Actor.Name}, alert.Actor.Link, alert.Actor.Avatar, alert.ASID), nil } // Not that many events for us to handle in a forum @@ -92,13 +92,13 @@ func BuildAlert(alert Alert, user User /* The current user */) (out string, err } // Store the forum ID in the targetUser column instead of making a new one? o.O // Add an additional column for extra information later on when we add the ability to link directly to posts. We don't need the forum data for now... - return buildAlertString(phrases.GetTmplPhrase("alerts.forum_new_topic"), []string{alert.Actor.Name, topic.Title}, topic.Link, alert.Actor.Avatar, alert.ASID), nil + return buildAlertString(".forum_new_topic", []string{alert.Actor.Name, topic.Title}, topic.Link, alert.Actor.Avatar, alert.ASID), nil } - return buildAlertString(phrases.GetTmplPhrase("alerts.forum_unknown_action"), []string{alert.Actor.Name}, "", alert.Actor.Avatar, alert.ASID), nil + return buildAlertString(".forum_unknown_action", []string{alert.Actor.Name}, "", alert.Actor.Avatar, alert.ASID), nil } var url, area string - var phraseName = "alerts." + alert.ElementType + var phraseName = "." + alert.ElementType switch alert.ElementType { case "topic": topic, err := Topics.Get(alert.ElementID) @@ -145,7 +145,7 @@ func BuildAlert(alert Alert, user User /* The current user */) (out string, err phraseName += "_reply" } - return buildAlertString(phrases.GetTmplPhrase(phraseName), []string{alert.Actor.Name, area}, url, alert.Actor.Avatar, alert.ASID), nil + return buildAlertString(phraseName, []string{alert.Actor.Name, area}, url, alert.Actor.Avatar, alert.ASID), nil } func buildAlertString(msg string, sub []string, path string, avatar string, asid int) string { @@ -157,7 +157,7 @@ func buildAlertString(msg string, sub []string, path string, avatar string, asid substring = substring[:len(substring)-1] } - return `{"msg":"` + escapeTextInJson(msg) + `","sub":[` + substring + `],"path":"` + escapeTextInJson(path) + `","avatar":"` + escapeTextInJson(avatar) + `","asid":"` + strconv.Itoa(asid) + `"}` + return `{"msg":"` + escapeTextInJson(msg) + `","sub":[` + substring + `],"path":"` + escapeTextInJson(path) + `","avatar":"` + escapeTextInJson(avatar) + `","id":` + strconv.Itoa(asid) + `}` } func AddActivityAndNotifyAll(actor int, targetUser int, event string, elementType string, elementID int) error { diff --git a/public/global.js b/public/global.js index dad38319..e6693f40 100644 --- a/public/global.js +++ b/public/global.js @@ -48,7 +48,7 @@ function bindToAlerts() { url: "/api/?action=set&module=dismiss-alert", type: "POST", dataType: "json", - data: { asid: $(this).attr("data-asid") }, + data: { id: $(this).attr("data-asid") }, error: ajaxError, success: () => { window.location.href = this.getAttribute("href"); @@ -59,21 +59,22 @@ function bindToAlerts() { function addAlert(msg, notice = false) { var mmsg = msg.msg; + if(mmsg[0]==".") mmsg = phraseBox["alerts"]["alerts"+mmsg]; if("sub" in msg) { for(var i = 0; i < msg.sub.length; i++) mmsg = mmsg.replace("\{"+i+"\}", msg.sub[i]); } let aItem = Template_alert({ - ASID: msg.asid, + ASID: msg.id, Path: msg.path, Avatar: msg.avatar || "", Message: mmsg }) - //alertMapping[msg.asid] = aItem; + //alertMapping[msg.id] = aItem; let div = document.createElement('div'); div.innerHTML = aItem.trim(); - alertMapping[msg.asid] = div.firstChild; - alertList.push(msg.asid); + alertMapping[msg.id] = div.firstChild; + alertList.push(msg.id); if(notice) { // TODO: Add some sort of notification queue to avoid flooding the end-user with notices? @@ -257,7 +258,7 @@ function runWebSockets(resume = false) { else if("event" in data) { if(data.event == "dismiss-alert"){ Object.keys(alertMapping).forEach((key) => { - if(key==data.asid) { + if(key==data.id) { alertCount--; let index = -1; for(var i = 0; i < alertList.length; i++) { diff --git a/routes.go b/routes.go index a6a47f83..d0481253 100644 --- a/routes.go +++ b/routes.go @@ -51,11 +51,11 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError switch r.FormValue("module") { // TODO: Split this into it's own function case "dismiss-alert": - asid, err := strconv.Atoi(r.FormValue("asid")) + id, err := strconv.Atoi(r.FormValue("id")) if err != nil { - return c.PreErrorJS("Invalid asid", w, r) + return c.PreErrorJS("Invalid id", w, r) } - res, err := stmts.deleteActivityStreamMatch.Exec(user.ID, asid) + res, err := stmts.deleteActivityStreamMatch.Exec(user.ID, id) if err != nil { return c.InternalError(err, w, r) } @@ -65,7 +65,7 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError } // Don't want to throw an internal error due to a socket closing if c.EnableWebsockets && count > 0 { - _ = c.WsHub.PushMessage(user.ID, `{"event":"dismiss-alert","asid":`+strconv.Itoa(asid)+`}`) + _ = c.WsHub.PushMessage(user.ID, `{"event":"dismiss-alert","id":`+strconv.Itoa(id)+`}`) } w.Write(successJSONBytes) // TODO: Split this into it's own function