Skip to content

Commit ce13cf7

Browse files
[IMP] im_livechat: refactor _createThread
This commit refactor `_createThread` to use `discuss.channel` and rename it `_createChannel` closes odoo#232298 Related: odoo/enterprise#97629 Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
1 parent 69f4a43 commit ce13cf7

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

addons/im_livechat/static/src/embed/common/attachment_upload_service_patch.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { patch } from "@web/core/utils/patch";
55
patch(AttachmentUploadService.prototype, {
66
async upload(thread, composer, file, options) {
77
if (thread.channel?.channel_type === "livechat" && thread.isTransient) {
8-
thread = await this.env.services["im_livechat.livechat"].persist(thread);
9-
if (!thread) {
8+
const channel = await this.env.services["im_livechat.livechat"].persist(thread);
9+
if (!channel) {
1010
return;
1111
}
12+
thread = channel.thread;
1213
thread.readyToSwapDeferred.resolve();
1314
composer = thread.composer;
1415
}

addons/im_livechat/static/src/embed/common/livechat_service.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ export class LivechatService {
5454
* @returns {Promise<import("models").Thread|undefined>}
5555
*/
5656
async open(options = {}) {
57-
const thread = await this._createThread({ persist: false, options });
58-
await thread?.openChatWindow({ focus: true });
59-
return thread;
57+
const channel = await this._createChannel({ persist: false, options });
58+
await channel?.openChatWindow({ focus: true });
6059
}
6160

6261
/**
@@ -75,25 +74,25 @@ export class LivechatService {
7574
await this.store.ChatWindow.get({ thread: temporaryThread })?.close({ force: true });
7675
temporaryThread?.delete();
7776
};
78-
const savedThread = await this._createThread({ originThread: thread, persist: true });
79-
if (!savedThread) {
77+
const savedChannel = await this._createChannel({ originThread: thread, persist: true });
78+
if (!savedChannel) {
8079
await deleteTemporary();
8180
return;
8281
}
83-
savedThread.fetchNewMessages();
82+
savedChannel.fetchNewMessages();
8483
this.env.services["mail.store"].initialize();
85-
savedThread.readyToSwapDeferred.then(async () => {
86-
if (!savedThread?.exists()) {
84+
savedChannel.readyToSwapDeferred.then(async () => {
85+
if (!savedChannel?.exists()) {
8786
return;
8887
}
8988
// Do not load unread messaes: new messages were loaded to avoid
9089
// flickering, we do not want another load that would result in the
9190
// same issue.
92-
savedThread.scrollUnread = false;
91+
savedChannel.scrollUnread = false;
9392
deleteTemporary();
94-
savedThread.openChatWindow({ focus: true });
93+
savedChannel.openChatWindow({ focus: true });
9594
});
96-
return savedThread;
95+
return savedChannel;
9796
}
9897

9998
/**
@@ -110,7 +109,7 @@ export class LivechatService {
110109
* @param {import("models").Thread} [param0.originThread]
111110
* @returns {Promise<import("models").Thread>}
112111
*/
113-
async _createThread({ originThread, persist = false, options = {} }) {
112+
async _createChannel({ originThread, persist = false, options = {} }) {
114113
const { store_data, channel_id } = await rpc(
115114
"/im_livechat/get_session",
116115
{
@@ -136,7 +135,7 @@ export class LivechatService {
136135
channel.livechat_operator_id.id,
137136
ONE_DAY_TTL * 7
138137
);
139-
return channel.thread;
138+
return channel;
140139
}
141140

142141
getSessionExtraParams(thread, options) {

addons/im_livechat/static/src/embed/common/thread_model_patch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ patch(Thread.prototype, {
118118
});
119119
this.messages.push(temporaryMsg);
120120
this?.chatbot?._simulateTyping(2 ** 31 - 1);
121-
const thread = await this.store.env.services["im_livechat.livechat"].persist(this);
121+
const channel = await this.store.env.services["im_livechat.livechat"].persist(this);
122122
temporaryMsg.author_id = this.store.self; // Might have been created after persist.
123-
if (!thread) {
123+
if (!channel) {
124124
return;
125125
}
126-
await thread.isLoadedDeferred;
127-
return thread.post(...arguments).then(() => thread.readyToSwapDeferred.resolve());
126+
await channel.isLoadedDeferred;
127+
return channel.post(...arguments).then(() => channel.readyToSwapDeferred.resolve());
128128
}
129129
const message = await super.post(...arguments);
130130
await this.chatbot?.processAnswer(message);

0 commit comments

Comments
 (0)