chat: Promisify addMessage()

This commit is contained in:
Richard Hansen 2021-10-25 19:51:37 -04:00
parent 3f7f629eeb
commit caac4bf711

View file

@ -105,7 +105,7 @@ exports.chat = (() => {
this._pad.collabClient.sendMessage({type: 'CHAT_MESSAGE', text}); this._pad.collabClient.sendMessage({type: 'CHAT_MESSAGE', text});
$('#chatinput').val(''); $('#chatinput').val('');
}, },
addMessage(msg, increment, isHistoryAdd) { async addMessage(msg, increment, isHistoryAdd) {
// correct the time // correct the time
msg.time += this._pad.clientTimeOffset; msg.time += this._pad.clientTimeOffset;
@ -161,49 +161,47 @@ exports.chat = (() => {
ctx.sticky = true; ctx.sticky = true;
} }
// Call chat message hook await hooks.aCallAll('chatNewMessage', ctx);
hooks.aCallAll('chatNewMessage', ctx, () => { const cls = authorClass(ctx.author);
const cls = authorClass(ctx.author); const chatMsg = $('<p>')
const chatMsg = $('<p>') .attr('data-authorId', ctx.author)
.attr('data-authorId', ctx.author) .addClass(cls)
.addClass(cls) .append($('<b>').text(`${ctx.authorName}:`))
.append($('<b>').text(`${ctx.authorName}:`)) .append($('<span>')
.append($('<span>') .addClass('time')
.addClass('time') .addClass(cls)
.addClass(cls) // Hook functions are trusted to not introduce an XSS vulnerability by adding
// Hook functions are trusted to not introduce an XSS vulnerability by adding // unescaped user input to ctx.timeStr.
// unescaped user input to ctx.timeStr. .html(ctx.timeStr))
.html(ctx.timeStr)) .append(' ')
.append(' ') // ctx.text was HTML-escaped before calling the hook. Hook functions are trusted to not
// ctx.text was HTML-escaped before calling the hook. Hook functions are trusted to not // introduce an XSS vulnerability by adding unescaped user input.
// introduce an XSS vulnerability by adding unescaped user input. .append($('<div>').html(ctx.text).contents());
.append($('<div>').html(ctx.text).contents()); if (isHistoryAdd) chatMsg.insertAfter('#chatloadmessagesbutton');
if (isHistoryAdd) chatMsg.insertAfter('#chatloadmessagesbutton'); else $('#chattext').append(chatMsg);
else $('#chattext').append(chatMsg);
// should we increment the counter?? // should we increment the counter??
if (increment && !isHistoryAdd) { if (increment && !isHistoryAdd) {
// Update the counter of unread messages // Update the counter of unread messages
let count = Number($('#chatcounter').text()); let count = Number($('#chatcounter').text());
count++; count++;
$('#chatcounter').text(count); $('#chatcounter').text(count);
if (!chatOpen && ctx.duration > 0) { if (!chatOpen && ctx.duration > 0) {
$.gritter.add({ $.gritter.add({
text: $('<p>') text: $('<p>')
.append($('<span>').addClass('author-name').text(ctx.authorName)) .append($('<span>').addClass('author-name').text(ctx.authorName))
// ctx.text was HTML-escaped before calling the hook. Hook functions are trusted // ctx.text was HTML-escaped before calling the hook. Hook functions are trusted
// to not introduce an XSS vulnerability by adding unescaped user input. // to not introduce an XSS vulnerability by adding unescaped user input.
.append($('<div>').html(ctx.text).contents()), .append($('<div>').html(ctx.text).contents()),
sticky: ctx.sticky, sticky: ctx.sticky,
time: 5000, time: 5000,
position: 'bottom', position: 'bottom',
class_name: 'chat-gritter-msg', class_name: 'chat-gritter-msg',
}); });
}
} }
if (!isHistoryAdd) this.scrollDown(); }
}); if (!isHistoryAdd) this.scrollDown();
}, },
init(pad) { init(pad) {
this._pad = pad; this._pad = pad;