- Clone import: defer FurnitureData.json writes and append all entries in a single batched write instead of one read-modify-write per item, removing the main serialization bottleneck for large batches. - Clone import: raise SSE batch concurrency cap from 5 to 10 and bump the clone client/route default from 2 to 6. - Add a flush hook to runSseBatch so callers can batch deferred work before batch_complete is emitted, and surface flush errors as an error event. - Enable Next.js Cache Components (instant: false opt-out) and silence the related build warnings in next.config.ts. - Switch isomorphic-dompurify to dompurify and refresh dependencies.
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
import { desc, inArray } from "drizzle-orm";
|
|
import { apiError, apiJson } from "@/lib/api";
|
|
import { bearerUserId } from "@/lib/api-auth";
|
|
import { db, RadioShouts, User } from "@/lib/db";
|
|
|
|
// Latest 50 radio shouts with their author's username/look resolved. Mirrors the
|
|
// query behind the public /radio/shouts page (radio_shouts ordered by created_at
|
|
// desc, then joined to users by user_id).
|
|
|
|
// Max shout length (radio_shouts.message is TEXT; cap to keep posts sane).
|
|
const MAX_MESSAGE_LENGTH = 255;
|
|
|
|
export async function GET(_req: Request) {
|
|
try {
|
|
const shouts = await db
|
|
.select()
|
|
.from(RadioShouts)
|
|
.orderBy(desc(RadioShouts.createdAt))
|
|
.limit(50);
|
|
|
|
// Resolve author usernames/looks. radio_shouts.user_id is an UnsignedBigInt
|
|
// while users.id is an Int, so narrow to Number for the lookup.
|
|
const authorIds = Array.from(new Set(shouts.map((s) => Number(s.userId))));
|
|
const authors = authorIds.length
|
|
? await db
|
|
.select({ id: User.id, username: User.username, look: User.look })
|
|
.from(User)
|
|
.where(inArray(User.id, authorIds))
|
|
: [];
|
|
const authorById = new Map(authors.map((a) => [a.id, a]));
|
|
|
|
const data = shouts.map((s) => {
|
|
const author = authorById.get(Number(s.userId));
|
|
return {
|
|
id: s.id,
|
|
userId: s.userId,
|
|
username: author?.username ?? null,
|
|
look: author?.look ?? null,
|
|
message: s.message,
|
|
createdAt: s.createdAt,
|
|
};
|
|
});
|
|
|
|
return apiJson({ shouts: data });
|
|
} catch {
|
|
// DB unavailable — serve an empty list rather than a 500.
|
|
return apiJson({ shouts: [] }, { status: 200 });
|
|
}
|
|
}
|
|
|
|
// Post a new radio shout as the Bearer-authed user into radio_shouts.
|
|
export async function POST(req: Request) {
|
|
const uid = await bearerUserId(req);
|
|
if (!uid) return apiError("Unauthorized", 401);
|
|
|
|
const body = (await req.json().catch(() => ({}))) as { message?: unknown };
|
|
const message = typeof body.message === "string" ? body.message.trim() : "";
|
|
|
|
if (!message) {
|
|
return apiError("Message is required", 422);
|
|
}
|
|
if (message.length > MAX_MESSAGE_LENGTH) {
|
|
return apiError(
|
|
`Message must be at most ${MAX_MESSAGE_LENGTH} characters`,
|
|
422,
|
|
);
|
|
}
|
|
|
|
try {
|
|
const now = new Date();
|
|
await db.insert(RadioShouts).values({
|
|
userId: BigInt(uid),
|
|
message,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
|
|
return apiJson({ ok: true });
|
|
} catch {
|
|
// DB write failed — fail soft rather than a 500.
|
|
return apiError("Could not post shout", 503);
|
|
}
|
|
}
|