35 lines
966 B
TypeScript
35 lines
966 B
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { sendMessage, decryptIncomingMessage } from "@/utils/rooms/messages";
|
|
|
|
export type SendMessageState = { error: string } | { messageId: string } | null;
|
|
|
|
export async function sendMessageAction(
|
|
roomId: string,
|
|
_prevState: SendMessageState,
|
|
formData: FormData,
|
|
): Promise<SendMessageState> {
|
|
const value = formData.get("message");
|
|
if (typeof value !== "string" || !value.trim()) {
|
|
return { error: "Message cannot be empty." };
|
|
}
|
|
|
|
const result = await sendMessage(roomId, value.trim());
|
|
if (result.error) {
|
|
return { error: result.error };
|
|
}
|
|
|
|
revalidatePath(`/rooms/${roomId}`);
|
|
return { messageId: result.messageId };
|
|
}
|
|
|
|
export async function decryptMessageAction(
|
|
roomId: string,
|
|
encryptedValue: string,
|
|
iv: string,
|
|
authTag: string,
|
|
): Promise<{ plaintext: string } | { error: string }> {
|
|
return decryptIncomingMessage(roomId, encryptedValue, iv, authTag);
|
|
}
|