added messages api and socket workaround
This commit is contained in:
parent
16ce64ba61
commit
e0162c2d5e
20
lib/current-profile-pages.ts
Normal file
20
lib/current-profile-pages.ts
Normal file
@ -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;
|
||||||
|
}
|
93
pages/api/socket/messages.ts
Normal file
93
pages/api/socket/messages.ts
Normal file
@ -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" });
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user