Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions controllers/chatController.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ exports.initiateChat = async (req, res) => {
const userId = req.user.id;
const { anotherUserId } = req.body;

const anotherUser = await User.findById(anotherUserId);
if (!anotherUser) {
return res.status(400).send("This user does not exist");
}

const user = await User.findById(userId, 'friends');
const friends = user.friends;

Expand All @@ -87,7 +92,6 @@ exports.initiateChat = async (req, res) => {
user.conversations.push(newConvo._id);
await user.save();

const anotherUser = await User.findById(anotherUserId);
anotherUser.friends.push(userId);
anotherUser.conversations.push(newConvo._id);
await anotherUser.save();
Expand Down Expand Up @@ -127,14 +131,3 @@ exports.deleteConvo = async (req, res) => {
res.status(500).send(err.message);
}
}

// exports.createConvo = async (req, res) => {
// try {
// new Conversation({
// users: [req.user.id, req.body.anotherId]
// }).save()
// res.status(200).send("Okay")
// } catch (err) {
// res.status(500).send(err.message);
// }
// }
2 changes: 1 addition & 1 deletion utils/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exports.chatConfig = (io) => {
convo.messages.push(newMessage);
await convo.save();

socket.to(socket.activeConvo).emit('receive new message', message);
socket.to(socket.activeConvo).emit('receive new message', newMessage);
} catch (err) {
// Change into log later
console.error(e);
Expand Down