diff --git a/spec/retractions.js b/spec/retractions.js index 93dcbd7a7..c219110de 100644 --- a/spec/retractions.js +++ b/spec/retractions.js @@ -285,6 +285,12 @@ view.model.sendMessage('hello world'); await u.waitUntil(() => view.el.querySelectorAll('.chat-msg').length === 1); + const message = view.model.messages.at(0); + expect(view.model.messages.length).toBe(1); + expect(message.get('retracted')).toBeFalsy(); + expect(message.get('editable')).toBeTruthy(); + + const retract_button = await u.waitUntil(() => view.el.querySelector('.chat-msg__content .chat-msg__action-retract')); retract_button.click(); await u.waitUntil(() => u.isVisible(document.querySelector('#converse-modals .modal'))); @@ -304,9 +310,9 @@ ``+ ``); - const message = view.model.messages.at(0); expect(view.model.messages.length).toBe(1); expect(message.get('retracted')).toBeTruthy(); + expect(message.get('editable')).toBeFalsy(); expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1); const el = view.el.querySelector('.chat-msg--retracted .chat-msg__message'); expect(el.textContent.trim()).toBe('Romeo Montague has removed this message'); @@ -565,7 +571,8 @@ const message = view.model.messages.at(0); expect(message.get('retracted')).toBeTruthy(); - expect(message.get('is_ephemeral')).toBe(false); + expect(message.get('is_ephemeral')).toBeTruthy(); + expect(message.get('editable')).toBeFalsy(); const stanza_id = message.get(`stanza_id ${muc_jid}`); // The server responds with a retraction message @@ -584,7 +591,8 @@ expect(view.model.messages.length).toBe(1); expect(view.model.messages.at(0).get('retracted')).toBeTruthy(); - expect(view.model.messages.at(0).get('is_ephemeral')).toBe(false); + expect(view.model.messages.at(0).get('is_ephemeral')).toBe(true); + expect(view.model.messages.at(0).get('editable')).toBe(false); expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1); const el = view.el.querySelector('.chat-msg--retracted .chat-msg__message div'); expect(el.textContent).toBe('romeo has removed this message'); @@ -628,7 +636,6 @@ _converse.connection._dataRecv(test_utils.createRequest(error)); await u.waitUntil(() => view.el.querySelectorAll('.chat-error').length === 1); - await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 0); expect(view.model.messages.length).toBe(1); expect(view.model.messages.at(0).get('retracted')).toBeFalsy(); diff --git a/src/converse-chatview.js b/src/converse-chatview.js index c4c471cba..68e87dbfd 100644 --- a/src/converse-chatview.js +++ b/src/converse-chatview.js @@ -960,21 +960,6 @@ converse.plugins.add('converse-chatview', { this.insertIntoTextArea('', true, false); }, - /** - * Retract one of your messages in this chat - * @private - * @method _converse.ChatBoxView#retractOwnMessage - * @param { _converse.Message } message - The message which we're retracting. - */ - retractOwnMessage(message) { - this.model.sendRetractionMessage(message); - message.save({ - 'retracted': (new Date()).toISOString(), - 'retracted_id': message.get('origin_id'), - 'is_ephemeral': true - }); - }, - async onMessageRetractButtonClicked (ev) { ev.preventDefault(); const msg_el = u.ancestor(ev.target, '.message'); @@ -995,7 +980,7 @@ converse.plugins.add('converse-chatview', { } const result = await _converse.api.confirm(__('Confirm'), messages); if (result) { - this.retractOwnMessage(message); + this.model.retractOwnMessage(message); } }, diff --git a/src/converse-muc-views.js b/src/converse-muc-views.js index 9b2cbf1ef..15a3101ea 100644 --- a/src/converse-muc-views.js +++ b/src/converse-muc-views.js @@ -969,12 +969,8 @@ converse.plugins.add('converse-muc-views', { * @param { _converse.Message } message - The message which we're retracting. */ retractOwnMessage(message) { - this.model.sendRetractionMessage(message) + this.model.retractOwnMessage(message) .catch(e => { - message.save({ - 'retracted': undefined, - 'retracted_id': undefined - }); const errmsg = __('Sorry, something went wrong while trying to retract your message.'); if (u.isErrorStanza(e)) { this.showErrorMessage(errmsg); @@ -984,10 +980,6 @@ converse.plugins.add('converse-muc-views', { } log.error(e); }); - message.save({ - 'retracted': (new Date()).toISOString(), - 'retracted_id': message.get('origin_id') - }); }, /** diff --git a/src/headless/converse-chat.js b/src/headless/converse-chat.js index 2024158b9..4e1eba767 100644 --- a/src/headless/converse-chat.js +++ b/src/headless/converse-chat.js @@ -708,6 +708,22 @@ converse.plugins.add('converse-chat', { } }, + /** + * Retract one of your messages in this chat + * @private + * @method _converse.ChatBoxView#retractOwnMessage + * @param { _converse.Message } message - The message which we're retracting. + */ + retractOwnMessage(message) { + this.sendRetractionMessage(message) + message.save({ + 'retracted': (new Date()).toISOString(), + 'retracted_id': message.get('origin_id'), + 'is_ephemeral': true, + 'editable': false + }); + }, + /** * Sends a message stanza to retract a message in this chat * @private @@ -899,8 +915,7 @@ converse.plugins.add('converse-chat', { } if (_converse.allow_message_corrections === 'all') { attrs.editable = !(attrs.file || attrs.retracted || 'oob_url' in attrs); - } else if ((_converse.allow_message_corrections === 'last') && - (send_time > this.get('time_sent'))) { + } else if ((_converse.allow_message_corrections === 'last') && (send_time > this.get('time_sent'))) { this.set({'time_sent': send_time}); const msg = this.messages.findWhere({'editable': true}); if (msg) { diff --git a/src/headless/converse-muc.js b/src/headless/converse-muc.js index 337828cf6..c8d957edb 100644 --- a/src/headless/converse-muc.js +++ b/src/headless/converse-muc.js @@ -647,6 +647,35 @@ converse.plugins.add('converse-muc', { return promise; }, + /** + * Retract one of your messages in this chat + * @private + * @method _converse.ChatRoom#retractOwnMessage + * @param { _converse.Message } message - The message which we're retracting. + */ + async retractOwnMessage(message) { + const editable = message.get('editable'); + const is_ephemeral = message.get('is_ephemeral'); + // Optimistic save + message.save({ + 'retracted': (new Date()).toISOString(), + 'retracted_id': message.get('origin_id'), + 'is_ephemeral': true, + 'editable': false + }); + try { + await this.sendRetractionMessage(message) + } catch (e) { + message.save({ + is_ephemeral, + editable, + 'retracted': undefined, + 'retracted_id': undefined, + }); + throw e; + } + }, + /** * Sends a message stanza to retract a message in this groupchat. * @private