Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 3800728

Browse files
authored
FIX: clear uploads after successfully posting new PM (#1307)
This PR addresses a bug where uploads weren't being cleared after successfully posting a new private message in the AI bot conversations interface. Here's what the changes do: ## Main Fix: - Makes the `prepareAndSubmitToBot()` method async and adds proper error handling - Adds `this.uploads.clear()` after successful submission to clear all uploads - Adds a test to verify that the "New Question" button properly resets the UI with no uploads ## Additional Improvements: 1. **Dynamic Character Length Validation**: - Uses `siteSettings.min_personal_message_post_length` instead of hardcoded 10 characters - Updates the error message to show the dynamic character count - Adds proper pluralization in the localization file for the error message 2. **Bug Fixes**: - Adds null checks with optional chaining (`link?.topic?.id`) in the sidebar code to prevent potential errors 3. **Code Organization**: - Moves error handling from the service to the controller for better separation of concerns
1 parent 9196546 commit 3800728

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

assets/javascripts/discourse/controllers/discourse-ai-bot-conversations.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Controller from "@ember/controller";
33
import { action } from "@ember/object";
44
import { getOwner } from "@ember/owner";
55
import { service } from "@ember/service";
6+
import { popupAjaxError } from "discourse/lib/ajax-error";
67
import UppyUpload from "discourse/lib/uppy/uppy-upload";
78
import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin";
89
import { clipboardHelpers } from "discourse/lib/utilities";
@@ -172,10 +173,15 @@ export default class DiscourseAiBotConversations extends Controller {
172173
}
173174

174175
@action
175-
prepareAndSubmitToBot() {
176+
async prepareAndSubmitToBot() {
176177
// Pass uploads to the service before submitting
177178
this.aiBotConversationsHiddenSubmit.uploads = this.uploads;
178-
this.aiBotConversationsHiddenSubmit.submitToBot();
179+
try {
180+
await this.aiBotConversationsHiddenSubmit.submitToBot();
181+
this.uploads.clear();
182+
} catch (error) {
183+
popupAjaxError(error);
184+
}
179185
}
180186

181187
_autoExpandTextarea() {

assets/javascripts/discourse/services/ai-bot-conversations-hidden-submit.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { next } from "@ember/runloop";
33
import Service, { service } from "@ember/service";
44
import { tracked } from "@ember-compat/tracked-built-ins";
55
import { ajax } from "discourse/lib/ajax";
6-
import { popupAjaxError } from "discourse/lib/ajax-error";
76
import { getUploadMarkdown } from "discourse/lib/uploads";
87
import { i18n } from "discourse-i18n";
98

@@ -13,6 +12,7 @@ export default class AiBotConversationsHiddenSubmit extends Service {
1312
@service composer;
1413
@service dialog;
1514
@service router;
15+
@service siteSettings;
1616

1717
@tracked loading = false;
1818

@@ -33,10 +33,14 @@ export default class AiBotConversationsHiddenSubmit extends Service {
3333

3434
@action
3535
async submitToBot() {
36-
if (this.inputValue.length < 10) {
36+
if (
37+
this.inputValue.length <
38+
this.siteSettings.min_personal_message_post_length
39+
) {
3740
return this.dialog.alert({
3841
message: i18n(
39-
"discourse_ai.ai_bot.conversations.min_input_length_message"
42+
"discourse_ai.ai_bot.conversations.min_input_length_message",
43+
{ count: this.siteSettings.min_personal_message_post_length }
4044
),
4145
didConfirm: () => this.focusInput(),
4246
didCancel: () => this.focusInput(),
@@ -89,8 +93,6 @@ export default class AiBotConversationsHiddenSubmit extends Service {
8993
});
9094

9195
this.router.transitionTo(response.post_url);
92-
} catch (e) {
93-
popupAjaxError(e);
9496
} finally {
9597
this.loading = false;
9698
}

assets/javascripts/initializers/ai-conversations-sidebar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ export default {
319319

320320
// force Glimmer to re-render that one link
321321
this.links = this.links.map((link) =>
322-
link.topic.id === topic.id
322+
link?.topic?.id === topic.id
323323
? new AiConversationLink(topic)
324324
: link
325325
);

config/locales/client.en.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,9 @@ en:
726726
disclaimer: "Generative AI can make mistakes. Verify important information."
727727
placeholder: "Ask a question..."
728728
new: "New Question"
729-
min_input_length_message: "Message must be longer than 10 characters"
729+
min_input_length_message:
730+
one: "Message must be 1 character or longer"
731+
other: "Message must be %{count} characters or longer"
730732
messages_sidebar_title: "Conversations"
731733
today: "Today"
732734
last_7_days: "Last 7 days"

spec/system/ai_bot/homepage_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@
131131
expect(topic_page).to have_content("Here are two image attachments")
132132
expect(page).to have_css(".cooked img", count: 2)
133133
end
134+
135+
find(".ai-new-question-button").click
136+
expect(ai_pm_homepage).to have_homepage
137+
expect(page).to have_no_css(".ai-bot-upload")
134138
end
135139

136140
it "allows removing an upload before submission" do

spec/system/page_objects/components/ai_pm_homepage.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ def submit
2020
def has_too_short_dialog?
2121
page.find(
2222
".dialog-content",
23-
text: I18n.t("js.discourse_ai.ai_bot.conversations.min_input_length_message"),
23+
text:
24+
I18n.t(
25+
"js.discourse_ai.ai_bot.conversations.min_input_length_message",
26+
count: SiteSetting.min_personal_message_post_length,
27+
),
2428
)
2529
end
2630

0 commit comments

Comments
 (0)