summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandre Kühn <aku@odoo.com>2018-07-05 13:17:15 +0200
committerAlexandre Kühn <aku@odoo.com>2018-07-19 13:58:34 +0200
commit0b6243104e7d898ce9ddadf9cac928c9893e8e68 (patch)
tree08f6b423ad16b57db08757c3e40d29ae370bd611
parent68c5ebbbeafd474e0ee3f77669072163e5dd65a5 (diff)
[IMP] im_livechat, website_livechat: rename `channel` to `livechat`, use livechat model
In im_livechat, it now handles a {mail.model.WebsiteLivechat}. Note that this website livechat model is dumb with messages: it stores messages just before rendering, because this is im_livechat that handles messages. This should be improved in the future.
-rw-r--r--addons/im_livechat/static/src/js/im_livechat.js49
-rw-r--r--addons/im_livechat/static/src/js/models/website_livechat.js74
-rw-r--r--addons/im_livechat/views/im_livechat_channel_templates.xml3
-rw-r--r--addons/website_livechat/views/website_livechat.xml3
4 files changed, 102 insertions, 27 deletions
diff --git a/addons/im_livechat/static/src/js/im_livechat.js b/addons/im_livechat/static/src/js/im_livechat.js
index a63b731fd04..7096143d2d6 100644
--- a/addons/im_livechat/static/src/js/im_livechat.js
+++ b/addons/im_livechat/static/src/js/im_livechat.js
@@ -10,6 +10,7 @@ var time = require('web.time');
var utils = require('web.utils');
var Widget = require('web.Widget');
+var WebsiteLivechat = require('im_livechat.model.WebsiteLivechat');
var WebsiteLivechatMessage = require('im_livechat.model.WebsiteLivechatMessage');
var WebsiteLivechatWindow = require('im_livechat.WebsiteLivechatWindow');
@@ -54,7 +55,11 @@ var LivechatButton = Widget.extend({
button_text: _t("Chat with one of our collaborators"),
default_message: _t("How may I help you?"),
});
- this._channel = null;
+
+ this._history = null;
+ // livechat model
+ this._livechat = null;
+ // livechat window
this._chatWindow = null;
this._messages = [];
this._serverURL = serverURL;
@@ -125,7 +130,7 @@ var LivechatButton = Widget.extend({
_askFeedback: function () {
this._chatWindow.$('.o_thread_composer input').prop('disabled', true);
- var feedback = new Feedback(this, this._channel);
+ var feedback = new Feedback(this, this._livechat);
feedback.replace(this._chatWindow.threadWidget.$el);
feedback.on('send_message', this, this._sendMessage);
@@ -143,13 +148,13 @@ var LivechatButton = Widget.extend({
* @param {Array} notification
*/
_handleNotification: function (notification){
- if (this._channel && (notification[0] === this._channel.uuid)) {
+ if (this._livechat && (notification[0] === this._livechat.getUUID())) {
if (notification[1]._type === 'history_command') { // history request
var cookie = utils.get_cookie(LIVECHAT_COOKIE_HISTORY);
var history = cookie ? JSON.parse(cookie) : [];
session.rpc('/im_livechat/history', {
- pid: this._channel.operator_pid[0],
- channel_uuid: this._channel.uuid,
+ pid: this._livechat.getOperatorPID()[0],
+ channel_uuid: this._livechat.getUUID(),
page_history: history,
});
} else { // normal message
@@ -200,15 +205,15 @@ var LivechatButton = Widget.extend({
if (!livechatData || !livechatData.operator_pid) {
alert(_t("None of our collaborators seems to be available, please try again later."));
} else {
- self._channel = new WebsiteLivechat(livechatData);
- self._openChatWindow(channel); // AKU: TODO
+ self._livechat = new WebsiteLivechat(livechatData);
+ self._openChatWindow();
self._sendWelcomeMessage();
self._renderMessages();
- self._busBus.add_channel(channel.uuid); // AKU: TODO
+ self._busBus.add_channel(self._livechat.getUUID());
self._busBus.start_polling();
- utils.set_cookie('im_livechat_session', JSON.stringify(channel), 60*60); // AKU: TODO
+ utils.set_cookie('im_livechat_session', JSON.stringify(self._livechat.toData()), 60*60);
utils.set_cookie('im_livechat_auto_popup', JSON.stringify(false), 60*60);
}
}).always(function () {
@@ -217,15 +222,14 @@ var LivechatButton = Widget.extend({
}, 200, true),
/**
* @private
- * @param {Object} channel
*/
- _openChatWindow: function (channel) {
+ _openChatWindow: function () {
var self = this;
var options = {
displayStars: false,
placeholder: this.options.input_placeholder || "",
};
- this._chatWindow = new WebsiteLivechatWindow(this, channel, options);
+ this._chatWindow = new WebsiteLivechatWindow(this, this._livechat, options);
this._chatWindow.appendTo($('body')).then(function () {
self._chatWindow.$el.css({right: 0, bottom: 0});
self.$el.hide();
@@ -260,7 +264,8 @@ var LivechatButton = Widget.extend({
*/
_renderMessages: function () {
var shouldScroll = !this._chatWindow.isFolded() && this._chatWindow.threadWidget.isAtBottom();
- this._chatWindow.render(this._messages); // AKU: TODO store messages in website livechat thread
+ this._livechat.setMessages(this._messages);
+ this._chatWindow.render();
if (shouldScroll) {
this._chatWindow.threadWidget.scrollToBottom();
}
@@ -273,7 +278,7 @@ var LivechatButton = Widget.extend({
_sendMessage: function (message) {
var self = this;
return session
- .rpc('/mail/chat_post', {uuid: this._channel.uuid, message_content: message.content})
+ .rpc('/mail/chat_post', {uuid: this._livechat.getUUID(), message_content: message.content})
.then(function () {
self._chatWindow.threadWidget.scrollToBottom();
});
@@ -286,9 +291,9 @@ var LivechatButton = Widget.extend({
this._addMessage({
id: '_welcome',
attachment_ids: [],
- author_id: this._channel.operator_pid,
+ author_id: this._livechat.getOperatorPID(),
body: this.options.default_message,
- channel_ids: [this._channel.id],
+ channel_ids: [this._livechat.getID()],
date: time.datetime_to_str(new Date()),
tracking_value_ids: [],
}, {prepend: true});
@@ -313,7 +318,7 @@ var LivechatButton = Widget.extend({
* @private
*/
_onSaveChat: function () {
- utils.set_cookie('im_livechat_session', JSON.stringify(this._channel), 60*60);
+ utils.set_cookie('im_livechat_session', JSON.stringify(this._livechat.toData()), 60*60);
},
});
@@ -332,9 +337,13 @@ var Feedback = Widget.extend({
'click .o_rating_submit_button': '_onClickSend',
},
- init: function (parent, channel) {
+ /**
+ * @param {?} parent
+ * @param {im_livechat.model.WebsiteLivechat} livechat
+ */
+ init: function (parent, livechat) {
this._super(parent);
- this._channel = channel;
+ this._livechat = livechat;
this.server_origin = session.origin;
this.rating = undefined;
},
@@ -350,7 +359,7 @@ var Feedback = Widget.extend({
_sendFeedback: function (options) {
var self = this;
var args = {
- uuid: this._channel.uuid,
+ uuid: this._livechat.getUUID(),
rate: this.rating,
reason : options.reason
};
diff --git a/addons/im_livechat/static/src/js/models/website_livechat.js b/addons/im_livechat/static/src/js/models/website_livechat.js
index 2131b149657..a2c49979154 100644
--- a/addons/im_livechat/static/src/js/models/website_livechat.js
+++ b/addons/im_livechat/static/src/js/models/website_livechat.js
@@ -1,7 +1,7 @@
odoo.define('im_livechat.model.WebsiteLivechat', function (require) {
"use strict";
-var AbstractThread = require('mail.AbstractThread');
+var AbstractThread = require('mail.model.AbstractThread');
/**
* Thread model that represents a livechat on the website-side. This livechat
@@ -10,20 +10,34 @@ var AbstractThread = require('mail.AbstractThread');
var WebsiteLivechat = AbstractThread.extend({
/**
* @param {Object} livechatData
+ * @param {boolean} [livechatData.folded] states whether the livechat is
+ * folded or not. It is considered only if this is defined and it is a
+ * boolean value.
* @param {integer} livechatData.id the ID of this livechat.
* @param {integer} [livechatData.message_unread_counter=0] the unread
* counter of this livechat.
+ * @param {Array} livechatData.operator_pid
* @param {string} livechatData.name the name of this livechat.
- * @param {string} livechatData.state if 'folded', the livechat is folded.
+ * @param {string} [livechatData.state] if 'folded', the livechat is folded.
+ * This is ignored if `folded` is provided and is a boolean value.
+ * @param {string} livechatData.uuid the UUID of this livechat.
*/
init: function (livechatData) {
var params = { data: livechatData };
this._super(params);
- this._folded = livechatData.state === 'folded';
+ this._folded = false;
this._name = livechatData.name;
+ this._operatorPID = livechatData.operator_pid;
this._unreadCounter = livechatData.message_unread_counter || 0;
+ this._uuid = livechatData.uuid;
+
+ if (_.isBoolean(livechatData.folded)) {
+ this._folded = livechatData.folded;
+ } else {
+ this._folded = livechatData.state === 'folded';
+ }
},
//--------------------------------------------------------------------------
@@ -31,35 +45,81 @@ var WebsiteLivechat = AbstractThread.extend({
//--------------------------------------------------------------------------
/**
- * @abstract
- * @returns {mail.model.AbstractMessage[]}
+ * @returns {im_livechat.model.WebsiteLivechatMessage[]}
*/
- getMessages: function () {},
+ getMessages: function () {
+ return this._messages;
+ },
/**
* @returns {string}
*/
- getTitle: function () {
+ getName: function () {
return this._name;
},
/**
+ * @returns {Array}
+ */
+ getOperatorPID: function () {
+ return this._operatorPID;
+ },
+ /**
+ * @returns {string}
+ */
+ getTitle: function () {
+ return this.getName();
+ },
+ /**
* @returns {integer}
*/
getUnreadCounter: function () {
return this._unreadCounter;
},
/**
+ * @returns {string}
+ */
+ getUUID: function () {
+ return this._uuid;
+ },
+ /**
* Increments the unread counter of this livechat by 1 unit.
*/
incrementUnreadCounter: function () {
this._unreadCounter++;
},
/**
+ * @returns {boolean}
+ */
+ isFolded: function () {
+ return this._folded;
+ },
+ /**
* Resets the unread counter of this livechat to 0.
*/
resetUnreadCounter: function () {
this._unreadCounter = 0;
},
/**
+ * AKU: hack for the moment
+ *
+ * @param {im_livechat.model.WebsiteLivechatMessage[]} messages
+ */
+ setMessages: function (messages) {
+ this._messages = messages;
+ },
+ /**
+ * @returns {Object}
+ */
+ toData: function () {
+ return {
+ folded: this.isFolded(),
+ id: this.getID(),
+ message_unread_counter: this.getUnreadCounter(),
+ operator_pid: this.getOperatorPID(),
+ name: this.getName(),
+ uuid: this.getUUID(),
+ };
+ },
+ /**
* @param {boolean} folded
*/
updateFoldState: function (folded) {
diff --git a/addons/im_livechat/views/im_livechat_channel_templates.xml b/addons/im_livechat/views/im_livechat_channel_templates.xml
index 5e46ae79d16..b12cba1b621 100644
--- a/addons/im_livechat/views/im_livechat_channel_templates.xml
+++ b/addons/im_livechat/views/im_livechat_channel_templates.xml
@@ -144,6 +144,9 @@
<script type="text/javascript" src="/mail/static/src/js/document_viewer.js"></script>
<script type="text/javascript" src="/im_livechat/static/src/js/im_livechat.js"></script>
<!-- Models -->
+ <!-- threads -->
+ <script type="text/javascript" src="/mail/static/src/js/models/threads/abstract_thread.js"></script>
+ <script type="text/javascript" src="/im_livechat/static/src/js/models/website_livechat.js"></script>
<!-- messages -->
<script type="text/javascript" src="/mail/static/src/js/models/messages/abstract_message.js"></script>
<script type="text/javascript" src="/im_livechat/static/src/js/models/website_livechat_message.js"></script>
diff --git a/addons/website_livechat/views/website_livechat.xml b/addons/website_livechat/views/website_livechat.xml
index 6914f75520b..8adf8c07555 100644
--- a/addons/website_livechat/views/website_livechat.xml
+++ b/addons/website_livechat/views/website_livechat.xml
@@ -17,6 +17,9 @@
<script type="text/javascript" src="/mail/static/src/js/utils.js"></script>
<script type="text/javascript" src="/im_livechat/static/src/js/im_livechat.js"></script>
<!-- Models -->
+ <!-- threads -->
+ <script type="text/javascript" src="/mail/static/src/js/models/threads/abstract_thread.js"></script>
+ <script type="text/javascript" src="/im_livechat/static/src/js/models/website_livechat.js"></script>
<!-- messages -->
<script type="text/javascript" src="/mail/static/src/js/models/messages/abstract_message.js"></script>
<script type="text/javascript" src="/im_livechat/static/src/js/models/website_livechat_message.js"></script>