RealTime and openChat

This commit is contained in:
2025-06-30 22:22:08 +03:30
parent 5afe3662d4
commit ee037532a1
20 changed files with 2222 additions and 275 deletions
@@ -49,19 +49,47 @@ public class ContactDatabase {
}
}
public boolean blockContact(UUID user_id, UUID contact_id) {
String sql = "UPDATE contacts SET is_blocked = TRUE WHERE user_id = ? AND contact_id = ?";
try (Connection connection = getConnection()) {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setObject(1, user_id);
stmt.setObject(2, contact_id);
return stmt.executeUpdate() > 0;
public static boolean toggleBlock(UUID userId, UUID targetId) {
String selectSql = "SELECT is_blocked FROM contacts WHERE user_id = ? AND contact_id = ?";
String updateSql = "UPDATE contacts SET is_blocked = ? WHERE user_id = ? AND contact_id = ?";
try (Connection conn = getConnection();
PreparedStatement selectStmt = conn.prepareStatement(selectSql)) {
selectStmt.setObject(1, userId);
selectStmt.setObject(2, targetId);
ResultSet rs = selectStmt.executeQuery();
if (rs.next()) {
boolean currentlyBlocked = rs.getBoolean("is_blocked");
try (PreparedStatement updateStmt = conn.prepareStatement(updateSql)) {
updateStmt.setBoolean(1, !currentlyBlocked);
updateStmt.setObject(2, userId);
updateStmt.setObject(3, targetId);
updateStmt.executeUpdate();
}
return !currentlyBlocked;
} else {
// اگر رابطه وجود نداره، اول باید کاربر رو به contact ها اضافه کنیم
String insertSql = "INSERT INTO contacts (user_id, contact_id, is_blocked) VALUES (?, ?, ?)";
try (PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
insertStmt.setObject(1, userId);
insertStmt.setObject(2, targetId);
insertStmt.setBoolean(3, true);
insertStmt.executeUpdate();
}
return true;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return false;
}
public boolean unblockContact(UUID user_id, UUID contact_id) {
String sql = "UPDATE contacts SET is_blocked = FALSE WHERE user_id = ? AND contact_id = ?";
try (Connection connection = getConnection()) {
@@ -160,6 +188,76 @@ public class ContactDatabase {
}
public static boolean deleteChatOneSide(UUID currentUserId, UUID otherUserId) {
String sql = """
UPDATE private_chat
SET user1_deleted = CASE WHEN user1_id = ? THEN TRUE ELSE user1_deleted END,
user2_deleted = CASE WHEN user2_id = ? THEN TRUE ELSE user2_deleted END
WHERE (user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)
""";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, currentUserId);
stmt.setObject(2, currentUserId);
stmt.setObject(3, currentUserId);
stmt.setObject(4, otherUserId);
stmt.setObject(5, otherUserId);
stmt.setObject(6, currentUserId);
return stmt.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean deleteChatBoth(UUID currentUserId, UUID otherUserId) {
String sqlDeleteMessages = """
DELETE FROM messages
WHERE receiver_type = 'private' AND (
(sender_id = ? AND receiver_id = ?) OR
(sender_id = ? AND receiver_id = ?)
)
""";
String sqlDeleteChat = """
DELETE FROM private_chat
WHERE (user1_id = ? AND user2_id = ?) OR (user1_id = ? AND user2_id = ?)
""";
try (Connection conn = getConnection()) {
conn.setAutoCommit(false);
try (PreparedStatement stmtMsg = conn.prepareStatement(sqlDeleteMessages);
PreparedStatement stmtChat = conn.prepareStatement(sqlDeleteChat)) {
stmtMsg.setObject(1, currentUserId);
stmtMsg.setObject(2, otherUserId);
stmtMsg.setObject(3, otherUserId);
stmtMsg.setObject(4, currentUserId);
stmtMsg.executeUpdate();
stmtChat.setObject(1, currentUserId);
stmtChat.setObject(2, otherUserId);
stmtChat.setObject(3, otherUserId);
stmtChat.setObject(4, currentUserId);
stmtChat.executeUpdate();
conn.commit();
return true;
} catch (SQLException e) {
conn.rollback();
e.printStackTrace();
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}