From 25bf460ac684a9d74454dfa29b6ccbffb2cbcad3 Mon Sep 17 00:00:00 2001 From: John McLear Date: Sun, 29 Mar 2020 18:41:28 +0000 Subject: [PATCH] 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) --- src/static/js/chat.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/static/js/chat.js b/src/static/js/chat.js index 8cc1d76c1..0b31428b6 100755 --- a/src/static/js/chat.js +++ b/src/static/js/chat.js @@ -132,6 +132,16 @@ var chat = (function() var timeStr = hours + ":" + minutes; //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) { if (c == ".") return "-";