summaryrefslogtreecommitdiff
path: root/addons/website_livechat
diff options
context:
space:
mode:
authorAlexandre Kühn <aku@odoo.com>2020-09-10 16:34:50 +0000
committerAlexandre Kühn <aku@odoo.com>2020-09-24 17:59:12 +0000
commitb39f39b598d3a930034f9981712cf5ec41ad301c (patch)
tree26265a176e9731898ab4b9a13c5fdd048c775fa4 /addons/website_livechat
parent5fe3ad548e410fbe2eddd7f67aa4b89b81746f1a (diff)
[FIX] im_livechat, mail, website_livechat: open chat website visitor
Also fix an issue with livechats not being considered in 'chat' filter of messaging menu. Task-Id 2282426 closes odoo/odoo#58466 X-original-commit: 631e52536964763bbfe857305f023e5e67084e95 Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
Diffstat (limited to 'addons/website_livechat')
-rw-r--r--addons/website_livechat/models/website_visitor.py30
-rw-r--r--addons/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js32
-rw-r--r--addons/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler_tests.js98
-rw-r--r--addons/website_livechat/static/tests/helpers/mock_models.js33
-rw-r--r--addons/website_livechat/static/tests/helpers/mock_server.js52
-rw-r--r--addons/website_livechat/views/assets.xml6
-rw-r--r--addons/website_livechat/views/website_livechat.xml5
7 files changed, 247 insertions, 9 deletions
diff --git a/addons/website_livechat/models/website_visitor.py b/addons/website_livechat/models/website_visitor.py
index 21314c896b8..8a0017a7fed 100644
--- a/addons/website_livechat/models/website_visitor.py
+++ b/addons/website_livechat/models/website_visitor.py
@@ -57,8 +57,13 @@ class WebsiteVisitor(models.Model):
operator = self.env.user
country = visitor.country_id
visitor_name = "%s (%s)" % (visitor.display_name, country.name) if country else visitor.display_name
+ channel_partner_to_add = [(4, operator.partner_id.id)]
+ if visitor.partner_id:
+ channel_partner_to_add.append((4, visitor.partner_id.id))
+ else:
+ channel_partner_to_add.append((4, self.env.ref('base.public_partner').id))
mail_channel_vals_list.append({
- 'channel_partner_ids': [(4, operator.partner_id.id)],
+ 'channel_partner_ids': channel_partner_to_add,
'livechat_channel_id': visitor.website_id.channel_id.id,
'livechat_operator_id': self.env.user.partner_id.id,
'channel_type': 'livechat',
@@ -73,6 +78,25 @@ class WebsiteVisitor(models.Model):
if mail_channel_vals_list:
mail_channels = self.env['mail.channel'].create(mail_channel_vals_list)
# Open empty chatter to allow the operator to start chatting with the visitor.
- mail_channels_info = mail_channels.channel_info('channel_minimize')
+ values = {
+ 'fold_state': 'open',
+ 'is_minimized': True,
+ }
+ mail_channels_uuid = mail_channels.mapped('uuid')
+ domain = [('partner_id', '=', self.env.user.partner_id.id), ('channel_id.uuid', 'in', mail_channels_uuid)]
+ channel_partners = self.env['mail.channel.partner'].search(domain)
+ channel_partners.write(values)
+ mail_channels_info = mail_channels.channel_info('send_chat_request')
+ notifications = []
for mail_channel_info in mail_channels_info:
- self.env['bus.bus'].sendone((self._cr.dbname, 'res.partner', operator.partner_id.id), mail_channel_info)
+ notifications.append([(self._cr.dbname, 'res.partner', operator.partner_id.id), mail_channel_info])
+ self.env['bus.bus'].sendmany(notifications)
+
+ def _link_to_partner(self, partner, update_values=None):
+ """ Adapt partner in members of related livechats """
+ if partner:
+ self.mail_channel_ids.channel_partner_ids = [
+ (3, self.env.ref('base.public_partner').id),
+ (4, partner.id),
+ ]
+ super(WebsiteVisitor, self)._link_to_partner(partner, update_values=update_values)
diff --git a/addons/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js b/addons/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js
new file mode 100644
index 00000000000..d43957ba374
--- /dev/null
+++ b/addons/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js
@@ -0,0 +1,32 @@
+odoo.define('website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js', function (require) {
+'use strict';
+
+const { registerInstancePatchModel } = require('mail/static/src/model/model_core.js');
+
+registerInstancePatchModel('mail.messaging_notification_handler', 'website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js', {
+
+ //----------------------------------------------------------------------
+ // Private
+ //----------------------------------------------------------------------
+
+ /**
+ * @override
+ */
+ _handleNotificationPartner(data) {
+ const { info } = data;
+ if (info === 'send_chat_request') {
+ this._handleNotificationPartnerChannel(data);
+ const channel = this.env.models['mail.thread'].findFromIdentifyingData({
+ id: data.id,
+ model: 'mail.channel',
+ });
+ this.env.messaging.chatWindowManager.openThread(channel, {
+ makeActive: true,
+ });
+ return;
+ }
+ return this._super(data);
+ },
+});
+
+});
diff --git a/addons/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler_tests.js b/addons/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler_tests.js
new file mode 100644
index 00000000000..4130af641b3
--- /dev/null
+++ b/addons/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler_tests.js
@@ -0,0 +1,98 @@
+odoo.define('website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler_tests.js', function (require) {
+'use strict';
+
+const {
+ afterEach,
+ afterNextRender,
+ beforeEach,
+ start,
+} = require('mail/static/src/utils/test_utils.js');
+
+const FormView = require('web.FormView');
+const {
+ mock: {
+ intercept,
+ },
+} = require('web.test_utils');
+
+QUnit.module('website_livechat', {}, function () {
+QUnit.module('models', {}, function () {
+QUnit.module('messaging_notification_handler', {}, function () {
+QUnit.module('messaging_notification_handler_tests.js', {
+ beforeEach() {
+ beforeEach(this);
+
+ this.start = async params => {
+ const { env, widget } = await start(Object.assign({}, {
+ data: this.data,
+ }, params));
+ this.env = env;
+ this.widget = widget;
+ };
+ },
+ afterEach() {
+ afterEach(this);
+ },
+});
+
+QUnit.test('should open chat window on send chat request to website visitor', async function (assert) {
+ assert.expect(3);
+
+ this.data['website.visitor'].records.push({
+ id: 11,
+ name: "Visitor #11",
+ });
+ await this.start({
+ data: this.data,
+ hasChatWindow: true,
+ hasView: true,
+ // View params
+ View: FormView,
+ model: 'website.visitor',
+ arch: `
+ <form>
+ <header>
+ <button name="action_send_chat_request" string="Send chat request" class="btn btn-primary" type="button"/>
+ </header>
+ <field name="name"/>
+ </form>
+ `,
+ res_id: 11,
+ });
+ intercept(this.widget, 'execute_action', payload => {
+ this.env.services.rpc({
+ route: '/web/dataset/call_button',
+ params: {
+ args: [payload.data.env.resIDs],
+ kwargs: { context: payload.data.env.context },
+ method: payload.data.action_data.name,
+ model: payload.data.env.model,
+ }
+ });
+ });
+
+ await afterNextRender(() =>
+ document.querySelector('button[name="action_send_chat_request"]').click()
+ );
+ assert.containsOnce(
+ document.body,
+ '.o_ChatWindow',
+ "should have a chat window open after sending chat request to website visitor"
+ );
+ assert.hasClass(
+ document.querySelector('.o_ChatWindow'),
+ 'o-focused',
+ "chat window of livechat should be focused on open"
+ );
+ assert.strictEqual(
+ document.querySelector('.o_ChatWindowHeader_name').textContent,
+ "Visitor #11",
+ "chat window of livechat should have name of visitor in the name"
+ );
+});
+
+});
+});
+});
+
+});
diff --git a/addons/website_livechat/static/tests/helpers/mock_models.js b/addons/website_livechat/static/tests/helpers/mock_models.js
new file mode 100644
index 00000000000..93ae70331a8
--- /dev/null
+++ b/addons/website_livechat/static/tests/helpers/mock_models.js
@@ -0,0 +1,33 @@
+odoo.define('website_livechat/static/tests/helpers/mock_models.js', function (require) {
+'use strict';
+
+const MockModels = require('mail/static/tests/helpers/mock_models.js');
+
+MockModels.patch('website_livechat/static/tests/helpers/mock_models.js', T =>
+ class extends T {
+
+ //----------------------------------------------------------------------
+ // Public
+ //----------------------------------------------------------------------
+
+ /**
+ * @override
+ */
+ static generateData() {
+ const data = super.generateData(...arguments);
+ Object.assign(data, {
+ 'website.visitor': {
+ fields: {},
+ records: [],
+ },
+ });
+ Object.assign(data['mail.channel'].fields, {
+ livechat_visitor_id: { string: "Visitor", type: 'many2one', relation: 'website.visitor' },
+ });
+ return data;
+ }
+
+ }
+);
+
+});
diff --git a/addons/website_livechat/static/tests/helpers/mock_server.js b/addons/website_livechat/static/tests/helpers/mock_server.js
new file mode 100644
index 00000000000..8a75f07abfe
--- /dev/null
+++ b/addons/website_livechat/static/tests/helpers/mock_server.js
@@ -0,0 +1,52 @@
+odoo.define('website_livechat/static/tests/helpers/mock_server.js', function (require) {
+'use strict';
+
+require('im_livechat/static/tests/helpers/mock_server.js'); // ensure mail overrides are applied first
+
+const MockServer = require('web.MockServer');
+
+MockServer.include({
+ /**
+ * Simulate a 'call_button' operation from a view.
+ *
+ * @override
+ */
+ _mockCallButton({ args, kwargs, method, model }) {
+ if (model === 'website.visitor' && method === 'action_send_chat_request') {
+ return this._mockWebsiteVisitorActionSendChatRequest(args[0]);
+ }
+ return this._super(...arguments);
+ },
+ /**
+ * @private
+ * @param {integer[]} ids
+ */
+ _mockWebsiteVisitorActionSendChatRequest(ids) {
+ const visitors = this._getRecords('website.visitor', [['id', 'in', ids]]);
+ for (const visitor of visitors) {
+ const country = visitor.country_id
+ ? this._getRecords('res.country', [['id', '=', visitor.country_id]])
+ : undefined;
+ const visitor_name = `${visitor.display_name}${country ? `(${country.name})` : ''}`;
+ const members = [this.currentPartnerId];
+ if (visitor.partner_id) {
+ members.push(visitor.partner_id);
+ } else {
+ members.push(this.publicPartnerId);
+ }
+ const livechatId = this._mockCreate('mail.channel', {
+ anonymous_name: visitor_name,
+ channel_type: 'livechat',
+ livechat_operator_id: this.currentPartnerId,
+ members,
+ public: 'private',
+ });
+ // notify operator
+ const channelInfo = this._mockMailChannelChannelInfo([livechatId], 'send_chat_request')[0];
+ const notification = [[false, 'res.partner', this.currentPartnerId], channelInfo];
+ this._widget.call('bus_service', 'trigger', 'notification', [notification]);
+ }
+ },
+});
+
+});
diff --git a/addons/website_livechat/views/assets.xml b/addons/website_livechat/views/assets.xml
index 0311dbdf6e8..92f52b64f75 100644
--- a/addons/website_livechat/views/assets.xml
+++ b/addons/website_livechat/views/assets.xml
@@ -5,6 +5,7 @@
<template id="assets_backend" name="website livechat assets" inherit_id="web.assets_backend">
<xpath expr="script[last()]" position="after">
<script type="text/javascript" src="/website_livechat/static/src/bugfix/bugfix.js"></script>
+ <script type="text/javascript" src="/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js"/>
</xpath>
<xpath expr="link[last()]" position="after">
<link rel="stylesheet" type="text/scss" href="/website_livechat/static/src/bugfix/bugfix.scss"/>
@@ -21,7 +22,10 @@
<template id="qunit_suite" name="website_livechat_tests" inherit_id="web.qunit_suite_tests">
<xpath expr="." position="inside">
- <script type="text/javascript" src="/website_livechat/static/src/bugfix/bugfix_tests.js"></script>
+ <script type="text/javascript" src="/website_livechat/static/src/bugfix/bugfix_tests.js"/>
+ <script type="text/javascript" src="/website_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler_tests.js"/>
+ <script type="text/javascript" src="/website_livechat/static/tests/helpers/mock_models.js"/>
+ <script type="text/javascript" src="/website_livechat/static/tests/helpers/mock_server.js"/>
</xpath>
</template>
</data>
diff --git a/addons/website_livechat/views/website_livechat.xml b/addons/website_livechat/views/website_livechat.xml
index 210d7398e00..49080d84a2c 100644
--- a/addons/website_livechat/views/website_livechat.xml
+++ b/addons/website_livechat/views/website_livechat.xml
@@ -1,11 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
- <template id="assets_backend" inherit_id="website.assets_backend" name="website_livechat assets backend">
- <xpath expr="." position="inside">
- </xpath>
- </template>
-
<!--
Integrate Livechat in Common Frontend for Website
Template registering all the assets required to execute the Livechat from a page containing Odoo