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());
} else {
leaveChat(chat.getId(), "group");
refreshChatList();
// refreshChatList();
}
return false;
}
@@ -1395,7 +1395,7 @@ public class ActionHandler {
}
JSONObject selected = admins.getJSONObject(choice);
String newOwnerId = selected.getString("internal_id"); // ✅ اصلاح شده
String newOwnerId = selected.getString("internal_id");
JSONObject promoteReq = new JSONObject();
promoteReq.put("action", "transfer_channel_ownership");
@@ -23,6 +23,8 @@ public class IncomingMessageListener implements Runnable {
String line;
while ((line = in.readLine()) != null) {
JSONObject response = new JSONObject(line);
System.out.println("📥 Received raw line: " + line);
@@ -77,7 +79,7 @@ public class IncomingMessageListener implements Runnable {
"update_group_or_channel", "chat_deleted",
"blocked_by_user", "unblocked_by_user", "message_seen",
"removed_from_group", "removed_from_channel",
"became_admin", "removed_admin" -> true;
"became_admin", "removed_admin", "ownership_transferred" -> true;
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.");
String chatId = msg.getString("chat_id");
String chatType = msg.getString("chat_type");
ActionHandler.requestChatInfo(chatId, chatType);
new Thread(() -> {
try {
handleAdminRoleChanged(msg);
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
case "became_admin", "removed_admin", "ownership_transferred" -> {
@@ -190,7 +196,7 @@ public class IncomingMessageListener implements Runnable {
entry.ifPresent(chat -> chat.setPermissions(perm));
// 3. set currentChatId
Session.currentChatId = chatUUID.toString(); // ❗ باید قبل از بررسی شرط‌ها باشه
Session.currentChatId = chatUUID.toString();
System.out.println("🧪 Checking refresh conditions...");
System.out.println("🔹 inChatMenu: " + Session.inChatMenu);
@@ -1143,16 +1143,30 @@ public class ClientHandler implements Runnable {
//RealTime
if (success) {
List<UUID> members = GroupDatabase.getMemberUUIDs(groupId);
Group group = GroupDatabase.findByInternalUUID(groupId);
// 1. ارسال ownership_transferred فقط به owner جدید
RealTimeEventDispatcher.sendOwnershipTransferred("group", groupId, groupId.toString(), List.of(newOwner.getInternal_uuid()));
//update for new owner
RealTimeEventDispatcher.sendOwnershipTransferred(
"group",
groupId,
group.getGroup_name(),
List.of(newOwner.getInternal_uuid())
);
// 2. ارسال update_group_or_channel برای همه اعضا
RealTimeEventDispatcher.sendGroupOrChannelUpdate("group", groupId, groupId.toString(), "", "", members);
//Update for everyone
RealTimeEventDispatcher.sendGroupOrChannelUpdate(
"group",
groupId,
group.getGroup_name(),
group.getImage_url(),
group.getDescription(),
members
);
}
response = success
? new ResponseModel("success", "Ownership transferred.")
: new ResponseModel("error", "Failed to transfer ownership.");
@@ -1564,15 +1578,29 @@ public class ClientHandler implements Runnable {
//RealTime
if (success) {
List<UUID> subscribers = ChannelDatabase.getChannelSubscriberUUIDs(channelId);
Channel channel = ChannelDatabase.findByInternalUUID(channelId); // 🟢 گرفتن اطلاعات کامل
// 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 تمام افراد
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
? new ResponseModel("success", "Ownership transferred successfully.")