chat: in addMessage(), be tolerant when userId is missing

For whatever reason (a bug, a database corruption, ...) the userId field in
"msg" can sometimes be missing.

In this case, let's be defensive, use "unknown" as userId and issue a warning
in the console, instead of crashing the client.

Fixes #3731 (really a patch, the underlying issue is still present)
This commit is contained in:
John McLear 2020-03-29 18:41:28 +00:00 committed by muxator
parent fa3e4b146a
commit 25bf460ac6

View file

@ -132,6 +132,16 @@ var chat = (function()
var timeStr = hours + ":" + minutes; var timeStr = hours + ":" + minutes;
//create the authorclass //create the authorclass
if (!msg.userId) {
/*
* If, for a bug or a database corruption, the message coming from the
* server does not contain the userId field (see for example #3731),
* let's be defensive and replace it with "unknown".
*/
msg.userId = "unknown";
console.warn('The "userId" field of a chat message coming from the server was not present. Replacing with "unknown". This may be a bug or a database corruption.');
}
var authorClass = "author-" + msg.userId.replace(/[^a-y0-9]/g, function(c) var authorClass = "author-" + msg.userId.replace(/[^a-y0-9]/g, function(c)
{ {
if (c == ".") return "-"; if (c == ".") return "-";