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 };
|
||||
|
||||
@@ -990,6 +990,28 @@ async function removeUserFromGroupById(conversation_id, user_id) {
|
||||
}
|
||||
}
|
||||
|
||||
async function isConversationMember(user_id, conversation_id) {
|
||||
const query = `
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM Memberships
|
||||
WHERE user_id = $1
|
||||
AND conversation_id = $2
|
||||
) AS is_member;
|
||||
`;
|
||||
|
||||
try {
|
||||
const result = await client.query(query, [user_id, conversation_id]);
|
||||
return result.rows[0].is_member;
|
||||
} catch (e) {
|
||||
console.error(
|
||||
`Failed to verify membership for user_id: ${user_id} in conversation_id: ${conversation_id}`,
|
||||
e,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
client,
|
||||
insertUser,
|
||||
@@ -1010,4 +1032,5 @@ module.exports = {
|
||||
deleteMessage,
|
||||
getMembers,
|
||||
removeUserFromGroupById,
|
||||
isConversationMember,
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ const {
|
||||
deleteContact,
|
||||
deleteMessage,
|
||||
removeUserFromGroupById,
|
||||
isConversationMember,
|
||||
} = require("../db/db");
|
||||
const { isValidUsername } = require("../utils/filter");
|
||||
const { verifyJwtToken } = require("../auth/jwt");
|
||||
@@ -76,9 +77,24 @@ function initializeSocket(io) {
|
||||
console.error("(socket) Failed to get user conversations");
|
||||
}
|
||||
|
||||
socket.on("join room", (msg) => {
|
||||
console.log("Join room for: ", username, "room: ", msg);
|
||||
socket.join(msg);
|
||||
socket.on("join room", async (conversation_id, callback) => {
|
||||
const isMember = await isConversationMember(
|
||||
socket.user_id,
|
||||
conversation_id,
|
||||
);
|
||||
if (isMember) {
|
||||
console.log("Join room for: ", username, "room: ", conversation_id);
|
||||
socket.join(conversation_id);
|
||||
return callback({
|
||||
status: "ok",
|
||||
message: "Successfully joined to conversation",
|
||||
});
|
||||
} else {
|
||||
return callback({
|
||||
status: "error",
|
||||
message: "You are not member of this conversation",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("chat message", async (msg, callback) => {
|
||||
|
||||
Reference in New Issue
Block a user