From 0fbccc2c4ca871f76d4aff0419fa63de7e6f6279 Mon Sep 17 00:00:00 2001 From: Partow Roshani Date: Tue, 1 Jul 2025 16:06:29 +0330 Subject: [PATCH] Edit group info --- .../Client/ActionHandler.java | 87 +++++++++++++++---- .../Database/GroupDatabase.java | 47 +++++++++- .../Server/ClientHandler.java | 41 ++++++++- 3 files changed, 155 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java b/src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java index ba18656..7980f72 100644 --- a/src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java +++ b/src/main/java/org/to/telegramfinalproject/Client/ActionHandler.java @@ -797,14 +797,14 @@ public class ActionHandler { } case "5" -> { leaveChat(chat.getId(), "channel"); - return false; // خروج کامل از چت + return false; } case "0" -> { - return false; // بازگشت به لیست چت‌ها + return false; } default -> System.out.println("Invalid choice."); } - return true; // همچنان در منو باقی بمان + return true; } @@ -1105,24 +1105,81 @@ public class ActionHandler { private void editGroupInfo(UUID groupId) { - System.out.print("Enter new group name: "); - String newName = scanner.nextLine().trim(); - - System.out.print("Enter new group description: "); - String newDesc = scanner.nextLine().trim(); - JSONObject req = new JSONObject(); - req.put("action", "edit_group_info"); - req.put("group_id", groupId.toString()); - req.put("name", newName); - req.put("description", newDesc); + req.put("action", "get_chat_info"); + req.put("receiver_id", groupId.toString()); + req.put("receiver_type", "group"); JSONObject res = sendWithResponse(req); - if (res != null) - System.out.println(res.getString("message")); + if (res == null || !res.getString("status").equals("success")) { + System.out.println("❌ Failed to fetch group info."); + return; + } + + JSONObject data = res.getJSONObject("data"); + + String currentId = data.getString("id"); + String currentName = data.getString("name"); + String currentDesc = data.optString("description", "None"); + String currentImage = data.optString("image_url", "None"); + + System.out.println("\n--- Current Group Info ---"); + System.out.println("1. Group ID: " + currentId); + System.out.println("2. Name: " + currentName); + System.out.println("3. Description: " + currentDesc); + System.out.println("4. Image URL: " + currentImage); + System.out.println("0. Cancel"); + + System.out.print("Select the field you want to edit (0-4): "); + String choice = scanner.nextLine().trim(); + + String newGroupId = currentId; + String newName = currentName; + String newDesc = currentDesc; + String newImage = currentImage; + + switch (choice) { + case "1" -> { + System.out.print("Enter new Group ID: "); + newGroupId = scanner.nextLine().trim(); + } + case "2" -> { + System.out.print("Enter new Group Name: "); + newName = scanner.nextLine().trim(); + } + case "3" -> { + System.out.print("Enter new Description: "); + newDesc = scanner.nextLine().trim(); + } + case "4" -> { + System.out.print("Enter new Image URL: "); + newImage = scanner.nextLine().trim(); + } + case "0" -> { + System.out.println("Cancelled."); + return; + } + default -> { + System.out.println("Invalid choice."); + return; + } + } + + JSONObject editReq = new JSONObject(); + editReq.put("action", "edit_group_info"); + editReq.put("group_id", groupId.toString()); // internal_uuid + editReq.put("new_group_id", newGroupId); + editReq.put("name", newName); + editReq.put("description", newDesc); + editReq.put("image_url", newImage); + + JSONObject editRes = sendWithResponse(editReq); + if (editRes != null) + System.out.println(editRes.getString("message")); } + private void viewChannelSubscribers(UUID channelId) { JSONObject req = new JSONObject(); req.put("action", "view_channel_subscribers"); diff --git a/src/main/java/org/to/telegramfinalproject/Database/GroupDatabase.java b/src/main/java/org/to/telegramfinalproject/Database/GroupDatabase.java index d8313a4..1eb550b 100644 --- a/src/main/java/org/to/telegramfinalproject/Database/GroupDatabase.java +++ b/src/main/java/org/to/telegramfinalproject/Database/GroupDatabase.java @@ -4,10 +4,7 @@ import org.json.JSONArray; import org.json.JSONObject; import org.to.telegramfinalproject.Models.Group; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -142,6 +139,48 @@ public class GroupDatabase { } + public static boolean updateGroupInfo(UUID internalUUID, String newGroupId, String name, String description, String imageUrl) { + String sql = "UPDATE groups SET group_id = ?, group_name = ?, description = ?, image_url = ? WHERE internal_uuid = ?"; + + try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, newGroupId); + stmt.setString(2, name); + stmt.setString(3, description); + if (imageUrl == null) { + stmt.setNull(4, Types.VARCHAR); + } else { + stmt.setString(4, imageUrl); + } + stmt.setObject(5, internalUUID); + + int affectedRows = stmt.executeUpdate(); + return affectedRows > 0; + + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + + + public static boolean isGroupIdUnique(String groupId, UUID excludeUUID) { + String sql = "SELECT COUNT(*) FROM groups WHERE group_id = ? AND internal_uuid != ?"; + + try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, groupId); + stmt.setObject(2, excludeUUID); + ResultSet rs = stmt.executeQuery(); + if (rs.next()) { + return rs.getInt(1) == 0; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return false; + } + + + public static void addMember(UUID groupInternalId, UUID userId) { String sql = "INSERT INTO group_members (group_id, user_id) VALUES (?, ?) ON CONFLICT DO NOTHING"; try (Connection conn = ConnectionDb.connect(); PreparedStatement stmt = conn.prepareStatement(sql)) { diff --git a/src/main/java/org/to/telegramfinalproject/Server/ClientHandler.java b/src/main/java/org/to/telegramfinalproject/Server/ClientHandler.java index 7eb2de9..4f64bae 100644 --- a/src/main/java/org/to/telegramfinalproject/Server/ClientHandler.java +++ b/src/main/java/org/to/telegramfinalproject/Server/ClientHandler.java @@ -393,10 +393,10 @@ public class ClientHandler implements Runnable { data.put("internal_id", group.getInternal_uuid().toString()); data.put("name", group.getGroup_name()); data.put("image_url", group.getImage_url()); + data.put("description", group.getDescription() != null ? group.getDescription() : ""); data.put("type", "group"); data.put("id", group.getGroup_id()); - // اضافه کردن owner و admin بودن boolean isOwner = GroupDatabase.isOwner(group.getInternal_uuid(), currentUser.getInternal_uuid()); boolean isAdmin = GroupDatabase.isAdmin(group.getInternal_uuid(), currentUser.getInternal_uuid()); @@ -409,6 +409,7 @@ public class ClientHandler implements Runnable { } + case "channel" -> { Channel channel = ChannelDatabase.findByChannelId(id); if (channel != null) { @@ -627,6 +628,44 @@ public class ClientHandler implements Runnable { } + case "edit_group_info": { + try { + UUID groupUUID = UUID.fromString(requestJson.getString("group_id")); // internal_uuid + String newGroupId = requestJson.getString("new_group_id").trim(); // شناسه نمایشی جدید + String name = requestJson.optString("name"); + String description = requestJson.optString("description", null); + String imageUrl = requestJson.has("image_url") && !requestJson.isNull("image_url") + ? requestJson.getString("image_url") : null; + + boolean isOwner = GroupDatabase.isOwner(groupUUID, currentUser.getInternal_uuid()); + if (!isOwner && !GroupPermissionUtil.canEditGroup(groupUUID, currentUser.getInternal_uuid())) { + response = new ResponseModel("error", "You don't have permission to edit this group."); + break; + } + + if (isOwner && !GroupDatabase.isGroupIdUnique(newGroupId, groupUUID)) { + response = new ResponseModel("error", "Group ID is already taken by another group."); + break; + } + + boolean updated = GroupDatabase.updateGroupInfo(groupUUID, newGroupId, name, description, imageUrl); + response = updated + ? new ResponseModel("success", "Group info updated successfully.") + : new ResponseModel("error", "Failed to update group info."); + + //if (updated) { + //RealTimeEventDispatcher.sendGroupOrChannelUpdate(groupUUID, "group", name); + //} + + } catch (Exception e) { + response = new ResponseModel("error", "Error updating group: " + e.getMessage()); + } + break; + } + + + + case "view_channel_admins": { UUID channelId = UUID.fromString(requestJson.getString("channel_id"));