Fixed join channel and group bugs

This commit is contained in:
2025-06-12 20:53:44 +03:30
parent 2b972c4b7c
commit 5afe3662d4
5 changed files with 94 additions and 14 deletions
@@ -85,15 +85,16 @@ public class ActionHandler {
} }
private void joinGroupOrChannel(String type, String id) { private void joinGroupOrChannel(String type, String uuid) {
JSONObject req = new JSONObject(); JSONObject req = new JSONObject();
req.put("action", "join_" + type); req.put("action", "join_" + type);
req.put("user_id", Session.currentUser.getString("user_id")); req.put("user_id", Session.currentUser.getString("internalUUID")); // ✅ internal_uuid لازم است
req.put("id", id); req.put("id", uuid); // ✅ این هم internal_uuid است
send(req); send(req);
} }
private ChatEntry fetchChatInfo(String receiverId, String receiverType) { private ChatEntry fetchChatInfo(String receiverId, String receiverType) {
JSONObject req = new JSONObject(); JSONObject req = new JSONObject();
req.put("action", "get_chat_info"); req.put("action", "get_chat_info");
@@ -312,12 +313,13 @@ public class ActionHandler {
} else { } else {
joinGroupOrChannel(type, uuid); joinGroupOrChannel(type, uuid);
ChatEntry newChat = fetchChatInfo(uuid, type); ChatEntry newChat = fetchChatInfo(id, type);
refreshChatList(); refreshChatList();
openChat(newChat); openChat(newChat);
} }
} }
case "message" -> { case "message" -> {
String receiverId = selected.getString("receiver_id"); String receiverId = selected.getString("receiver_id");
String receiverType = selected.getString("receiver_type"); String receiverType = selected.getString("receiver_type");
@@ -252,4 +252,31 @@ public class ChannelDatabase {
} }
} }
public static Channel findByInternalUUID(UUID internalUUID) {
String sql = "SELECT * FROM channels WHERE internal_uuid = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, internalUUID);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
Channel channel = new Channel();
channel.setInternal_uuid(UUID.fromString(rs.getString("internal_uuid")));
channel.setChannel_id(rs.getString("channel_id"));
channel.setChannel_name(rs.getString("channel_name"));
channel.setImage_url(rs.getString("image_url"));
channel.setCreator_id(UUID.fromString(rs.getString("creator_id")));
channel.setDescription(rs.getString("description"));
channel.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
return channel;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
} }
@@ -256,4 +256,32 @@ public class GroupDatabase {
} }
} }
public static Group findByInternalUUID(UUID internalUUID) {
String sql = "SELECT * FROM groups WHERE internal_uuid = ?";
try (Connection conn = ConnectionDb.connect();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, internalUUID);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
Group group = new Group();
group.setInternal_uuid(UUID.fromString(rs.getString("internal_uuid")));
group.setGroup_id(rs.getString("group_id"));
group.setGroup_name(rs.getString("group_name"));
group.setImage_url(rs.getString("image_url"));
group.setCreator_id(UUID.fromString(rs.getString("creator_id")));
group.setDescription(rs.getString("description"));
group.setCreated_at(rs.getTimestamp("created_at").toLocalDateTime());
return group;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
} }
@@ -109,9 +109,13 @@ public class JsonUtil {
public static JSONArray groupMemberListToJson(List<GroupMember> members) { public static JSONArray groupMemberListToJson(List<GroupMember> members) {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
if (members == null) {
return array;
}
for (GroupMember m : members) { for (GroupMember m : members) {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("group_id", m.getGroup_id().toString()); obj.put("group_id", m.getGroup_id().toString());
@@ -120,22 +124,31 @@ public class JsonUtil {
obj.put("role", m.getRole()); obj.put("role", m.getRole());
array.put(obj); array.put(obj);
} }
return array; return array;
} }
public static JSONArray channelSubscribeToJson(List<ChannelSubscribe> subscribes){
public static JSONArray channelSubscribeToJson(List<ChannelSubscribe> subscribes) {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
for(ChannelSubscribe s :subscribes ){
if (subscribes == null) {
return array;
}
for (ChannelSubscribe s : subscribes) {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("channel_id",s.getChannel_id().toString()); obj.put("channel_id", s.getChannel_id().toString());
obj.put("user_id", s.getUser_id().toString()); obj.put("user_id", s.getUser_id().toString());
obj.put("Subscribed_at", s.getJoin_at().toString()); obj.put("Subscribed_at", s.getJoin_at().toString());
array.put(obj);
} }
return array; return array;
} }
public static JSONArray chatListToJson(List<ChatEntry> chatList) { public static JSONArray chatListToJson(List<ChatEntry> chatList) {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
@@ -228,10 +228,15 @@ public class ClientHandler implements Runnable {
} }
case "join_group": { case "join_group": {
UUID userUUID = new userDatabase().findByUserId(requestJson.getString("user_id")).getInternal_uuid(); UUID userUUID = UUID.fromString(requestJson.getString("user_id")); // مستقیم internalUUID دریافت می‌کنیم
UUID groupUUID = GroupDatabase.findInternalUUIDByGroupId(requestJson.getString("id")); Group group = GroupDatabase.findByInternalUUID(UUID.fromString(requestJson.getString("id")));
if (group == null) {
response = new ResponseModel("error", "Group not found.");
break;
}
boolean joined = GroupDatabase.addMemberToGroup(userUUID, groupUUID);
boolean joined = GroupDatabase.addMemberToGroup(userUUID, group.getInternal_uuid());
response = joined response = joined
? new ResponseModel("success", "Joined group.") ? new ResponseModel("success", "Joined group.")
: new ResponseModel("error", "Failed to join group."); : new ResponseModel("error", "Failed to join group.");
@@ -239,10 +244,15 @@ public class ClientHandler implements Runnable {
} }
case "join_channel": { case "join_channel": {
UUID userUUID = new userDatabase().findByUserId(requestJson.getString("user_id")).getInternal_uuid(); UUID userUUID = UUID.fromString(requestJson.getString("user_id")); // مستقیم internalUUID دریافت می‌کنیم
UUID channelUUID = ChannelDatabase.findInternalUUIDByChannelId(requestJson.getString("id")); Channel channel = ChannelDatabase.findByInternalUUID(UUID.fromString(requestJson.getString("id")));
if (channel == null) {
response = new ResponseModel("error", "Channel not found.");
break;
}
boolean joined = ChannelDatabase.addSubscriberToChannel(userUUID, channelUUID);
boolean joined = ChannelDatabase.addSubscriberToChannel(userUUID, channel.getInternal_uuid());
response = joined response = joined
? new ResponseModel("success", "Joined channel.") ? new ResponseModel("success", "Joined channel.")
: new ResponseModel("error", "Failed to join channel."); : new ResponseModel("error", "Failed to join channel.");