From 68c5ebbbeafd474e0ee3f77669072163e5dd65a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Thu, 5 Jul 2018 12:08:08 +0200 Subject: [PATCH] [IMP] im_livechat, mail: website livechat model Make use of a website livechat model, so that im_livechat can make use of this model. This provides an almost similar model for threads in the backend and in the frontend. As a consequence, im_livechat and website livechat windows have been adapted to handle a website livechat model. --- .../im_livechat/static/src/js/im_livechat.js | 12 ++-- .../static/src/js/models/website_livechat.js | 72 +++++++++++++++++++ .../static/src/js/website_livechat_window.js | 36 ++++------ .../src/js/models/threads/abstract_thread.js | 2 +- .../threads/create_mode_document_thread.js | 2 +- addons/mail/static/src/js/thread_widget.js | 2 +- 6 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 addons/im_livechat/static/src/js/models/website_livechat.js diff --git a/addons/im_livechat/static/src/js/im_livechat.js b/addons/im_livechat/static/src/js/im_livechat.js index 964edddbadc..a63b731fd04 100644 --- a/addons/im_livechat/static/src/js/im_livechat.js +++ b/addons/im_livechat/static/src/js/im_livechat.js @@ -196,19 +196,19 @@ var LivechatButton = Widget.extend({ anonymous_name : this.options.default_username, }, {shadow: true}); } - def.then(function (channel) { - if (!channel || !channel.operator_pid) { + def.then(function (livechatData) { + if (!livechatData || !livechatData.operator_pid) { alert(_t("None of our collaborators seems to be available, please try again later.")); } else { - self._channel = channel; - self._openChatWindow(channel); + self._channel = new WebsiteLivechat(livechatData); + self._openChatWindow(channel); // AKU: TODO self._sendWelcomeMessage(); self._renderMessages(); - self._busBus.add_channel(channel.uuid); + self._busBus.add_channel(channel.uuid); // AKU: TODO self._busBus.start_polling(); - utils.set_cookie('im_livechat_session', JSON.stringify(channel), 60*60); + utils.set_cookie('im_livechat_session', JSON.stringify(channel), 60*60); // AKU: TODO utils.set_cookie('im_livechat_auto_popup', JSON.stringify(false), 60*60); } }).always(function () { diff --git a/addons/im_livechat/static/src/js/models/website_livechat.js b/addons/im_livechat/static/src/js/models/website_livechat.js new file mode 100644 index 00000000000..2131b149657 --- /dev/null +++ b/addons/im_livechat/static/src/js/models/website_livechat.js @@ -0,0 +1,72 @@ +odoo.define('im_livechat.model.WebsiteLivechat', function (require) { +"use strict"; + +var AbstractThread = require('mail.AbstractThread'); + +/** + * Thread model that represents a livechat on the website-side. This livechat + * is not linked to the mail service. + */ +var WebsiteLivechat = AbstractThread.extend({ + /** + * @param {Object} livechatData + * @param {integer} livechatData.id the ID of this livechat. + * @param {integer} [livechatData.message_unread_counter=0] the unread + * counter of this livechat. + * @param {string} livechatData.name the name of this livechat. + * @param {string} livechatData.state if 'folded', the livechat is folded. + */ + init: function (livechatData) { + var params = { data: livechatData }; + + this._super(params); + + this._folded = livechatData.state === 'folded'; + this._name = livechatData.name; + this._unreadCounter = livechatData.message_unread_counter || 0; + }, + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * @abstract + * @returns {mail.model.AbstractMessage[]} + */ + getMessages: function () {}, + /** + * @returns {string} + */ + getTitle: function () { + return this._name; + }, + /** + * @returns {integer} + */ + getUnreadCounter: function () { + return this._unreadCounter; + }, + /** + * Increments the unread counter of this livechat by 1 unit. + */ + incrementUnreadCounter: function () { + this._unreadCounter++; + }, + /** + * Resets the unread counter of this livechat to 0. + */ + resetUnreadCounter: function () { + this._unreadCounter = 0; + }, + /** + * @param {boolean} folded + */ + updateFoldState: function (folded) { + this._folded = folded; + }, +}); + +return WebsiteLivechat; + +}); diff --git a/addons/im_livechat/static/src/js/website_livechat_window.js b/addons/im_livechat/static/src/js/website_livechat_window.js index 660db0ad71e..c335b752aa0 100644 --- a/addons/im_livechat/static/src/js/website_livechat_window.js +++ b/addons/im_livechat/static/src/js/website_livechat_window.js @@ -10,21 +10,14 @@ var AbstractThreadWindow = require('mail.AbstractThreadWindow'); */ var LivechatWindow = AbstractThreadWindow.extend({ /** - * The thread of a live chat window is just an object containing some - * server data of the livechat - * * @override - * @param {mail.Manager} parent - * @param {Object} data - * @param {integer} data.id - * @param {string} data.name - * @param {string} data.state - * @param {integer} [data.message_unread_counter=0] + * @param {?} parent + * @param {im_livechat.model.WebsiteLivechat} thread */ - init: function (parent, data) { + init: function (parent, thread) { this._super.apply(this, arguments); - this._data = data; + this._thread = thread; }, //-------------------------------------------------------------------------- @@ -42,35 +35,35 @@ var LivechatWindow = AbstractThreadWindow.extend({ * @returns {string} */ getTitle: function () { - return this._data.name; + return this._thread.getTitle(); }, /** * @override * @returns {integer} */ getUnreadCounter: function () { - return this._data.message_unread_counter; + return this._thread.getUnreadCounter(); }, /** * @override * @returns {boolean} */ hasThread: function () { - return !!this._data; + return !!this._thread; }, /** * Increment the unread counter of this thread window, which is equivalent * to incrementing the unread counter of the related channel */ incrementUnreadCounter: function () { - this._data.message_unread_counter++; + this._thread.incrementUnreadCounter(); }, /** * @override * @returns {boolean} */ isFolded: function () { - return (this._data.state === 'folded'); + return this._thread.isFolded(); }, /** * Warn the parent widget (LivechatButton) @@ -86,7 +79,7 @@ var LivechatWindow = AbstractThreadWindow.extend({ * Reset the unread counter of the livechat window */ resetUnreadCounter: function () { - this._data.message_unread_counter = 0; + this._thread.resetUnreadCounter(); this._renderHeader(); }, @@ -102,7 +95,7 @@ var LivechatWindow = AbstractThreadWindow.extend({ * @returns {integer} */ _getThreadID: function () { - return this._data.id; + return this._thread.getID(); }, /** * @override @@ -118,14 +111,9 @@ var LivechatWindow = AbstractThreadWindow.extend({ * @param {boolean} folded */ _updateThreadFoldState: function (folded) { - this._data.state = folded ? 'folded' : 'open'; + this._thread.updateFoldState(folded); this.updateVisualFoldState(); }, - - //-------------------------------------------------------------------------- - // Handlers - //-------------------------------------------------------------------------- - }); return LivechatWindow; diff --git a/addons/mail/static/src/js/models/threads/abstract_thread.js b/addons/mail/static/src/js/models/threads/abstract_thread.js index 788a04307a7..08cf5cec2ab 100644 --- a/addons/mail/static/src/js/models/threads/abstract_thread.js +++ b/addons/mail/static/src/js/models/threads/abstract_thread.js @@ -13,7 +13,7 @@ var AbstractThread = Class.extend({ /** * @param {Object} params * @param {Object} params.data - * @param {Object} params.data.id the ID of this thread + * @param {integer|string} params.data.id the ID of this thread */ init: function (params) { this._id = params.data.id; diff --git a/addons/mail/static/src/js/models/threads/create_mode_document_thread.js b/addons/mail/static/src/js/models/threads/create_mode_document_thread.js index ab396984b54..da632de0470 100644 --- a/addons/mail/static/src/js/models/threads/create_mode_document_thread.js +++ b/addons/mail/static/src/js/models/threads/create_mode_document_thread.js @@ -12,7 +12,7 @@ var _t = core._t; /** * Special document thread that is linked to a new document that is in create * mode. This thread only display a single (fake) message which displays the - * user avatar and the message 'creating a new record...'. + * user avatar and the message 'Creating a new record...'. * * Note: it uses AbstractThread and AbstractMessage, because this is a fake * thread which contains a fake message. We do not want to register both of them diff --git a/addons/mail/static/src/js/thread_widget.js b/addons/mail/static/src/js/thread_widget.js index 65fb90fb35a..ab0dea1bece 100644 --- a/addons/mail/static/src/js/thread_widget.js +++ b/addons/mail/static/src/js/thread_widget.js @@ -122,7 +122,7 @@ var ThreadWidget = Widget.extend({ selectedMessageID: this._selectedMessageID, }); - // dict where key is message ID, and value is whether it should hide + // dict where key is message ID, and value is whether it should display // the author of message or not visually var displayAuthorMessages = {};