Edit create group/channel form
This commit is contained in:
@@ -57,14 +57,41 @@ public class ActionHandler {
|
||||
|
||||
public void register() {
|
||||
System.out.println("Register form: \n");
|
||||
System.out.print("Username: ");
|
||||
String username = this.scanner.nextLine();
|
||||
System.out.print("User id: ");
|
||||
String user_id = this.scanner.nextLine();
|
||||
System.out.print("Password: ");
|
||||
String password = this.scanner.nextLine();
|
||||
System.out.print("Profile name: ");
|
||||
String profile_name = this.scanner.nextLine();
|
||||
|
||||
String username, user_id, password, profile_name;
|
||||
|
||||
while (true) {
|
||||
System.out.print("Username: ");
|
||||
username = scanner.nextLine().trim();
|
||||
if (username.isEmpty()) {
|
||||
System.out.println("❌ Username cannot be empty.");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.print("User id: ");
|
||||
user_id = scanner.nextLine().trim();
|
||||
if (user_id.isEmpty()) {
|
||||
System.out.println("❌ User ID cannot be empty.");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.print("Password: ");
|
||||
password = scanner.nextLine();
|
||||
if (password.isEmpty()) {
|
||||
System.out.println("❌ Password cannot be empty.");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.print("Profile name: ");
|
||||
profile_name = scanner.nextLine().trim();
|
||||
if (profile_name.isEmpty()) {
|
||||
System.out.println("❌ Profile name cannot be empty.");
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
JSONObject request = new JSONObject();
|
||||
request.put("action", "register");
|
||||
@@ -212,9 +239,9 @@ public class ActionHandler {
|
||||
System.err.println("Error fetching chat info: " + e.getMessage());
|
||||
}
|
||||
|
||||
// اگر شکست خورد، internalId نامعتبر میسازیم (برای جلوگیری از null)
|
||||
//if invalid make unknown modle
|
||||
return new ChatEntry(
|
||||
UUID.randomUUID(), // ساخت یک UUID موقت (ولی اشتباه)
|
||||
UUID.randomUUID(),
|
||||
receiverId,
|
||||
"[Unknown " + receiverType + "]",
|
||||
"",
|
||||
@@ -237,7 +264,7 @@ public class ActionHandler {
|
||||
if (response.has("data") && !response.isNull("data")) {
|
||||
JSONObject data = response.getJSONObject("data");
|
||||
|
||||
// ❗ چک کن که chat_list وجود داره
|
||||
//is chat list available
|
||||
if (!data.has("chat_list") || data.isNull("chat_list")) {
|
||||
System.out.println("❌ chat_list not found in response data.");
|
||||
return;
|
||||
@@ -283,8 +310,19 @@ public class ActionHandler {
|
||||
|
||||
|
||||
public void createGroup() {
|
||||
System.out.print("Enter group ID: ");
|
||||
String groupId = scanner.nextLine();
|
||||
String groupId = null;
|
||||
|
||||
while (groupId == null){
|
||||
System.out.print("Enter group ID: ");
|
||||
String input = scanner.nextLine();
|
||||
if (!input.trim().isEmpty()) {
|
||||
groupId = input;
|
||||
} else {
|
||||
System.out.println("Group ID can't be empty.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
String groupName = null;
|
||||
while (groupName == null) {
|
||||
System.out.print("Enter group name: ");
|
||||
@@ -311,8 +349,18 @@ public class ActionHandler {
|
||||
|
||||
|
||||
public void createChannel() {
|
||||
System.out.print("Enter channel ID: ");
|
||||
String channelId = scanner.nextLine();
|
||||
String channelId = null;
|
||||
|
||||
while (channelId == null){
|
||||
System.out.print("Enter channel ID: ");
|
||||
String input = scanner.nextLine();
|
||||
if (!input.trim().isEmpty()) {
|
||||
channelId = input;
|
||||
} else {
|
||||
System.out.println("Channel ID can't be empty.");
|
||||
}
|
||||
|
||||
}
|
||||
String channelName = null;
|
||||
while (channelName == null) {
|
||||
System.out.print("Enter channel name: ");
|
||||
@@ -367,8 +415,8 @@ public class ActionHandler {
|
||||
response = incoming;
|
||||
break;
|
||||
} else {
|
||||
// این یک پیام Real-Time است
|
||||
handleRealTime(incoming); // ✅
|
||||
//real time
|
||||
handleRealTime(incoming);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1377,7 +1425,7 @@ public class ActionHandler {
|
||||
}
|
||||
|
||||
JSONObject selected = eligible.get(choice);
|
||||
String targetInternalUUID = selected.getString("internal_uuid"); // دقت کن internal_uuid
|
||||
String targetInternalUUID = selected.getString("internal_uuid");
|
||||
|
||||
JSONObject permissions = new JSONObject();
|
||||
System.out.print("Can add members? (true/false): ");
|
||||
@@ -1394,7 +1442,7 @@ public class ActionHandler {
|
||||
JSONObject promoteReq = new JSONObject();
|
||||
promoteReq.put("action", "add_admin_to_group");
|
||||
promoteReq.put("group_id", groupId.toString());
|
||||
promoteReq.put("user_id", targetInternalUUID); // ارسال internal_uuid واقعی
|
||||
promoteReq.put("user_id", targetInternalUUID);
|
||||
promoteReq.put("permissions", permissions);
|
||||
|
||||
JSONObject promoteRes = sendWithResponse(promoteReq);
|
||||
@@ -2111,26 +2159,7 @@ public class ActionHandler {
|
||||
|
||||
|
||||
|
||||
public void addAdminToEntity(String type, UUID entityId) {
|
||||
System.out.print("Enter user ID to promote to admin: ");
|
||||
String targetUserId = scanner.nextLine().trim();
|
||||
|
||||
JSONObject permissions = new JSONObject();
|
||||
System.out.print("Can send messages? (true/false): ");
|
||||
permissions.put("can_send", Boolean.parseBoolean(scanner.nextLine()));
|
||||
System.out.print("Can edit info? (true/false): ");
|
||||
permissions.put("can_edit", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
JSONObject req = new JSONObject();
|
||||
req.put("action", type.equals("group") ? "add_admin_to_group" : "add_admin_to_channel");
|
||||
req.put(type + "_id", entityId.toString());
|
||||
req.put("target_user_id", targetUserId);
|
||||
req.put("permissions", permissions);
|
||||
|
||||
send(req);
|
||||
JSONObject res = getResponse();
|
||||
System.out.println(res.getString("message"));
|
||||
}
|
||||
|
||||
|
||||
private void editAdminPermissions(UUID chatId, String chatType) {
|
||||
@@ -2390,12 +2419,12 @@ public class ActionHandler {
|
||||
ActionHandler.requestChatList();
|
||||
if (Session.inChatListMenu) {
|
||||
System.out.println("\n📬 Updated Chat List:");
|
||||
ActionHandler.displayChatList(); // این متدی باشه که چتها رو نمایش بده
|
||||
ActionHandler.displayChatList(); //Show chats
|
||||
System.out.print("Select a chat by number: ");
|
||||
}
|
||||
}
|
||||
|
||||
Thread.sleep(300); // جلوگیری از مصرف بیمورد CPU
|
||||
Thread.sleep(300);
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ ChatStateMonitor crashed: " + e.getMessage());
|
||||
}
|
||||
@@ -2420,7 +2449,7 @@ public class ActionHandler {
|
||||
if (Session.refreshCurrentChatMenu && Session.inChatMenu && Session.currentChatId != null) {
|
||||
System.out.println("🔁 [Refresher] Refresh requested. Searching for chat...");
|
||||
|
||||
// جستجو در لیست چتها با استفاده از currentChatId
|
||||
|
||||
ChatEntry chat = Session.chatList.stream()
|
||||
.filter(e -> e.getId().toString().equals(Session.currentChatId))
|
||||
.findFirst()
|
||||
@@ -2540,20 +2569,7 @@ public class ActionHandler {
|
||||
|
||||
|
||||
|
||||
// public static void requestPermissions(String chatId, String chatType) {
|
||||
// JSONObject permissionReq = new JSONObject();
|
||||
// permissionReq.put("action", "get_permissions");
|
||||
// permissionReq.put("receiver_id", chatId);
|
||||
// permissionReq.put("receiver_type", chatType);
|
||||
// TelegramClient.send(permissionReq);
|
||||
// }
|
||||
//
|
||||
// public void refreshCurrentChat() {
|
||||
// switch (Session.currentChatType.toLowerCase()) {
|
||||
// case "group" -> showGroupChatMenu(Session.currentChatEntry);
|
||||
// case "channel" -> showChannelChatMenu(Session.currentChatEntry);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
private void startMenuRefresherThread() {
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.to.telegramfinalproject.Database;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.to.telegramfinalproject.Models.Group;
|
||||
import org.to.telegramfinalproject.Models.User;
|
||||
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -629,4 +630,53 @@ public class GroupDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isMember(UUID groupId, UUID userId) {
|
||||
String sql = "SELECT 1 FROM group_members WHERE group_id = ? AND user_id = ?";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setObject(1, groupId);
|
||||
stmt.setObject(2, userId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// public static List<User> searchGroupMembers(UUID groupId, String keyword) {
|
||||
// List<User> result = new ArrayList<>();
|
||||
// String sql = """
|
||||
// SELECT u.*
|
||||
// FROM users u
|
||||
// JOIN group_members gm ON gm.user_id = u.internal_uuid
|
||||
// WHERE gm.group_id = ?
|
||||
// AND (
|
||||
// LOWER(u.profile_name) LIKE ?
|
||||
// OR LOWER(u.username) LIKE ?
|
||||
// OR LOWER(u.user_id) LIKE ?
|
||||
// )
|
||||
// """;
|
||||
//
|
||||
// try (Connection conn = ConnectionDb.connect();
|
||||
// PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
//
|
||||
// stmt.setObject(1, groupId);
|
||||
// String likePattern = "%" + keyword.toLowerCase() + "%";
|
||||
// stmt.setString(2, likePattern);
|
||||
// stmt.setString(3, likePattern);
|
||||
// stmt.setString(4, likePattern);
|
||||
//
|
||||
// ResultSet rs = stmt.executeQuery();
|
||||
// while (rs.next()) {
|
||||
// User user = User.fromResultSet(rs);
|
||||
// result.add(user);
|
||||
// }
|
||||
//
|
||||
// } catch (SQLException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -1672,6 +1672,41 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
|
||||
// case "search_chat_members": {
|
||||
// if (currentUser == null) {
|
||||
// response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
// UUID chatId = UUID.fromString(requestJson.getString("chat_id"));
|
||||
// String chatType = requestJson.getString("chat_type");
|
||||
// String query = requestJson.getString("query").toLowerCase();
|
||||
//
|
||||
// List<User> matched = new ArrayList<>();
|
||||
//
|
||||
// if (chatType.equals("group")) {
|
||||
// if (!GroupDatabase.isMember(chatId, currentUser.getInternal_uuid())) {
|
||||
// response = new ResponseModel("error", "You are not a member of this group.");
|
||||
// break;
|
||||
// }
|
||||
// matched = GroupDatabase.searchGroupMembers(chatId, query);
|
||||
// } else if (chatType.equals("channel")) {
|
||||
// if (!ChannelDatabase.isAdmin(chatId, currentUser.getInternal_uuid())||!ChannelDatabase.isOwner(chatId, currentUser.getInternal_uuid())) {
|
||||
// response = new ResponseModel("error", "Only admins or owner can search subscribers.");
|
||||
// break;
|
||||
// }
|
||||
// matched = ChannelDatabase.searchSubscribers(chatId, query);
|
||||
// } else {
|
||||
// response = new ResponseModel("error", "Invalid chat type.");
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
// JSONArray arr = new JSONArray();
|
||||
// for (User u : matched) arr.put(u.toJSON());
|
||||
// response = new ResponseModel("success", "Results found", Map.of("results", arr));
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user