From 1d5511421d33579602b7c52c0384ad5e3081604b Mon Sep 17 00:00:00 2001 From: Aine Date: Thu, 7 Nov 2024 12:15:18 +0200 Subject: [PATCH] better rate limits handling --- src/components/UserRateLimits.tsx | 15 ++++++++++----- src/i18n/de.ts | 2 +- src/i18n/en.ts | 2 +- src/i18n/fa.ts | 2 +- src/i18n/fr.ts | 2 +- src/i18n/it.ts | 2 +- src/i18n/ru.ts | 2 +- src/i18n/zh.ts | 2 +- src/synapse/dataProvider.ts | 14 +++++++++++++- 9 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/components/UserRateLimits.tsx b/src/components/UserRateLimits.tsx index 30925df..8d5b0cf 100644 --- a/src/components/UserRateLimits.tsx +++ b/src/components/UserRateLimits.tsx @@ -4,11 +4,16 @@ import { useDataProvider, useNotify, useRecordContext, useTranslate } from "reac import { TextField } from "@mui/material"; import { useFormContext } from "react-hook-form"; -const RateLimitRow = ({ limit, value, updateRateLimit }: { limit: string, value: number, updateRateLimit: (limit: string, value: number) => void }) => { +const RateLimitRow = ({ limit, value, updateRateLimit }: { limit: string, value: any, updateRateLimit: (limit: string, value: any) => void }) => { const translate = useTranslate(); const handleChange = (event: React.ChangeEvent) => { - updateRateLimit(limit, parseInt(event.target.value)); + const value = parseInt(event.target.value); + if (isNaN(value)) { + updateRateLimit(limit, null); + return; + } + updateRateLimit(limit, value); }; return { const form = useFormContext(); const dataProvider = useDataProvider(); const [rateLimits, setRateLimits] = useState({ - messages_per_second: 0, - burst_count: 0, + messages_per_second: "", // we are setting string here to make the number field empty by default, null is prohibited by the field validation + burst_count: "", }); if (!record) { @@ -67,7 +72,7 @@ export const UserRateLimits = () => { fetchRateLimits(); }, []); - const updateRateLimit = async (limit: string, value: number) => { + const updateRateLimit = async (limit: string, value: any) => { let updatedRateLimits = { ...rateLimits, [limit]: value }; setRateLimits(updatedRateLimits); form.setValue(`rates.${limit}`, value, { shouldDirty: true }); diff --git a/src/i18n/de.ts b/src/i18n/de.ts index 3020f82..c107e9d 100644 --- a/src/i18n/de.ts +++ b/src/i18n/de.ts @@ -194,7 +194,7 @@ const de: SynapseTranslationMessages = { }, limits: { messages_per_second: "Nachrichten pro Sekunde", - messages_per_second_text: "Die Anzahl der Aktionen, die in einer Sekunde durchgeführt werden können. 0 bedeutet, dass die Rate-Limitierung für diesen Benutzer deaktiviert ist.", + messages_per_second_text: "Die Anzahl der Aktionen, die in einer Sekunde durchgeführt werden können.", burst_count: "Burst-Anzahl", burst_count_text: "Die Anzahl der Aktionen, die vor der Begrenzung durchgeführt werden können.", } diff --git a/src/i18n/en.ts b/src/i18n/en.ts index f5185b5..491d56a 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -167,7 +167,7 @@ const en: SynapseTranslationMessages = { }, limits: { messages_per_second: "Messages per second", - messages_per_second_text: "The number of actions that can be performed in a second. 0 mean that ratelimiting is disabled for this user", + messages_per_second_text: "The number of actions that can be performed in a second.", burst_count: "Burst count", burst_count_text: "How many actions that can be performed before being limited.", } diff --git a/src/i18n/fa.ts b/src/i18n/fa.ts index db98252..eca17c6 100644 --- a/src/i18n/fa.ts +++ b/src/i18n/fa.ts @@ -159,7 +159,7 @@ const fa: SynapseTranslationMessages = { }, limits: { messages_per_second: "پیام در ثانیه", - messages_per_second_text: "تعداد عملیاتی که می تواند در یک ثانیه انجام شود. 0 به معنای غیرفعال کردن محدودیت برای این کاربر است.", + messages_per_second_text: "تعداد عملیاتی که می تواند در یک ثانیه انجام شود.", burst_count: "تعداد پیچیدگی", burst_count_text: "تعداد عملیاتی که می تواند قبل از محدودیت انجام شود.", } diff --git a/src/i18n/fr.ts b/src/i18n/fr.ts index 919f649..939f3e2 100644 --- a/src/i18n/fr.ts +++ b/src/i18n/fr.ts @@ -161,7 +161,7 @@ const fr: SynapseTranslationMessages = { }, limits: { messages_per_second: "Messages par seconde", - messages_per_second_text: "Le nombre d'actions que l'utilisateur peut effectuer par seconde. 0 signifie que la limitation est désactivée pour cet utilisateur.", + messages_per_second_text: "Le nombre d'actions que l'utilisateur peut effectuer par seconde.", burst_count: "Compteur de pics", burst_count_text: "Le nombre d'actions que l'utilisateur peut effectuer avant d'être limité.", } diff --git a/src/i18n/it.ts b/src/i18n/it.ts index 14609ae..4e23369 100644 --- a/src/i18n/it.ts +++ b/src/i18n/it.ts @@ -160,7 +160,7 @@ const it: SynapseTranslationMessages = { }, limits: { messages_per_second: "Messaggi al secondo", - messages_per_second_text: "Il numero di azioni che l'utente può eseguire al secondo. 0 significa che la limitazione è disabilitata per questo utente.", + messages_per_second_text: "Il numero di azioni che l'utente può eseguire al secondo.", burst_count: "Burst-conteggio", burst_count_text: "Il numero di azioni che l'utente può eseguire prima di essere limitato.", } diff --git a/src/i18n/ru.ts b/src/i18n/ru.ts index 15477ca..7ed9a32 100644 --- a/src/i18n/ru.ts +++ b/src/i18n/ru.ts @@ -197,7 +197,7 @@ const ru: SynapseTranslationMessages = { }, limits: { messages_per_second: "Сообщений в секунду", - messages_per_second_text: "Количество действий, которые могут быть выполнены в секунду. 0 означает, что ограничение на количество действий отключено для этого пользователя.", + messages_per_second_text: "Количество действий, которые могут быть выполнены в секунду.", burst_count: "Burst-счётчик", burst_count_text: "Количество действий, которые могут быть выполнены до ограничения.", } diff --git a/src/i18n/zh.ts b/src/i18n/zh.ts index 780ca33..cd4d8f0 100644 --- a/src/i18n/zh.ts +++ b/src/i18n/zh.ts @@ -184,7 +184,7 @@ const zh: SynapseTranslationMessages = { }, limits: { messages_per_second: "每秒消息数", - messages_per_second_text: "每秒可以执行的操作数。0 表示禁用此用户的限制。", + messages_per_second_text: "每秒可以执行的操作数。", burst_count: "Burst-计数", burst_count_text: "在限制之前可以执行的操作数。", } diff --git a/src/synapse/dataProvider.ts b/src/synapse/dataProvider.ts index afb481e..9537dca 100644 --- a/src/synapse/dataProvider.ts +++ b/src/synapse/dataProvider.ts @@ -830,9 +830,21 @@ const baseDataProvider: SynapseDataProvider = { return json as RateLimitsModel; }, setRateLimits: async (id: Identifier, rateLimits: RateLimitsModel) => { + const filtered = Object.entries(rateLimits). + filter(([key, value]) => value !== null && value !== undefined). + reduce((obj, [key, value]) => { + obj[key] = value; + return obj; + }, {}); + const base_url = storage.getItem("base_url"); const endpoint_url = `${base_url}/_synapse/admin/v1/users/${encodeURIComponent(returnMXID(id))}/override_ratelimit`; - await jsonClient(endpoint_url, { method: "POST", body: JSON.stringify(rateLimits) }); + if (Object.keys(filtered).length === 0) { + await jsonClient(endpoint_url, { method: "DELETE" }); + return + } + + await jsonClient(endpoint_url, { method: "POST", body: JSON.stringify(filtered) }); }, };