[FIX] im_livechat: avoid useless message fetch when chat is created

When a live chat is created on the server, the `/mail/thread/messages`
route is called. This is not required since we know for a fact the
thread was just created. This PR prevent this useless request.

task-3646201

closes odoo/odoo#146785

Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
This commit is contained in:
tsm-odoo
2023-12-20 09:55:06 +00:00
parent 67d975b94b
commit ba5585fea7
5 changed files with 54 additions and 5 deletions
@@ -2,5 +2,7 @@ declare module "models" {
export interface Thread {
chatbotTypingMessage: Message,
livechatWelcomeMessage: Message,
chatbotScriptId: number | null,
isNewlyCreated: boolean,
}
}
@@ -26,7 +26,10 @@ patch(Thread.prototype, {
patch(ThreadService.prototype, {
async fetchNewMessages(thread) {
if (thread.type !== "livechat" || this.livechatService.state === SESSION_STATE.PERSISTED) {
if (
thread.type !== "livechat" ||
(this.livechatService.state === SESSION_STATE.PERSISTED && !thread.isNewlyCreated)
) {
return super.fetchNewMessages(...arguments);
}
},
@@ -184,6 +184,7 @@ export class LivechatService {
*/
async getOrCreateThread({ persist = false } = {}) {
let threadData = this.sessionCookie;
let isNewlyCreated = false;
if (!threadData || (!threadData.uuid && persist)) {
const chatbotScriptId = this.sessionCookie
? this.sessionCookie.chatbot_script_id
@@ -199,6 +200,7 @@ export class LivechatService {
},
{ shadow: true }
);
isNewlyCreated = true;
}
if (!threadData?.operator_pid) {
this.notificationService.add(_t("No available collaborator, please try again later."));
@@ -213,9 +215,10 @@ export class LivechatService {
const thread = this.store.Thread.insert({
...threadData,
id: threadData.id ?? this.TEMPORARY_ID,
isLoaded: !threadData.id,
isLoaded: !threadData.id || isNewlyCreated,
model: "discuss.channel",
type: "livechat",
isNewlyCreated,
});
this.state = thread.uuid ? SESSION_STATE.PERSISTED : SESSION_STATE.CREATED;
if (this.state === SESSION_STATE.PERSISTED && !this.sessionInitialized) {
@@ -53,12 +53,16 @@ patch(Thread, {
});
patch(Thread.prototype, {
chatbotScriptId: null,
setup() {
super.setup();
this.chatbotTypingMessage = Record.one("Message");
this.livechatWelcomeMessage = Record.one("Message");
this.chatbotScriptId = null;
/**
* Indicates whether this thread was just created (i.e. no reload occurs
* since the creation).
*/
this.isNewlyCreated = false;
},
get isLastMessageFromCustomer() {
@@ -6,8 +6,10 @@ import { loadDefaultConfig, start } from "@im_livechat/../tests/embed/helper/tes
import { Command } from "@mail/../tests/helpers/command";
import { click, contains } from "@web/../tests/utils";
import { cookie } from "@web/core/browser/cookie";
import { click, contains, insertText } from "@web/../tests/utils";
import { triggerHotkey } from "@web/../tests/helpers/utils";
import { Deferred } from "@web/core/utils/concurrency";
QUnit.module("livechat service");
@@ -49,3 +51,38 @@ QUnit.test("previous operator prioritized", async () => {
click(".o-livechat-LivechatButton");
await contains(".o-mail-Message-author", { text: "John Doe" });
});
QUnit.test("Only necessary requests are made when creating a new chat", async (assert) => {
await startServer();
await loadDefaultConfig();
const linkPreviewDeferred = new Deferred();
await start({
mockRPC(route) {
if (!route.includes("assets")) {
assert.step(route);
}
if (route === "/mail/link_preview") {
linkPreviewDeferred.resolve();
}
},
});
await contains(".o-livechat-LivechatButton");
assert.verifySteps([
"/im_livechat/init",
"/web/webclient/load_menus", // called because menu_service is loaded in qunit bundle
"/mail/load_message_failures", // called because mail/core/web is loaded in qunit bundle
]);
await click(".o-livechat-LivechatButton");
assert.verifySteps(["/im_livechat/get_session"]);
await insertText(".o-mail-Composer-input", "Hello!");
assert.verifySteps([]);
await triggerHotkey("Enter");
await contains(".o-mail-Message", { text: "Hello!" });
await linkPreviewDeferred;
assert.verifySteps([
"/im_livechat/get_session",
"/mail/init_messaging",
"/mail/message/post",
"/mail/link_preview",
]);
});