fixed real time for transfer the ownership and update group/channel info

This commit is contained in:
2025-07-22 17:00:36 +03:30
parent 84890539da
commit 42c92c5a2d
3 changed files with 48 additions and 14 deletions
@@ -873,7 +873,7 @@ public class ActionHandler {
transferOwnershipAndLeave(chat.getId()); transferOwnershipAndLeave(chat.getId());
} else { } else {
leaveChat(chat.getId(), "group"); leaveChat(chat.getId(), "group");
refreshChatList(); // refreshChatList();
} }
return false; return false;
} }
@@ -1395,7 +1395,7 @@ public class ActionHandler {
} }
JSONObject selected = admins.getJSONObject(choice); JSONObject selected = admins.getJSONObject(choice);
String newOwnerId = selected.getString("internal_id"); // ✅ اصلاح شده String newOwnerId = selected.getString("internal_id");
JSONObject promoteReq = new JSONObject(); JSONObject promoteReq = new JSONObject();
promoteReq.put("action", "transfer_channel_ownership"); promoteReq.put("action", "transfer_channel_ownership");
@@ -23,6 +23,8 @@ public class IncomingMessageListener implements Runnable {
String line; String line;
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
JSONObject response = new JSONObject(line); JSONObject response = new JSONObject(line);
System.out.println("📥 Received raw line: " + line); System.out.println("📥 Received raw line: " + line);
@@ -77,7 +79,7 @@ public class IncomingMessageListener implements Runnable {
"update_group_or_channel", "chat_deleted", "update_group_or_channel", "chat_deleted",
"blocked_by_user", "unblocked_by_user", "message_seen", "blocked_by_user", "unblocked_by_user", "message_seen",
"removed_from_group", "removed_from_channel", "removed_from_group", "removed_from_channel",
"became_admin", "removed_admin" -> true; "became_admin", "removed_admin", "ownership_transferred" -> true;
default -> false; default -> false;
}; };
} }
@@ -104,11 +106,15 @@ public class IncomingMessageListener implements Runnable {
} }
} }
case "update_group_or_channel" -> { case "chat_updated" -> {
System.out.println("\n🔄 Group/Channel info updated."); System.out.println("\n🔄 Group/Channel info updated.");
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 "became_admin", "removed_admin", "ownership_transferred" -> { case "became_admin", "removed_admin", "ownership_transferred" -> {
@@ -190,7 +196,7 @@ public class IncomingMessageListener implements Runnable {
entry.ifPresent(chat -> chat.setPermissions(perm)); entry.ifPresent(chat -> chat.setPermissions(perm));
// 3. set 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);
@@ -1143,16 +1143,30 @@ public class ClientHandler implements Runnable {
//RealTime //RealTime
if (success) { if (success) {
List<UUID> members = GroupDatabase.getMemberUUIDs(groupId); List<UUID> members = GroupDatabase.getMemberUUIDs(groupId);
Group group = GroupDatabase.findByInternalUUID(groupId);
// 1. ارسال ownership_transferred فقط به owner جدید //update for new owner
RealTimeEventDispatcher.sendOwnershipTransferred("group", groupId, groupId.toString(), List.of(newOwner.getInternal_uuid())); RealTimeEventDispatcher.sendOwnershipTransferred(
"group",
groupId,
group.getGroup_name(),
List.of(newOwner.getInternal_uuid())
);
// 2. ارسال update_group_or_channel برای همه اعضا //Update for everyone
RealTimeEventDispatcher.sendGroupOrChannelUpdate("group", groupId, groupId.toString(), "", "", members); RealTimeEventDispatcher.sendGroupOrChannelUpdate(
"group",
groupId,
group.getGroup_name(),
group.getImage_url(),
group.getDescription(),
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.");
@@ -1564,15 +1578,29 @@ public class ClientHandler implements Runnable {
//RealTime //RealTime
if (success) { if (success) {
List<UUID> subscribers = ChannelDatabase.getChannelSubscriberUUIDs(channelId); List<UUID> subscribers = ChannelDatabase.getChannelSubscriberUUIDs(channelId);
Channel channel = ChannelDatabase.findByInternalUUID(channelId); // 🟢 گرفتن اطلاعات کامل
// 1. ارسال ownership_transferred فقط به owner جدید // 1. ارسال ownership_transferred فقط به owner جدید
RealTimeEventDispatcher.sendOwnershipTransferred("channel", channelId, channelId.toString(), List.of(newOwner.getInternal_uuid())); RealTimeEventDispatcher.sendOwnershipTransferred(
"channel",
channelId,
channel.getChannel_name(), // اسم واقعی کانال
List.of(newOwner.getInternal_uuid())
);
// 2. ارسال update_group_or_channel برای بروزرسانی UI تمام افراد // 2. ارسال update_group_or_channel برای بروزرسانی UI تمام افراد
RealTimeEventDispatcher.sendGroupOrChannelUpdate("channel", channelId, channelId.toString(), "", "", subscribers); RealTimeEventDispatcher.sendGroupOrChannelUpdate(
"channel",
channelId,
channel.getChannel_name(), // name واقعی
channel.getImage_url(), // image
channel.getDescription(), // description
subscribers
);
} }
response = success response = success
? new ResponseModel("success", "Ownership transferred successfully.") ? new ResponseModel("success", "Ownership transferred successfully.")