summaryrefslogtreecommitdiff
path: root/addons/website_livechat/static/src/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/website_livechat/static/src/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/website_livechat/static/src/models')
-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/src/models/thread/thread.js45
-rw-r--r--addons/website_livechat/static/src/models/visitor/visitor.js169
4 files changed, 344 insertions, 0 deletions
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 00000000..d43957ba
--- /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 00000000..4130af64
--- /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/src/models/thread/thread.js b/addons/website_livechat/static/src/models/thread/thread.js
new file mode 100644
index 00000000..46d80ca7
--- /dev/null
+++ b/addons/website_livechat/static/src/models/thread/thread.js
@@ -0,0 +1,45 @@
+odoo.define('website_livechat/static/src/models/thread/thread.js', function (require) {
+'use strict';
+
+const {
+ registerClassPatchModel,
+ registerFieldPatchModel,
+} = require('mail/static/src/model/model_core.js');
+const { many2one } = require('mail/static/src/model/model_field.js');
+
+registerClassPatchModel('mail.thread', 'website_livechat/static/src/models/thread/thread.js', {
+
+ //----------------------------------------------------------------------
+ // Public
+ //----------------------------------------------------------------------
+
+ /**
+ * @override
+ */
+ convertData(data) {
+ const data2 = this._super(data);
+ if ('visitor' in data) {
+ if (data.visitor) {
+ data2.visitor = [[
+ 'insert',
+ this.env.models['website_livechat.visitor'].convertData(data.visitor)
+ ]];
+ } else {
+ data2.visitor = [['unlink']];
+ }
+ }
+ return data2;
+ },
+
+});
+
+registerFieldPatchModel('mail.thread', 'website_livechat/static/src/models/thread/thread.js', {
+ /**
+ * Visitor connected to the livechat.
+ */
+ visitor: many2one('website_livechat.visitor', {
+ inverse: 'threads',
+ }),
+});
+
+});
diff --git a/addons/website_livechat/static/src/models/visitor/visitor.js b/addons/website_livechat/static/src/models/visitor/visitor.js
new file mode 100644
index 00000000..8c1b417e
--- /dev/null
+++ b/addons/website_livechat/static/src/models/visitor/visitor.js
@@ -0,0 +1,169 @@
+odoo.define('website_livechat/static/src/models/partner/partner.js', function (require) {
+'use strict';
+
+const { registerNewModel } = require('mail/static/src/model/model_core.js');
+const { attr, many2one, one2many } = require('mail/static/src/model/model_field.js');
+
+function factory(dependencies) {
+
+ class Visitor extends dependencies['mail.model'] {
+ //----------------------------------------------------------------------
+ // Public
+ //----------------------------------------------------------------------
+
+ /**
+ * @override
+ */
+ static convertData(data) {
+ const data2 = {};
+ if ('country_id' in data) {
+ if (data.country_id) {
+ data2.country = [['insert', {
+ id: data.country_id,
+ code: data.country_code,
+ }]];
+ } else {
+ data2.country = [['unlink']];
+ }
+ }
+ if ('history' in data) {
+ data2.history = data.history;
+ }
+ if ('is_connected' in data) {
+ data2.is_connected = data.is_connected;
+ }
+ if ('lang' in data) {
+ data2.lang = data.lang;
+ }
+ if ('name' in data) {
+ data2.name = data.name;
+ }
+ if ('partner_id' in data) {
+ if (data.partner_id) {
+ data2.partner = [['insert', { id: data.partner_id }]];
+ } else {
+ data2.partner = [['unlink']];
+ }
+ }
+ if ('website' in data) {
+ data2.website = data.website;
+ }
+ return data2;
+ }
+
+ //----------------------------------------------------------------------
+ // Private
+ //----------------------------------------------------------------------
+
+ /**
+ * @private
+ * @returns {string}
+ */
+ _computeAvatarUrl() {
+ if (!this.partner) {
+ return '/mail/static/src/img/smiley/avatar.jpg';
+ }
+ return this.partner.avatarUrl;
+ }
+
+ /**
+ * @private
+ * @returns {mail.country}
+ */
+ _computeCountry() {
+ if (this.partner && this.partner.country) {
+ return [['link', this.partner.country]];
+ }
+ if (this.country) {
+ return [['link', this.country]];
+ }
+ return [['unlink']];
+ }
+
+ /**
+ * @private
+ * @returns {string}
+ */
+ _computeNameOrDisplayName() {
+ if (this.partner) {
+ return this.partner.nameOrDisplayName;
+ }
+ return this.name;
+ }
+ }
+
+ Visitor.fields = {
+ /**
+ * Url to the avatar of the visitor.
+ */
+ avatarUrl: attr({
+ compute: '_computeAvatarUrl',
+ dependencies: [
+ 'partner',
+ 'partnerAvatarUrl',
+ ],
+ }),
+ /**
+ * Country of the visitor.
+ */
+ country: many2one('mail.country', {
+ compute: '_computeCountry',
+ dependencies: [
+ 'country',
+ 'partnerCountry',
+ ],
+ }),
+ /**
+ * Browsing history of the visitor as a string.
+ */
+ history: attr(),
+ /**
+ * Determine whether the visitor is connected or not.
+ */
+ is_connected: attr(),
+ /**
+ * Name of the language of the visitor. (Ex: "English")
+ */
+ lang: attr(),
+ /**
+ * Name of the visitor.
+ */
+ name: attr(),
+ nameOrDisplayName: attr({
+ compute: '_computeNameOrDisplayName',
+ dependencies: [
+ 'name',
+ 'partnerNameOrDisplayName',
+ ],
+ }),
+ /**
+ * Partner linked to this visitor, if any.
+ */
+ partner: many2one('mail.partner'),
+ partnerAvatarUrl: attr({
+ related: 'partner.avatarUrl',
+ }),
+ partnerCountry: many2one('mail.country',{
+ related: 'partner.country',
+ }),
+ partnerNameOrDisplayName: attr({related: 'partner.nameOrDisplayName'}),
+ /**
+ * Threads with this visitor as member
+ */
+ threads: one2many('mail.thread', {
+ inverse: 'visitor',
+ }),
+ /**
+ * Name of the website on which the visitor is connected. (Ex: "Website 1")
+ */
+ website: attr(),
+ };
+
+ Visitor.modelName = 'website_livechat.visitor';
+
+ return Visitor;
+}
+
+registerNewModel('website_livechat.visitor', factory);
+
+});