fixed real time bugs

This commit is contained in:
2025-07-21 18:59:24 +03:30
parent 541f800877
commit 84890539da
4 changed files with 45 additions and 66 deletions
@@ -231,9 +231,17 @@ public class ActionHandler {
try { try {
JSONObject response = TelegramClient.responseQueue.take(); JSONObject response = TelegramClient.responseQueue.take();
if (response != null && response.getString("status").equals("success")) { if (response != null && response.getString("status").equals("success")) {
if (response.has("data") && !response.isNull("data")) { if (response.has("data") && !response.isNull("data")) {
JSONObject data = response.getJSONObject("data"); JSONObject data = response.getJSONObject("data");
// ❗ چک کن که chat_list وجود داره
if (!data.has("chat_list") || data.isNull("chat_list")) {
System.out.println("❌ chat_list not found in response data.");
return;
}
JSONArray chatListJson = data.getJSONArray("chat_list"); JSONArray chatListJson = data.getJSONArray("chat_list");
List<ChatEntry> chatList = new ArrayList<>(); List<ChatEntry> chatList = new ArrayList<>();
@@ -255,9 +263,9 @@ public class ActionHandler {
} }
Session.chatList = chatList; Session.chatList = chatList;
System.out.println("✅ Chat list updated."); System.out.println("✅ Chat list updated. Total: " + chatList.size());
} else { } else {
System.out.println("⚠️ Response has no chat list data."); System.out.println("⚠️ Response has no data object.");
} }
} else { } else {
if (response.has("message") && !response.isNull("message")) { if (response.has("message") && !response.isNull("message")) {
@@ -273,7 +281,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();
@@ -622,11 +629,6 @@ public class ActionHandler {
return; return;
} }
// if (Session.forceRefreshChatList) {
// System.out.println("🔁 Refresh triggered by real-time event.");
// requestChatList(); // با sendWithResponse جواب می‌گیری
// Session.forceRefreshChatList = false;
// }
@@ -869,7 +871,6 @@ public class ActionHandler {
case "9" -> { case "9" -> {
if (isOwner) { if (isOwner) {
transferOwnershipAndLeave(chat.getId()); transferOwnershipAndLeave(chat.getId());
refreshChatList();
} else { } else {
leaveChat(chat.getId(), "group"); leaveChat(chat.getId(), "group");
refreshChatList(); refreshChatList();
@@ -1003,7 +1004,6 @@ public class ActionHandler {
case "9" -> { case "9" -> {
if (isOwner) { if (isOwner) {
transferChannelOwnershipAndLeave(chat.getId()); transferChannelOwnershipAndLeave(chat.getId());
refreshChatList();
return false; return false;
} else { } else {
System.out.println("❌ You don't have permission."); System.out.println("❌ You don't have permission.");
@@ -2139,32 +2139,7 @@ public class ActionHandler {
} }
// public static void handleChatListResponse(JSONObject response) {
// if (response.getString("status").equals("success")) {
// JSONArray chats = response.getJSONArray("data");
//
// Session.chatList.clear();
//
// for (int i = 0; i < chats.length(); i++) {
// JSONObject chatJson = chats.getJSONObject(i);
//
// UUID internalId = UUID.fromString(chatJson.getString("internal_id"));
// String displayId = chatJson.getString("id");
// String name = chatJson.getString("name");
// String imageUrl = chatJson.optString("image_url", "");
// String type = chatJson.getString("type");
// LocalDateTime lastMessageTime = LocalDateTime.parse(chatJson.getString("last_message_time"));
//
// ChatEntry chat = new ChatEntry(internalId, displayId, name, imageUrl, type, lastMessageTime);
// Session.chatList.add(chat);
// }
//
// System.out.println("\n✅ Updated Chat List:");
// displayChatList();
// } else {
// System.out.println("⚠️ Failed to fetch chat list: " + response.getString("message"));
// }
// }
public static void displayChatList() { public static void displayChatList() {
if (Session.chatList == null || Session.chatList.isEmpty()) { if (Session.chatList == null || Session.chatList.isEmpty()) {
@@ -40,7 +40,6 @@ public class IncomingMessageListener implements Runnable {
TelegramClient.responseQueue.put(response); TelegramClient.responseQueue.put(response);
} }
// ⚠️ بسیار مهم: دیگر بررسی action انجام نده
continue; continue;
} }
@@ -112,11 +111,11 @@ public class IncomingMessageListener implements Runnable {
ActionHandler.requestChatInfo(chatId, chatType); ActionHandler.requestChatInfo(chatId, chatType);
} }
case "became_admin", "removed_admin" -> { case "became_admin", "removed_admin", "ownership_transferred" -> {
System.out.println("🧩 Detected admin role change. Calling handler..."); System.out.println("🧩 Detected admin/owner role change. Calling handler...");
new Thread(() -> { new Thread(() -> {
try { try {
handleAdminRoleChanged(msg); // ✅ صدا زدن در ترد جداگانه handleAdminRoleChanged(msg); //new thread
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -124,27 +123,7 @@ public class IncomingMessageListener implements Runnable {
} }
// 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());
// });
// }
@@ -166,7 +145,7 @@ public class IncomingMessageListener implements Runnable {
System.out.println("\n🔄 Your admin status changed. Updating chat info..."); System.out.println("\n🔄 Your admin status changed. Updating chat info...");
try { try {
// 1. دریافت chat info // 1. get chat info
JSONObject chatInfoReq = new JSONObject(); JSONObject chatInfoReq = new JSONObject();
chatInfoReq.put("action", "get_chat_info"); chatInfoReq.put("action", "get_chat_info");
chatInfoReq.put("receiver_id", chatId); chatInfoReq.put("receiver_id", chatId);
@@ -196,7 +175,7 @@ public class IncomingMessageListener implements Runnable {
Session.currentChatEntry = chat; Session.currentChatEntry = chat;
}); });
// 2. گرفتن permission // 2. get permission
JSONObject permissionReq = new JSONObject(); JSONObject permissionReq = new JSONObject();
if (chatType.equalsIgnoreCase("group")) { if (chatType.equalsIgnoreCase("group")) {
permissionReq.put("action", "get_group_permissions"); permissionReq.put("action", "get_group_permissions");
@@ -210,10 +189,9 @@ public class IncomingMessageListener implements Runnable {
JSONObject perm = permissionResp.getJSONObject("data"); JSONObject perm = permissionResp.getJSONObject("data");
entry.ifPresent(chat -> chat.setPermissions(perm)); entry.ifPresent(chat -> chat.setPermissions(perm));
// 3. ست کردن currentChatId // 3. set currentChatId
Session.currentChatId = chatUUID.toString(); // ❗ باید قبل از بررسی شرط‌ها باشه Session.currentChatId = chatUUID.toString(); // ❗ باید قبل از بررسی شرط‌ها باشه
// 🔄 بررسی شرایط و آپدیت منو
System.out.println("🧪 Checking refresh conditions..."); System.out.println("🧪 Checking refresh conditions...");
System.out.println("🔹 inChatMenu: " + Session.inChatMenu); System.out.println("🔹 inChatMenu: " + Session.inChatMenu);
System.out.println("🔹 currentChatId: " + Session.currentChatId); System.out.println("🔹 currentChatId: " + Session.currentChatId);
@@ -1143,10 +1143,16 @@ public class ClientHandler implements Runnable {
//RealTime //RealTime
if (success) { if (success) {
List<UUID> members = GroupDatabase.getMemberUUIDs(groupId); List<UUID> members = GroupDatabase.getMemberUUIDs(groupId);
// 1. ارسال ownership_transferred فقط به owner جدید
RealTimeEventDispatcher.sendOwnershipTransferred("group", groupId, groupId.toString(), List.of(newOwner.getInternal_uuid()));
// 2. ارسال update_group_or_channel برای همه اعضا
RealTimeEventDispatcher.sendGroupOrChannelUpdate("group", groupId, groupId.toString(), "", "", members); RealTimeEventDispatcher.sendGroupOrChannelUpdate("group", groupId, groupId.toString(), "", "", members);
} }
response = success response = success
? new ResponseModel("success", "Ownership transferred.") ? new ResponseModel("success", "Ownership transferred.")
: new ResponseModel("error", "Failed to transfer ownership."); : new ResponseModel("error", "Failed to transfer ownership.");
@@ -1558,9 +1564,15 @@ public class ClientHandler implements Runnable {
//RealTime //RealTime
if (success) { if (success) {
List<UUID> subscribers = ChannelDatabase.getChannelSubscriberUUIDs(channelId); List<UUID> subscribers = ChannelDatabase.getChannelSubscriberUUIDs(channelId);
// 1. ارسال ownership_transferred فقط به owner جدید
RealTimeEventDispatcher.sendOwnershipTransferred("channel", channelId, channelId.toString(), List.of(newOwner.getInternal_uuid()));
// 2. ارسال update_group_or_channel برای بروزرسانی UI تمام افراد
RealTimeEventDispatcher.sendGroupOrChannelUpdate("channel", channelId, channelId.toString(), "", "", subscribers); RealTimeEventDispatcher.sendGroupOrChannelUpdate("channel", channelId, channelId.toString(), "", "", subscribers);
} }
response = success response = success
? new ResponseModel("success", "Ownership transferred successfully.") ? new ResponseModel("success", "Ownership transferred successfully.")
@@ -1586,7 +1598,7 @@ public class ClientHandler implements Runnable {
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());
@@ -264,7 +264,7 @@ public class RealTimeEventDispatcher {
public static void notifyBecameAdmin(String type, UUID chatId, String chatName, String imageUrl, UUID userId) { public static void notifyBecameAdmin(String type, UUID chatId, String chatName, String imageUrl, UUID userId) {
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
data.put("chat_type", type); // group یا channel data.put("chat_type", type); // group or channel
data.put("chat_id", chatId.toString()); data.put("chat_id", chatId.toString());
data.put("chat_name", chatName); data.put("chat_name", chatName);
data.put("image_url", imageUrl); data.put("image_url", imageUrl);
@@ -291,4 +291,18 @@ public class RealTimeEventDispatcher {
sendToUser(userId, event); sendToUser(userId, event);
} }
public static void sendOwnershipTransferred(String type, UUID chatId, String chatName, List<UUID> affectedUsers) {
JSONObject data = new JSONObject();
data.put("chat_type", type); // "group" or "channel"
data.put("chat_id", chatId.toString());
data.put("chat_name", chatName);
JSONObject event = new JSONObject();
event.put("action", "ownership_transferred");
event.put("data", data);
broadcastToUsers(affectedUsers, event);
}
} }