verifying if user is member of conversation before joining
This commit is contained in:
@@ -108,7 +108,7 @@ function ContactsList({
|
||||
}}
|
||||
>
|
||||
<span className="flex-shrink-0">
|
||||
usrId:{contact.user_id} cnvId:{contact.conversation_id}{' '}
|
||||
{/*usrId:{contact.user_id} cnvId:{contact.conversation_id}*/}
|
||||
{contact.username}
|
||||
</span>
|
||||
{contact.type === 'group' ? (
|
||||
|
||||
@@ -62,22 +62,34 @@ function Chat() {
|
||||
}
|
||||
}, []);
|
||||
|
||||
function initializeContact(newContact: ContactsProps) {
|
||||
async function initializeContact(newContact: ContactsProps) {
|
||||
setMessages([]); // Clear messages from previous contact
|
||||
localStorage.setItem('contact', JSON.stringify(newContact)); // Set current contact in localstorage
|
||||
setCurrentContact(newContact);
|
||||
console.log('Initialized contact: ', newContact);
|
||||
joinRoom(newContact.conversation_id);
|
||||
fetchMessages(newContact.conversation_id).catch((e) =>
|
||||
console.error('Failed to fetch messages: ', e),
|
||||
);
|
||||
setContactsList((prevContacts) =>
|
||||
prevContacts.map((c) =>
|
||||
c.username === newContact.username ? { ...c, read: true } : c,
|
||||
),
|
||||
);
|
||||
|
||||
console.log('Current contact is now: ', newContact);
|
||||
try {
|
||||
const joinResult = await joinRoom(newContact.conversation_id);
|
||||
if (!joinResult.success) {
|
||||
setErrorMessage(joinResult.message);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
await fetchMessages(newContact.conversation_id);
|
||||
} catch (e) {
|
||||
console.error('Failed to fetch messages: ', e);
|
||||
return false;
|
||||
}
|
||||
setContactsList((prevContacts) =>
|
||||
prevContacts.map((c) =>
|
||||
c.username === newContact.username ? { ...c, read: true } : c,
|
||||
),
|
||||
);
|
||||
console.log('Current contact is now: ', newContact);
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error('Failed to initialize contact:', e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const fetchMessages = async (conversation_id: number) => {
|
||||
|
||||
@@ -4,9 +4,10 @@ let socket: Socket | null = null;
|
||||
function initializeSocket(token: string): Socket | null {
|
||||
if (!socket && token) {
|
||||
socket = io({
|
||||
auth: {
|
||||
token: token,
|
||||
},
|
||||
auth: { token },
|
||||
reconnection: true,
|
||||
reconnectionAttempts: 5,
|
||||
reconnectionDelay: 1000,
|
||||
});
|
||||
|
||||
socket.on('connect', () => console.log('connected'));
|
||||
@@ -20,10 +21,38 @@ function initializeSocket(token: string): Socket | null {
|
||||
return socket;
|
||||
}
|
||||
|
||||
function joinRoom(conversation_id: number) {
|
||||
if (!socket) return;
|
||||
socket.emit('join room', conversation_id);
|
||||
console.log('sent on join room: ', conversation_id);
|
||||
}
|
||||
async function joinRoom(
|
||||
conversation_id: number,
|
||||
): Promise<{ success: boolean; message: string }> {
|
||||
console.log('Attempting to join room:', conversation_id);
|
||||
|
||||
if (!socket) {
|
||||
return {
|
||||
success: false,
|
||||
message: 'Socket connection not available',
|
||||
};
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
socket!.emit(
|
||||
'join room',
|
||||
conversation_id,
|
||||
(response: { status: 'ok' | 'error'; message: string }) => {
|
||||
if (response.status === 'ok') {
|
||||
console.log('Successfully joined room:', response.message);
|
||||
resolve({
|
||||
success: true,
|
||||
message: response.message,
|
||||
});
|
||||
} else {
|
||||
console.error('Failed to join room:', response.message);
|
||||
resolve({
|
||||
success: false,
|
||||
message: response.message,
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
export { initializeSocket, joinRoom, socket };
|
||||
|
||||
Reference in New Issue
Block a user