[FIX] mail_bot: fix OdooBot not starting

- fix initializer start override (typo in method name)
- properly wrap async code in models
- disable OdooBot for System user
- use existing python method to create chat

task-2284584

closes odoo/odoo#53669

X-original-commit: 47bbfeab9e238ab542d16bf4ed17d60ad6aea50d
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
This commit is contained in:
Sébastien Theys
2020-06-25 12:54:26 +00:00
parent 7ab957d7b0
commit 6da8fac5f4
5 changed files with 93 additions and 18 deletions
+4
View File
@@ -6,5 +6,9 @@
<field name="email">odoobot@example.com</field>
<field name="image_1920" type="base64" file="mail/static/src/img/odoobot.png"/>
</record>
<record id="base.user_root" model="res.users">
<field name="odoobot_state">disabled</field>
</record>
</data>
</odoo>
+2 -8
View File
@@ -14,15 +14,9 @@ class Channel(models.Model):
@api.model
def init_odoobot(self):
if self.env.user.odoobot_state in [False, 'not_initialized']:
partner = self.env.user.partner_id
odoobot_id = self.env['ir.model.data'].xmlid_to_res_id("base.partner_root")
channel = self.with_context(mail_create_nosubscribe=True).create({
'channel_partner_ids': [(4, partner.id), (4, odoobot_id)],
'public': 'private',
'channel_type': 'chat',
'email_send': False,
'name': 'OdooBot'
})
channel_info = self.channel_get([odoobot_id])
channel = self.browse(channel_info['id'])
message = _("Hello,<br/>Odoo's chat helps employees collaborate efficiently. I'm here to help you discover its features.<br/><b>Try to send me an emoji</b> <span class=\"o_odoobot_command\">:)</span>")
channel.sudo().message_post(body=message, author_id=odoobot_id, message_type="comment", subtype_xmlid="mail.mt_comment")
self.env.user.odoobot_state = 'onboarding_emoji'
@@ -11,23 +11,28 @@ registerInstancePatchModel('mail.messaging_initializer', 'mail_bot/static/src/mo
/**
* @private
*/
_showOdoobotTimeout() {
setTimeout(() => {
this.env.session.odoobot_initialized = true;
this.env.services.rpc({
model: 'mail.channel',
method: 'init_odoobot',
});
}, 2 * 60 * 1000);
async _initializeOdooBot() {
await this.async(() => new Promise(resolve =>
this.env.browser.setTimeout(resolve, 2 * 60 * 1000)
));
const data = await this.async(() => this.env.services.rpc({
model: 'mail.channel',
method: 'init_odoobot',
}));
if (!data) {
return;
}
this.env.session.odoobot_initialized = true;
},
/**
* @override
*/
async _start() {
async start() {
await this.async(() => this._super());
if ('odoobot_initialized' in this.env.session && !this.env.session.odoobot_initialized) {
this._showOdoobotTimeout();
this._initializeOdooBot();
}
},
});
@@ -0,0 +1,71 @@
odoo.define('mail_bot/static/src/models/messaging_initializer/messaging_initializer_tests.js', function (require) {
"use strict";
const {
afterEach: utilsAfterEach,
beforeEach: utilsBeforeEach,
start: utilsStart,
} = require('mail/static/src/utils/test_utils.js');
QUnit.module('mail_bot', {}, function () {
QUnit.module('models', {}, function () {
QUnit.module('messaging_initializer', {}, function () {
QUnit.module('messaging_initializer_tests.js', {
beforeEach() {
utilsBeforeEach(this);
this.start = async params => {
let { env, widget } = await utilsStart(Object.assign({}, params, {
data: this.data,
}));
this.env = env;
this.widget = widget;
};
},
afterEach() {
if (this.widget) {
this.widget.destroy();
}
this.env = undefined;
utilsAfterEach(this);
},
});
QUnit.test('OdooBot initialized after 2 minutes', async function (assert) {
assert.expect(3);
await this.start({
env: {
session: {
odoobot_initialized: false,
},
},
hasTimeControl: true,
async mockRPC(route, args) {
if (args.method === 'init_odoobot') {
assert.step('init_odoobot');
return;
}
return this._super(...arguments);
},
});
await this.env.testUtils.advanceTime(119 * 1000);
assert.verifySteps(
[],
"should not have initialized OdooBot after 1 minute 59 seconds"
);
await this.env.testUtils.advanceTime(1 * 1000);
assert.verifySteps(
['init_odoobot'],
"should have initialized OdooBot after 2 minutes"
);
});
});
});
});
});
+1
View File
@@ -23,6 +23,7 @@
<xpath expr="." position="inside">
<script type="text/javascript" src="/mail_bot/static/src/bugfix/bugfix_tests.js"></script>
<script type="text/javascript" src="/mail_bot/static/src/components/messaging_menu/messaging_menu_tests.js"></script>
<script type="text/javascript" src="/mail_bot/static/src/models/messaging_initializer/messaging_initializer_tests.js"></script>
<script type="text/javascript" src="/mail_bot/static/src/widgets/notification_alert/notification_alert_tests.js"></script>
</xpath>
</template>