RealTime notify updated
This commit is contained in:
@@ -6,16 +6,19 @@ import org.to.telegramfinalproject.Models.ChatEntry;
|
||||
import org.to.telegramfinalproject.Models.SearchRequestModel;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
import static org.to.telegramfinalproject.Database.ChannelDatabase.addSubscriberToChannel;
|
||||
|
||||
public class ActionHandler {
|
||||
private final PrintWriter out;
|
||||
private final BufferedReader in;
|
||||
private final Scanner scanner;
|
||||
public static volatile boolean forceExitChat = false;
|
||||
|
||||
|
||||
|
||||
public ActionHandler(PrintWriter out, BufferedReader in, Scanner scanner) {
|
||||
this.out = out;
|
||||
@@ -353,6 +356,7 @@ public class ActionHandler {
|
||||
|
||||
|
||||
chatList.add(entry);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -625,11 +629,36 @@ public class ActionHandler {
|
||||
req.put("action", "get_messages");
|
||||
req.put("receiver_id", chat.getId());
|
||||
req.put("receiver_type", chat.getType());
|
||||
send(req);
|
||||
|
||||
JSONObject res = sendWithResponse(req);
|
||||
if (res == null || !res.getString("status").equals("success")) {
|
||||
System.out.println("❌ Failed to fetch messages.");
|
||||
return;
|
||||
}
|
||||
|
||||
JSONArray messages = res.getJSONObject("data").getJSONArray("messages");
|
||||
System.out.println("\n🔓 Messages fetched:");
|
||||
System.out.println("─────────────────────────────────────────────");
|
||||
for (int i = 0; i < messages.length(); i++) {
|
||||
JSONObject m = messages.getJSONObject(i);
|
||||
String senderId = m.getString("sender_id");
|
||||
String content = m.getString("content");
|
||||
String time = m.getString("send_at");
|
||||
|
||||
String label = senderId.equals(Session.currentUser.getString("internal_uuid")) ? "You" : "Other";
|
||||
System.out.println("[" + time + "] " + label + ": " + content);
|
||||
}
|
||||
System.out.println("─────────────────────────────────────────────");
|
||||
|
||||
boolean stayInChat = true;
|
||||
|
||||
while (stayInChat) {
|
||||
if (forceExitChat) {
|
||||
System.out.println("⚠️ You have been removed from this chat or chat was deleted. Returning to chat list...");
|
||||
forceExitChat = false;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (chat.getType().trim().toLowerCase()) {
|
||||
case "private" -> stayInChat = showPrivateChatMenu(chat);
|
||||
case "group" -> stayInChat = showGroupChatMenu(chat);
|
||||
@@ -1752,6 +1781,7 @@ public class ActionHandler {
|
||||
private JSONObject getResponse() {
|
||||
try {
|
||||
return TelegramClient.responseQueue.take();
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException("Failed to get server response");
|
||||
}
|
||||
@@ -1774,54 +1804,54 @@ public class ActionHandler {
|
||||
|
||||
|
||||
|
||||
public void processIncomingEvents() {
|
||||
try {
|
||||
while (in.ready()) {
|
||||
String line = in.readLine();
|
||||
if (line == null) continue;
|
||||
|
||||
JSONObject response = new JSONObject(line);
|
||||
if (!response.has("action")) continue;
|
||||
|
||||
String action = response.getString("action");
|
||||
|
||||
switch (action) {
|
||||
case "new_message" -> {
|
||||
JSONObject msg = response.getJSONObject("data");
|
||||
System.out.println("\n🔔 New Message:");
|
||||
System.out.println("From: " + msg.getString("sender"));
|
||||
System.out.println("Time: " + msg.getString("time"));
|
||||
System.out.println("Content: " + msg.getString("content"));
|
||||
System.out.print(">> ");
|
||||
}
|
||||
|
||||
case "user_status_changed" -> {
|
||||
JSONObject msg = response.getJSONObject("data");
|
||||
System.out.println("\n🔄 User Status Changed:");
|
||||
System.out.println("User: " + msg.getString("user_id"));
|
||||
System.out.println("Status: " + msg.getString("status"));
|
||||
System.out.print(">> ");
|
||||
}
|
||||
|
||||
case "update_group_or_channel" -> {
|
||||
JSONObject data = response.getJSONObject("data");
|
||||
System.out.println("\n📢 " + data.getString("chat_type") + " updated: " + data.getString("new_name"));
|
||||
System.out.print(">> ");
|
||||
}
|
||||
|
||||
|
||||
default -> {
|
||||
if (!action.equals("search")) {
|
||||
System.out.println("\n❓ Unknown action received: " + action);
|
||||
System.out.print(">> ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("🔴 Failed to process event: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
// public void processIncomingEvents() {
|
||||
// try {
|
||||
// while (in.ready()) {
|
||||
// String line = in.readLine();
|
||||
// if (line == null) continue;
|
||||
//
|
||||
// JSONObject response = new JSONObject(line);
|
||||
// if (!response.has("action")) continue;
|
||||
//
|
||||
// String action = response.getString("action");
|
||||
//
|
||||
// switch (action) {
|
||||
// case "new_message" -> {
|
||||
// JSONObject msg = response.getJSONObject("data");
|
||||
// System.out.println("\n🔔 New Message:");
|
||||
// System.out.println("From: " + msg.getString("sender"));
|
||||
// System.out.println("Time: " + msg.getString("time"));
|
||||
// System.out.println("Content: " + msg.getString("content"));
|
||||
// System.out.print(">> ");
|
||||
// }
|
||||
//
|
||||
// case "user_status_changed" -> {
|
||||
// JSONObject msg = response.getJSONObject("data");
|
||||
// System.out.println("\n🔄 User Status Changed:");
|
||||
// System.out.println("User: " + msg.getString("user_id"));
|
||||
// System.out.println("Status: " + msg.getString("status"));
|
||||
// System.out.print(">> ");
|
||||
// }
|
||||
//
|
||||
// case "update_group_or_channel" -> {
|
||||
// JSONObject data = response.getJSONObject("data");
|
||||
// System.out.println("\n📢 " + data.getString("chat_type") + " updated: " + data.getString("new_name"));
|
||||
// System.out.print(">> ");
|
||||
// }
|
||||
//
|
||||
//
|
||||
// default -> {
|
||||
// if (!action.equals("search")) {
|
||||
// System.out.println("\n❓ Unknown action received: " + action);
|
||||
// System.out.print(">> ");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// System.out.println("🔴 Failed to process event: " + e.getMessage());
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
public void addAdminToEntity(String type, UUID entityId) {
|
||||
@@ -1987,5 +2017,55 @@ public class ActionHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public static void requestChatList() throws IOException {
|
||||
System.out.println("🟢 [requestChatList] Sending chat list request...");
|
||||
|
||||
JSONObject req = new JSONObject();
|
||||
req.put("action", "get_chat_list");
|
||||
req.put("user_id", TelegramClient.loggedInUserId.toString());
|
||||
|
||||
System.out.println("📤 [SEND] " + req.toString(2));
|
||||
|
||||
TelegramClient.send(req);
|
||||
}
|
||||
|
||||
public static void requestChatInfo(String chatId, String chatType) throws IOException {
|
||||
JSONObject req = new JSONObject();
|
||||
req.put("action", "get_chat_info");
|
||||
req.put("receiver_id", chatId);
|
||||
req.put("receiver_type", chatType);
|
||||
TelegramClient.send(req);
|
||||
}
|
||||
|
||||
|
||||
// public static void handleChatListResponse(JSONObject response) {
|
||||
// if (response.getString("status").equals("success")) {
|
||||
// JSONArray chats = response.getJSONArray("data");
|
||||
//
|
||||
// TelegramClient.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);
|
||||
// TelegramClient.chatList.add(chat);
|
||||
// }
|
||||
//
|
||||
// System.out.println("\n✅ Updated Chat List:");
|
||||
// displayChatList();
|
||||
// } else {
|
||||
// System.out.println("⚠️ Failed to fetch chat list: " + response.getString("message"));
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ public class EventProcessorThread extends Thread {
|
||||
setDaemon(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
handler.processIncomingEvents();
|
||||
} catch (InterruptedException ignored) {}
|
||||
}
|
||||
}
|
||||
// @Override
|
||||
// public void run() {
|
||||
// while (true) {
|
||||
// try {
|
||||
// Thread.sleep(2000);
|
||||
// handler.processIncomingEvents();
|
||||
// } catch (InterruptedException ignored) {}
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.to.telegramfinalproject.Client;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class IncomingMessageListener implements Runnable {
|
||||
private final BufferedReader in;
|
||||
@@ -22,7 +23,11 @@ public class IncomingMessageListener implements Runnable {
|
||||
System.out.println("📥 Received raw line: " + line);
|
||||
|
||||
if (response.has("action")) {
|
||||
System.out.println("🎯 [Listener] Action received: " + response.toString(2));
|
||||
|
||||
String action = response.getString("action");
|
||||
System.out.println("🎯 Received action: " + action);
|
||||
|
||||
if (isRealTimeEvent(action)) {
|
||||
handleRealTimeEvent(response);
|
||||
} else {
|
||||
@@ -36,7 +41,8 @@ public class IncomingMessageListener implements Runnable {
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("🔴 Listener stopped: " + e.getMessage());
|
||||
System.out.println("🔴 [Listener] Crashed due to: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,10 +58,44 @@ public class IncomingMessageListener implements Runnable {
|
||||
};
|
||||
}
|
||||
|
||||
private void handleRealTimeEvent(JSONObject response) {
|
||||
private void handleRealTimeEvent(JSONObject response) throws IOException {
|
||||
String action = response.getString("action");
|
||||
JSONObject msg = response.getJSONObject("data");
|
||||
|
||||
switch (action) {
|
||||
case "added_to_group", "added_to_channel",
|
||||
"removed_from_group", "removed_from_channel", "chat_deleted" -> {
|
||||
System.out.println("\n🔄 Chat list changed. Updating...");
|
||||
ActionHandler.requestChatList();
|
||||
|
||||
if (action.equals("removed_from_group") || action.equals("removed_from_channel") || action.equals("chat_deleted")) {
|
||||
System.out.println("🚫 You were removed from the chat or chat was deleted. Exiting...");
|
||||
ActionHandler.forceExitChat = true;
|
||||
}
|
||||
}
|
||||
|
||||
case "update_group_or_channel" -> {
|
||||
System.out.println("\n🔄 Group/Channel info updated.");
|
||||
String chatId = msg.getString("chat_id");
|
||||
String chatType = msg.getString("chat_type");
|
||||
ActionHandler.requestChatInfo(chatId, chatType);
|
||||
}
|
||||
|
||||
case "became_admin", "removed_admin" -> {
|
||||
System.out.println("\n🔄 Your admin status changed. Updating chat info...");
|
||||
String chatId = msg.getString("chat_id");
|
||||
String chatType = msg.getString("chat_type");
|
||||
ActionHandler.requestChatInfo(chatId, chatType);
|
||||
}
|
||||
|
||||
default -> displayRealTimeMessage(action, msg);
|
||||
}
|
||||
|
||||
System.out.print(">> ");
|
||||
}
|
||||
|
||||
|
||||
private void displayRealTimeMessage(String action, JSONObject msg) {
|
||||
switch (action) {
|
||||
case "new_message" -> {
|
||||
System.out.println("\n🔔 New Message:");
|
||||
@@ -63,76 +103,36 @@ public class IncomingMessageListener implements Runnable {
|
||||
System.out.println("Time: " + msg.getString("time"));
|
||||
System.out.println("Content: " + msg.getString("content"));
|
||||
}
|
||||
|
||||
case "message_edited" -> {
|
||||
System.out.println("\n✏️ Message Edited:");
|
||||
System.out.println("ID: " + msg.getString("message_id"));
|
||||
System.out.println("New Content: " + msg.getString("new_content"));
|
||||
System.out.println("Edit Time: " + msg.getString("edited_at"));
|
||||
}
|
||||
|
||||
case "message_deleted" -> {
|
||||
System.out.println("\n🗑️ Message Deleted:");
|
||||
System.out.println("Message ID: " + msg.getString("message_id"));
|
||||
}
|
||||
|
||||
case "user_status_changed" -> {
|
||||
System.out.println("\n🔄 User Status Changed:");
|
||||
System.out.println("User: " + msg.getString("user_id"));
|
||||
System.out.println("Status: " + msg.getString("status"));
|
||||
}
|
||||
|
||||
case "added_to_group" -> {
|
||||
System.out.println("\n👥 You were added to a group: " + msg.getString("chat_name"));
|
||||
}
|
||||
|
||||
case "added_to_channel" -> {
|
||||
System.out.println("\n📢 You were added to a channel: " + msg.getString("chat_name"));
|
||||
}
|
||||
|
||||
case "update_group_or_channel" -> {
|
||||
System.out.println("\n🔄 Group/Channel updated: " + msg.getString("new_name"));
|
||||
}
|
||||
|
||||
case "chat_deleted" -> {
|
||||
System.out.println("\n🗑️ Chat deleted: " + msg.getString("chat_id"));
|
||||
}
|
||||
|
||||
case "blocked_by_user" -> {
|
||||
System.out.println("\n⛔ You were blocked by user: " + msg.getString("blocker_id"));
|
||||
}
|
||||
|
||||
case "unblocked_by_user" -> {
|
||||
System.out.println("\n✅ You were unblocked by user: " + msg.getString("unblocker_id"));
|
||||
}
|
||||
|
||||
case "message_seen" -> {
|
||||
System.out.println("\n👁️ Your message was seen:");
|
||||
System.out.println("Message ID: " + msg.getString("message_id"));
|
||||
System.out.println("Seen at: " + msg.getString("seen_at"));
|
||||
}
|
||||
|
||||
case "removed_from_group" -> {
|
||||
System.out.println("\n🚫 You were removed from group: " + msg.getString("chat_id"));
|
||||
}
|
||||
|
||||
case "removed_from_channel" -> {
|
||||
System.out.println("\n🚫 You were removed from channel: " + msg.getString("chat_id"));
|
||||
}
|
||||
|
||||
case "became_admin" -> {
|
||||
System.out.println("\n⭐ You are now an admin in: " + msg.getString("chat_name"));
|
||||
}
|
||||
|
||||
case "removed_admin" -> {
|
||||
System.out.println("\n⚠️ You are no longer an admin in: " + msg.getString("chat_name"));
|
||||
}
|
||||
|
||||
default -> {
|
||||
System.out.println("\n❓ Unknown real-time action: " + action);
|
||||
System.out.println(msg.toString(2));
|
||||
}
|
||||
}
|
||||
|
||||
System.out.print(">> ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,24 +15,32 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
public class TelegramClient {
|
||||
private static final String SERVER_HOST = "localhost";
|
||||
private static final int SERVER_PORT = 8000;
|
||||
private Socket socket;
|
||||
private static Socket socket;
|
||||
private BufferedReader in;
|
||||
private PrintWriter out;
|
||||
private final Scanner scanner;
|
||||
ActionHandler handler = null;
|
||||
private ActionHandler handler;
|
||||
public static BlockingQueue<JSONObject> responseQueue = new LinkedBlockingQueue<>();
|
||||
public static UUID loggedInUserId = null;
|
||||
|
||||
private static TelegramClient instance;
|
||||
|
||||
public TelegramClient() {
|
||||
this.scanner = new Scanner(System.in);
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static TelegramClient getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
this.socket = new Socket(SERVER_HOST, SERVER_PORT);
|
||||
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
||||
this.out = new PrintWriter(this.socket.getOutputStream(), true);
|
||||
socket = new Socket(SERVER_HOST, SERVER_PORT);
|
||||
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
out = new PrintWriter(socket.getOutputStream(), true);
|
||||
System.out.println("✅ Connected to Telegram Server");
|
||||
this.handler = new ActionHandler(this.out, this.in, this.scanner);
|
||||
handler = new ActionHandler(out, in, scanner);
|
||||
|
||||
Thread listenerThread = new Thread(new IncomingMessageListener(in));
|
||||
listenerThread.setDaemon(true);
|
||||
@@ -60,7 +68,10 @@ public class TelegramClient {
|
||||
handler.loginHandler();
|
||||
if (Session.currentUser != null) {
|
||||
System.out.println("✅ Login successful.");
|
||||
|
||||
UUID internalId = UUID.fromString(Session.currentUser.getString("internal_uuid"));
|
||||
loggedInUserId = internalId;
|
||||
|
||||
handler.userMenu(internalId);
|
||||
} else {
|
||||
System.out.println("❌ Login failed.");
|
||||
@@ -75,6 +86,21 @@ public class TelegramClient {
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(JSONObject req) {
|
||||
try {
|
||||
responseQueue.clear(); // optional: clear old responses
|
||||
getInstance().out.println(req.toString());
|
||||
System.out.println("📤 [SEND] " + req.toString(2));
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ Error sending request: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new TelegramClient().start();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user