RealTime updated(add amin/remove admin)

This commit is contained in:
2025-07-21 13:44:22 +03:30
parent b387137a00
commit 541f800877
6 changed files with 414 additions and 24 deletions
@@ -21,13 +21,19 @@ public class ActionHandler {
private final BufferedReader in; private final BufferedReader in;
private final Scanner scanner; private final Scanner scanner;
public static volatile boolean forceExitChat = false; public static volatile boolean forceExitChat = false;
public static ActionHandler instance;
private void handleRealTime(JSONObject json) throws IOException {
IncomingMessageListener listener = new IncomingMessageListener(this.in);
listener.handleRealTimeEvent (json);
}
public ActionHandler(PrintWriter out, BufferedReader in, Scanner scanner) { public ActionHandler(PrintWriter out, BufferedReader in, Scanner scanner) {
this.out = out; this.out = out;
this.in = in; this.in = in;
this.scanner = scanner; this.scanner = scanner;
ActionHandler.instance = this;
} }
public void loginHandler() { public void loginHandler() {
@@ -225,9 +231,10 @@ public class ActionHandler {
try { try {
JSONObject response = TelegramClient.responseQueue.take(); JSONObject response = TelegramClient.responseQueue.take();
if (response != null) { if (response != null && response.getString("status").equals("success")) {
if (response.getString("status").equals("success")) { if (response.has("data") && !response.isNull("data")) {
JSONArray chatListJson = response.getJSONObject("data").getJSONArray("chat_list"); JSONObject data = response.getJSONObject("data");
JSONArray chatListJson = data.getJSONArray("chat_list");
List<ChatEntry> chatList = new ArrayList<>(); List<ChatEntry> chatList = new ArrayList<>();
for (Object obj : chatListJson) { for (Object obj : chatListJson) {
@@ -250,7 +257,13 @@ public class ActionHandler {
Session.chatList = chatList; Session.chatList = chatList;
System.out.println("✅ Chat list updated."); System.out.println("✅ Chat list updated.");
} else { } else {
System.out.println("⚠️ Response has no chat list data.");
}
} else {
if (response.has("message") && !response.isNull("message")) {
System.out.println("❌ Failed to refresh chat list: " + response.getString("message")); System.out.println("❌ Failed to refresh chat list: " + response.getString("message"));
} else {
System.out.println("❌ Failed to refresh chat list.");
} }
} }
} catch (Exception e) { } catch (Exception e) {
@@ -261,7 +274,6 @@ public class ActionHandler {
public void createGroup() { public void createGroup() {
System.out.print("Enter group ID: "); System.out.print("Enter group ID: ");
String groupId = scanner.nextLine(); String groupId = scanner.nextLine();
@@ -338,19 +350,35 @@ public class ActionHandler {
String action = request.getString("action"); String action = request.getString("action");
this.out.println(request.toString()); this.out.println(request.toString());
JSONObject response = TelegramClient.responseQueue.take(); JSONObject response = null;
// منتظر بمون تا پاسخ واقعی (یعنی status داشته باشه) بیاد
while (true) {
JSONObject incoming = TelegramClient.responseQueue.take();
if (incoming.has("status")) {
response = incoming;
break;
} else {
// این یک پیام Real-Time است
handleRealTime(incoming); // ✅
}
}
if (response == null) { if (response == null) {
System.out.println("⚠️ No response received."); System.out.println("⚠️ No usable response received.");
return; return;
} }
System.out.println("✅ Server Response: " + response.getString("message")); if (response.has("message") && !response.isNull("message")) {
System.out.println("✅ Server Response: " + response.getString("message"));
}
String status = response.getString("status"); String status = response.optString("status", "error");
if (!"success".equals(status) || !response.has("data") || response.isNull("data")) if (!"success".equals(status) || !response.has("data") || response.isNull("data"))
return; return;
switch (action) { switch (action) {
case "login": case "login":
case "register": case "register":
@@ -685,6 +713,14 @@ public class ActionHandler {
boolean stayInChat = true; boolean stayInChat = true;
Session.currentChatId = chat.getId().toString();
Session.currentChatEntry = chat;
Session.currentChatType = chat.getType().toLowerCase();
Session.inChatMenu = true;
startMenuRefresherThread();
while (stayInChat) { while (stayInChat) {
if (forceExitChat) { if (forceExitChat) {
System.out.println("⚠️ You have been removed from this chat or chat was deleted. Returning to chat list..."); System.out.println("⚠️ You have been removed from this chat or chat was deleted. Returning to chat list...");
@@ -702,6 +738,12 @@ public class ActionHandler {
} }
} }
} }
Session.inChatMenu = false;
Session.currentChatId = null;
Session.currentChatEntry = null;
Session.currentChatType = null;
} }
@@ -1522,7 +1564,7 @@ public class ActionHandler {
} }
JSONObject selected = eligible.get(choice); JSONObject selected = eligible.get(choice);
String targetInternalUUID = selected.getString("internal_uuid"); String targetInternalUUID = selected.getString("user_id");
JSONObject removeReq = new JSONObject(); JSONObject removeReq = new JSONObject();
removeReq.put("action", "remove_admin_from_group"); removeReq.put("action", "remove_admin_from_group");
@@ -2036,7 +2078,7 @@ public class ActionHandler {
} }
private static JSONObject sendWithResponse(JSONObject request) { public static JSONObject sendWithResponse(JSONObject request) {
try { try {
if (!request.has("action") || request.isNull("action")) { if (!request.has("action") || request.isNull("action")) {
System.err.println("❌ Invalid request: missing action."); System.err.println("❌ Invalid request: missing action.");
@@ -2174,6 +2216,187 @@ public class ActionHandler {
} }
public static class CurrentChatMenuRefresher implements Runnable {
private final ActionHandler handler;
public CurrentChatMenuRefresher(ActionHandler handler) {
this.handler = handler;
}
@Override
public void run() {
System.out.println("🔁 Menu refresher thread running...");
while (true) {
try {
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()
.orElse(null);
if (chat != null) {
Session.currentChatEntry = chat;
Session.refreshCurrentChatMenu = false;
String type = chat.getType().toLowerCase();
System.out.println("\n🔁 Your permissions have changed. Menu updated:");
if (type.equals("group")) {
handler.printGroupMenuOnly(chat);
} else if (type.equals("channel")) {
handler.printChannelMenuOnly(chat);
}
System.out.print("Select an option: ");
} else {
System.out.println("⚠️ No matching chat found for ID: " + Session.currentChatId);
}
}
Thread.sleep(200);
} catch (Exception e) {
System.out.println("❌ CurrentChatMenuRefresher crashed: " + e.getMessage());
e.printStackTrace();
}
}
}
}
public void printGroupMenuOnly(ChatEntry chat) {
boolean isAdmin = chat.isAdmin();
boolean isOwner = chat.isOwner();
JSONObject perms = getGroupPermissions(chat.getId());
System.out.println("\n--- Group Chat Menu (Auto-refreshed) ---");
System.out.println("1. Send Message");
System.out.println("2. View Members");
if (isOwner || (isAdmin && perms.optBoolean("can_add_members", false))) {
System.out.println("3. Add Member");
}
if (isOwner || (isAdmin && perms.optBoolean("can_edit_group", false))) {
System.out.println("4. Edit Group Info");
}
if (isOwner || (isAdmin && perms.optBoolean("can_add_admins", false))) {
System.out.println("5. Add Admin");
}
if (isOwner || (isAdmin && perms.optBoolean("can_remove_admins", false))) {
System.out.println("6. Remove Admin");
}
if (isOwner || (isAdmin && perms.optBoolean("can_remove_members", false))) {
System.out.println("7. Remove member");
}
if (isOwner) {
System.out.println("8. Delete Group");
System.out.println("9. Leave Group (Transfer ownership required)");
} else {
System.out.println("9. Leave Group");
}
System.out.println("0. Back to Chat List");
}
public void printChannelMenuOnly(ChatEntry chat) {
boolean isAdmin = chat.isAdmin();
boolean isOwner = chat.isOwner();
JSONObject perms = getChannelPermissions(chat.getId());
System.out.println("\n--- Channel Menu (Auto-refreshed) ---");
if (isOwner || (isAdmin && perms.optBoolean("can_post", false))) {
System.out.println("1. Send Post");
}
if (isOwner || isAdmin) {
System.out.println("2. View Subscribers");
}
if (isOwner || (isAdmin && perms.optBoolean("can_add_members", false))) {
System.out.println("3. Add Subscriber");
}
if (isOwner || (isAdmin && perms.optBoolean("can_remove_members", false))) {
System.out.println("4. Remove Subscriber");
}
if (isOwner || (isAdmin && perms.optBoolean("can_edit_channel", false))) {
System.out.println("5. Edit Channel Info");
}
if (isOwner || (isAdmin && perms.optBoolean("can_add_admins", false))) {
System.out.println("6. Add Admin");
}
if (isOwner || (isAdmin && perms.optBoolean("can_remove_admins", false))) {
System.out.println("7. Remove Admin");
}
if (isOwner) {
System.out.println("8. Delete Channel");
System.out.println("9. Leave Channel (Transfer ownership required)");
} else {
System.out.println("8. Leave Channel");
}
System.out.println("0. Back to Chat List");
}
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() {
Thread refresher = new Thread(() -> {
System.out.println("🟡 Refresher tick. refreshCurrentChatMenu = " + Session.refreshCurrentChatMenu);
while (true) {
try {
Thread.sleep(500);
if (Session.refreshCurrentChatMenu) {
Session.refreshCurrentChatMenu = false;
if (Session.currentChatEntry == null) {
System.out.println("⚠️ currentChatEntry is null. Skipping...");
continue; // ❗ اینجا بجای return باید continue باشه
}
switch (Session.currentChatEntry.getType()) {
case "group" -> showGroupChatMenu(Session.currentChatEntry);
case "channel" -> showChannelChatMenu(Session.currentChatEntry);
}
}
} catch (Exception ignored) {}
}
});
refresher.setDaemon(true);
refresher.start();
}
} }
@@ -1,9 +1,12 @@
package org.to.telegramfinalproject.Client; package org.to.telegramfinalproject.Client;
import org.json.JSONObject; import org.json.JSONObject;
import org.to.telegramfinalproject.Models.ChatEntry;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
public class IncomingMessageListener implements Runnable { public class IncomingMessageListener implements Runnable {
@@ -26,13 +29,23 @@ public class IncomingMessageListener implements Runnable {
//if it has reqID answer //if it has reqID answer
if (response.has("request_id")) { if (response.has("request_id")) {
String requestId = response.getString("request_id"); String requestId = response.getString("request_id");
System.out.println("📬 Response with request_id: " + requestId);
System.out.println("📬 Full response: " + response.toString(2));
BlockingQueue<JSONObject> queue = TelegramClient.pendingResponses.get(requestId); BlockingQueue<JSONObject> queue = TelegramClient.pendingResponses.get(requestId);
if (queue != null) { if (queue != null) {
queue.put(response); // send it to the correct line queue.put(response);
continue; } else {
System.out.println("⚠️ No pending queue for request_id = " + requestId + ". Putting in responseQueue...");
TelegramClient.responseQueue.put(response);
} }
// ⚠️ بسیار مهم: دیگر بررسی action انجام نده
continue;
} }
//if it has action check it //if it has action check it
if (response.has("action")) { if (response.has("action")) {
String action = response.getString("action"); String action = response.getString("action");
@@ -70,10 +83,11 @@ public class IncomingMessageListener implements Runnable {
}; };
} }
private void handleRealTimeEvent(JSONObject response) throws IOException { void handleRealTimeEvent(JSONObject response) throws IOException {
String action = response.getString("action"); String action = response.getString("action");
JSONObject msg = response.getJSONObject("data"); JSONObject msg = response.getJSONObject("data");
switch (action) { switch (action) {
case "added_to_group", "added_to_channel", case "added_to_group", "added_to_channel",
"removed_from_group", "removed_from_channel", "chat_deleted" -> { "removed_from_group", "removed_from_channel", "chat_deleted" -> {
@@ -99,18 +113,136 @@ public class IncomingMessageListener implements Runnable {
} }
case "became_admin", "removed_admin" -> { case "became_admin", "removed_admin" -> {
System.out.println("\n🔄 Your admin status changed. Updating chat info..."); System.out.println("🧩 Detected admin role change. Calling handler...");
String chatId = msg.getString("chat_id"); new Thread(() -> {
String chatType = msg.getString("chat_type"); try {
ActionHandler.requestChatInfo(chatId, chatType); handleAdminRoleChanged(msg); // ✅ صدا زدن در ترد جداگانه
} catch (IOException e) {
e.printStackTrace();
}
}).start();
} }
// case "get_chat_info" -> {
// JSONObject chatData = msg.getJSONObject("data");
// UUID chatUUID = UUID.fromString(chatData.getString("internal_id"));
//
// Optional<ChatEntry> entry = Session.chatList.stream()
// .filter(e -> e.getId().equals(chatUUID))
// .findFirst();
//
// entry.ifPresent(chat -> {
// chat.setAdmin(chatData.optBoolean("is_admin", false));
// chat.setOwner(chatData.optBoolean("is_owner", false));
//
// if (Session.currentChatId != null && Session.currentChatId.equals(chat.getId())) {
// Session.currentChatEntry = chat;
// Session.refreshCurrentChatMenu = true;
//
// }
//
// System.out.println("✅ Chat info updated → admin: " + chat.isAdmin() + ", owner: " + chat.isOwner());
// });
// }
default -> displayRealTimeMessage(action, msg); default -> displayRealTimeMessage(action, msg);
} }
System.out.print(">> "); System.out.print(">> ");
} }
private void handleAdminRoleChanged(JSONObject data) throws IOException {
String chatType = data.getString("chat_type");
String chatId = data.optString("group_id", data.optString("channel_id", data.optString("chat_id", null)));
if (chatId == null) {
System.out.println("⚠️ No valid ID found in real-time data: " + data.toString(2));
return;
}
System.out.println("\n🔄 Your admin status changed. Updating chat info...");
try {
// 1. دریافت chat info
JSONObject chatInfoReq = new JSONObject();
chatInfoReq.put("action", "get_chat_info");
chatInfoReq.put("receiver_id", chatId);
chatInfoReq.put("receiver_type", chatType);
System.out.println("📤 Sending get_chat_info: " + chatInfoReq);
JSONObject chatInfoResp = ActionHandler.sendWithResponse(chatInfoReq);
JSONObject chatData = chatInfoResp.getJSONObject("data");
UUID chatUUID = UUID.fromString(chatData.getString("internal_id"));
Optional<ChatEntry> entry = Session.chatList.stream()
.filter(e -> e.getId().equals(chatUUID))
.findFirst();
if (entry.isEmpty()) {
System.out.println("❌ Chat not found in session.");
return;
}
entry.ifPresent(chat -> {
chat.setAdmin(chatData.optBoolean("is_admin", false));
chat.setOwner(chatData.optBoolean("is_owner", false));
chat.setName(chatData.optString("name", ""));
chat.setDisplayId(chatData.optString("id", ""));
chat.setImageUrl(chatData.optString("image_url", ""));
chat.setType(chatData.optString("type", ""));
Session.currentChatEntry = chat;
});
// 2. گرفتن permission
JSONObject permissionReq = new JSONObject();
if (chatType.equalsIgnoreCase("group")) {
permissionReq.put("action", "get_group_permissions");
permissionReq.put("group_id", chatId);
} else {
permissionReq.put("action", "get_channel_permissions");
permissionReq.put("channel_id", chatId);
}
JSONObject permissionResp = ActionHandler.sendWithResponse(permissionReq);
JSONObject perm = permissionResp.getJSONObject("data");
entry.ifPresent(chat -> chat.setPermissions(perm));
// 3. ست کردن currentChatId
Session.currentChatId = chatUUID.toString(); // ❗ باید قبل از بررسی شرط‌ها باشه
// 🔄 بررسی شرایط و آپدیت منو
System.out.println("🧪 Checking refresh conditions...");
System.out.println("🔹 inChatMenu: " + Session.inChatMenu);
System.out.println("🔹 currentChatId: " + Session.currentChatId);
System.out.println("🔹 chatUUID: " + chatUUID);
if (Session.inChatMenu && Session.currentChatId != null && Session.currentChatId.equals(chatUUID.toString())) {
synchronized (Session.class) {
Session.refreshCurrentChatMenu = true;
}
System.out.println("✅ Admin status updated. Refreshing menu...");
} else {
System.out.println("❌ Refresh conditions not met.");
}
} catch (Exception e) {
System.out.println("❌ Exception while handling admin role change: " + e.getMessage());
e.printStackTrace();
}
}
private void displayRealTimeMessage(String action, JSONObject msg) { private void displayRealTimeMessage(String action, JSONObject msg) {
switch (action) { switch (action) {
case "new_message" -> { case "new_message" -> {
@@ -16,6 +16,12 @@ public class Session {
public static volatile boolean forceRefreshChatList = false; public static volatile boolean forceRefreshChatList = false;
public static volatile boolean backToChatList = false; public static volatile boolean backToChatList = false;
public static boolean inChatListMenu = false; public static boolean inChatListMenu = false;
public static String currentChatType = null;
public static volatile boolean inChatMenu = false;
public static volatile boolean refreshCurrentChatMenu = false;
public static String currentChatId = null;
public static ChatEntry currentChatEntry = null;
@@ -73,6 +73,8 @@ public class TelegramClient {
if (Session.currentUser != null) { if (Session.currentUser != null) {
System.out.println("✅ Login successful."); System.out.println("✅ Login successful.");
new Thread(new ActionHandler.ChatStateMonitor(out)).start(); new Thread(new ActionHandler.ChatStateMonitor(out)).start();
// new Thread(new ActionHandler.CurrentChatMenuRefresher(this.handler)).start();
UUID internalId = UUID.fromString(Session.currentUser.getString("internal_uuid")); UUID internalId = UUID.fromString(Session.currentUser.getString("internal_uuid"));
loggedInUserId = internalId; loggedInUserId = internalId;
@@ -102,6 +104,9 @@ public class TelegramClient {
} }
} }
public static Socket getSocket() { public static Socket getSocket() {
return socket; return socket;
} }
@@ -37,6 +37,10 @@ public class ChatEntry {
} }
public ChatEntry() {
}
// 🟩 گتر و ستر جدید // 🟩 گتر و ستر جدید
public boolean isOwner() { public boolean isOwner() {
return isOwner; return isOwner;
@@ -54,7 +58,6 @@ public class ChatEntry {
isAdmin = admin; isAdmin = admin;
} }
// سایر گترها
public UUID getId() { public UUID getId() {
return internalId; return internalId;
} }
@@ -87,4 +90,22 @@ public class ChatEntry {
public void setPermissions(JSONObject permissions) { public void setPermissions(JSONObject permissions) {
this.permissions = permissions; this.permissions = permissions;
} }
public void setName(String name) {this.name = name;
}
public void setDisplayId(String id) {this.displayId = id;
}
public void setImageUrl(String image_url) {this.imageUrl = image_url;
}
public void setType(String type) {this.type =type;
}
public void setId(String internalId) {this.internalId = UUID.fromString(internalId);
}
} }
@@ -1533,6 +1533,7 @@ public class ClientHandler implements Runnable {
} }
case "transfer_channel_ownership": { case "transfer_channel_ownership": {
if (currentUser == null) { if (currentUser == null) {
response = new ResponseModel("error", "Unauthorized. Please login first."); response = new ResponseModel("error", "Unauthorized. Please login first.");
@@ -1573,21 +1574,23 @@ public class ClientHandler implements Runnable {
response = new ResponseModel("error", "Unknown action: " + action); response = new ResponseModel("error", "Unknown action: " + action);
} }
if (response == null) {
response = new ResponseModel("error", "No response generated for action: " + action);
}
JSONObject responseJson = new JSONObject(); JSONObject responseJson = new JSONObject();
responseJson.put("status", response.getStatus()); responseJson.put("status", response.getStatus());
responseJson.put("message", response.getMessage()); responseJson.put("message", response.getMessage());
responseJson.put("data", response.getData() != null ? response.getData() : JSONObject.NULL); responseJson.put("data", response.getData() != null ? response.getData() : JSONObject.NULL);
if (requestJson.has("request_id")) {
response.setRequestId(requestJson.getString("request_id"));
}
if (requestJson.has("request_id")) { if (requestJson.has("request_id")) {
String requestId = requestJson.getString("request_id"); String requestId = requestJson.getString("request_id");
response.setRequestId(requestId); response.setRequestId(requestId);
responseJson.put("request_id", requestId); responseJson.put("request_id", requestId); // ✅ بدون این، کلاینت قفل میشه
} }
out.println(responseJson.toString());
out.println(responseJson.toString());
System.out.println("📤 Sent response: " + responseJson.toString(2));
} }
} catch (IOException e) { } catch (IOException e) {