From 7f161c77a14934a1a6d100a4e31ca16a44ab62a7 Mon Sep 17 00:00:00 2001 From: Isztof Date: Sun, 10 Sep 2023 11:57:48 +0200 Subject: [PATCH] Adding some required folder structure --- .../dashboard/chat/[chatId]/page.tsx | 0 app/(dashboard)/dashboard/layout.tsx | 0 app/(map)/map/page.tsx | 1 - app/api/friends/add/route.ts | 72 +++++++++++++++++++ app/api/hello/route.ts | 3 - components/ui/AddFriendButton.tsx | 2 +- components/ui/SidebarChatList.tsx | 0 helpers/get-friends-by-user-id.ts | 7 ++ helpers/redis.ts | 23 ++++++ lib/validations/add-friend.ts | 3 - 10 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 app/(dashboard)/dashboard/chat/[chatId]/page.tsx create mode 100644 app/(dashboard)/dashboard/layout.tsx create mode 100644 app/api/friends/add/route.ts delete mode 100644 app/api/hello/route.ts create mode 100644 components/ui/SidebarChatList.tsx create mode 100644 helpers/get-friends-by-user-id.ts create mode 100644 helpers/redis.ts diff --git a/app/(dashboard)/dashboard/chat/[chatId]/page.tsx b/app/(dashboard)/dashboard/chat/[chatId]/page.tsx new file mode 100644 index 0000000..e69de29 diff --git a/app/(dashboard)/dashboard/layout.tsx b/app/(dashboard)/dashboard/layout.tsx new file mode 100644 index 0000000..e69de29 diff --git a/app/(map)/map/page.tsx b/app/(map)/map/page.tsx index 32a3c28..40904df 100644 --- a/app/(map)/map/page.tsx +++ b/app/(map)/map/page.tsx @@ -6,7 +6,6 @@ const page = async ({}) => { const session = await getServerSession(authOptions) return ( -

My Page

 {JSON.stringify(session)}
diff --git a/app/api/friends/add/route.ts b/app/api/friends/add/route.ts new file mode 100644 index 0000000..6260c5b --- /dev/null +++ b/app/api/friends/add/route.ts @@ -0,0 +1,72 @@ +import { fetchRedis } from '@/helpers/redis' +import { authOptions } from "@/lib/auth" +import { db } from '@/lib/db' +import { addFriendValidator } from "@/lib/validations/add-friend" +import { getServerSession } from "next-auth" +import {z} from "zod" + +export async function POST(req: Request) { + try { + const body = await req.json() + + const {email: emailToAdd} = addFriendValidator.parse(body.email) // if this parse fails a z error is going to be thrown + + console.log("The email being passed is: " + emailToAdd) + + const idToAdd = (await fetchRedis('get', `user:email:${emailToAdd}`)) as string + + console.log(`The id that is passed is: ${idToAdd}`) + + if(!idToAdd) { + return new Response('This person does not exist', {status: 400} ) + } + + + + const session = await getServerSession(authOptions) + + if(!session) { + return new Response('Unauthorized', {status: 401}) + } + + + //const data = await RESTResponse.json() as {result: string} + + + if(idToAdd === session.user.id) { + return new Response('You cannot add yourself as a friend', { + status:400, + }) + } + + //check if user is already added + const isAlreadyAddded = await (fetchRedis('sismember', `user:${idToAdd}:incoming_friend_requests`, session.user.id)) as 0 | 1 + + if (isAlreadyAddded) { + return new Response('Already added this user', {status: 400}) + } + //const idToAdd = data.result + + //check if user is already in the friends list + const isAlreadyFriends = (await fetchRedis( + 'sismember', `user:${session.user.id}:friends`, // we are checking in the friends list of the user who is already logged in whtether the id to add already exists + idToAdd + )) as 0 | 1 + + if (isAlreadyFriends) { + return new Response('Already Friends with this user') + } + + //valid request, send friend request + + db.sadd(`user${idToAdd}:incoming_friend_requests`, session.user.id) + + return new Response("OK") + + } catch (error) { + if(error instanceof z.ZodError) { + return new Response('Invalid request payload') + } + return new Response('Invalid request payload', {status: 422}) + } +} \ No newline at end of file diff --git a/app/api/hello/route.ts b/app/api/hello/route.ts deleted file mode 100644 index d1cc6ee..0000000 --- a/app/api/hello/route.ts +++ /dev/null @@ -1,3 +0,0 @@ -export async function GET(request: Request) { - return new Response('Hello, Next.js!') -} diff --git a/components/ui/AddFriendButton.tsx b/components/ui/AddFriendButton.tsx index 04ffb91..158cbb8 100644 --- a/components/ui/AddFriendButton.tsx +++ b/components/ui/AddFriendButton.tsx @@ -21,7 +21,7 @@ const AddFriendButton: FC = ({}) => { const addFriend = async (email: string) => { try { const validatedEmail = addFriendValidator.parse({email}) - await axios.post('/api/fiends/add', { + await axios.post('/api/friends/add', { email: validatedEmail, }) setShowSuccessState(true) diff --git a/components/ui/SidebarChatList.tsx b/components/ui/SidebarChatList.tsx new file mode 100644 index 0000000..e69de29 diff --git a/helpers/get-friends-by-user-id.ts b/helpers/get-friends-by-user-id.ts new file mode 100644 index 0000000..e44a840 --- /dev/null +++ b/helpers/get-friends-by-user-id.ts @@ -0,0 +1,7 @@ +import { fetchRedis } from "./redis" + +export const getFriendsByUserId = async (userId: string) => { + //retrieve friends for current user + //const friendIds = fetchRedis('smembers', `user:${userId}:friends`) as string[] + +} \ No newline at end of file diff --git a/helpers/redis.ts b/helpers/redis.ts new file mode 100644 index 0000000..59d7ccd --- /dev/null +++ b/helpers/redis.ts @@ -0,0 +1,23 @@ +const upstashRedisRestUrl = process.env.UPSTASH_REDIS_REST_URL +const authToken = process.env.USTASH_REDIS_REST_TOKEN + +type Command = 'zrange' | 'sismember' | 'get' | 'smembers' + +export async function fetchRedis( + command: Command, + ...args: (string | number) [ + ] +) { + const commandUrl = `${upstashRedisRestUrl}/${command}/${args.join('/')}` + + const response = await fetch(commandUrl, { + headers: {Authorization: `Bearer${process.env.UPSTASH_REDIS_REST_TOKEN}` }, + cache: 'no-store', + }) + + if(!response.ok) { + throw new Error(`Error executing Redis command: ${response.statusText}`) + } + const data = await response.json() + return data.result +} \ No newline at end of file diff --git a/lib/validations/add-friend.ts b/lib/validations/add-friend.ts index 9873757..24cbb14 100644 --- a/lib/validations/add-friend.ts +++ b/lib/validations/add-friend.ts @@ -5,6 +5,3 @@ export const addFriendValidator = z.object({ email: z.string().email(), }) -const random = { - idk: 'asd', -} \ No newline at end of file