code refactor, added animated message deletion, remove message button is not displayed only where you can actually use it
This commit is contained in:
@@ -810,7 +810,7 @@ async function contactSuggestion(username) {
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteMessage(user_id, message_id) {
|
||||
async function deleteMessage(user_id, conversation_id, message_id) {
|
||||
const checkMessageOwnershipQuery = `
|
||||
SELECT user_id FROM Messages WHERE message_id = $1;
|
||||
`;
|
||||
@@ -820,17 +820,19 @@ async function deleteMessage(user_id, message_id) {
|
||||
`;
|
||||
|
||||
try {
|
||||
const checkResult = await client.query(checkMessageOwnershipQuery, [
|
||||
message_id,
|
||||
]);
|
||||
if (checkResult.rows.length === 0) {
|
||||
return { message: "Message not found." };
|
||||
}
|
||||
|
||||
const messageOwnerId = checkResult.rows[0].user_id;
|
||||
if (user_id !== messageOwnerId) {
|
||||
console.error("User is not authorized to delete this message");
|
||||
return { message: "It's not your message bro" };
|
||||
const isAdminResult = await isAdmin(user_id, conversation_id);
|
||||
if (!isAdminResult) {
|
||||
const ownershipResult = await client.query(checkMessageOwnershipQuery, [
|
||||
message_id,
|
||||
]);
|
||||
if (ownershipResult.rows.length === 0) {
|
||||
return { message: "Message not found." };
|
||||
}
|
||||
const messageOwnerId = ownershipResult.rows[0].user_id;
|
||||
if (user_id !== messageOwnerId) {
|
||||
console.error("User is not authorized to delete this message");
|
||||
return { message: "It's not your message bro" };
|
||||
}
|
||||
}
|
||||
const deleteResult = await client.query(deleteMessageQuery, [message_id]);
|
||||
if (deleteResult.rowCount > 0) {
|
||||
|
||||
@@ -179,7 +179,6 @@ function initializeSocket(io) {
|
||||
});
|
||||
|
||||
socket.on("delete message", async (msg, callback) => {
|
||||
console.log("(socket) delete message for message_id: ", msg);
|
||||
const { conversation_id, message_id } = msg;
|
||||
if (!message_id) {
|
||||
return callback({ status: "error", message: "No message id provided" });
|
||||
@@ -191,7 +190,11 @@ function initializeSocket(io) {
|
||||
});
|
||||
}
|
||||
|
||||
const result = await deleteMessage(socket.user_id, message_id);
|
||||
const result = await deleteMessage(
|
||||
socket.user_id,
|
||||
conversation_id,
|
||||
message_id,
|
||||
);
|
||||
if (result?.message !== undefined) {
|
||||
return callback({ status: "error", message: result.message });
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user