Skip to content

Commit 406cc9f

Browse files
committed
Implemented API for chat
1 parent 6dfc990 commit 406cc9f

File tree

3 files changed

+118
-3
lines changed

3 files changed

+118
-3
lines changed

components/chat/chat-input.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { Form, FormField, FormControl, FormItem } from "@/components/ui/form";
88
import { Input } from "@/components/ui/input";
99
import { Plus, Smile } from "lucide-react";
1010

11+
import axios from "axios";
12+
import qs from "query-string";
13+
1114
interface ChatInputProps {
1215
apiUrl: string;
1316
query: Record<string, any>;
@@ -34,8 +37,19 @@ export const ChatInput: React.FC<ChatInputProps> = ({
3437

3538
const isLoading = form.formState.isSubmitting;
3639

37-
const onSubmit = async (value: z.infer<typeof formSchema>) => {
38-
console.log(value);
40+
const onSubmit = async (values: z.infer<typeof formSchema>) => {
41+
try {
42+
const url = qs.stringifyUrl({
43+
url: apiUrl,
44+
query,
45+
});
46+
47+
await axios.post(url, values);
48+
49+
form.reset();
50+
} catch (error) {
51+
console.log(error);
52+
}
3953
};
4054

4155
return (
@@ -58,7 +72,9 @@ export const ChatInput: React.FC<ChatInputProps> = ({
5872
<Input
5973
disabled={isLoading}
6074
className="px-14 py-6 bg-zinc-200/90 dark:bg-zinc-700/75 border-none border-0 focus-visible:ring-0 focus-visible:ring-offset-0 text-zinc-600 dark:text-zinc-200"
61-
placeholder={`Message ${type === "conversation" ? name : "#"}${name}`}
75+
placeholder={`Message ${
76+
type === "conversation" ? name : "#"
77+
}${name}`}
6278
{...field}
6379
/>
6480
<div className="absolute top-8 right-8">

lib/current-profile-pages.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getAuth } from "@clerk/nextjs/server";
2+
3+
import { db } from "@/lib/db";
4+
import { NextApiRequest } from "next";
5+
6+
export const currentProfilePages = async (req: NextApiRequest) => {
7+
const { userId } = getAuth(req);
8+
9+
if (!userId) return null;
10+
11+
const profile = await db.profile.findFirst({
12+
where: { userId },
13+
});
14+
15+
return profile;
16+
};

pages/api/socket/messages.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { currentProfilePages } from "@/lib/current-profile-pages";
2+
import { db } from "@/lib/db";
3+
import { NextApiResponseServerIo } from "@/types";
4+
import { NextApiRequest } from "next";
5+
6+
export default async function handler(
7+
req: NextApiRequest,
8+
res: NextApiResponseServerIo
9+
) {
10+
if (req.method !== "POST")
11+
return res.status(405).json({ message: "Method not allowed" });
12+
13+
try {
14+
const profile = await currentProfilePages(req);
15+
const { content, fileUrl } = req.body;
16+
17+
const { serverId, channelId } = req.query;
18+
19+
if (!profile) return res.status(401).json({ message: "Unauthorized" });
20+
if (!serverId)
21+
return res.status(400).json({ message: "Server id is required" });
22+
if (!channelId)
23+
return res.status(400).json({ message: "Channel id is required" });
24+
if (!content)
25+
return res.status(400).json({ message: "Content is required" });
26+
27+
const server = await db.server.findFirst({
28+
where: {
29+
id: serverId as string,
30+
members: {
31+
some: {
32+
profileId: profile.id,
33+
},
34+
},
35+
},
36+
include: {
37+
members: true,
38+
},
39+
});
40+
41+
if (!server) return res.status(404).json({ message: "Server not found" });
42+
43+
const channel = await db.channel.findFirst({
44+
where: {
45+
id: channelId as string,
46+
serverId: server.id,
47+
},
48+
});
49+
50+
if (!channel) return res.status(404).json({ message: "Channel not found" });
51+
52+
const member = server.members.find(
53+
(member) => member.profileId === profile.id
54+
);
55+
56+
if (!member) return res.status(401).json({ message: "Unauthorized" });
57+
58+
const message = await db.message.create({
59+
data: {
60+
content,
61+
fileUrl,
62+
memberId: member.id,
63+
channelId: channel.id as string,
64+
},
65+
include: {
66+
member: {
67+
include: {
68+
profile: true,
69+
},
70+
},
71+
},
72+
});
73+
74+
const channelKey = `chat:${channelId}:messages`;
75+
76+
res?.socket?.server?.io.emit(channelKey, message);
77+
78+
return res.status(200).json({ message });
79+
} catch (error) {
80+
console.log("[MESSAGES_POST_ERROR]", error);
81+
return res.status(500).json({ message: "Internal server error" });
82+
}
83+
}

0 commit comments

Comments
 (0)