Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Chat: a real 404 for someone else's chat, a route skeleton, and the share page beside its route

Adarsh19cf09ed2081
diff --git a/src/app/(studio)/chat/[id]/page.tsx b/src/app/(studio)/chat/[id]/page.tsx
index fa247df5..b5880ce6 100644
--- a/src/app/(studio)/chat/[id]/page.tsx
+++ b/src/app/(studio)/chat/[id]/page.tsx
@@ -1,11 +1,18 @@
import type { Metadata } from "next";
+import { headers } from "next/headers";
+import { notFound } from "next/navigation";
+import { connection } from "next/server";
import { Suspense } from "react";
import { ThreadView } from "@/components/chat/thread-view";
+import { recordFor } from "@/server/records";
export const metadata: Metadata = { title: "Chat" };
async function Thread({ params }: { params: PageProps<"/chat/[id]">["params"] }) {
const { id } = await params;
+ await connection();
+ // Someone else's chat, or one that's gone: a real not-found (the proxy answers a page load first).
+ if (!recordFor({ kind: "chat", ids: [id] }, await headers())) notFound();
return <ThreadView id={id} />;
}
diff --git a/src/app/(studio)/chat/loading.tsx b/src/app/(studio)/chat/loading.tsx
new file mode 100644
index 00000000..cec96360
--- /dev/null
+++ b/src/app/(studio)/chat/loading.tsx
@@ -0,0 +1 @@
+export { default } from "@/components/studio/route-loading";
diff --git a/src/app/c/[token]/page.tsx b/src/app/c/[token]/page.tsx
index 8540d9ac..4318b183 100644
--- a/src/app/c/[token]/page.tsx
+++ b/src/app/c/[token]/page.tsx
@@ -3,7 +3,7 @@ import { headers } from "next/headers";
import { notFound } from "next/navigation";
import { connection } from "next/server";
import { cache, Suspense } from "react";
-import { ChatSharePage } from "@/components/chat/share-page";
+import { ChatSharePage } from "../share-page";
import { countsAsView } from "@/lib/share";
import { countShareView, findChatShare } from "@/server/chat/store";
diff --git a/src/components/chat/share-page.tsx b/src/app/c/share-page.tsx
similarity index 96%
rename from src/components/chat/share-page.tsx
rename to src/app/c/share-page.tsx
index c1192c07..8659b938 100644
--- a/src/components/chat/share-page.tsx
+++ b/src/app/c/share-page.tsx
@@ -7,9 +7,9 @@ import { MANGO_CHILLI } from "@/lib/chat/catalog";
import { ADULT_OK_KEY } from "@/lib/chat/share";
import type { ChatShareSnapshot } from "@/lib/chat/types";
import { cn } from "@/lib/utils";
-import { CharacterFace } from "./character-face";
-import { CHILLI_BG } from "./dialogs";
-import { Markdown, ReplyBody } from "./prose";
+import { CharacterFace } from "@/components/chat/character-face";
+import { CHILLI_BG } from "@/components/chat/dialogs";
+import { Markdown, ReplyBody } from "@/components/chat/prose";
/**
* A shared chat's public page (/c/<token>): the character (or the game or tool), the shared lines, and a way in for the
diff --git a/src/lib/record-pages.ts b/src/lib/record-pages.ts
index 5017b4e1..47b40b7f 100644
--- a/src/lib/record-pages.ts
+++ b/src/lib/record-pages.ts
@@ -10,7 +10,7 @@ import { HUB_MODES, type HubKind } from "./hubs";
import { STARTER_IDS, starterCovered } from "./starters/ids";
import type { StarterSurface } from "./starters/types";
-export type RecordKind = "cinema" | "canvas" | "assist-project" | "layers" | "shorts" | "brand" | "element" | "channel" | "episode" | "explainer" | "series" | "swap-project";
+export type RecordKind = "cinema" | "canvas" | "assist-project" | "layers" | "shorts" | "brand" | "element" | "channel" | "episode" | "explainer" | "series" | "swap-project" | "chat";
/** A record a page needs, with its ids in the order the lookup takes them. */
export interface RecordRef {
@@ -35,6 +35,7 @@ const PAGES: [RegExp, (m: string[]) => RecordRef][] = [
[new RegExp(`^/explainer/${SEG}$`), ([id]) => ({ kind: "explainer", ids: [id] })],
[new RegExp(`^/content-factory/${SEG}$`), ([id]) => ({ kind: "series", ids: [id] })],
[new RegExp(`^/character-swap/${SEG}$`), ([id]) => ({ kind: "swap-project", ids: [id] })],
+ [new RegExp(`^/chat/${SEG}$`), ([id]) => ({ kind: "chat", ids: [id] })],
];
/** Static pages that a record pattern would also match (/ads/multiplier is not a brand). */
diff --git a/src/server/records.ts b/src/server/records.ts
index 413ca4d2..6815037d 100644
--- a/src/server/records.ts
+++ b/src/server/records.ts
@@ -18,6 +18,8 @@ const LOOKUPS: Record<RecordRef["kind"], string> = {
explainer: "SELECT 1 FROM faceless_episodes WHERE id = ?",
series: "SELECT 1 FROM content_series WHERE id = ?",
"swap-project": "SELECT 1 FROM swap_projects WHERE id = ?",
+ // The owner's own chats (an account's are checked by accountRecordExists).
+ chat: "SELECT 1 FROM account_chat_threads WHERE id = ? AND account_id = 'owner'",
};
/**
@@ -55,6 +57,14 @@ const SHARED_TABLE = new Set<RecordRef["kind"]>(["channel", "episode", "explaine
* the page's kind isn't, and the check is recordExists's). Never the owner's records or another account's.
*/
export function accountRecordExists(ref: RecordRef, accountId: string): boolean | null {
+ // Fuobe Chat keeps every account's chats in one table, by account (src/server/chat/store.ts).
+ if (ref.kind === "chat") {
+ try {
+ return !!db().prepare("SELECT 1 FROM account_chat_threads WHERE id = ? AND account_id = ?").get(ref.ids[0], accountId);
+ } catch {
+ return true;
+ }
+ }
const kind = ACCOUNT_KINDS[ref.kind];
if (!kind) return null;
try {
diff --git a/tests/chat.test.ts b/tests/chat.test.ts
index 245ca012..0107026c 100644
--- a/tests/chat.test.ts
+++ b/tests/chat.test.ts
@@ -204,6 +204,14 @@ describe("an account's chats are its own", () => {
expect((await threadRoute.GET(req(cookies.a, `/api/chat/threads/${t.id}`), ctx({ id: t.id }))).status).toBe(200);
});
+ it("opens a chat's page only for its own account: anyone else gets a real 404", async () => {
+ const t = store.createThread(a, { kind: "chat", mature: false });
+ const page = (cookie?: string) => proxy(new NextRequest(`http://localhost:3000/chat/${t.id}`, { headers: { host: "localhost:3000", ...(cookie && { cookie }) } }));
+ expect((await page(cookies.a)).headers.get("x-middleware-next")).toBe("1");
+ expect((await page(cookies.b)).status).toBe(404);
+ expect((await page()).status).toBe(404);
+ });
+
it("a guest gets the sign-up answer from the routes too", async () => {
const r = await threadsRoute.POST(new Request("http://localhost:3000/api/chat/threads", { method: "POST", headers: { host: "localhost:3000", "content-type": "application/json" }, body: "{}" }));
expect(r.status).toBe(401);
diff --git a/tests/record-pages.test.ts b/tests/record-pages.test.ts
index b2a22ea0..48bef5b9 100644
--- a/tests/record-pages.test.ts
+++ b/tests/record-pages.test.ts
@@ -59,7 +59,8 @@ describe("record pages (real 404s)", () => {
// (/canvas/saved).
// An owner-only page that reads its id through an owner-only API (Run details: /api/admin/generations/<id> answers
// 404 itself) isn't a record page either: a 404 from the proxy would tell anyone whether that run exists.
- const catalog = /^\/(apps|audio|video-tools|upscale|s|canvas\/(start|try|draft|saved))\/\[|\/start\/\[id\]$|^\/settings\/runs\/\[id\]$/;
+ // A shared chat (/c/<token>) is a share token too: its page checks it.
+ const catalog = /^\/(apps|audio|video-tools|upscale|s|c|canvas\/(start|try|draft|saved))\/\[|\/start\/\[id\]$|^\/settings\/runs\/\[id\]$/;
for (const route of routes) {
if (!route.includes("[")) {
expect(recordForPath(route), route).toBeNull();