From 6d15b9afb784b1339f0036477bbfc6c331dc56b0 Mon Sep 17 00:00:00 2001 From: slawk0 Date: Sun, 15 Dec 2024 17:24:44 +0100 Subject: [PATCH] initialize contact again when added to group or left --- client/src/components/chat/AddGroupMember.tsx | 4 +- client/src/components/chat/ContactProfile.tsx | 4 +- client/src/components/chat/ContactsList.tsx | 23 +++-- .../src/components/chat/ParticipantsBar.tsx | 85 +++++++++++++------ client/src/components/chat/UserProfile.tsx | 4 +- client/src/pages/Chat.tsx | 8 +- server/socket/socket.js | 54 ++++++++---- 7 files changed, 123 insertions(+), 59 deletions(-) diff --git a/client/src/components/chat/AddGroupMember.tsx b/client/src/components/chat/AddGroupMember.tsx index e520feb..07931f1 100644 --- a/client/src/components/chat/AddGroupMember.tsx +++ b/client/src/components/chat/AddGroupMember.tsx @@ -42,7 +42,7 @@ function AddGroupMember({ contact }: AddGroupMemberProps) { setIsLoading(false); setNotFound(false); } - } catch (e) { + } catch (e: any) { setIsLoading(false); console.error('Error fetching suggestions:', e); setErrorMessage( @@ -85,7 +85,7 @@ function AddGroupMember({ contact }: AddGroupMemberProps) { modalRef.current.close(); } reset(); - } catch (e) { + } catch (e: any) { console.error('Failed to add group member: ', e); setIsLoading(false); setErrorMessage(e.response?.data?.message || 'Failed to add member'); diff --git a/client/src/components/chat/ContactProfile.tsx b/client/src/components/chat/ContactProfile.tsx index 222ce20..32f9695 100644 --- a/client/src/components/chat/ContactProfile.tsx +++ b/client/src/components/chat/ContactProfile.tsx @@ -12,7 +12,9 @@ function ContactProfile({ contact }: ContactProfileProps) {
profile img -

{contact ? contact.username : null}

+

+ {contact?.user_id} {contact ? contact.username : null} +

diff --git a/client/src/components/chat/ContactsList.tsx b/client/src/components/chat/ContactsList.tsx index ee70fb9..4bfc488 100644 --- a/client/src/components/chat/ContactsList.tsx +++ b/client/src/components/chat/ContactsList.tsx @@ -16,7 +16,7 @@ import { } from '@/components/ui/alert-dialog'; type ContactsListProps = { - InitializeContact: (contact: ContactsProps) => void; + initializeContact: (contact: ContactsProps) => void; setContactsList: React.Dispatch>; contactsList: ContactsProps[]; setCurrentContact: React.Dispatch>; @@ -27,7 +27,7 @@ type ContactsListProps = { }; function ContactsList({ - InitializeContact, + initializeContact, contactsList, setContactsList, setCurrentContact, @@ -44,10 +44,19 @@ function ContactsList({ useEffect(() => { if (!socket) return; - socket?.on('added to group', () => { - console.log('added to group, fetching contacts'); - fetchContacts(); - }); + socket.on( + 'added to group', + (msg: { + username: string; + user_id: number; + group_id: number; + isadmin: false; + }) => { + console.log('added to group, fetching contacts'); + console.error(msg); + fetchContacts(); + }, + ); return () => { socket?.off('added to group'); @@ -103,7 +112,7 @@ function ContactsList({
  • { - InitializeContact(contact); + initializeContact(contact); updateContactStatus(contact, true); }} > diff --git a/client/src/components/chat/ParticipantsBar.tsx b/client/src/components/chat/ParticipantsBar.tsx index 4cae6c9..0df65ec 100644 --- a/client/src/components/chat/ParticipantsBar.tsx +++ b/client/src/components/chat/ParticipantsBar.tsx @@ -21,14 +21,37 @@ type ParticipantsProps = { type ParticipantsBarProps = { contact: ContactsProps | null; + initializeContact: (contact: ContactsProps) => void; + currentContact: ContactsProps | null; }; -function ParticipantsBar({ contact }: ParticipantsBarProps) { +function ParticipantsBar({ + contact, + initializeContact, + currentContact, +}: ParticipantsBarProps) { const [participants, setParticipants] = useState([]); const [isGroupAdmin, setIsGroupAdmin] = useState(false); const user: UsernameType = useOutletContext(); const [isLoading, setIsLoading] = useState(false); + const getParticipants = async () => { + try { + const response = await axiosClient.get( + `/api/chat/groups/getMembers/${contact?.conversation_id}`, + ); + console.log( + 'getParticipants for: ', + contact?.conversation_id, + 'response: ', + response.data, + ); + setParticipants(response.data); + } catch (e) { + console.error('Failed to get chat participants: ', e); + } + }; + useEffect(() => { if (participants.length > 0 && user?.user_id) { const userIsAdmin = participants.some( @@ -41,22 +64,6 @@ function ParticipantsBar({ contact }: ParticipantsBarProps) { useEffect(() => { if (contact) { - const getParticipants = async () => { - try { - const response = await axiosClient.get( - `/api/chat/groups/getMembers/${contact.conversation_id}`, - ); - console.log( - 'getParticipants for: ', - contact.conversation_id, - 'response: ', - response.data, - ); - setParticipants(response.data); - } catch (e) { - console.error('Failed to get chat participants: ', e); - } - }; getParticipants(); } }, [contact]); @@ -75,6 +82,25 @@ function ParticipantsBar({ contact }: ParticipantsBarProps) { const { group_id } = msg; console.log('Added to group: ', msg); console.log('Current participants: ', participants); + console.log( + 'HDDSLKDFLDK: ', + currentContact?.conversation_id, + currentContact?.user_id, + ); + if ( + msg.group_id == currentContact?.conversation_id && + msg.user_id == user?.user_id + ) { + initializeContact({ + read: true, + username: msg.username, + user_id: msg.user_id, + id: currentContact?.id, + type: 'group', + conversation_id: msg.group_id, + last_active: new Date().toString(), + }); + } if (group_id === contact.conversation_id) { setParticipants((prevMembers) => { @@ -90,16 +116,19 @@ function ParticipantsBar({ contact }: ParticipantsBarProps) { }, ); - socket.on( - 'left group', - (msg: { user_id: number; conversation_id: number }) => { - const { user_id } = msg; - console.log(`(socket on "left group")`, msg); - setParticipants((prevMembers) => - prevMembers.filter((member) => member.user_id !== user_id), - ); - }, - ); + socket.on('left group', (msg: { user_id: number; group_id: number }) => { + console.log(`(socket on "left group")`, msg); + // Initialize contact again to clear messages and participants list + if ( + msg.group_id == currentContact?.conversation_id && + msg.user_id == user?.user_id + ) { + initializeContact(currentContact); + } + setParticipants((prevMembers) => + prevMembers.filter((member) => member.user_id !== msg.user_id), + ); + }); return () => { socket?.off('added to group'); @@ -113,7 +142,7 @@ function ParticipantsBar({ contact }: ParticipantsBarProps) { socket?.emit( 'remove user from group', { - conversation_id: contact?.conversation_id, + group_id: contact?.conversation_id, user_id: userId, }, (response: { status: 'ok' | 'error'; message: string }) => { diff --git a/client/src/components/chat/UserProfile.tsx b/client/src/components/chat/UserProfile.tsx index 4818aac..cbe5339 100644 --- a/client/src/components/chat/UserProfile.tsx +++ b/client/src/components/chat/UserProfile.tsx @@ -24,7 +24,9 @@ function UserProfile() {
  • -

    {user.username}

    +

    + {user?.user_id} {user.username} +

    diff --git a/client/src/pages/Chat.tsx b/client/src/pages/Chat.tsx index c07550e..2e55983 100644 --- a/client/src/pages/Chat.tsx +++ b/client/src/pages/Chat.tsx @@ -198,7 +198,7 @@ function Chat() { InitializeContact={initializeContact} /> - + ) : null} diff --git a/server/socket/socket.js b/server/socket/socket.js index 9a93369..2539b14 100644 --- a/server/socket/socket.js +++ b/server/socket/socket.js @@ -221,29 +221,47 @@ function initializeSocket(io) { }); socket.on("remove user from group", async (msg, callback) => { - const { conversation_id, user_id } = msg; - if (!conversation_id) { + const { group_id, user_id } = msg; + + if (!group_id || !user_id) { return callback({ status: "error", - message: "No conversation id provided", + message: "Missing required parameters", }); } - if (!user_id) { - return callback({ status: "error", message: "No user id provided" }); + + try { + const result = await removeUserFromGroupById(group_id, user_id); + + if (result?.message) { + return callback({ status: "error", message: result.message }); + } + + // Get all sockets in the room + const socketsInRoom = await io.in(group_id).fetchSockets(); + + for (const socketInstance of socketsInRoom) { + if (socketInstance.user_id === user_id) { + socketInstance.leave(group_id); + } + } + + io.to(group_id).to(user_id).emit("left group", { + group_id, + user_id, + }); + + return callback({ + status: "ok", + message: "Successfully removed user from group", + }); + } catch (error) { + console.error("Failed to remove user from group:", error); + return callback({ + status: "error", + message: "Internal server error", + }); } - - const result = await removeUserFromGroupById(conversation_id, user_id); - - if (result?.message) { - return callback({ status: "error", messsage: result.message }); - } - - io.to(conversation_id).emit("left group", { conversation_id, user_id }); - - return callback({ - status: "ok", - message: "Successfully removed user from group", - }); }); socket.on("disconnect", (reason) => {