diff options
| author | Dhravya <[email protected]> | 2024-04-12 18:49:11 -0700 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-04-12 18:49:11 -0700 |
| commit | dcf166fdeefa0ae2685750c9f2a041cf5b118791 (patch) | |
| tree | aecf00fa0a26119ef7cf871a6e3b13664fd20982 /apps/web/src/app/api | |
| parent | added overlay for importing tweets (diff) | |
| parent | new route /spaces (diff) | |
| download | supermemory-dcf166fdeefa0ae2685750c9f2a041cf5b118791.tar.xz supermemory-dcf166fdeefa0ae2685750c9f2a041cf5b118791.zip | |
Merge branch 'main' of https://github.com/Dhravya/supermemory
Diffstat (limited to 'apps/web/src/app/api')
| -rw-r--r-- | apps/web/src/app/api/spaces/route.ts | 70 | ||||
| -rw-r--r-- | apps/web/src/app/api/store/route.ts | 40 |
2 files changed, 82 insertions, 28 deletions
diff --git a/apps/web/src/app/api/spaces/route.ts b/apps/web/src/app/api/spaces/route.ts new file mode 100644 index 00000000..1e1aeba7 --- /dev/null +++ b/apps/web/src/app/api/spaces/route.ts @@ -0,0 +1,70 @@ +import { db } from "@/server/db"; +import { sessions, space, users } from "@/server/db/schema"; +import { eq } from "drizzle-orm"; +import { NextRequest, NextResponse } from "next/server"; + +export async function GET(req: NextRequest) { + + const token = + req.cookies.get("next-auth.session-token")?.value ?? + req.cookies.get("__Secure-authjs.session-token")?.value ?? + req.cookies.get("authjs.session-token")?.value ?? + req.headers.get("Authorization")?.replace("Bearer ", ""); + + if (!token) { + return new Response( + JSON.stringify({ message: "Invalid Key, session not found." }), + { status: 404 }, + ); + } + + if (process.env.RATELIMITER) { + const { success } = await process.env.RATELIMITER.limit({ key: token }); + + if (!success) { + return new Response(JSON.stringify({ message: "Rate limit exceeded" }), { + status: 429, + }); + } + } + + const sessionData = await db + .select() + .from(sessions) + .where(eq(sessions.sessionToken, token!)); + + if (!sessionData || sessionData.length === 0) { + return new Response( + JSON.stringify({ message: "Invalid Key, session not found." }), + { status: 404 }, + ); + } + + const userData = await db + .select() + .from(users) + .where(eq(users.id, sessionData[0].userId)) + .limit(1); + + if (!userData || userData.length === 0) { + return NextResponse.json( + { message: "Invalid Key, session not found." }, + { status: 404 }, + ); + } + + const user = userData[0] + + + const spaces = await db + .select() + .from(space) + .where(eq(space.user, user.id)) + .all() + + return NextResponse.json({ + message: "OK", + data: spaces + }, { status: 200 }) + +} diff --git a/apps/web/src/app/api/store/route.ts b/apps/web/src/app/api/store/route.ts index af7fa76e..d592bc53 100644 --- a/apps/web/src/app/api/store/route.ts +++ b/apps/web/src/app/api/store/route.ts @@ -1,5 +1,5 @@ import { db } from "@/server/db"; -import { and, eq, sql } from "drizzle-orm"; +import { and, eq, sql, inArray } from "drizzle-orm"; import { contentToSpace, sessions, @@ -67,17 +67,15 @@ export async function POST(req: NextRequest) { const data = (await req.json()) as { pageContent: string; url: string; - space?: string; + spaces?: string[]; }; const metadata = await getMetaData(data.url); - let id: number | undefined = undefined; + let storeToSpaces = data.spaces; - let storeToSpace = data.space; - - if (!storeToSpace) { - storeToSpace = "none"; + if (!storeToSpaces) { + storeToSpaces = []; } const count = await db @@ -89,7 +87,7 @@ export async function POST(req: NextRequest) { console.log("count", count[0].count); - const storedContentId = await db.insert(storedContent).values({ + const { id } = (await db.insert(storedContent).values({ content: data.pageContent, title: metadata.title, description: metadata.description, @@ -98,9 +96,7 @@ export async function POST(req: NextRequest) { image: metadata.image, savedAt: new Date(), user: session.user.id, - }); - - id = storedContentId.meta.last_row_id; + }).returning({ id: storedContent.id }))[0]; if (!id) { return NextResponse.json( @@ -109,27 +105,15 @@ export async function POST(req: NextRequest) { ); } - let spaceID = 0; - const spaceData = await db .select() .from(space) - .where(and(eq(space.name, storeToSpace), eq(space.user, session.user.id))) - .limit(1); - spaceID = spaceData[0]?.id; - - if (!spaceData || spaceData.length === 0) { - const spaceId = await db.insert(space).values({ - name: storeToSpace, - user: session.user.id, - }); - spaceID = spaceId.meta.last_row_id; - } + .where(and(inArray(space.name, storeToSpaces), eq(space.user, session.user.id))) + .all() - await db.insert(contentToSpace).values({ - contentId: id as number, - spaceId: spaceID, - }); + await Promise.all([spaceData.forEach(async space => { + await db.insert(contentToSpace).values({ contentId: id, spaceId: space.id }) + })]) const res = (await Promise.race([ fetch("https://cf-ai-backend.dhravya.workers.dev/add", { |