Reduce the amount of data sent in alert payloads.

This commit is contained in:
Azareal 2019-05-07 12:33:33 +10:00
parent 839df17de3
commit 09aa0558ab
3 changed files with 17 additions and 16 deletions

View File

@ -79,7 +79,7 @@ func BuildAlert(alert Alert, user User /* The current user */) (out string, err
}*/ }*/
if alert.Event == "friend_invite" { 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 // 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 // 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... // 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 url, area string
var phraseName = "alerts." + alert.ElementType var phraseName = "." + alert.ElementType
switch alert.ElementType { switch alert.ElementType {
case "topic": case "topic":
topic, err := Topics.Get(alert.ElementID) topic, err := Topics.Get(alert.ElementID)
@ -145,7 +145,7 @@ func BuildAlert(alert Alert, user User /* The current user */) (out string, err
phraseName += "_reply" 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 { 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] 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 { func AddActivityAndNotifyAll(actor int, targetUser int, event string, elementType string, elementID int) error {

View File

@ -48,7 +48,7 @@ function bindToAlerts() {
url: "/api/?action=set&module=dismiss-alert", url: "/api/?action=set&module=dismiss-alert",
type: "POST", type: "POST",
dataType: "json", dataType: "json",
data: { asid: $(this).attr("data-asid") }, data: { id: $(this).attr("data-asid") },
error: ajaxError, error: ajaxError,
success: () => { success: () => {
window.location.href = this.getAttribute("href"); window.location.href = this.getAttribute("href");
@ -59,21 +59,22 @@ function bindToAlerts() {
function addAlert(msg, notice = false) { function addAlert(msg, notice = false) {
var mmsg = msg.msg; var mmsg = msg.msg;
if(mmsg[0]==".") mmsg = phraseBox["alerts"]["alerts"+mmsg];
if("sub" in msg) { if("sub" in msg) {
for(var i = 0; i < msg.sub.length; i++) mmsg = mmsg.replace("\{"+i+"\}", msg.sub[i]); for(var i = 0; i < msg.sub.length; i++) mmsg = mmsg.replace("\{"+i+"\}", msg.sub[i]);
} }
let aItem = Template_alert({ let aItem = Template_alert({
ASID: msg.asid, ASID: msg.id,
Path: msg.path, Path: msg.path,
Avatar: msg.avatar || "", Avatar: msg.avatar || "",
Message: mmsg Message: mmsg
}) })
//alertMapping[msg.asid] = aItem; //alertMapping[msg.id] = aItem;
let div = document.createElement('div'); let div = document.createElement('div');
div.innerHTML = aItem.trim(); div.innerHTML = aItem.trim();
alertMapping[msg.asid] = div.firstChild; alertMapping[msg.id] = div.firstChild;
alertList.push(msg.asid); alertList.push(msg.id);
if(notice) { if(notice) {
// TODO: Add some sort of notification queue to avoid flooding the end-user with notices? // 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) { else if("event" in data) {
if(data.event == "dismiss-alert"){ if(data.event == "dismiss-alert"){
Object.keys(alertMapping).forEach((key) => { Object.keys(alertMapping).forEach((key) => {
if(key==data.asid) { if(key==data.id) {
alertCount--; alertCount--;
let index = -1; let index = -1;
for(var i = 0; i < alertList.length; i++) { for(var i = 0; i < alertList.length; i++) {

View File

@ -51,11 +51,11 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError
switch r.FormValue("module") { switch r.FormValue("module") {
// TODO: Split this into it's own function // TODO: Split this into it's own function
case "dismiss-alert": case "dismiss-alert":
asid, err := strconv.Atoi(r.FormValue("asid")) id, err := strconv.Atoi(r.FormValue("id"))
if err != nil { 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 { if err != nil {
return c.InternalError(err, w, r) 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 // Don't want to throw an internal error due to a socket closing
if c.EnableWebsockets && count > 0 { 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) w.Write(successJSONBytes)
// TODO: Split this into it's own function // TODO: Split this into it's own function