From bad8d093f503109c1f85a8c3e3a3e9ea3734e31b Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Mon, 6 Nov 2023 07:22:00 +0100 Subject: [PATCH] [FIX] mail: incorrect message order on previous messages load MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Discuss models use relationnal fields. Since [1], the implementation of the `unshift` method changed for `many` fields. The new implementation is incorrect: unshifting several records results in an insertion on the inverse order which differs from the native implementation. This issue broke the loading of older messages in discuss: older messages are inserted with the `unshift` method thus in the inverse order. [1]: https://github.com/odoo/odoo/pull/136539 closes odoo/odoo#140747 Signed-off-by: Alexandre Kühn (aku) --- addons/mail/static/src/core/common/record.js | 4 +-- addons/mail/static/tests/core/record_tests.js | 29 +++++++++++++++++++ .../mail/static/tests/thread/thread_tests.js | 3 +- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/addons/mail/static/src/core/common/record.js b/addons/mail/static/src/core/common/record.js index 956dcd229b5..19cfac2c2ab 100644 --- a/addons/mail/static/src/core/common/record.js +++ b/addons/mail/static/src/core/common/record.js @@ -215,8 +215,8 @@ export class RecordList extends Array { } /** @param {R[]} records */ unshift(...records) { - for (const val of records) { - const r = this._insert(val, (r3) => { + for (let i = records.length - 1; i >= 0; i--) { + const r = this._insert(records[i], (r3) => { this.data.unshift(r3.localId); r3.__uses__.add(this); }); diff --git a/addons/mail/static/tests/core/record_tests.js b/addons/mail/static/tests/core/record_tests.js index 51b1ff4fe97..a24c6cdf455 100644 --- a/addons/mail/static/tests/core/record_tests.js +++ b/addons/mail/static/tests/core/record_tests.js @@ -149,3 +149,32 @@ QUnit.test("Trusted insert on html field with { html: true }", async (assert) => assert.strictEqual(hello.body.toString(), "

hello

"); assert.strictEqual(world.body, "

world

"); }); + +QUnit.test("Unshift preserves order", async (assert) => { + (class Message extends Record { + static id = "id"; + id; + }).register(); + (class Thread extends Record { + static id = "name"; + name; + messages = Record.many("Message"); + }).register(); + const store = await start(); + const thread = store.Thread.insert({ name: "General" }); + thread.messages.unshift({ id: 3 }, { id: 2 }, { id: 1 }); + assert.deepEqual( + thread.messages.map((msg) => msg.id), + [3, 2, 1] + ); + thread.messages.unshift({ id: 6 }, { id: 5 }, { id: 4 }); + assert.deepEqual( + thread.messages.map((msg) => msg.id), + [6, 5, 4, 3, 2, 1] + ); + thread.messages.unshift({ id: 7 }); + assert.deepEqual( + thread.messages.map((msg) => msg.id), + [7, 6, 5, 4, 3, 2, 1] + ); +}); diff --git a/addons/mail/static/tests/thread/thread_tests.js b/addons/mail/static/tests/thread/thread_tests.js index 62a632596bb..0aa8b5a82ce 100644 --- a/addons/mail/static/tests/thread/thread_tests.js +++ b/addons/mail/static/tests/thread/thread_tests.js @@ -49,7 +49,7 @@ QUnit.test("load more messages from channel (auto-load on scroll)", async () => }); for (let i = 0; i <= 60; i++) { pyEnv["mail.message"].create({ - body: "not empty", + body: i.toString(), model: "discuss.channel", res_id: channelId, }); @@ -60,6 +60,7 @@ QUnit.test("load more messages from channel (auto-load on scroll)", async () => await contains(".o-mail-Thread", { scroll: "bottom" }); await scroll(".o-mail-Thread", 0); await contains(".o-mail-Message", { count: 60 }); + await contains(".o-mail-Message", { text: "30", after: [".o-mail-Message", { text: "29" }] }); }); QUnit.test("show message subject when subject is not the same as the thread name", async () => {