-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from shuyi320/main
chatroom
- Loading branch information
Showing
7 changed files
with
166 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Schema, model } from "mongoose"; | ||
|
||
const chatRoomModel = new Schema({ | ||
roomId: String, | ||
users: [String], | ||
createdAt: { type: Date, default: Date.now() } | ||
}); | ||
|
||
const chatRoom = model("chat", chatRoomModel); | ||
|
||
|
||
export async function createChatRoom(data) { | ||
const result = await chatRoom.create(data) | ||
return result; | ||
} | ||
|
||
|
||
export async function getAllChatRooms(userId) { | ||
const data = await chatRoom.findAll({ | ||
users: { $eq: userId } | ||
}) | ||
if (!data) return null | ||
return data; | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
import { Schema, model } from "mongoose"; | ||
|
||
const messageModel = new Schema({ | ||
chatId: String, | ||
roomId: String, | ||
senderId: String, | ||
message: String, | ||
createdAt: { type: Date, default: Date.now() } | ||
}); | ||
|
||
const chat = model("chat", messageModel); | ||
const messages = model("chat", messageModel); | ||
|
||
|
||
export async function create(data) { | ||
const result = await chat.create(data) | ||
return result; | ||
export async function sendMessage(data) { | ||
const message = await messages.create(data) | ||
return message; | ||
} | ||
|
||
|
||
export async function getChat(chatID) { | ||
const data = await chat.findAll({ | ||
chatID: { $eq: chatID } | ||
export async function getMessages(roomId) { | ||
const data = await messages.findAll({ | ||
roomId: { $eq: roomId } | ||
}).sort({"createdAt": 1}) | ||
if (!data) return null | ||
return data; | ||
} | ||
|
||
export async function update(id, data) { | ||
return await chat.updateOne({ chatID: id }, data); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Router } from "express"; | ||
import { createChatRoom, getAllChatRooms } from "../database/models/chatRoomModel.js"; | ||
|
||
const router = Router(); | ||
|
||
router.post('/', async (req, res) => { | ||
try { | ||
const result = await createChatRoom(req.body) | ||
console.log('Room Created: ', result) | ||
res.status(200).json(result) | ||
} catch (error) { | ||
res.status(400).json("Error Creating ChatRoom: ", error) | ||
} | ||
}) | ||
|
||
router.get('/:userId', async (req, res) => { | ||
try { | ||
const { userId } = req.params; | ||
const chatRooms = await getAllChatRooms(userId) | ||
if (!chatRooms) { | ||
return res.status(404).json({ error: "Chat Room Not Found" }) | ||
} | ||
return res.status(200).json(chatRooms); | ||
|
||
} catch (error) { | ||
console.log(error) | ||
return res.status(400).json({ error: error.message }) | ||
} | ||
}) | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,57 @@ | ||
import { Router } from "express"; | ||
import { createClerkClient } from '@clerk/backend' | ||
import { getUserByID, create } from "../database/models/userInfo.js" | ||
import { getUserByID, getUserByName, getFriends, addFriend, create } from "../database/models/userInfo.js" | ||
|
||
const router = Router(); | ||
|
||
// return a users list | ||
router.get('/', async (req, res) => { | ||
const clerkClient = createClerkClient({ secretKey: process.env.CLERK_SECRET_KEY }) | ||
try { | ||
// Fetch all users from Clerk | ||
const response = await clerkClient.users.getUserList() | ||
const users = response.data | ||
// Optionally format the user data as needed | ||
console.log('users:', users); | ||
const userList = users.map(user => ({ | ||
id: user.id, | ||
email: user.emailAddresses[0]?.emailAddress, | ||
username: user.username, | ||
createdAt: user.createdAt, | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
roles: user, | ||
})); | ||
|
||
res.json(userList); | ||
} catch (error) { | ||
console.error('Error fetching user list:', error); | ||
res.status(500).json({ message: 'Failed to fetch user list' }); | ||
} | ||
}); | ||
|
||
|
||
//check if the user exits in the db | ||
router.get('/:userID', async (req, res) => { | ||
const userID = req.params.userID | ||
const response = await getUserByID(userID) | ||
if (!response) { | ||
res.json(false) | ||
} else { | ||
res.json(true) | ||
} | ||
router.get('/:userName', async (req, res) => { | ||
const { userName } = req.params | ||
const user = await getUserByName(userName) | ||
if (!user) | ||
return res.status(404).json({ error: 'User not found' }); | ||
|
||
return res.status(200).json({ user }); | ||
}) | ||
|
||
router.post('/:userID', async(req,res)=>{ | ||
router.post('/', async (req, res) => { | ||
const data = req.body; | ||
try { | ||
await create(req.body) | ||
res.json("success!") | ||
await create(data) | ||
res.json("User saved to database!") | ||
} catch (error) { | ||
res.status(500).send({ message: error.message }) | ||
} | ||
}) | ||
|
||
router.post('/addFriend', async (req, res) => { | ||
try { | ||
const { userId, friendId } = req.body; | ||
console.log(`Request to add friend: ${userId} -> ${friendId}`); | ||
|
||
if (userId === friendId) { | ||
return res.status(422).json({ error: "You can't friend yourself." }); | ||
} | ||
|
||
const user = await getUserByID(userId); // This should work if User is defined correctly | ||
const friend = await getUserByID(friendId); | ||
|
||
if (!user || !friend) { | ||
return res.status(404).json({ error: "User or friend not found." }); | ||
} | ||
|
||
const existingFriendship = getFriends(userId); | ||
|
||
if (existingFriendship.contains(friendId)) { | ||
return res.status(422).json({ error: "Friendship already exists." }); | ||
} | ||
|
||
addFriend(user, friend); | ||
|
||
return res.status(200).json({ message: "Friend added successfully.", friendId }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(400).json({ error: error.message }); | ||
} | ||
}) | ||
|
||
export default router; |