Edit transfer ownerShip

This commit is contained in:
2025-07-22 23:07:03 +03:30
parent 28af57f181
commit 626b3c2fb4
@@ -1121,23 +1121,49 @@ public class ActionHandler {
return; return;
} }
System.out.println("--- Admins List ---"); 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++) { for (int i = 0; i < admins.length(); i++) {
JSONObject admin = admins.getJSONObject(i); JSONObject admin = admins.getJSONObject(i);
System.out.printf("%d. %s (%s)\n", i + 1, admin.getString("profile_name"), admin.getString("user_id")); 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: "); System.out.print("Select a new owner by number: ");
int choice = Integer.parseInt(scanner.nextLine()) - 1; 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()) { if (choice < 0 || choice >= admins.length()) {
System.out.println("Invalid selection."); System.out.println("Invalid selection. Please try again.");
return; continue;
} }
JSONObject selected = admins.getJSONObject(choice); JSONObject selected = admins.getJSONObject(choice);
String newOwnerId = selected.getString("user_id"); 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(); JSONObject promoteReq = new JSONObject();
promoteReq.put("action", "transfer_group_ownership"); promoteReq.put("action", "transfer_group_ownership");
promoteReq.put("group_id", groupId.toString()); promoteReq.put("group_id", groupId.toString());
@@ -1150,9 +1176,13 @@ public class ActionHandler {
} }
System.out.println("✅ Ownership transferred successfully."); System.out.println("✅ Ownership transferred successfully.");
success = true;
}
leaveChat(groupId, "group"); leaveChat(groupId, "group");
} }
private void removeMemberFromGroup(UUID groupId) { private void removeMemberFromGroup(UUID groupId) {
JSONObject req = new JSONObject(); JSONObject req = new JSONObject();
req.put("action", "view_group_members"); req.put("action", "view_group_members");
@@ -1455,27 +1485,54 @@ public class ActionHandler {
JSONArray admins = res.getJSONObject("data").getJSONArray("admins"); JSONArray admins = res.getJSONObject("data").getJSONArray("admins");
if (admins.length() == 0) { 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; return;
} }
UUID currentUserId = UUID.fromString(Session.getUserUUID());
boolean success = false;
while (!success) {
System.out.println("\n--- Admins List ---"); System.out.println("\n--- Admins List ---");
for (int i = 0; i < admins.length(); i++) { for (int i = 0; i < admins.length(); i++) {
JSONObject admin = admins.getJSONObject(i); JSONObject admin = admins.getJSONObject(i);
System.out.printf("%d. %s (%s)\n", i + 1, admin.getString("profile_name"), admin.getString("user_id")); 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: "); System.out.print("Select a new owner by number: ");
int choice = Integer.parseInt(scanner.nextLine()) - 1; String input = scanner.nextLine().trim();
if (choice < 0 || choice >= admins.length()) { if (input.equals("0")) {
System.out.println("Invalid selection."); System.out.println("❌ Ownership transfer canceled.");
return; return;
} }
JSONObject selected = admins.getJSONObject(choice); int choice;
String newOwnerId = selected.getString("internal_id"); 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(); JSONObject promoteReq = new JSONObject();
promoteReq.put("action", "transfer_channel_ownership"); promoteReq.put("action", "transfer_channel_ownership");
promoteReq.put("channel_id", channelId.toString()); promoteReq.put("channel_id", channelId.toString());
@@ -1488,6 +1545,10 @@ public class ActionHandler {
} }
System.out.println("✅ Ownership transferred successfully."); System.out.println("✅ Ownership transferred successfully.");
success = true; // Break loop
}
// Continue execution after successful ownership transfer
leaveChat(channelId, "channel"); leaveChat(channelId, "channel");
} }