[FIX] mail: prevent "record created" from being twice in the chatter

task-2280184

closes odoo/odoo#56694

X-original-commit: 1af2cc1b2190623739085de83870fb415cbdccf1
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
This commit is contained in:
Sébastien Theys
2020-08-27 17:14:52 +00:00
committed by Alexandre Kühn
parent b18ec3b024
commit 9965e35490
3 changed files with 88 additions and 3 deletions
@@ -156,7 +156,7 @@
</t>
<div class="o_Message_content" t-ref="content">
<t t-raw="message.prettyBody"/>
<t t-if="message.subtype_description">
<t t-if="message.subtype_description and !message.isBodyEqualSubtypeDescription">
<p t-esc="message.subtype_description"/>
</t>
<t t-if="trackingValues.length > 0">
@@ -108,7 +108,8 @@ QUnit.test('basic rendering', async function (assert) {
1,
"message should display the content"
);
assert.strictEqual(messageEl.querySelector(`:scope .o_Message_content`).innerHTML,
assert.strictEqual(
messageEl.querySelector(`:scope .o_Message_content`).innerHTML,
"<p>Test</p>",
"message should display the correct content"
);
@@ -738,6 +739,50 @@ QUnit.test('prevent attachment delete on non-authored message', async function (
);
});
QUnit.test('subtype description should be displayed if it is different than body', async function (assert) {
assert.expect(2);
await this.start();
const message = this.env.models['mail.message'].create({
body: "<p>Hello</p>",
id: 100,
subtype_description: 'Bonjour',
});
await this.createMessageComponent(message);
assert.containsOnce(
document.body,
'.o_Message_content',
"message should have content"
);
assert.strictEqual(
document.querySelector(`.o_Message_content`).textContent,
"HelloBonjour",
"message content should display both body and subtype description when they are different"
);
});
QUnit.test('subtype description should not be displayed if it is similar to body', async function (assert) {
assert.expect(2);
await this.start();
const message = this.env.models['mail.message'].create({
body: "<p>Hello</p>",
id: 100,
subtype_description: 'hello',
});
await this.createMessageComponent(message);
assert.containsOnce(
document.body,
'.o_Message_content',
"message should have content"
);
assert.strictEqual(
document.querySelector(`.o_Message_content`).textContent,
"Hello",
"message content should display only body when subtype description is similar"
);
});
});
});
});
@@ -4,7 +4,7 @@ odoo.define('mail/static/src/models/message/message.js', function (require) {
const emojis = require('mail.emojis');
const { registerNewModel } = require('mail/static/src/model/model_core.js');
const { attr, many2many, many2one, one2many } = require('mail/static/src/model/model_field.js');
const { addLink, parseAndTransform } = require('mail.utils');
const { addLink, htmlToTextContentInline, parseAndTransform } = require('mail.utils');
const { str_to_datetime } = require('web.time');
@@ -349,6 +349,18 @@ function factory(dependencies) {
);
}
/**
* @private
* @returns {boolean}
*/
_computeIsBodyEqualSubtypeDescription() {
if (!this.body || !this.subtype_description) {
return false;
}
const inlineBody = htmlToTextContentInline(this.body);
return inlineBody.toLowerCase() === this.subtype_description.toLowerCase();
}
/**
* @private
* @returns {boolean}
@@ -480,6 +492,34 @@ function factory(dependencies) {
'messagingCurrentPartner',
],
}),
/**
* States whether `body` and `subtype_description` contain similar
* values.
*
* This is necessary to avoid displaying both of them together when they
* contain duplicate information. This will especially happen with
* messages that are posted automatically at the creation of a record
* (messages that serve as tracking messages). They do have hard-coded
* "record created" body while being assigned a subtype with a
* description that states the same information.
*
* Fixing newer messages is possible by not assigning them a duplicate
* body content, but the check here is still necessary to handle
* existing messages.
*
* Limitations:
* - A translated subtype description might not match a non-translatable
* body created by a user with a different language.
* - Their content might be mostly but not exactly the same.
*/
isBodyEqualSubtypeDescription: attr({
compute: '_computeIsBodyEqualSubtypeDescription',
default: false,
dependencies: [
'body',
'subtype_description',
],
}),
isModeratedByCurrentPartner: attr({
compute: '_computeIsModeratedByCurrentPartner',
default: false,