Remove admin in group chat
This commit is contained in:
@@ -1179,20 +1179,6 @@ public class ActionHandler {
|
||||
|
||||
|
||||
|
||||
private void removeAdminFromGroup(UUID groupId) {
|
||||
System.out.print("Enter user_id to remove from admin: ");
|
||||
String userId = scanner.nextLine().trim();
|
||||
|
||||
JSONObject req = new JSONObject();
|
||||
req.put("action", "remove_admin_from_group");
|
||||
req.put("group_id", groupId.toString());
|
||||
req.put("user_id", userId);
|
||||
|
||||
JSONObject res = sendWithResponse(req);
|
||||
if (res != null)
|
||||
System.out.println(res.getString("message"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void removeSubscriberFromChannel(UUID channelId) {
|
||||
@@ -1410,6 +1396,66 @@ public class ActionHandler {
|
||||
}
|
||||
|
||||
|
||||
private void removeAdminFromGroup(UUID groupId) {
|
||||
JSONObject req = new JSONObject();
|
||||
req.put("action", "view_group_admins");
|
||||
req.put("group_id", groupId.toString());
|
||||
|
||||
JSONObject res = sendWithResponse(req);
|
||||
if (res == null || !res.getString("status").equals("success")) {
|
||||
System.out.println("❌ Failed to fetch admins.");
|
||||
return;
|
||||
}
|
||||
|
||||
JSONArray admins = res.getJSONObject("data").getJSONArray("admins");
|
||||
List<JSONObject> eligible = new ArrayList<>();
|
||||
|
||||
System.out.println("\n--- Admins List ---");
|
||||
for (int i = 0; i < admins.length(); i++) {
|
||||
JSONObject admin = admins.getJSONObject(i);
|
||||
String role = admin.getString("role");
|
||||
String profileName = admin.getString("profile_name");
|
||||
String userId = admin.getString("user_id");
|
||||
|
||||
if (!role.equals("owner")) {
|
||||
eligible.add(admin);
|
||||
System.out.printf("%d. %s (%s)\n", eligible.size(), profileName, userId);
|
||||
}
|
||||
}
|
||||
|
||||
if (eligible.isEmpty()) {
|
||||
System.out.println("⚠️ No removable admins.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.print("Select an admin to remove: ");
|
||||
int choice;
|
||||
try {
|
||||
choice = Integer.parseInt(scanner.nextLine()) - 1;
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ Invalid input.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (choice < 0 || choice >= eligible.size()) {
|
||||
System.out.println("❌ Invalid selection.");
|
||||
return;
|
||||
}
|
||||
|
||||
JSONObject selected = eligible.get(choice);
|
||||
String targetInternalUUID = selected.getString("internal_uuid");
|
||||
|
||||
JSONObject removeReq = new JSONObject();
|
||||
removeReq.put("action", "remove_admin_from_group");
|
||||
removeReq.put("group_id", groupId.toString());
|
||||
removeReq.put("target_user_id", targetInternalUUID);
|
||||
|
||||
JSONObject removeRes = sendWithResponse(removeReq);
|
||||
if (removeRes != null)
|
||||
System.out.println(removeRes.getString("message"));
|
||||
}
|
||||
|
||||
|
||||
private void removeAdminFromChannel(UUID channelId) {
|
||||
JSONObject req = new JSONObject();
|
||||
req.put("action", "view_channel_admins");
|
||||
|
||||
@@ -377,7 +377,7 @@ public class GroupDatabase {
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "member"; // پیشفرض
|
||||
return "member";
|
||||
}
|
||||
|
||||
|
||||
@@ -406,10 +406,12 @@ public class GroupDatabase {
|
||||
public static List<JSONObject> getGroupAdminsAndOwner(UUID groupId) {
|
||||
List<JSONObject> admins = new ArrayList<>();
|
||||
|
||||
String sql = "SELECT gm.user_id, gm.role, gm.permissions, u.profile_name " +
|
||||
"FROM group_members gm " +
|
||||
"JOIN users u ON gm.user_id = u.internal_uuid " +
|
||||
"WHERE gm.group_id = ? AND gm.role IN ('owner', 'admin')";
|
||||
String sql = """
|
||||
SELECT u.internal_uuid, u.user_id, gm.role, gm.permissions, u.profile_name
|
||||
FROM group_members gm
|
||||
JOIN users u ON gm.user_id = u.internal_uuid
|
||||
WHERE gm.group_id = ? AND gm.role IN ('owner', 'admin')
|
||||
""";
|
||||
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
@@ -419,7 +421,8 @@ public class GroupDatabase {
|
||||
|
||||
while (rs.next()) {
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("user_id", rs.getObject("user_id").toString());
|
||||
obj.put("internal_uuid", rs.getObject("internal_uuid").toString()); // این قسمت اضافه شد
|
||||
obj.put("user_id", rs.getString("user_id"));
|
||||
obj.put("role", rs.getString("role"));
|
||||
obj.put("permissions", new JSONObject(rs.getString("permissions")));
|
||||
obj.put("profile_name", rs.getString("profile_name"));
|
||||
|
||||
@@ -35,7 +35,7 @@ public class RegisterForm {
|
||||
public void initialize() {
|
||||
|
||||
try {
|
||||
connection = new ClientConnection("localhost", 12345);
|
||||
connection = new ClientConnection("localhost", 8000);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Could not connect to server: " + e.getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user