From 17b14ba60d2f53d65ea9b5225c7f0cd2c70b985b Mon Sep 17 00:00:00 2001 From: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com> Date: Fri, 9 May 2025 12:08:37 +0200 Subject: [PATCH] fix(ticket): disallow empty message --- api/src/api/errors.rs | 4 ++++ api/src/api/tickets.rs | 4 ++++ frontend/islands/TicketMessageInput.tsx | 27 +++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/api/src/api/errors.rs b/api/src/api/errors.rs index 4376cc62..42ffe452 100644 --- a/api/src/api/errors.rs +++ b/api/src/api/errors.rs @@ -245,6 +245,10 @@ errors!( status: NOT_FOUND, "The requested ticket was not found.", }, + TicketMessageEmpty { + status: BAD_REQUEST, + "The ticket message is empty.", + }, TicketMetaNotValid { status: BAD_REQUEST, "The metadata for the ticket is not in a valid format, should be a key-value of strings.", diff --git a/api/src/api/tickets.rs b/api/src/api/tickets.rs index 25ed1443..994fb9cc 100644 --- a/api/src/api/tickets.rs +++ b/api/src/api/tickets.rs @@ -117,6 +117,10 @@ pub async fn post_message_handler( return Err(ApiError::TicketNotFound); } + if new_message.message.is_empty() { + return Err(ApiError::TicketMessageEmpty); + } + let (message, message_author) = db .ticket_add_message(id, current_user.id, new_message) .await?; diff --git a/frontend/islands/TicketMessageInput.tsx b/frontend/islands/TicketMessageInput.tsx index 3ad8226a..0a002750 100644 --- a/frontend/islands/TicketMessageInput.tsx +++ b/frontend/islands/TicketMessageInput.tsx @@ -1,5 +1,5 @@ // Copyright 2024 the JSR authors. All rights reserved. MIT license. -import { useState } from "preact/hooks"; +import { useEffect, useState } from "preact/hooks"; import { TbCheck, TbClock } from "tb-icons"; import { AdminUpdateTicketRequest, @@ -13,6 +13,17 @@ export function TicketMessageInput( { ticket, user }: { ticket: Ticket; user: FullUser }, ) { const [message, setMessage] = useState(""); + const [error, setError] = useState(null); + + useEffect(() => { + if (error) { + const timeout = setTimeout(() => { + setError(null); + }, 3000); // 3 seconds + + return () => clearTimeout(timeout); + } + }, [error]); return (
{ e.preventDefault(); + if (message.trim() === "") { + setError("Message cannot be empty"); + return; + } + api.post( path`/tickets/${ticket.id}`, { @@ -42,7 +58,14 @@ export function TicketMessageInput( placeholder="Type your message here..." onChange={(e) => setMessage(e.currentTarget!.value)} /> -
+
+ {error && ( +
+

+ {error} +

+
+ )} {user.isStaff && (