From c15149646a1e52a0b588577b2b0524bb524fc544 Mon Sep 17 00:00:00 2001 From: Partow Roshani Date: Fri, 5 Sep 2025 23:13:28 +0330 Subject: [PATCH] Delete chat --- .../Client/IncomingMessageListener.java | 2 + .../Database/ConnectionDb.java | 2 +- .../UI/UserInfoController.java | 106 +++++++++++++++++- 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/to/telegramfinalproject/Client/IncomingMessageListener.java b/src/main/java/org/to/telegramfinalproject/Client/IncomingMessageListener.java index 6c7be2a..13c9b76 100644 --- a/src/main/java/org/to/telegramfinalproject/Client/IncomingMessageListener.java +++ b/src/main/java/org/to/telegramfinalproject/Client/IncomingMessageListener.java @@ -336,6 +336,8 @@ public class IncomingMessageListener implements Runnable { displayRealTimeMessage(action, msg); } + + default :{ System.out.println("\n❓ Unknown real-time action: " + action); System.out.println(msg.toString(2)); diff --git a/src/main/java/org/to/telegramfinalproject/Database/ConnectionDb.java b/src/main/java/org/to/telegramfinalproject/Database/ConnectionDb.java index 8d72664..b58f4c0 100644 --- a/src/main/java/org/to/telegramfinalproject/Database/ConnectionDb.java +++ b/src/main/java/org/to/telegramfinalproject/Database/ConnectionDb.java @@ -7,7 +7,7 @@ import java.sql.SQLException; public class ConnectionDb { private static final String JDBC_URL = "jdbc:postgresql://localhost:5432/Telegram"; private static final String USERNAME = "postgres"; - private static final String PASSWORD = "124postpass"; + private static final String PASSWORD = "Partow@1384"; public ConnectionDb() { } diff --git a/src/main/java/org/to/telegramfinalproject/UI/UserInfoController.java b/src/main/java/org/to/telegramfinalproject/UI/UserInfoController.java index cf90a85..7993547 100644 --- a/src/main/java/org/to/telegramfinalproject/UI/UserInfoController.java +++ b/src/main/java/org/to/telegramfinalproject/UI/UserInfoController.java @@ -15,6 +15,8 @@ import org.to.telegramfinalproject.Client.Session; import org.to.telegramfinalproject.Models.ChatEntry; import java.net.URL; +import java.util.Optional; +import java.util.UUID; public class UserInfoController { @@ -38,6 +40,8 @@ public class UserInfoController { @FXML private ImageView moreIcon; @FXML private ImageView bioIcon; @FXML private ImageView usernameIcon; + @FXML private Button deleteChatButton; + private String otherUserId; // internal_uuid of the other user @@ -72,7 +76,7 @@ public class UserInfoController { } }); - deleteChatItem.setOnAction(e -> handleDeleteChat()); + deleteChatItem.setOnAction(e -> onDeleteChatClicked()); blockItem.setOnAction(e -> handleBlock()); unblockItem.setOnAction(e -> handleUnblock()); @@ -163,6 +167,106 @@ public class UserInfoController { } } + + + @FXML + private void onDeleteChatClicked() { +// if (!"private".equalsIgnoreCase(Session.currentChatType)) { +// new Alert(Alert.AlertType.INFORMATION, "This action is available only for private chats.").showAndWait(); +// return; +// } + + UUID chatId = Session.currentChatEntry != null + ? Session.currentChatEntry.getId() + : (Session.currentChatId != null ? UUID.fromString(Session.currentChatId) : null); + + if (chatId == null) { + new Alert(Alert.AlertType.ERROR, "Invalid chat id.").showAndWait(); + return; + } + + ButtonType oneSide = new ButtonType("Delete one-sided", ButtonBar.ButtonData.LEFT); + ButtonType bothSide = new ButtonType("Delete both-sided", ButtonBar.ButtonData.OK_DONE); + ButtonType cancel = ButtonType.CANCEL; + + Alert dlg = new Alert( + Alert.AlertType.CONFIRMATION, + "Choose how you want to delete this private chat:", + oneSide, bothSide, cancel + ); + dlg.setHeaderText("Delete Private Chat"); + + Optional res = dlg.showAndWait(); + if (res.isEmpty() || res.get() == cancel) return; + + boolean both = (res.get() == bothSide); + performDeletePrivateChat(chatId, both); + } + + + private void performDeletePrivateChat(UUID chatId, boolean both) { + // نال‌سیف: هر کدوم هست disable کن + if (deleteChatButton != null) deleteChatButton.setDisable(true); + if (deleteChatItem != null) deleteChatItem.setDisable(true); + if (infoMoreMenu != null && infoMoreMenu.isShowing()) infoMoreMenu.hide(); + if (infoMoreButton != null) infoMoreButton.setDisable(true); + + new Thread(() -> { + JSONObject req = new JSONObject() + .put("action", "delete_private_chat") + .put("chat_id", chatId.toString()) + .put("both", both); + + JSONObject resp = ActionHandler.sendWithResponse(req); + + Platform.runLater(() -> { + // re-enable نال‌سیف + if (deleteChatButton != null) deleteChatButton.setDisable(false); + if (deleteChatItem != null) deleteChatItem.setDisable(false); + if (infoMoreButton != null) infoMoreButton.setDisable(false); + + if (resp != null && "success".equalsIgnoreCase(resp.optString("status"))) { + removeChatFromSessionAndGoBack(chatId); + MainController.getInstance().closeOverlay(profileCard.getParent()); + +// new Alert(Alert.AlertType.INFORMATION, +// "Chat deleted " + (both ? "for both sides." : "only for you.") +// ).showAndWait(); + } else { + String msg = (resp != null ? resp.optString("message", "Unknown error") + : "No response from server."); + new Alert(Alert.AlertType.ERROR, "Failed to delete chat: " + msg).showAndWait(); + } + }); + }).start(); + } + + + private void removeChatFromSessionAndGoBack(UUID chatId) { +// if (Session.chatList != null) Session.chatList.removeIf(e -> chatId.equals(e.getId())); +// if (Session.activeChats != null) Session.activeChats.removeIf(e -> chatId.equals(e.getId())); +// if (Session.archivedChats != null) Session.archivedChats.removeIf(e -> chatId.equals(e.getId())); + + if (Session.currentChatId != null && Session.currentChatId.equals(chatId.toString())) { + Session.currentChatId = null; + Session.currentChatType = null; + Session.currentChatEntry = null; + Session.inChatMenu = false; + + MainController.getInstance().closeOverlay(profileCard.getParent()); + + AppRouter.showMain(); + return; + } + + try { + MainController.getInstance().refreshChatListUI(); + } catch (Exception ignore) { + AppRouter.showMain(); + } + } + + private void handleBlock() { JSONObject req = new JSONObject() .put("action", "block_user")