import type { SQLOutputValue } from "node:sqlite"; import type { ChatMessage } from "../../shared/chat.ts"; export interface ChatMessageRow { id: string; user_id: string; body: string; created_at: string; updated_at: string | null; author_username: string; author_avatar_mime: string | null; // Reply columns: reply_to_id is the stored target; the author/body preview are // LEFT-JOINed from the target and go null when there's no reply (or it was // since deleted). reply_to_id: string | null; reply_to_author: string | null; reply_to_body: string | null; [key: string]: SQLOutputValue; } export function isChatMessageRow(obj: unknown): obj is ChatMessageRow { return !!obj && typeof obj === "object" && "id" in obj && typeof obj.id === "string" && "user_id" in obj && typeof obj.user_id === "string" && "body" in obj && typeof obj.body === "string" && "created_at" in obj && typeof obj.created_at === "string" && "updated_at" in obj && (typeof obj.updated_at === "string" || obj.updated_at === null) && "author_username" in obj && typeof obj.author_username === "string" && "author_avatar_mime" in obj && (typeof obj.author_avatar_mime === "string" || obj.author_avatar_mime === null) && "reply_to_id" in obj && (typeof obj.reply_to_id === "string" || obj.reply_to_id === null) && "reply_to_author" in obj && (typeof obj.reply_to_author === "string" || obj.reply_to_author === null) && "reply_to_body" in obj && (typeof obj.reply_to_body === "string" || obj.reply_to_body === null); } export function chatMessageRowToApi(row: ChatMessageRow): ChatMessage { return { id: row.id, userId: row.user_id, body: row.body, createdAt: new Date(row.created_at), updatedAt: row.updated_at ? new Date(row.updated_at) : undefined, authorUsername: row.author_username, authorAvatarMime: row.author_avatar_mime ?? undefined, replyToId: row.reply_to_id ?? undefined, replyToAuthor: row.reply_to_author ?? undefined, replyToBody: row.reply_to_body ?? undefined, }; }