From a13b575b725b2475eefcaddad52da697efaa5a0f Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 8 Sep 2018 23:50:15 +1000 Subject: [PATCH] Alert dismissals should now be fanned out to other tabs and devices listening to the alert feed. --- mysql.go | 2 +- public/global.js | 25 ++++++++++++++++++++----- routes.go | 10 +++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/mysql.go b/mysql.go index b376fd34..ffe7c128 100644 --- a/mysql.go +++ b/mysql.go @@ -53,7 +53,7 @@ func initMySQL() (err error) { // TODO: Is there a less noisy way of doing this for tests? log.Print("Preparing getActivityFeedByWatcher statement.") - stmts.getActivityFeedByWatcher, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID FROM `activity_stream_matches` INNER JOIN `activity_stream` ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE `watcher` = ? ORDER BY activity_stream.asid DESC LIMIT 8") + stmts.getActivityFeedByWatcher, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID FROM `activity_stream_matches` INNER JOIN `activity_stream` ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE `watcher` = ? ORDER BY activity_stream.asid DESC LIMIT 16") if err != nil { return errors.WithStack(err) } diff --git a/public/global.js b/public/global.js index 5e58d5b6..c93e6ef6 100644 --- a/public/global.js +++ b/public/global.js @@ -51,10 +51,8 @@ function addAlert(msg, notice = false) { Avatar: msg.avatar || "", Message: mmsg }) - alertMapping[msg.asid] = aItem; alertList.push(msg.asid); - if(alertList.length > 8) alertList.shift(); if(notice) { // TODO: Add some sort of notification queue to avoid flooding the end-user with notices? @@ -206,15 +204,32 @@ function runWebSockets() { wsAlertEvent(data); } else if("event" in data) { if(data.event == "dismiss-alert"){ - Object.keys(alertBuffer).forEach((key) => { + Object.keys(alertMapping).forEach((key) => { if(key==data.asid) { alertCount--; - for(var i = 0; i < alertList.length;i++) { + let index = -1; + for(var i = 0; i < alertList.length; i++) { if(alertList[i]==key) { - alertList.splice(i); + alertList[i] = 0; + index = i; } } + + for(var i = index; (i+1) < alertList.length; i++) { + alertList[i] = alertList[i+1]; + } + alertList.splice(alertList.length-1,1); delete alertMapping[key]; + + + + // TODO: Add support for other alert feeds like PM Alerts + var generalAlerts = document.getElementById("general_alerts"); + if(alertList.length < 8) { + loadAlerts(generalAlerts); + } else { + updateAlertList(generalAlerts); + } } }); } diff --git a/routes.go b/routes.go index 0bd9b04c..bf167d93 100644 --- a/routes.go +++ b/routes.go @@ -48,10 +48,18 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user common.User) common.R if err != nil { return common.PreErrorJS("Invalid asid", w, r) } - _, err = stmts.deleteActivityStreamMatch.Exec(user.ID, asid) + res, err := stmts.deleteActivityStreamMatch.Exec(user.ID, asid) if err != nil { return common.InternalError(err, w, r) } + count, err := res.RowsAffected() + if err != nil { + return common.InternalError(err, w, r) + } + // Don't want to throw an internal error due to a socket closing + if common.EnableWebsockets && count > 0 { + _ = common.WsHub.PushMessage(user.ID, `{"event":"dismiss-alert","asid":`+strconv.Itoa(asid)+`}`) + } case "alerts": // A feed of events tailored for a specific user if !user.Loggedin { w.Write(phraseLoginAlerts)