From 398eaf0429ebc8dbb2221254c516ba6be701570d Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 25 May 2023 09:29:31 +0200 Subject: [PATCH] FIX: prevents saving draft in incorrect channel (#21741) This commit regroups 3 changes - serializes channel ID and json draft when calling the debouncer instead of inside the debounced function, it seems very unlikely to happen, but in a case where the debouncing wouldn't be canceled and the new message not set yet, we could save the draft on an invalid channel - cancel persist draft handler when changing channel - ensures we exit early when channel is not set --- .../javascripts/discourse/components/chat-composer.js | 3 +++ .../discourse/components/chat/composer/channel.js | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-composer.js b/plugins/chat/assets/javascripts/discourse/components/chat-composer.js index 771efb0053d..82a7085671d 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-composer.js +++ b/plugins/chat/assets/javascripts/discourse/components/chat-composer.js @@ -146,8 +146,11 @@ export default class ChatComposer extends Component { @action didUpdateChannel() { + this.cancelPersistDraft(); + if (!this.args.channel) { this.composer.message = null; + return; } this.composer.message = diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/composer/channel.js b/plugins/chat/assets/javascripts/discourse/components/chat/composer/channel.js index 7ec57d2a1f3..49a67daa4b7 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat/composer/channel.js +++ b/plugins/chat/assets/javascripts/discourse/components/chat/composer/channel.js @@ -29,16 +29,15 @@ export default class ChatComposerChannel extends ChatComposer { this._persistHandler = discourseDebounce( this, this._debouncedPersistDraft, + this.args.channel.id, + this.currentMessage.toJSONDraft(), 2000 ); } @action - _debouncedPersistDraft() { - this.chatApi.saveDraft( - this.args.channel.id, - this.currentMessage.toJSONDraft() - ); + _debouncedPersistDraft(channelId, jsonDraft) { + this.chatApi.saveDraft(channelId, jsonDraft); } get placeholder() {