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 | |
| 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')
| -rw-r--r-- | apps/web/public/twitter.svg | 1 | ||||
| -rw-r--r-- | apps/web/public/web.svg | 3 | ||||
| -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 |
5 files changed, 168 insertions, 5 deletions
diff --git a/apps/web/public/twitter.svg b/apps/web/public/twitter.svg new file mode 100644 index 00000000..0819971e --- /dev/null +++ b/apps/web/public/twitter.svg @@ -0,0 +1 @@ +<svg viewBox="0 0 256 209" width="256" height="209" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid"><path d="M256 25.45c-9.42 4.177-19.542 7-30.166 8.27 10.845-6.5 19.172-16.793 23.093-29.057a105.183 105.183 0 0 1-33.351 12.745C205.995 7.201 192.346.822 177.239.822c-29.006 0-52.523 23.516-52.523 52.52 0 4.117.465 8.125 1.36 11.97-43.65-2.191-82.35-23.1-108.255-54.876-4.52 7.757-7.11 16.78-7.11 26.404 0 18.222 9.273 34.297 23.365 43.716a52.312 52.312 0 0 1-23.79-6.57c-.003.22-.003.44-.003.661 0 25.447 18.104 46.675 42.13 51.5a52.592 52.592 0 0 1-23.718.9c6.683 20.866 26.08 36.05 49.062 36.475-17.975 14.086-40.622 22.483-65.228 22.483-4.24 0-8.42-.249-12.529-.734 23.243 14.902 50.85 23.597 80.51 23.597 96.607 0 149.434-80.031 149.434-149.435 0-2.278-.05-4.543-.152-6.795A106.748 106.748 0 0 0 256 25.45" fill="#55acee"/></svg>
\ No newline at end of file diff --git a/apps/web/public/web.svg b/apps/web/public/web.svg new file mode 100644 index 00000000..dffbed97 --- /dev/null +++ b/apps/web/public/web.svg @@ -0,0 +1,3 @@ +<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> + <path stroke-linecap="round" stroke-linejoin="round" d="M12 21a9.004 9.004 0 0 0 8.716-6.747M12 21a9.004 9.004 0 0 1-8.716-6.747M12 21c2.485 0 4.5-4.03 4.5-9S14.485 3 12 3m0 18c-2.485 0-4.5-4.03-4.5-9S9.515 3 12 3m0 0a8.997 8.997 0 0 1 7.843 4.582M12 3a8.997 8.997 0 0 0-7.843 4.582m15.686 0A11.953 11.953 0 0 1 12 10.5c-2.998 0-5.74-1.1-7.843-2.918m15.686 0A8.959 8.959 0 0 1 21 12c0 .778-.099 1.533-.284 2.253m0 0A17.919 17.919 0 0 1 12 16.5c-3.162 0-6.133-.815-8.716-2.247m0 0A9.015 9.015 0 0 1 3 12c0-1.605.42-3.113 1.157-4.418" /> +</svg>
\ No newline at end of file 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; |