From bb079f54cb175c850fe46fe48bf6319c4d792d16 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 27 Feb 2024 10:21:36 +0100 Subject: [PATCH] FIX: better handling of error on create DM (#25908) Prior to this fix we were correctly failing but just returning a generic error. --- .../chat/api/direct_messages_controller.rb | 3 +++ .../components/chat/user-card-button.gjs | 20 +++++++++++++------ plugins/chat/config/locales/server.en.yml | 1 + 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/plugins/chat/app/controllers/chat/api/direct_messages_controller.rb b/plugins/chat/app/controllers/chat/api/direct_messages_controller.rb index 547e521d3f3..fde4637d776 100644 --- a/plugins/chat/app/controllers/chat/api/direct_messages_controller.rb +++ b/plugins/chat/app/controllers/chat/api/direct_messages_controller.rb @@ -17,6 +17,9 @@ class Chat::Api::DirectMessagesController < Chat::ApiController on_failed_policy(:satisfies_dms_max_users_limit) do |policy| render_json_dump({ error: policy.reason }, status: 400) end + on_failed_policy(:can_create_direct_message) do |policy| + render_json_dump({ error: I18n.t("chat.errors.invalid_direct_message") }, status: 400) + end on_failed_policy(:actor_allows_dms) do render_json_error(I18n.t("chat.errors.actor_disallowed_dms")) end diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/user-card-button.gjs b/plugins/chat/assets/javascripts/discourse/components/chat/user-card-button.gjs index 702971d277f..d2d21b76cde 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat/user-card-button.gjs +++ b/plugins/chat/assets/javascripts/discourse/components/chat/user-card-button.gjs @@ -2,6 +2,7 @@ import Component from "@glimmer/component"; import { action } from "@ember/object"; import { inject as service } from "@ember/service"; import DButton from "discourse/components/d-button"; +import { popupAjaxError } from "discourse/lib/ajax-error"; export default class ChatUserCardButton extends Component { @service chat; @@ -13,13 +14,20 @@ export default class ChatUserCardButton extends Component { } @action - startChatting() { - return this.chat - .upsertDmChannel({ usernames: [this.args.user.username] }) - .then((channel) => { - this.router.transitionTo("chat.channel", ...channel.routeModels); - this.appEvents.trigger("card:close"); + async startChatting() { + try { + const channel = await this.chat.upsertDmChannel({ + usernames: [this.args.user.username], }); + + if (channel) { + this.router.transitionTo("chat.channel", ...channel.routeModels); + } + } catch (error) { + popupAjaxError(error); + } finally { + this.appEvents.trigger("card:close"); + } }