From ab59d0bf0935001a65182fec343638310cd73fd8 Mon Sep 17 00:00:00 2001 From: "Francois (fge)" Date: Fri, 11 Sep 2020 15:54:13 +0000 Subject: [PATCH] [FIX] mail: no longer display unlinked attachments in chatter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task-2337952 closes odoo/odoo#57631 X-original-commit: 55cb4a82ce2b59bc73ea05afa5971c96989b6159 Signed-off-by: Alexandre Kühn (aku) --- .../mail/static/src/models/thread/thread.js | 12 ++-- .../form_renderer/form_renderer_tests.js | 69 +++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/addons/mail/static/src/models/thread/thread.js b/addons/mail/static/src/models/thread/thread.js index efd33f9a0ce..ea67741f9a5 100644 --- a/addons/mail/static/src/models/thread/thread.js +++ b/addons/mail/static/src/models/thread/thread.js @@ -418,11 +418,13 @@ function factory(dependencies) { fields: ['id', 'name', 'mimetype'], orderBy: [{ name: 'id', asc: false }], })); - for (const attachmentData of attachmentsData) { - this.env.models['mail.attachment'].insert(Object.assign({ - originThread: [['link', this]], - }, this.env.models['mail.attachment'].convertData(attachmentData))); - } + this.update({ + originThreadAttachments: [['insert-and-replace', + attachmentsData.map(data => + this.env.models['mail.attachment'].convertData(data) + ) + ]], + }); this.update({ areAttachmentsLoaded: true }); } diff --git a/addons/mail/static/src/widgets/form_renderer/form_renderer_tests.js b/addons/mail/static/src/widgets/form_renderer/form_renderer_tests.js index cffd73b252f..88c95bdd748 100644 --- a/addons/mail/static/src/widgets/form_renderer/form_renderer_tests.js +++ b/addons/mail/static/src/widgets/form_renderer/form_renderer_tests.js @@ -853,6 +853,75 @@ QUnit.test('Form view not scrolled when switching record', async function (asser ); }); +QUnit.test('Attachments that have been unlinked from server should be visually unlinked from record', async function (assert) { + // Attachments that have been fetched from a record at certain time and then + // removed from the server should be reflected on the UI when the current + // partner accesses this record again. + assert.expect(2); + + this.data['res.partner'].records.push( + { display_name: "Partner1", id: 11 }, + { display_name: "Partner2", id: 12 } + ); + this.data['ir.attachment'].records.push( + { + id: 11, + mimetype: 'text.txt', + res_id: 11, + res_model: 'res.partner', + }, + { + id: 12, + mimetype: 'text.txt', + res_id: 11, + res_model: 'res.partner', + } + ); + await this.createView({ + data: this.data, + hasView: true, + // View params + View: FormView, + model: 'res.partner', + res_id: 11, + viewOptions: { + ids: [11, 12], + index: 0, + }, + arch: ` +
+ + + +
+ +
+
+ `, + }); + assert.strictEqual( + document.querySelector('.o_ChatterTopbar_buttonCount').textContent, + '2', + "Partner1 should have 2 attachments initially" + ); + + // The attachment links are updated on (re)load, + // so using pager is a way to reload the record "Partner1". + await afterNextRender(() => + document.querySelector('.o_pager_next').click() + ); + // Simulate unlinking attachment 12 from Partner 1. + this.data['ir.attachment'].records.find(a => a.id === 11).res_id = 0; + await afterNextRender(() => + document.querySelector('.o_pager_previous').click() + ); + assert.strictEqual( + document.querySelector('.o_ChatterTopbar_buttonCount').textContent, + '1', + "Partner1 should now have 1 attachment after it has been unlinked from server" + ); +}); + }); }); });