protect `conversation` trpc routes

master
Avraham Sakal 3 weeks ago
parent a7709e8abd
commit fc70806b10

@ -17,7 +17,7 @@ import { createCaller as createCallerMessages } from "./messages.js";
import { createCaller as createCallerFactTriggers } from "./fact-triggers.js"; import { createCaller as createCallerFactTriggers } from "./fact-triggers.js";
import { factTriggers } from "./fact-triggers.js"; import { factTriggers } from "./fact-triggers.js";
import { MODEL_NAME } from "../provider.js"; import { MODEL_NAME } from "../provider.js";
import type { Fact, FactTrigger } from "@database/common"; import type { FactTrigger } from "@database/common";
const mainSystemPrompt = ({ const mainSystemPrompt = ({
systemPrompt, systemPrompt,
@ -70,6 +70,10 @@ export const chat = router({
ctx, ctx,
}) { }) {
const { dbClient, openrouter, jwt } = ctx; const { dbClient, openrouter, jwt } = ctx;
if (!jwt) {
yield { status: "error" as const, message: "Unauthorized" };
return;
}
const factsCaller = createCallerFacts(ctx); const factsCaller = createCallerFacts(ctx);
const messagesCaller = createCallerMessages(ctx); const messagesCaller = createCallerMessages(ctx);
const factTriggerCaller = createCallerFactTriggers(ctx); const factTriggerCaller = createCallerFactTriggers(ctx);

@ -14,30 +14,40 @@ export const conversations = router({
}), }),
fetchOne: publicProcedure fetchOne: publicProcedure
.input((x) => x as { id: string }) .input((x) => x as { id: string })
.query(async ({ input: { id }, ctx: { dbClient } }) => { .query(async ({ input: { id }, ctx: { dbClient, jwt } }) => {
const userId = jwt?.id as string | null;
if (!userId) return null;
const row = await dbClient const row = await dbClient
.selectFrom("conversations") .selectFrom("conversations")
.selectAll() .selectAll()
.where("id", "=", id) .where("id", "=", id)
.where("userId", "=", userId)
.execute(); .execute();
return row[0]; return row[0];
}), }),
start: publicProcedure.mutation(async ({ ctx: { dbClient, jwt } }) => { start: publicProcedure.mutation(async ({ ctx: { dbClient, jwt } }) => {
const row = { const userId = jwt?.id as string | null;
title: "New Conversation", if (!userId) return null;
userId: jwt?.id as string,
};
const insertedRows = await dbClient const insertedRows = await dbClient
.insertInto("conversations") .insertInto("conversations")
.values(row) .values({
title: "New Conversation",
userId: jwt?.id as string,
})
.returningAll() .returningAll()
.execute(); .execute();
return insertedRows[0]; return insertedRows[0];
}), }),
deleteOne: publicProcedure deleteOne: publicProcedure
.input((x) => x as { id: string }) .input((x) => x as { id: string })
.mutation(async ({ input: { id }, ctx: { dbClient } }) => { .mutation(async ({ input: { id }, ctx: { dbClient, jwt } }) => {
await dbClient.deleteFrom("conversations").where("id", "=", id).execute(); const userId = jwt?.id as string | null;
if (!userId) return { ok: false };
await dbClient
.deleteFrom("conversations")
.where("id", "=", id)
.where("userId", "=", userId)
.execute();
return { ok: true }; return { ok: true };
}), }),
updateTitle: publicProcedure updateTitle: publicProcedure
@ -48,21 +58,32 @@ export const conversations = router({
title: string; title: string;
} }
) )
.mutation(async ({ input: { id, title }, ctx: { dbClient } }) => { .mutation(async ({ input: { id, title }, ctx: { dbClient, jwt } }) => {
const userId = jwt?.id as string | null;
if (!userId) return { ok: false };
await dbClient await dbClient
.updateTable("conversations") .updateTable("conversations")
.set({ title }) .set({ title })
.where("id", "=", id) .where("id", "=", id)
.where("userId", "=", userId)
.execute(); .execute();
return { ok: true }; return { ok: true };
}), }),
fetchMessages: publicProcedure fetchMessages: publicProcedure
.input((x) => x as { conversationId: string }) .input((x) => x as { conversationId: string })
.query(async ({ input: { conversationId }, ctx: { dbClient } }) => { .query(async ({ input: { conversationId }, ctx: { dbClient, jwt } }) => {
const userId = jwt?.id as string | null;
if (!userId) return [];
const rows = await dbClient const rows = await dbClient
.selectFrom("messages") .selectFrom("messages")
.selectAll() .innerJoin(
"conversations",
"conversations.id",
"messages.conversationId"
)
.selectAll("messages")
.where("conversationId", "=", conversationId) .where("conversationId", "=", conversationId)
.where("conversations.userId", "=", userId)
.execute(); .execute();
return rows as Array<CommittedMessage>; return rows as Array<CommittedMessage>;
}), }),

Loading…
Cancel
Save