[IMP] im_livechat: markup-ify im_livechat

Mark the message body as safe pretty much as soon as we receive the
message from the server:

* when receiving message-type notifications
* while loading history

Also reorder history loading a bit while at it:

* convert willStart to async
* immediately reverse & wrap history right there, seems unnecessary to
  wait until willStart since `reverse()` works in-place anyway
* when loading messages into the thread, `_.each` seems unnecessary,
  Array#forEach will do fine
This commit is contained in:
Xavier Morel
2021-07-20 05:41:35 +00:00
parent 9f9d913fb7
commit 75f13db36a
2 changed files with 14 additions and 19 deletions
@@ -71,31 +71,25 @@ var LivechatButton = Widget.extend({
this._messages = [];
this._serverURL = serverURL;
},
willStart: function () {
var self = this;
async willStart() {
var cookie = utils.get_cookie('im_livechat_session');
var ready;
if (!cookie) {
ready = session.rpc('/im_livechat/init', { channel_id: this.options.channel_id })
.then(function (result) {
if (!result.available_for_me) {
return Promise.reject();
}
self._rule = result.rule;
});
} else {
if (cookie) {
var channel = JSON.parse(cookie);
ready = session.rpc('/mail/chat_history', { uuid: channel.uuid, limit: 100 })
.then(function (history) {
self._history = history;
});
this._history = await session.rpc('/mail/chat_history', {uuid: channel.uuid, limit: 100});
this._history.reverse().forEach(message => { message.body = utils.Markup(message.body); });
} else {
const result = await session.rpc('/im_livechat/init', {channel_id: this.options.channel_id});
if (!result.available_for_me) {
return Promise.reject();
}
this._rule = result.rule;
}
return ready.then(this._loadQWebTemplate.bind(this));
return this._loadQWebTemplate();
},
start: function () {
this.$el.text(this.options.button_text);
if (this._history) {
_.each(this._history.reverse(), this._addMessage.bind(this));
this._history.forEach(m => this._addMessage(m));
this._openChat();
} else if (!config.device.isMobile && this._rule.action === 'auto_popup') {
var autoPopupCookie = utils.get_cookie('im_livechat_auto_popup');
@@ -204,6 +198,7 @@ var LivechatButton = Widget.extend({
if (this._messages.some(message => message.getID() === notificationData.id)) {
return;
}
notificationData.body = utils.Markup(notificationData.body);
this._addMessage(notificationData);
if (this._chatWindow.isFolded() || !this._chatWindow.isAtBottom()) {
this._livechat.incrementUnreadCounter();
@@ -400,7 +400,7 @@
</span>
</p>
<div class="o_thread_message_content">
<t t-raw="message.getBody()"/>
<t t-out="message.getBody()"/>
<t t-if="message.hasTrackingValues()">
<t t-if="message.hasSubtypeDescription()">
<p><t t-esc="message.getSubtypeDescription()"/></p>