block/unblock fixed
This commit is contained in:
@@ -1140,6 +1140,21 @@ public class ActionHandler {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
JSONObject reqTarget = new JSONObject();
|
||||||
|
reqTarget.put("action", "get_private_chat_target");
|
||||||
|
reqTarget.put("chat_id", chat.getId());
|
||||||
|
|
||||||
|
JSONObject resTarget = sendWithResponse(reqTarget);
|
||||||
|
if (resTarget == null || !resTarget.getString("status").equals("success")) {
|
||||||
|
System.out.println("❌ Failed to fetch target user for private chat.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String otherUserId = resTarget.getJSONObject("data").getString("target_id");
|
||||||
|
chat.setOtherUserId(UUID.fromString(otherUserId));
|
||||||
|
|
||||||
|
|
||||||
JSONObject req = new JSONObject();
|
JSONObject req = new JSONObject();
|
||||||
req.put("action", "view_profile");
|
req.put("action", "view_profile");
|
||||||
req.put("target_id", chat.getOtherUserId()); // internal UUID
|
req.put("target_id", chat.getOtherUserId()); // internal UUID
|
||||||
@@ -1173,7 +1188,7 @@ public class ActionHandler {
|
|||||||
String input = scanner.nextLine();
|
String input = scanner.nextLine();
|
||||||
switch (input) {
|
switch (input) {
|
||||||
case "1" -> sendMessage(chat.getId(), "private");
|
case "1" -> sendMessage(chat.getId(), "private");
|
||||||
case "2" -> toggleBlock(chat.getId());
|
case "2" -> toggleBlock(chat.getOtherUserId());
|
||||||
case "3" -> {
|
case "3" -> {
|
||||||
deleteChat(chat.getId(), false);
|
deleteChat(chat.getId(), false);
|
||||||
return false;
|
return false;
|
||||||
@@ -2013,6 +2028,8 @@ public class ActionHandler {
|
|||||||
|
|
||||||
|
|
||||||
private void toggleBlock(UUID userId) {
|
private void toggleBlock(UUID userId) {
|
||||||
|
|
||||||
|
|
||||||
JSONObject req = new JSONObject();
|
JSONObject req = new JSONObject();
|
||||||
req.put("action", "toggle_block");
|
req.put("action", "toggle_block");
|
||||||
req.put("user_id", Session.getUserUUID());
|
req.put("user_id", Session.getUserUUID());
|
||||||
@@ -3142,7 +3159,7 @@ public class ActionHandler {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void sendMessage(UUID receiverId, String receiverType) {
|
public void sendMessage(UUID chatId, String receiverType) {
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
System.out.print("Enter your message: ");
|
System.out.print("Enter your message: ");
|
||||||
@@ -3163,11 +3180,11 @@ public class ActionHandler {
|
|||||||
System.out.print("File URL: ");
|
System.out.print("File URL: ");
|
||||||
String fileUrl = scanner.nextLine();
|
String fileUrl = scanner.nextLine();
|
||||||
System.out.print("File Type (IMAGE / VIDEO / FILE): ");
|
System.out.print("File Type (IMAGE / VIDEO / FILE): ");
|
||||||
String fileType = scanner.nextLine();
|
String fileType = scanner.nextLine().toUpperCase();
|
||||||
|
|
||||||
JSONObject fileJson = new JSONObject();
|
JSONObject fileJson = new JSONObject();
|
||||||
fileJson.put("file_url", fileUrl);
|
fileJson.put("file_url", fileUrl);
|
||||||
fileJson.put("file_type", fileType.toUpperCase());
|
fileJson.put("file_type", fileType);
|
||||||
attachmentsArray.put(fileJson);
|
attachmentsArray.put(fileJson);
|
||||||
|
|
||||||
System.out.print("Add another file? (yes/no): ");
|
System.out.print("Add another file? (yes/no): ");
|
||||||
@@ -3175,27 +3192,11 @@ public class ActionHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------ GET receiver_id if private ------------------------
|
// 🔹 فقط ارسال پیام با chat_id و receiver_type
|
||||||
UUID actualReceiverId = receiverId;
|
|
||||||
if (receiverType.equals("private")) {
|
|
||||||
JSONObject req = new JSONObject();
|
|
||||||
req.put("action", "get_or_create_private_chat");
|
|
||||||
req.put("user1", Session.currentPrivateChatUserId.toString());
|
|
||||||
req.put("user2", receiverId.toString());
|
|
||||||
|
|
||||||
JSONObject chatResponse = sendWithResponse(req);
|
|
||||||
if (chatResponse == null || !chatResponse.getString("status").equals("success")) {
|
|
||||||
System.out.println("❌ Failed to fetch private chat ID.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
actualReceiverId = UUID.fromString(chatResponse.getJSONObject("data").getString("chat_id"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------ SEND MESSAGE ------------------------
|
|
||||||
JSONObject messageJson = new JSONObject();
|
JSONObject messageJson = new JSONObject();
|
||||||
messageJson.put("action", "send_message");
|
messageJson.put("action", "send_message");
|
||||||
messageJson.put("receiver_type", receiverType);
|
messageJson.put("receiver_type", receiverType);
|
||||||
messageJson.put("receiver_id", actualReceiverId.toString());
|
messageJson.put("receiver_id", chatId.toString());
|
||||||
messageJson.put("content", content);
|
messageJson.put("content", content);
|
||||||
messageJson.put("message_type", messageType);
|
messageJson.put("message_type", messageType);
|
||||||
|
|
||||||
@@ -3216,6 +3217,7 @@ public class ActionHandler {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -258,25 +258,18 @@ public class MessageDatabase {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Message> privateChatHistory(UUID user1, UUID user2) {
|
public static List<Message> privateChatHistory(UUID chatId) {
|
||||||
List<Message> result = new ArrayList<>();
|
List<Message> result = new ArrayList<>();
|
||||||
String sql = """
|
String sql = """
|
||||||
SELECT * FROM messages
|
SELECT * FROM messages
|
||||||
WHERE receiver_type = 'private'
|
WHERE receiver_type = 'private'
|
||||||
AND (
|
AND receiver_id = ?
|
||||||
(sender_id = ? AND receiver_id = ?)
|
|
||||||
OR (sender_id = ? AND receiver_id = ?)
|
|
||||||
)
|
|
||||||
ORDER BY send_at
|
ORDER BY send_at
|
||||||
""";
|
""";
|
||||||
|
|
||||||
try (Connection conn = ConnectionDb.connect();
|
try (Connection conn = ConnectionDb.connect();
|
||||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||||
stmt.setObject(1, user1);
|
stmt.setObject(1, chatId);
|
||||||
stmt.setObject(2, user2);
|
|
||||||
stmt.setObject(3, user2);
|
|
||||||
stmt.setObject(4, user1);
|
|
||||||
|
|
||||||
ResultSet rs = stmt.executeQuery();
|
ResultSet rs = stmt.executeQuery();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
result.add(extractMessage(rs));
|
result.add(extractMessage(rs));
|
||||||
@@ -289,6 +282,7 @@ public class MessageDatabase {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static List<Message> groupChatHistory(UUID groupId) {
|
public static List<Message> groupChatHistory(UUID groupId) {
|
||||||
List<Message> result = new ArrayList<>();
|
List<Message> result = new ArrayList<>();
|
||||||
String sql = """
|
String sql = """
|
||||||
|
|||||||
@@ -164,13 +164,12 @@ public class ClientHandler implements Runnable {
|
|||||||
User otherUser = userDatabase.findByInternalUUID(otherId);
|
User otherUser = userDatabase.findByInternalUUID(otherId);
|
||||||
if (otherUser == null) continue;
|
if (otherUser == null) continue;
|
||||||
|
|
||||||
LocalDateTime lastMessageTime = MessageDatabase.getLastMessageTimeBetween(
|
LocalDateTime lastMessageTime = MessageDatabase.getLastMessageTime(chat.getChat_id(), "private");
|
||||||
currentUser.getInternal_uuid(), otherId, "private"
|
|
||||||
);
|
|
||||||
|
|
||||||
ChatEntry entry = new ChatEntry(
|
ChatEntry entry = new ChatEntry(
|
||||||
chat.getChat_id(), // 🔹 real private chat_id
|
chat.getChat_id(),
|
||||||
otherUser.getUser_id(), // نمایش عمومی طرف مقابل
|
otherUser.getUser_id(),
|
||||||
otherUser.getProfile_name(),
|
otherUser.getProfile_name(),
|
||||||
otherUser.getImage_url(),
|
otherUser.getImage_url(),
|
||||||
"private",
|
"private",
|
||||||
@@ -690,13 +689,12 @@ public class ClientHandler implements Runnable {
|
|||||||
User otherUser = userDatabase.findByInternalUUID(otherId);
|
User otherUser = userDatabase.findByInternalUUID(otherId);
|
||||||
if (otherUser == null) continue;
|
if (otherUser == null) continue;
|
||||||
|
|
||||||
LocalDateTime lastMessageTime = MessageDatabase.getLastMessageTimeBetween(
|
LocalDateTime lastMessageTime = MessageDatabase.getLastMessageTime(chat.getChat_id(), "private");
|
||||||
currentUser.getInternal_uuid(), otherId, "private"
|
|
||||||
);
|
|
||||||
|
|
||||||
ChatEntry entry = new ChatEntry(
|
ChatEntry entry = new ChatEntry(
|
||||||
chat.getChat_id(), // 🔹 real private chat_id
|
chat.getChat_id(),
|
||||||
otherUser.getUser_id(), // نمایش عمومی طرف مقابل
|
otherUser.getUser_id(),
|
||||||
otherUser.getProfile_name(),
|
otherUser.getProfile_name(),
|
||||||
otherUser.getImage_url(),
|
otherUser.getImage_url(),
|
||||||
"private",
|
"private",
|
||||||
@@ -1260,16 +1258,17 @@ public class ClientHandler implements Runnable {
|
|||||||
|
|
||||||
switch (receiverType) {
|
switch (receiverType) {
|
||||||
case "private" -> {
|
case "private" -> {
|
||||||
List<UUID> members = PrivateChatDatabase.getMembers(UUID.fromString(receiverId));
|
UUID chatId = UUID.fromString(receiverId);
|
||||||
if (members.size() != 2) {
|
List<UUID> members = PrivateChatDatabase.getMembers(chatId);
|
||||||
response = new ResponseModel("error", "Invalid private chat.");
|
if (!members.contains(currentUser.getInternal_uuid())) {
|
||||||
|
response = new ResponseModel("error", "You're not a member of this private chat.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
UUID otherUserId = members.get(0).equals(currentUser.getInternal_uuid()) ? members.get(1) : members.get(0);
|
messages = MessageDatabase.privateChatHistory(chatId);
|
||||||
messages = MessageDatabase.privateChatHistory(currentUser.getInternal_uuid(), otherUserId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
case "group" -> {
|
case "group" -> {
|
||||||
Group group = GroupDatabase.findByInternalUUID(UUID.fromString(receiverId));
|
Group group = GroupDatabase.findByInternalUUID(UUID.fromString(receiverId));
|
||||||
if (group == null) {
|
if (group == null) {
|
||||||
@@ -2083,6 +2082,8 @@ public class ClientHandler implements Runnable {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private ResponseModel handleSendMessage(JSONObject json) {
|
private ResponseModel handleSendMessage(JSONObject json) {
|
||||||
try {
|
try {
|
||||||
if (currentUser == null)
|
if (currentUser == null)
|
||||||
@@ -2093,12 +2094,8 @@ public class ClientHandler implements Runnable {
|
|||||||
String receiverType = json.getString("receiver_type");
|
String receiverType = json.getString("receiver_type");
|
||||||
UUID receiverId;
|
UUID receiverId;
|
||||||
|
|
||||||
if (receiverType.equals("private")) {
|
receiverId = UUID.fromString(json.getString("receiver_id"));
|
||||||
UUID receiverUserId = UUID.fromString(json.getString("receiver_user_id"));
|
|
||||||
receiverId = PrivateChatDatabase.getOrCreateChat(senderId, receiverUserId);
|
|
||||||
} else {
|
|
||||||
receiverId = UUID.fromString(json.getString("receiver_id")); // group یا channel
|
|
||||||
}
|
|
||||||
String content = json.optString("content", "");
|
String content = json.optString("content", "");
|
||||||
String messageType = json.optString("message_type", "TEXT");
|
String messageType = json.optString("message_type", "TEXT");
|
||||||
|
|
||||||
@@ -2123,15 +2120,13 @@ public class ClientHandler implements Runnable {
|
|||||||
return new ResponseModel("error", "Message inserted but failed to attach files.");
|
return new ResponseModel("error", "Message inserted but failed to attach files.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send real-time message
|
||||||
|
|
||||||
Message msg = new Message(messageId, senderId, receiverId, receiverType, content, messageType, LocalDateTime.now());
|
Message msg = new Message(messageId, senderId, receiverId, receiverType, content, messageType, LocalDateTime.now());
|
||||||
List<UUID> receivers = getReceiversForChat(receiverId, receiverType);
|
List<UUID> receivers = getReceiversForChat(receiverId, receiverType);
|
||||||
receivers.remove(senderId);
|
receivers.remove(senderId);
|
||||||
|
|
||||||
RealTimeEventDispatcher.sendNewMessage(msg, receivers);
|
RealTimeEventDispatcher.sendNewMessage(msg, receivers);
|
||||||
|
|
||||||
|
// Update chat list (last_message_time)
|
||||||
JSONObject chatUpdate = new JSONObject();
|
JSONObject chatUpdate = new JSONObject();
|
||||||
chatUpdate.put("chat_id", receiverId.toString());
|
chatUpdate.put("chat_id", receiverId.toString());
|
||||||
chatUpdate.put("chat_type", receiverType);
|
chatUpdate.put("chat_type", receiverType);
|
||||||
@@ -2145,7 +2140,6 @@ public class ClientHandler implements Runnable {
|
|||||||
RealTimeEventDispatcher.sendToUser(receiver, chatPayload);
|
RealTimeEventDispatcher.sendToUser(receiver, chatPayload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
JSONObject data = new JSONObject();
|
JSONObject data = new JSONObject();
|
||||||
data.put("message_id", messageId.toString());
|
data.put("message_id", messageId.toString());
|
||||||
return new ResponseModel("success", "Message sent successfully.", data);
|
return new ResponseModel("success", "Message sent successfully.", data);
|
||||||
@@ -2156,6 +2150,7 @@ public class ClientHandler implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private List<UUID> getReceiversForChat(UUID receiverId, String receiverType) {
|
private List<UUID> getReceiversForChat(UUID receiverId, String receiverType) {
|
||||||
switch (receiverType) {
|
switch (receiverType) {
|
||||||
case "private":
|
case "private":
|
||||||
|
|||||||
Reference in New Issue
Block a user