diff options
| author | Dhravya <[email protected]> | 2024-05-25 18:41:26 -0500 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-05-25 18:41:26 -0500 |
| commit | 075f45986fd4d198292226e64afb71b3515576b4 (patch) | |
| tree | 5c728356cd0310f1c1c012fd6618c72a836c314b /apps/web/src/app/api | |
| parent | added social material (diff) | |
| download | supermemory-075f45986fd4d198292226e64afb71b3515576b4.tar.xz supermemory-075f45986fd4d198292226e64afb71b3515576b4.zip | |
refactored UI, with shared components and UI, better rules and million lint
Diffstat (limited to 'apps/web/src/app/api')
| -rw-r--r-- | apps/web/src/app/api/[...nextauth]/route.ts | 2 | ||||
| -rw-r--r-- | apps/web/src/app/api/addTweetsToDb/route.ts | 91 | ||||
| -rw-r--r-- | apps/web/src/app/api/ask/route.ts | 78 | ||||
| -rw-r--r-- | apps/web/src/app/api/chat/route.ts | 109 | ||||
| -rw-r--r-- | apps/web/src/app/api/getCount/route.ts | 80 | ||||
| -rw-r--r-- | apps/web/src/app/api/me/route.ts | 48 | ||||
| -rw-r--r-- | apps/web/src/app/api/query/route.ts | 88 | ||||
| -rw-r--r-- | apps/web/src/app/api/spaces/route.ts | 72 | ||||
| -rw-r--r-- | apps/web/src/app/api/store/route.ts | 165 | ||||
| -rw-r--r-- | apps/web/src/app/api/vectorizeTweets/route.ts | 63 |
10 files changed, 0 insertions, 796 deletions
diff --git a/apps/web/src/app/api/[...nextauth]/route.ts b/apps/web/src/app/api/[...nextauth]/route.ts deleted file mode 100644 index db7d1fb8..00000000 --- a/apps/web/src/app/api/[...nextauth]/route.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { GET, POST } from "@/server/auth"; -export const runtime = "edge"; diff --git a/apps/web/src/app/api/addTweetsToDb/route.ts b/apps/web/src/app/api/addTweetsToDb/route.ts deleted file mode 100644 index 7fe2edba..00000000 --- a/apps/web/src/app/api/addTweetsToDb/route.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { db } from "@/server/db"; -import { eq } from "drizzle-orm"; -import { sessions, storedContent, users } from "@/server/db/schema"; -import { type NextRequest, NextResponse } from "next/server"; - -export const runtime = "edge"; - -interface TweetData { - tweetText: string; - postUrl: string; - authorName: string; - handle: string; - time: string; - saveToUser: string; -} - -export async function POST(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, TOKEN not found." }), - { status: 404 }, - ); - } - - 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 user = await db - .select() - .from(users) - .where(eq(users.id, sessionData[0].userId)) - .limit(1); - - if (!user || user.length === 0) { - return NextResponse.json( - { message: "Invalid Key, session not found." }, - { status: 404 }, - ); - } - - const session = { session: sessionData[0], user: user[0] }; - - const data = (await req.json()) as TweetData[]; - - for (const tweet of data) { - const { id } = ( - await db - .insert(storedContent) - .values({ - content: tweet.tweetText, - title: "Twitter Bookmark", - description: "", - url: tweet.postUrl, - baseUrl: "https://twitter.com", - image: "https://supermemory.dhr.wtf/twitter.svg", - savedAt: new Date(), - user: session.user.id, - type: "twitter-bookmark", - }) - .returning({ id: storedContent.id }) - )[0]; - - if (!id) { - return NextResponse.json( - { - message: "Error", - error: - "Something went wrong when inserting the tweet to storedContent", - }, - { status: 500 }, - ); - } - } - - return NextResponse.json({ message: "OK", data: "Success" }, { status: 200 }); -} diff --git a/apps/web/src/app/api/ask/route.ts b/apps/web/src/app/api/ask/route.ts deleted file mode 100644 index 17b24b3e..00000000 --- a/apps/web/src/app/api/ask/route.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { db } from "@/server/db"; -import { eq } from "drizzle-orm"; -import { sessions, users } from "@/server/db/schema"; -import { type NextRequest, NextResponse } from "next/server"; -import { env } from "@/env"; - -export const runtime = "edge"; - -export async function POST(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 (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 user = await db - .select() - .from(users) - .where(eq(users.id, sessionData[0].userId)) - .limit(1); - - if (!user || user.length === 0) { - return NextResponse.json( - { message: "Invalid Key, session not found." }, - { status: 404 }, - ); - } - - const body = (await req.json()) as { - query: string; - }; - - const resp = await fetch(`https://cf-ai-backend.dhravya.workers.dev/ask`, { - headers: { - "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY, - }, - method: "POST", - body: JSON.stringify({ - query: body.query, - }), - }); - - if (resp.status !== 200 || !resp.ok) { - const errorData = await resp.json(); - return new Response( - JSON.stringify({ message: "Error in CF function", error: errorData }), - { status: resp.status }, - ); - } - - // Stream the response back to the client - const { readable, writable } = new TransformStream(); - resp && resp.body!.pipeTo(writable); - - return new Response(readable, { status: 200 }); -} diff --git a/apps/web/src/app/api/chat/route.ts b/apps/web/src/app/api/chat/route.ts deleted file mode 100644 index c815070b..00000000 --- a/apps/web/src/app/api/chat/route.ts +++ /dev/null @@ -1,109 +0,0 @@ -import { db } from "@/server/db"; -import { eq } from "drizzle-orm"; -import { sessions, users } from "@/server/db/schema"; -import { type NextRequest, NextResponse } from "next/server"; -import { env } from "@/env"; -import { ChatHistory } from "../../../../types/memory"; - -export const runtime = "edge"; - -export async function POST(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 (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 user = await db - .select() - .from(users) - .where(eq(users.id, sessionData[0].userId)) - .limit(1); - - if (!user || user.length === 0) { - return NextResponse.json( - { message: "Invalid Key, session not found." }, - { status: 404 }, - ); - } - - const session = { session: sessionData[0], user: user[0] }; - - const query = new URL(req.url).searchParams.get("q"); - const spaces = new URL(req.url).searchParams.get("spaces"); - - const sourcesOnly = - new URL(req.url).searchParams.get("sourcesOnly") ?? "false"; - - const chatHistory = (await req.json()) as { - chatHistory: ChatHistory[]; - }; - - console.log("CHathistory", chatHistory); - - if (!query) { - return new Response(JSON.stringify({ message: "Invalid query" }), { - status: 400, - }); - } - - try { - const resp = await fetch( - `https://cf-ai-backend.dhravya.workers.dev/chat?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`, - { - headers: { - "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY, - }, - method: "POST", - body: JSON.stringify({ - chatHistory: chatHistory.chatHistory ?? [], - }), - }, - ); - - console.log("sourcesOnly", sourcesOnly); - - if (sourcesOnly == "true") { - const data = await resp.json(); - console.log("data", data); - return new Response(JSON.stringify(data), { status: 200 }); - } - - if (resp.status !== 200 || !resp.ok) { - const errorData = await resp.json(); - console.log(errorData); - return new Response( - JSON.stringify({ message: "Error in CF function", error: errorData }), - { status: resp.status }, - ); - } - - // Stream the response back to the client - const { readable, writable } = new TransformStream(); - resp && resp.body!.pipeTo(writable); - - return new Response(readable, { status: 200 }); - } catch {} -} diff --git a/apps/web/src/app/api/getCount/route.ts b/apps/web/src/app/api/getCount/route.ts deleted file mode 100644 index 9fe54f78..00000000 --- a/apps/web/src/app/api/getCount/route.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { db } from "@/server/db"; -import { and, eq, ne, sql } from "drizzle-orm"; -import { sessions, storedContent, users } from "@/server/db/schema"; -import { type NextRequest, NextResponse } from "next/server"; - -export const runtime = "edge"; - -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 }, - ); - } - - 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 user = await db - .select() - .from(users) - .where(eq(users.id, sessionData[0].userId)) - .limit(1); - - if (!user || user.length === 0) { - return NextResponse.json( - { message: "Invalid Key, session not found." }, - { status: 404 }, - ); - } - - const session = { session: sessionData[0], user: user[0] }; - - const tweetsCount = await db - .select({ - count: sql<number>`count(*)`.mapWith(Number), - }) - .from(storedContent) - .where( - and( - eq(storedContent.user, session.user.id), - eq(storedContent.type, "twitter-bookmark"), - ), - ); - - const pageCount = await db - .select({ - count: sql<number>`count(*)`.mapWith(Number), - }) - .from(storedContent) - .where( - and( - eq(storedContent.user, session.user.id), - ne(storedContent.type, "twitter-bookmark"), - ), - ); - - return NextResponse.json({ - tweetsCount: tweetsCount[0].count, - tweetsLimit: 1000, - pageCount: pageCount[0].count, - pageLimit: 100, - user: session.user.email, - }); -} diff --git a/apps/web/src/app/api/me/route.ts b/apps/web/src/app/api/me/route.ts deleted file mode 100644 index 6d269872..00000000 --- a/apps/web/src/app/api/me/route.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { db } from "@/server/db"; -import { eq } from "drizzle-orm"; -import { sessions, users } from "@/server/db/schema"; -import { type NextRequest, NextResponse } from "next/server"; -import { env } from "@/env"; - -export const runtime = "edge"; - -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 ", ""); - - const session = await db - .select() - .from(sessions) - .where(eq(sessions.sessionToken, token!)); - - if (!session || session.length === 0) { - return new Response( - JSON.stringify({ message: "Invalid Key, session not found." }), - { status: 404 }, - ); - } - - const user = await db - .select() - .from(users) - .where(eq(users.id, session[0].userId)) - .limit(1); - - if (!user || user.length === 0) { - return NextResponse.json( - { message: "Invalid Key, session not found." }, - { status: 404 }, - ); - } - - return new Response( - JSON.stringify({ - message: "OK", - data: { session: session[0], user: user[0] }, - }), - { status: 200 }, - ); -} diff --git a/apps/web/src/app/api/query/route.ts b/apps/web/src/app/api/query/route.ts deleted file mode 100644 index 5806841e..00000000 --- a/apps/web/src/app/api/query/route.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { db } from "@/server/db"; -import { eq } from "drizzle-orm"; -import { sessions, users } from "@/server/db/schema"; -import { type NextRequest, NextResponse } from "next/server"; -import { env } from "@/env"; - -export const runtime = "edge"; - -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 (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 user = await db - .select() - .from(users) - .where(eq(users.id, sessionData[0].userId)) - .limit(1); - - if (!user || user.length === 0) { - return NextResponse.json( - { message: "Invalid Key, session not found." }, - { status: 404 }, - ); - } - - const session = { session: sessionData[0], user: user[0] }; - - const query = new URL(req.url).searchParams.get("q"); - const sourcesOnly = - new URL(req.url).searchParams.get("sourcesOnly") ?? "false"; - - if (!query) { - return new Response(JSON.stringify({ message: "Invalid query" }), { - status: 400, - }); - } - - const resp = await fetch( - `https://cf-ai-backend.dhravya.workers.dev/query?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}`, - { - headers: { - "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY, - }, - }, - ); - - console.log(resp.status); - - if (resp.status !== 200 || !resp.ok) { - const errorData = await resp.json(); - console.log(errorData); - return new Response( - JSON.stringify({ message: "Error in CF function", error: errorData }), - { status: resp.status }, - ); - } - - // Stream the response back to the client - const { readable, writable } = new TransformStream(); - resp && resp.body!.pipeTo(writable); - - return new Response(readable, { status: 200 }); -} diff --git a/apps/web/src/app/api/spaces/route.ts b/apps/web/src/app/api/spaces/route.ts deleted file mode 100644 index d2685e9f..00000000 --- a/apps/web/src/app/api/spaces/route.ts +++ /dev/null @@ -1,72 +0,0 @@ -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 const runtime = "edge"; - -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 deleted file mode 100644 index 457eae2e..00000000 --- a/apps/web/src/app/api/store/route.ts +++ /dev/null @@ -1,165 +0,0 @@ -import { db } from "@/server/db"; -import { and, eq, sql, inArray } from "drizzle-orm"; -import { - contentToSpace, - sessions, - storedContent, - users, - space, -} from "@/server/db/schema"; -import { type NextRequest, NextResponse } from "next/server"; -import { env } from "@/env"; -import { getMetaData } from "@/server/helpers"; - -export const runtime = "edge"; - -export async function POST(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 user = await db - .select() - .from(users) - .where(eq(users.id, sessionData[0].userId)) - .limit(1); - - if (!user || user.length === 0) { - return NextResponse.json( - { message: "Invalid Key, session not found." }, - { status: 404 }, - ); - } - - const session = { session: sessionData[0], user: user[0] }; - - const data = (await req.json()) as { - pageContent: string; - url: string; - spaces?: string[]; - }; - - const metadata = await getMetaData(data.url); - let storeToSpaces = data.spaces; - - if (!storeToSpaces) { - storeToSpaces = []; - } - - const count = await db - .select({ - count: sql<number>`count(*)`.mapWith(Number), - }) - .from(storedContent) - .where( - and( - eq(storedContent.user, session.user.id), - eq(storedContent.type, "page"), - ), - ); - - if (count[0].count > 100) { - return NextResponse.json( - { message: "Error", error: "Limit exceeded" }, - { status: 499 }, - ); - } - - const { id } = ( - await db - .insert(storedContent) - .values({ - content: data.pageContent, - title: metadata.title, - description: metadata.description, - url: data.url, - baseUrl: metadata.baseUrl, - image: metadata.image, - savedAt: new Date(), - user: session.user.id, - }) - .returning({ id: storedContent.id }) - )[0]; - - if (!id) { - return NextResponse.json( - { message: "Error", error: "Error in CF function" }, - { status: 500 }, - ); - } - - if (storeToSpaces.length > 0) { - const spaceData = await db - .select() - .from(space) - .where( - and( - inArray(space.name, storeToSpaces ?? []), - eq(space.user, session.user.id), - ), - ) - .all(); - - 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", { - method: "POST", - headers: { - "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY, - }, - body: JSON.stringify({ ...data, user: session.user.email }), - }), - new Promise((_, reject) => - setTimeout(() => reject(new Error("Request timed out")), 40000), - ), - ])) as Response; - - if (res.status !== 200) { - console.log(res.status, res.statusText); - return NextResponse.json( - { message: "Error", error: "Error in CF function" }, - { status: 500 }, - ); - } - - return NextResponse.json({ message: "OK", data: "Success" }, { status: 200 }); -} diff --git a/apps/web/src/app/api/vectorizeTweets/route.ts b/apps/web/src/app/api/vectorizeTweets/route.ts deleted file mode 100644 index 63aa38f0..00000000 --- a/apps/web/src/app/api/vectorizeTweets/route.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { db } from "@/server/db"; -import { eq } from "drizzle-orm"; -import { sessions, storedContent, users } from "@/server/db/schema"; -import { type NextRequest, NextResponse } from "next/server"; -import { env } from "@/env"; - -export const runtime = "edge"; - -interface TweetData { - tweetText: string; - postUrl: string; - authorName: string; - handle: string; - time: string; - saveToUser: string; -} - -export async function POST(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 }, - ); - } - - 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 body = (await req.json()) as TweetData[]; - - console.log(body); - - const resp = await fetch( - `https://cf-ai-backend.dhravya.workers.dev/batchUploadTweets`, - { - headers: { - "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY, - }, - method: "POST", - body: JSON.stringify(body), - }, - ); - - return new Response(await resp.text(), { - status: resp.status, - headers: resp.headers, - }); -} |