Skip to content

Commit

Permalink
fix: changed mention regex
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHamed3699 committed Dec 26, 2024
1 parent 046b240 commit 2196232
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
31 changes: 17 additions & 14 deletions src/services/chatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@ export const getLastMessage = async (chats: any) => {
return lastMessages;
};

export const getUnreadMessages = async (chats: any, user: any) =>
Promise.all(
chats.map(async (chat: any) => ({
chatId: chat.chat._id,
unreadMessagesCount: await Message.countDocuments({
export const getUnreadMessages = async (chats: any, user: any) => {
const mentionRegex = /@[[^]]+](([^)]+))/g;
return Promise.all(
chats.map(async (chat: any) => {
const unreadMessages = await Message.find({
chatId: chat.chat._id,
senderId: { $ne: user._id },
readBy: { $nin: [user._id] },
}),
isMentioned:
(await Message.exists({
chatId: chat.chat._id,
readBy: { $nin: [user._id] },
senderId: { $ne: user._id },
content: new RegExp(`@${user.username}`, 'i'),
})) !== null,
}))
});
return {
chatId: chat.chat._id,
unreadMessagesCount: unreadMessages.length,
isMentioned:
unreadMessages.filter((message: any) =>
mentionRegex.test(message.content)
).length > 0,
};
})
);
};

export const getChats = async (
userId: mongoose.Types.ObjectId,
Expand Down
9 changes: 5 additions & 4 deletions src/sockets/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ const handleMessaging = async (
);
}
});
const res = {
messageId: message._id,
};
enableDestruction(socket, message, chatId);
ack({ success: true, message: 'Message sent successfully', res });
ack({
success: true,
message: 'Message sent successfully',
data: message,
});
};

const handleEditMessage = async (socket: Socket, data: any, ack: Function) => {
Expand Down

0 comments on commit 2196232

Please # to comment.