checking membership on insertingMessages, improved error messages for client

This commit is contained in:
slawk0
2024-12-13 17:31:50 +01:00
parent 374e64044c
commit c2379ff44e
7 changed files with 49 additions and 24 deletions

View File

@@ -162,21 +162,33 @@ async function insertMessage(
`senderId: ${senderId}, conversation_id: ${conversation_id}, content: ${content}, attachmentUrl: ${attachmentUrls}`,
);
const checkMembershipQuery = `
SELECT 1 FROM Memberships
WHERE conversation_id = $1 AND user_id = $2
LIMIT 1;
`;
const query = `
INSERT INTO Messages (conversation_id, user_id, content, attachment_urls)
VALUES ($1, $2, $3, $4)
RETURNING message_id, content, sent_at, attachment_urls, user_id AS sender_id, conversation_id;
`;
try {
const checkResult = await client.query(checkMembershipQuery, [
conversation_id,
senderId,
]);
if (checkResult.rows.length === 0) {
console.error("User is not a member of the conversation");
return [];
}
const result = await client.query(query, [
conversation_id,
senderId,
content,
attachmentUrls,
]);
console.log("insertmessageresult: ", result.rows);
updateConversationLastActive(conversation_id);
updateConversationLastActive(conversation_id, senderId);
return result.rows[0];
} catch (e) {
console.error("Failed to insert message ", e);
@@ -419,7 +431,8 @@ async function insertContact(userUsername, receiverUsername, read) {
receiverUsername,
]);
if (!usersResult.rows[0]) {
throw new Error("Users not found");
console.error("Users not found");
return null;
}
const { initiator_id, contact_id, contact_username } = usersResult.rows[0];
@@ -659,8 +672,21 @@ async function updateContactLastActive(user_id, contact_id) {
}
}
async function updateConversationLastActive(conversation_id) {
//TODO CHECK IF USER IS MEMBER OF THAT CONVERSATION
async function updateConversationLastActive(conversation_id, userId) {
const checkMembershipQuery = `
SELECT 1 FROM Memberships
WHERE conversation_id = $1 AND user_id = $2
LIMIT 1;
`;
const checkResult = await client.query(checkMembershipQuery, [
conversation_id,
userId,
]);
if (checkResult.rows.length === 0) {
console.error("User is not a member of the conversation");
}
const query = `
UPDATE Conversations
SET last_active = NOW()
@@ -683,9 +709,7 @@ async function getConversationsForUser(user_id) {
try {
const result = await client.query(query, [user_id]);
const conversationIds = result.rows.map((row) => row.conversation_id);
console.log("getconversationsforuser: ", conversationIds);
return conversationIds;
return result.rows.map((row) => row.conversation_id);
} catch (e) {
console.error("Failed to get conversations for user ", e);
return [];
@@ -768,10 +792,6 @@ async function getMembers(conversation_id) {
}
}
function getTime() {
return new Date();
}
module.exports = {
client,
insertUser,

View File

@@ -112,8 +112,12 @@ function initializeSocket(io) {
message: "Failed to insert message",
});
return;
} else if (insertedMessage.length === 0) {
callback({
status: "error",
message: "You are not an member of this conversation",
});
}
const { message_id, content, sent_at, sender_id, conversation_id } =
insertedMessage;
console.log("(socket) received from chat message", msg);