From e0162c2d5e5d9d6d504af208ac08e2b42e327b4d Mon Sep 17 00:00:00 2001 From: Bob Burningham Date: Thu, 26 Oct 2023 18:44:20 -0700 Subject: [PATCH] added messages api and socket workaround --- lib/current-profile-pages.ts | 20 ++++++++ pages/api/socket/messages.ts | 93 ++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 lib/current-profile-pages.ts create mode 100644 pages/api/socket/messages.ts diff --git a/lib/current-profile-pages.ts b/lib/current-profile-pages.ts new file mode 100644 index 0000000..9c34567 --- /dev/null +++ b/lib/current-profile-pages.ts @@ -0,0 +1,20 @@ +import { getAuth } from "@clerk/nextjs/server"; + +import { db } from "@/lib/db"; +import { NextApiRequest } from "next"; + +export const currentProfilePages = async (req: NextApiRequest) => { + const { userId } = getAuth(req); + + if (!userId) { + return null; + } + + const profile = await db.profile.findUnique({ + where: { + userId + }, + }); + + return profile; +} \ No newline at end of file diff --git a/pages/api/socket/messages.ts b/pages/api/socket/messages.ts new file mode 100644 index 0000000..b8e5073 --- /dev/null +++ b/pages/api/socket/messages.ts @@ -0,0 +1,93 @@ +import { currentProfilePages } from "@/lib/current-profile-pages"; +import { NextApiResponseServerIo } from "@/types"; +import { NextApiRequest } from "next"; +import { db } from "@/lib/db"; + +export default async function handler(req: NextApiRequest, res: NextApiResponseServerIo) { + if (req.method !== "POST") { + return res.status(405).json({ error: "Method not allowed" }); + } + + try { + const profile = await currentProfilePages(req); + const { content, fileUrl } = req.body; + const { serverId, channelId } = req.query; + + if (!profile) { + return res.status(401).json({ error: "Unauthorized" }); + } + + if (!serverId) { + return res.status(400).json({ error: "Server ID Missing" }); + } + + if (!channelId) { + return res.status(400).json({ error: "Channel ID Missing" }); + } + + if (!content) { + return res.status(400).json({ error: "Content Missing" }); + } + + const server= await db.server.findFirst({ + where: { + id: serverId as string, + members: { + some: { + profileId: profile.id + } + }, + }, + include: { + members: true + } + }); + + if (!server) { + return res.status(404).json({ error: "Server not found" }); + } + + const channel = await db.channel.findFirst({ + where: { + id: channelId as string, + serverId: server.id, + }, + }); + + if (!channel) { + return res.status(404).json({ error: "Channel not found" }); + } + + const member = server.members.find((member) => member.profileId === profile.id); + + if (!member) { + return res.status(401).json({ error: "Unauthorized" }); + } + + const message = await db.message.create({ + data: { + content, + fileUrl, + channelId: channelId as string, + memberId: member.id, + }, + include: { + member: { + include: { + profile: true + } + } + } + }); + + const channelKey = `chat:${channelId}:messages`; + + res?.socket?.server?.io?.emit(channelKey, message); + + return res.status(200).json({ message }); + } + catch (error) { + console.log("[MESSAGES_POST]", error); + return res.status(500).json({ message: "Internal Error" }); + } +} \ No newline at end of file