From 255dd92a65fe014b382229fde0d93456d5b4d3a5 Mon Sep 17 00:00:00 2001 From: Azareal Date: Fri, 30 Nov 2018 13:02:20 +1000 Subject: [PATCH] Fixed a data race in the client side alert logic. misc.js should now be loaded for guests too in Nox. Fixed the msgCount property not being set for guests in alerts. Added the alerts.no_alerts_short phrase. --- langs/english.json | 1 + public/global.js | 32 +++++++++++++++++--------------- public/init.js | 7 +++++-- routes.go | 2 +- templates/profile.html | 4 ++-- themes/nox/public/misc.js | 7 +++++-- themes/nox/theme.json | 5 ++--- 7 files changed, 33 insertions(+), 25 deletions(-) diff --git a/langs/english.json b/langs/english.json index 48f7ba0b..978c7180 100644 --- a/langs/english.json +++ b/langs/english.json @@ -349,6 +349,7 @@ "alerts.new_friend_invite":"You received a friend invite from {0}", "alerts.no_alerts":"You don't have any alerts", + "alerts.no_alerts_short":"No new alerts", "topics_click_topics_to_select":"Click the topics to select them", "topics_new_topic":"New Topic", diff --git a/public/global.js b/public/global.js index 61d1fd3e..6abe9dd4 100644 --- a/public/global.js +++ b/public/global.js @@ -4,7 +4,7 @@ var alertMapping = {}; var alertList = []; var alertCount = 0; var moreTopicCount = 0; -var conn; +var conn = false; var selectedTopics = []; var attachItemCallback = function(){} @@ -92,7 +92,8 @@ function updateAlertList(menuAlerts) { } bindToAlerts(); - runInitHook("after_update_alert_list"); + console.log("alertCount:",alertCount) + runInitHook("after_update_alert_list", alertCount); } function setAlertError(menuAlerts,msg) { @@ -117,6 +118,7 @@ function loadAlerts(menuAlerts) { for(var i in data.msgs) { addAlert(data.msgs[i]); } + console.log("data.msgCount:",data.msgCount) alertCount = data.msgCount; updateAlertList(menuAlerts) }, @@ -161,6 +163,7 @@ function wsAlertEvent(data) { for (var i = 0; i < alertList.length; i++) alist += alertMapping[alertList[i]]; // TODO: Add support for other alert feeds like PM Alerts var generalAlerts = document.getElementById("general_alerts"); + // TODO: Make sure we update alertCount here updateAlertList(generalAlerts, alist); } @@ -198,10 +201,8 @@ function runWebSockets() { return; } - if ("msg" in data) { - // TODO: Fix the data race where the alert template hasn't been loaded yet - wsAlertEvent(data); - } else if("event" in data) { + if ("msg" in data) wsAlertEvent(data); + else if("event" in data) { if(data.event == "dismiss-alert"){ Object.keys(alertMapping).forEach((key) => { if(key==data.asid) { @@ -286,18 +287,19 @@ function runWebSockets() { // We can only get away with this because template_alert has no phrases, otherwise it too would have to be part of the "dance", I miss Go concurrency :( loadScript("template_alert.js", () => { console.log("Loaded template_alert.js"); - $(document).ready(() => { - alertsInitted = true; - var alertMenuList = document.getElementsByClassName("menu_alerts"); - for(var i = 0; i < alertMenuList.length; i++) { - loadAlerts(alertMenuList[i]); - } + addInitHook("after_phrases", () => { + // TODO: The load part of loadAlerts could be done asynchronously while the update of the DOM could be deferred + $(document).ready(() => { + alertsInitted = true; + var alertMenuList = document.getElementsByClassName("menu_alerts"); + for(var i = 0; i < alertMenuList.length; i++) { + loadAlerts(alertMenuList[i]); + } + if(window["WebSocket"]) runWebSockets(); + }); }); }); - if(window["WebSocket"]) runWebSockets(); - else conn = false; - $(document).ready(mainInit); }); })(); diff --git a/public/init.js b/public/init.js index e6da919f..756e4879 100644 --- a/public/init.js +++ b/public/init.js @@ -9,6 +9,7 @@ var hooks = { "pre_init": [], "start_init": [], "end_init": [], + "after_phrases":[], "after_add_alert":[], "after_update_alert_list":[], }; @@ -32,8 +33,8 @@ function addHook(name, callback) { } // InitHooks are slightly special, as if they are run, then any adds after the initial run will run immediately, this is to deal with the async nature of script loads -function runInitHook(name) { - runHook(name); +function runInitHook(name, ...args) { + runHook(name,...args); ranInitHooks[name] = true; } @@ -121,6 +122,8 @@ function fetchPhrases() { console.log("adding phrase prefix '"+prefix+"' to box"); phraseBox[prefix] = prefixes[prefix]; }); + + runInitHook("after_phrases"); }); } diff --git a/routes.go b/routes.go index 5a3429d2..48523598 100644 --- a/routes.go +++ b/routes.go @@ -26,7 +26,7 @@ var successJSONBytes = []byte(`{"success":"1"}`) // TODO: Refactor this // TODO: Use the phrase system -var phraseLoginAlerts = []byte(`{"msgs":[{"msg":"Login to see your alerts","path":"/accounts/login"}]}`) +var phraseLoginAlerts = []byte(`{"msgs":[{"msg":"Login to see your alerts","path":"/accounts/login"}],"msgCount":0}`) // TODO: Refactor this endpoint func routeAPI(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError { diff --git a/templates/profile.html b/templates/profile.html index dea88360..aa52a43e 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -82,9 +82,9 @@ {{end}} - {{if .ItemList}}
+ {{end}} +
{{template "profile_comments_row.html" . }}
{{if .CurrentUser.Loggedin}} diff --git a/themes/nox/public/misc.js b/themes/nox/public/misc.js index 59a23503..7a3765d1 100644 --- a/themes/nox/public/misc.js +++ b/themes/nox/public/misc.js @@ -1,11 +1,14 @@ "use strict"; (() => { - addInitHook("after_update_alert_list", () => { + addInitHook("after_update_alert_list", (alertCount) => { + console.log("misc.js"); + console.log("alertCount:",alertCount); if(alertCount==0) { - $(".alerts").html("No new alerts"); + $(".alerts").html(phraseBox["alerts"]["alerts.no_alerts_short"]); $(".user_box").removeClass("has_alerts"); } else { + // TODO: Localise this $(".alerts").html(alertCount + " new alerts"); $(".user_box").addClass("has_alerts"); } diff --git a/themes/nox/theme.json b/themes/nox/theme.json index a76fa507..214af9bc 100644 --- a/themes/nox/theme.json +++ b/themes/nox/theme.json @@ -1,6 +1,6 @@ { "Name": "nox", - "FriendlyName": "Nox (WIP)", + "FriendlyName": "Nox (Incomplete)", "Version": "0.0.1", "Creator": "Azareal", "URL": "github.com/Azareal/Gosora", @@ -30,8 +30,7 @@ }, { "Name":"nox/misc.js", - "Location":"global", - "Loggedin":true + "Location":"global" } ] }