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.
This commit is contained in:
Azareal 2018-11-30 13:02:20 +10:00
parent 27fb79d4d5
commit 255dd92a65
7 changed files with 33 additions and 25 deletions

View File

@ -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",

View File

@ -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);
});
})();

View File

@ -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");
});
}

View File

@ -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 {

View File

@ -82,9 +82,9 @@
</form>
{{end}}
{{if .ItemList}}<div id="profile_comments_head" class="colstack_item colstack_head hash_hide">
<div id="profile_comments_head" class="colstack_item colstack_head hash_hide">
<div class="rowitem"><h1><a>{{lang "profile_comments_head"}}</a></h1></div>
</div>{{end}}
</div>
<div id="profile_comments" class="colstack_item hash_hide">{{template "profile_comments_row.html" . }}</div>
{{if .CurrentUser.Loggedin}}

View File

@ -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");
}

View File

@ -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"
}
]
}