initialize contact again when added to group or left

This commit is contained in:
slawk0
2024-12-15 17:24:44 +01:00
parent 8cad73d01d
commit 6d15b9afb7
7 changed files with 123 additions and 59 deletions

View File

@@ -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) => {