diff options
| author | Dhravya <[email protected]> | 2024-04-12 22:38:12 -0700 |
|---|---|---|
| committer | Dhravya <[email protected]> | 2024-04-12 22:38:12 -0700 |
| commit | 0d6cd7c447ccd4af05031d9897786abfee80b6b8 (patch) | |
| tree | abded39a0c9acc02399a69a1dd5c251d8e03ec83 /apps/web/src | |
| parent | Merge branch 'main' of https://github.com/Dhravya/supermemory (diff) | |
| download | supermemory-0d6cd7c447ccd4af05031d9897786abfee80b6b8.tar.xz supermemory-0d6cd7c447ccd4af05031d9897786abfee80b6b8.zip | |
added logic for importing all tweets
Diffstat (limited to 'apps/web/src')
| -rw-r--r-- | apps/web/src/app/api/addTweetsToDb/route.ts | 91 | ||||
| -rw-r--r-- | apps/web/src/app/api/getCount/route.ts | 66 | ||||
| -rw-r--r-- | apps/web/src/server/helpers.ts | 12 |
3 files changed, 164 insertions, 5 deletions
diff --git a/apps/web/src/app/api/addTweetsToDb/route.ts b/apps/web/src/app/api/addTweetsToDb/route.ts new file mode 100644 index 00000000..aba63267 --- /dev/null +++ b/apps/web/src/app/api/addTweetsToDb/route.ts @@ -0,0 +1,91 @@ +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, 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 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/getCount/route.ts b/apps/web/src/app/api/getCount/route.ts new file mode 100644 index 00000000..05a3aa30 --- /dev/null +++ b/apps/web/src/app/api/getCount/route.ts @@ -0,0 +1,66 @@ +import { db } from "@/server/db"; +import { and, eq, 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 count = await db + .select({ + count: sql<number>`count(*)`.mapWith(Number), + }) + .from(storedContent) + .where( + and( + eq(storedContent.user, session.user.id), + eq(storedContent.type, "twitter-bookmark"), + ), + ); + + return NextResponse.json({ + tweetsCount: count[0].count, + limit: 1000, + user: session.user.email, + }); +} diff --git a/apps/web/src/server/helpers.ts b/apps/web/src/server/helpers.ts index 519e4b17..109fa0c3 100644 --- a/apps/web/src/server/helpers.ts +++ b/apps/web/src/server/helpers.ts @@ -17,17 +17,19 @@ export async function getMetaData(url: string) { ? descriptionMatch[1] : "Description not found"; - // Extract Open Graph image - const imageMatch = html.match( - /<meta property="og:image" content="(.*?)"\s*\/?>/, + // Extract favicon + const faviconMatch = html.match( + /<link rel="(?:icon|shortcut icon)" href="(.*?)"\s*\/?>/, ); - const image = imageMatch ? imageMatch[1] : "Image not found"; + const favicon = faviconMatch + ? faviconMatch[1] + : "https://supermemory.dhr.wtf/web.svg"; // Prepare the metadata object const metadata = { title, description, - image, + image: favicon, baseUrl, }; return metadata; |