Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep the message when a chat send is rate limited

Régis Hanolba7dedb5c3cc
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
index ade0728435c..62d43618a12 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
@@ -751,16 +751,7 @@ export default class ChatChannel extends Component {
_onSendError(id, error) {
const stagedMessage =
this.args.channel.messagesManager.findStagedMessage(id);
- if (stagedMessage) {
- if (error.jqXHR?.responseJSON?.errors?.length) {
- // only network errors are retryable
- stagedMessage.message = "";
- stagedMessage.cooked = "";
- stagedMessage.error = error.jqXHR.responseJSON.errors[0];
- } else {
- stagedMessage.error = "network_error";
- }
- }
+ stagedMessage?.setSendError(error);
this.resetComposerMessage();
}
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-thread-list.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-thread-list.gjs
index 4aaf8fdcf01..07b785e8e88 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-thread-list.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-thread-list.gjs
@@ -145,7 +145,7 @@ export default class ChatThreadList extends Component {
@bind
loadThreads() {
- this.threadsCollection.load({ limit: 10 });
+ this.threadsCollection.load({ limit: 10 }).catch(() => {});
}
@bind
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs
index 8d485685f6d..8fdd2c8f8f9 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs
@@ -482,7 +482,26 @@ export default class ChatThread extends Component {
}
@action
- resendStagedMessage() {}
+ resendStagedMessage(stagedMessage) {
+ this.chatThreadPane.sending = true;
+
+ stagedMessage.error = null;
+
+ this.chatApi
+ .sendMessage(this.args.thread.channel.id, {
+ cooked: stagedMessage.cooked,
+ message: stagedMessage.message,
+ upload_ids: stagedMessage.uploads.map((upload) => upload.id),
+ staged_id: stagedMessage.id,
+ thread_id: this.args.thread.id,
+ })
+ .catch((error) => {
+ stagedMessage.setSendError(error);
+ })
+ .finally(() => {
+ this.chatThreadPane.sending = false;
+ });
+ }
async #sendNewMessage(message) {
if (this.chatThreadPane.sending) {
@@ -560,13 +579,7 @@ export default class ChatThread extends Component {
#onSendError(stagedId, error) {
const stagedMessage =
this.args.thread.messagesManager.findStagedMessage(stagedId);
- if (stagedMessage) {
- if (error.jqXHR?.responseJSON?.errors?.length) {
- stagedMessage.error = error.jqXHR.responseJSON.errors[0];
- } else {
- stagedMessage.error = "network_error";
- }
- }
+ stagedMessage?.setSendError(error);
this.resetComposerMessage();
}
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/list/index.gjs b/plugins/chat/assets/javascripts/discourse/components/chat/list/index.gjs
index 7b7e454d4c4..f177e72325b 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat/list/index.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat/list/index.gjs
@@ -54,7 +54,7 @@ export default class List extends Component {
}
async debouncedLoadCollection() {
- await this.args.collection.load({ limit: 10 });
+ await this.args.collection.load({ limit: 10 }).catch(() => {});
}
<template>
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/message/error.gjs b/plugins/chat/assets/javascripts/discourse/components/chat/message/error.gjs
index eb08180f9a6..5dc13c78b2b 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat/message/error.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat/message/error.gjs
@@ -1,24 +1,36 @@
-import { fn } from "@ember/helper";
+import { fn, get } from "@ember/helper";
import { eq } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
import { i18n } from "discourse-i18n";
+import {
+ NETWORK_ERROR,
+ RATE_LIMIT_COOLDOWN_ERROR,
+ RATE_LIMIT_ERROR,
+} from "discourse/plugins/chat/discourse/lib/chat-constants";
+
+const RETRY_TITLES = {
+ [NETWORK_ERROR]: "chat.retry_staged_message.title",
+ [RATE_LIMIT_ERROR]: "chat.retry_staged_message.rate_limited_title",
+};
const Error = <template>
{{#if @message.error}}
<div class="chat-message-error">
- {{#if (eq @message.error "network_error")}}
+ {{#if (get RETRY_TITLES @message.error)}}
<DButton
class="chat-message-error__retry-btn"
@action={{fn @onRetry @message}}
@icon="circle-exclamation"
>
<span class="chat-message-error__retry-btn-title">
- {{i18n "chat.retry_staged_message.title"}}
+ {{i18n (get RETRY_TITLES @message.error)}}
</span>
<span class="chat-message-error__retry-btn-action">
{{i18n "chat.retry_staged_message.action"}}
</span>
</DButton>
+ {{else if (eq @message.error RATE_LIMIT_COOLDOWN_ERROR)}}
+ {{i18n "chat.retry_staged_message.rate_limited_waiting"}}
{{else}}
{{@message.error}}
{{/if}}
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/routes/channel-info-members.gjs b/plugins/chat/assets/javascripts/discourse/components/chat/routes/channel-info-members.gjs
index bac9a7ab173..14acd0fcb6a 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat/routes/channel-info-members.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat/routes/channel-info-members.gjs
@@ -152,7 +152,7 @@ export default class ChatRouteChannelInfoMembers extends Component {
async debouncedLoad() {
this.loadingSlider.transitionStarted();
- await this.members.load({ limit: 20 });
+ await this.members.load({ limit: 20 }).catch(() => {});
this.loadingSlider.transitionEnded();
}
diff --git a/plugins/chat/assets/javascripts/discourse/lib/chat-constants.js b/plugins/chat/assets/javascripts/discourse/lib/chat-constants.js
index 9e63cc7663f..162e51613c4 100644
--- a/plugins/chat/assets/javascripts/discourse/lib/chat-constants.js
+++ b/plugins/chat/assets/javascripts/discourse/lib/chat-constants.js
@@ -53,3 +53,6 @@ export const CHAT_SEPARATE_SIDEBAR_MODE_NEVER = "never";
export const MATCH_QUALITY_EXACT = 1;
export const MATCH_QUALITY_PREFIX = 2;
export const MATCH_QUALITY_PARTIAL = 3;
+export const NETWORK_ERROR = "network_error";
+export const RATE_LIMIT_ERROR = "rate_limit_error";
+export const RATE_LIMIT_COOLDOWN_ERROR = "rate_limit_cooldown_error";
diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-message.js b/plugins/chat/assets/javascripts/discourse/models/chat-message.js
index 7ea3463e428..5f74d883767 100644
--- a/plugins/chat/assets/javascripts/discourse/models/chat-message.js
+++ b/plugins/chat/assets/javascripts/discourse/models/chat-message.js
@@ -1,5 +1,10 @@
import { cached, tracked } from "@glimmer/tracking";
import { trackedArray } from "@ember/reactive/collections";
+import {
+ isRateLimitError,
+ MAX_RATE_LIMIT_RETRY_SECONDS,
+ rateLimitWaitSeconds,
+} from "discourse/lib/ajax-error";
import { removeValueFromArray } from "discourse/lib/array-tools";
import { getOwnerWithFallback } from "discourse/lib/get-owner";
import getURL from "discourse/lib/get-url";
@@ -8,6 +13,11 @@ import { generateCookFunction, parseMentions } from "discourse/lib/text";
import { autoTrackedArray } from "discourse/lib/tracked-tools";
import Bookmark from "discourse/models/bookmark";
import User from "discourse/models/user";
+import {
+ NETWORK_ERROR,
+ RATE_LIMIT_COOLDOWN_ERROR,
+ RATE_LIMIT_ERROR,
+} from "discourse/plugins/chat/discourse/lib/chat-constants";
import transformAutolinks from "discourse/plugins/chat/discourse/lib/transform-auto-links";
import ChatMessageReaction from "discourse/plugins/chat/discourse/models/chat-message-reaction";
@@ -227,6 +237,26 @@ export default class ChatMessage {
}, 2000);
}
+ setSendError(error) {
+ if (!isRateLimitError(error)) {
+ this.error = error.jqXHR?.responseJSON?.errors?.[0] ?? NETWORK_ERROR;
+ return;
+ }
+
+ this.error = RATE_LIMIT_COOLDOWN_ERROR;
+
+ discourseLater(
+ () => {
+ if (this.isDestroying || this.isDestroyed) {
+ return;
+ }
+
+ this.error = RATE_LIMIT_ERROR;
+ },
+ Math.min(rateLimitWaitSeconds(error), MAX_RATE_LIMIT_RETRY_SECONDS) * 1000
+ );
+ }
+
incrementVersion() {
this.version++;
}
diff --git a/plugins/chat/config/locales/client.en.yml b/plugins/chat/config/locales/client.en.yml
index 588438b8f6b..4631d1fa9db 100644
--- a/plugins/chat/config/locales/client.en.yml
+++ b/plugins/chat/config/locales/client.en.yml
@@ -323,6 +323,8 @@ en:
message_too_long: "Message is too long, messages must be a maximum of %{count} characters."
retry_staged_message:
title: "Network error"
+ rate_limited_title: "Too many messages"
+ rate_limited_waiting: "Too many messages, wait a moment"
action: "Send again?"
bookmark_message: "Bookmark"
bookmark_message_edit: "Edit Bookmark"
diff --git a/plugins/chat/test/javascripts/acceptance/chat-message-retry-test.js b/plugins/chat/test/javascripts/acceptance/chat-message-retry-test.js
index a505c350be6..46e85cb4a58 100644
--- a/plugins/chat/test/javascripts/acceptance/chat-message-retry-test.js
+++ b/plugins/chat/test/javascripts/acceptance/chat-message-retry-test.js
@@ -1,7 +1,11 @@
-import { click, fillIn, visit } from "@ember/test-helpers";
+import { click, fillIn, visit, waitFor } from "@ember/test-helpers";
import { test } from "qunit";
-import pretender, { response } from "discourse/tests/helpers/create-pretender";
+import pretender, {
+ controllerRateLimit,
+ response,
+} from "discourse/tests/helpers/create-pretender";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+import { i18n } from "discourse-i18n";
const CHANNEL_ID = 11;
@@ -63,4 +67,33 @@ acceptance("Retry message on send failure", function (needs) {
.dom(".chat-message-error__retry-btn")
.doesNotExist("clears the retry button after a successful resend");
});
+
+ test("keeps the message and offers a retry when rate limited", async function (assert) {
+ pretender.post(`/chat/${CHANNEL_ID}`, () => {
+ sendAttempt += 1;
+ return sendAttempt === 1
+ ? controllerRateLimit(1)
+ : response({ success: "OK" });
+ });
+
+ await visit(`/chat/c/-/${CHANNEL_ID}`);
+ await fillIn(".chat-composer__input", "hello there");
+ await click(".chat-composer .-send");
+
+ await waitFor(".chat-message-error__retry-btn");
+
+ assert
+ .dom(".chat-message-text")
+ .hasText("hello there", "keeps the message the user typed");
+
+ assert
+ .dom(".chat-message-error__retry-btn-title")
+ .hasText(i18n("chat.retry_staged_message.rate_limited_title"));
+
+ await click(".chat-message-error__retry-btn");
+
+ assert
+ .dom(".chat-message-error__retry-btn")
+ .doesNotExist("clears the retry button after a successful resend");
+ });
});
diff --git a/plugins/chat/test/javascripts/components/chat-channel-info-members-test.gjs b/plugins/chat/test/javascripts/components/chat-channel-info-members-test.gjs
new file mode 100644
index 00000000000..c9a0260f593
--- /dev/null
+++ b/plugins/chat/test/javascripts/components/chat-channel-info-members-test.gjs
@@ -0,0 +1,42 @@
+import { getOwner } from "@ember/owner";
+import { fillIn, render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import sinon from "sinon";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender, {
+ response,
+ TOO_MANY_REQUESTS,
+} from "discourse/tests/helpers/create-pretender";
+import ChatRouteChannelInfoMembers from "discourse/plugins/chat/discourse/components/chat/routes/channel-info-members";
+import ChatFabricators from "discourse/plugins/chat/discourse/lib/fabricators";
+
+const CHANNEL_ID = 1;
+
+module("Component | ChatRouteChannelInfoMembers", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("ends the loading slider when loading members fails", async function (assert) {
+ pretender.get(`/chat/api/channels/${CHANNEL_ID}/memberships`, () =>
+ response(TOO_MANY_REQUESTS, { errors: ["Slow down"] })
+ );
+
+ const dialog = getOwner(this).lookup("service:dialog");
+ sinon.stub(dialog, "alert");
+
+ const channel = new ChatFabricators(getOwner(this)).channel({
+ id: CHANNEL_ID,
+ });
+
+ await render(
+ <template><ChatRouteChannelInfoMembers @channel={{channel}} /></template>
+ );
+
+ await fillIn(".c-channel-members__filter input", "bob");
+
+ assert.false(
+ getOwner(this).lookup("service:loading-slider").loading,
+ "the loading bar is not left running"
+ );
+ assert.true(dialog.alert.notCalled, "no dialog interrupts the user");
+ });
+});