Skip to content

fix(ticket): disallow empty message #1103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/src/api/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
4 changes: 4 additions & 0 deletions api/src/api/tickets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
27 changes: 25 additions & 2 deletions frontend/islands/TicketMessageInput.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,13 +13,29 @@ export function TicketMessageInput(
{ ticket, user }: { ticket: Ticket; user: FullUser },
) {
const [message, setMessage] = useState("");
const [error, setError] = useState<string | null>(null);

useEffect(() => {
if (error) {
const timeout = setTimeout(() => {
setError(null);
}, 3000); // 3 seconds

return () => clearTimeout(timeout);
}
}, [error]);

return (
<form
class="space-y-5"
onSubmit={(e) => {
e.preventDefault();

if (message.trim() === "") {
setError("Message cannot be empty");
return;
}

api.post(
path`/tickets/${ticket.id}`,
{
Expand All @@ -42,7 +58,14 @@ export function TicketMessageInput(
placeholder="Type your message here..."
onChange={(e) => setMessage(e.currentTarget!.value)}
/>
<div class="flex justify-end gap-4">
<div class="flex justify-end gap-4 items-center">
{error && (
<div class="text-red-500 font-semibold">
<p>
{error}
</p>
</div>
)}
<button type="submit" class="button-primary">Send message</button>
{user.isStaff && (
<button
Expand Down
Loading