From 626b3c2fb4101410811de860ff3d574167a779a0 Mon Sep 17 00:00:00 2001 From: Partow Roshani Date: Tue, 22 Jul 2025 23:07:03 +0330 Subject: [PATCH] Edit transfer ownerShip --- .../Client/ActionHandler.java | 171 ++++++++++++------ 1 file changed, 116 insertions(+), 55 deletions(-) diff --git a/src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java b/src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java index 93d2cdb..88e7114 100644 --- a/src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java +++ b/src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java @@ -1121,38 +1121,68 @@ public class ActionHandler { return; } - System.out.println("--- Admins List ---"); - for (int i = 0; i < admins.length(); i++) { - JSONObject admin = admins.getJSONObject(i); - System.out.printf("%d. %s (%s)\n", i + 1, admin.getString("profile_name"), admin.getString("user_id")); + UUID currentUserId = UUID.fromString(Session.getUserUUID()); + boolean success = false; + + while (!success) { + System.out.println("\n--- Admins List ---"); + for (int i = 0; i < admins.length(); i++) { + JSONObject admin = admins.getJSONObject(i); + System.out.printf("%d. %s (%s)%s\n", i + 1, + admin.getString("profile_name"), + admin.getString("user_id"), + admin.getString("user_id").equals(currentUserId.toString()) ? " 👑 (You)" : ""); + } + System.out.println("0. Cancel"); + + System.out.print("Select a new owner by number: "); + String input = scanner.nextLine().trim(); + + if (input.equals("0")) { + System.out.println("❌ Ownership transfer canceled."); + return; + } + + int choice; + try { + choice = Integer.parseInt(input) - 1; + } catch (NumberFormatException e) { + System.out.println("❗ Invalid input. Please enter a number."); + continue; + } + + if (choice < 0 || choice >= admins.length()) { + System.out.println("❗ Invalid selection. Please try again."); + continue; + } + + JSONObject selected = admins.getJSONObject(choice); + String newOwnerId = selected.getString("user_id"); + + if (newOwnerId.equals(currentUserId.toString())) { + System.out.println("⚠️ You cannot transfer ownership to yourself. Please select a different admin."); + continue; + } + + JSONObject promoteReq = new JSONObject(); + promoteReq.put("action", "transfer_group_ownership"); + promoteReq.put("group_id", groupId.toString()); + promoteReq.put("new_owner_user_id", newOwnerId); + + JSONObject promoteRes = sendWithResponse(promoteReq); + if (promoteRes == null || !promoteRes.getString("status").equals("success")) { + System.out.println("❌ Failed to transfer ownership."); + return; + } + + System.out.println("✅ Ownership transferred successfully."); + success = true; } - System.out.print("Select a new owner by number: "); - int choice = Integer.parseInt(scanner.nextLine()) - 1; - - if (choice < 0 || choice >= admins.length()) { - System.out.println("Invalid selection."); - return; - } - - JSONObject selected = admins.getJSONObject(choice); - String newOwnerId = selected.getString("user_id"); - - JSONObject promoteReq = new JSONObject(); - promoteReq.put("action", "transfer_group_ownership"); - promoteReq.put("group_id", groupId.toString()); - promoteReq.put("new_owner_user_id", newOwnerId); - - JSONObject promoteRes = sendWithResponse(promoteReq); - if (promoteRes == null || !promoteRes.getString("status").equals("success")) { - System.out.println("❌ Failed to transfer ownership."); - return; - } - - System.out.println("✅ Ownership transferred successfully."); leaveChat(groupId, "group"); } + private void removeMemberFromGroup(UUID groupId) { JSONObject req = new JSONObject(); req.put("action", "view_group_members"); @@ -1455,39 +1485,70 @@ public class ActionHandler { JSONArray admins = res.getJSONObject("data").getJSONArray("admins"); if (admins.length() == 0) { - System.out.println("⚠️ No other admins available. You cannot leave without promoting someone to owner."); + System.out.println("⚠️ No admins available. You cannot leave without transferring ownership."); return; } - System.out.println("\n--- Admins List ---"); - for (int i = 0; i < admins.length(); i++) { - JSONObject admin = admins.getJSONObject(i); - System.out.printf("%d. %s (%s)\n", i + 1, admin.getString("profile_name"), admin.getString("user_id")); + UUID currentUserId = UUID.fromString(Session.getUserUUID()); + boolean success = false; + + while (!success) { + System.out.println("\n--- Admins List ---"); + for (int i = 0; i < admins.length(); i++) { + JSONObject admin = admins.getJSONObject(i); + System.out.printf("%d. %s (%s)%s\n", i + 1, + admin.getString("profile_name"), + admin.getString("user_id"), + admin.getString("internal_uuid").equals(currentUserId.toString()) ? " 👑 (You)" : ""); + } + System.out.println("0. Cancel"); + + System.out.print("Select a new owner by number: "); + String input = scanner.nextLine().trim(); + + if (input.equals("0")) { + System.out.println("❌ Ownership transfer canceled."); + return; + } + + int choice; + try { + choice = Integer.parseInt(input) - 1; + } catch (NumberFormatException e) { + System.out.println("❗ Invalid input. Please enter a number."); + continue; + } + + if (choice < 0 || choice >= admins.length()) { + System.out.println("❗ Invalid selection. Please try again."); + continue; + } + + JSONObject selected = admins.getJSONObject(choice); + String newOwnerId = selected.getString("internal_uuid"); + + if (newOwnerId.equals(currentUserId.toString())) { + System.out.println("⚠️ You cannot transfer ownership to yourself. Please select a different admin."); + continue; + } + + // Send transfer request + JSONObject promoteReq = new JSONObject(); + promoteReq.put("action", "transfer_channel_ownership"); + promoteReq.put("channel_id", channelId.toString()); + promoteReq.put("new_owner_user_id", newOwnerId); + + JSONObject promoteRes = sendWithResponse(promoteReq); + if (promoteRes == null || !promoteRes.getString("status").equals("success")) { + System.out.println("❌ Failed to transfer ownership."); + return; + } + + System.out.println("✅ Ownership transferred successfully."); + success = true; // Break loop } - System.out.print("Select a new owner by number: "); - int choice = Integer.parseInt(scanner.nextLine()) - 1; - - if (choice < 0 || choice >= admins.length()) { - System.out.println("Invalid selection."); - return; - } - - JSONObject selected = admins.getJSONObject(choice); - String newOwnerId = selected.getString("internal_id"); - - JSONObject promoteReq = new JSONObject(); - promoteReq.put("action", "transfer_channel_ownership"); - promoteReq.put("channel_id", channelId.toString()); - promoteReq.put("new_owner_user_id", newOwnerId); - - JSONObject promoteRes = sendWithResponse(promoteReq); - if (promoteRes == null || !promoteRes.getString("status").equals("success")) { - System.out.println("❌ Failed to transfer ownership."); - return; - } - - System.out.println("✅ Ownership transferred successfully."); + // Continue execution after successful ownership transfer leaveChat(channelId, "channel"); }