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();
|
||||
}
|
||||
|
||||
@@ -327,6 +327,11 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
|
||||
case "add_contact": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
|
||||
UUID userUUID = new userDatabase().findByUserId(requestJson.getString("user_id")).getInternal_uuid();
|
||||
UUID contactUUID = UUID.fromString(requestJson.getString("contact_id"));
|
||||
|
||||
@@ -350,6 +355,10 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
|
||||
case "join_group": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID userUUID = UUID.fromString(requestJson.getString("user_id"));
|
||||
Group group = GroupDatabase.findByInternalUUID(UUID.fromString(requestJson.getString("id")));
|
||||
if (group == null) {
|
||||
@@ -373,6 +382,10 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
|
||||
case "join_channel": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID userUUID = UUID.fromString(requestJson.getString("user_id"));
|
||||
Channel channel = ChannelDatabase.findByInternalUUID(UUID.fromString(requestJson.getString("id")));
|
||||
if (channel == null) {
|
||||
@@ -489,22 +502,26 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "get_chat_list": {
|
||||
String userIdStr = requestJson.getString("user_id");
|
||||
User user = new userDatabase().findByUserId(userIdStr);
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
|
||||
List<Contact> contacts = ContactDatabase.getContacts(user.getInternal_uuid());
|
||||
List<Group> groups = GroupDatabase.getGroupsByUser(user.getInternal_uuid());
|
||||
List<Channel> channels = ChannelDatabase.getChannelsByUser(user.getInternal_uuid());
|
||||
List<Contact> contacts = ContactDatabase.getContacts(currentUser.getInternal_uuid());
|
||||
List<Group> groups = GroupDatabase.getGroupsByUser(currentUser.getInternal_uuid());
|
||||
List<Channel> channels = ChannelDatabase.getChannelsByUser(currentUser.getInternal_uuid());
|
||||
|
||||
List<ChatEntry> chatList = new ArrayList<>();
|
||||
|
||||
for (Contact contact : contacts) {
|
||||
User target = userDatabase.findByInternalUUID(contact.getContact_id());
|
||||
if (target == null) continue;
|
||||
LocalDateTime last = MessageDatabase.getLastMessageTimeBetween(user.getInternal_uuid(), target.getInternal_uuid(), "private");
|
||||
|
||||
LocalDateTime last = MessageDatabase.getLastMessageTimeBetween(currentUser.getInternal_uuid(), target.getInternal_uuid(), "private");
|
||||
|
||||
chatList.add(new ChatEntry(
|
||||
target.getInternal_uuid(), // internal UUID
|
||||
target.getUser_id(), // public display ID
|
||||
target.getInternal_uuid(),
|
||||
target.getUser_id(),
|
||||
target.getProfile_name(),
|
||||
target.getImage_url(),
|
||||
"private",
|
||||
@@ -516,8 +533,8 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
for (Group group : groups) {
|
||||
LocalDateTime last = MessageDatabase.getLastMessageTime(group.getInternal_uuid(), "group");
|
||||
boolean isOwner = GroupDatabase.isOwner(group.getInternal_uuid(), user.getInternal_uuid());
|
||||
boolean isAdmin = GroupDatabase.isAdmin(group.getInternal_uuid(), user.getInternal_uuid());
|
||||
boolean isOwner = GroupDatabase.isOwner(group.getInternal_uuid(), currentUser.getInternal_uuid());
|
||||
boolean isAdmin = GroupDatabase.isAdmin(group.getInternal_uuid(), currentUser.getInternal_uuid());
|
||||
|
||||
chatList.add(new ChatEntry(
|
||||
group.getInternal_uuid(),
|
||||
@@ -533,8 +550,8 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
for (Channel channel : channels) {
|
||||
LocalDateTime last = MessageDatabase.getLastMessageTime(channel.getInternal_uuid(), "channel");
|
||||
boolean isOwner = ChannelDatabase.isOwner(channel.getInternal_uuid(), user.getInternal_uuid());
|
||||
boolean isAdmin = ChannelDatabase.isAdmin(channel.getInternal_uuid(), user.getInternal_uuid());
|
||||
boolean isOwner = ChannelDatabase.isOwner(channel.getInternal_uuid(), currentUser.getInternal_uuid());
|
||||
boolean isAdmin = ChannelDatabase.isAdmin(channel.getInternal_uuid(), currentUser.getInternal_uuid());
|
||||
|
||||
chatList.add(new ChatEntry(
|
||||
channel.getInternal_uuid(),
|
||||
@@ -548,7 +565,6 @@ public class ClientHandler implements Runnable {
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
chatList.sort((a, b) -> {
|
||||
if (a.getLastMessageTime() == null) return 1;
|
||||
if (b.getLastMessageTime() == null) return -1;
|
||||
@@ -557,14 +573,19 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("chat_list", JsonUtil.chatListToJson(chatList));
|
||||
|
||||
response = new ResponseModel("success", "Chat list updated.", data);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
case "create_group": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
try {
|
||||
String groupId = requestJson.getString("group_id");
|
||||
String groupName = requestJson.getString("group_name");
|
||||
@@ -600,6 +621,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "create_channel": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
try {
|
||||
String channelId = requestJson.getString("channel_id");
|
||||
String channelName = requestJson.getString("channel_name");
|
||||
@@ -635,6 +660,10 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
|
||||
case "add_admin_to_channel": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
try {
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
UUID targetUserId = UUID.fromString(requestJson.getString("target_user_id"));
|
||||
@@ -681,6 +710,10 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
|
||||
case "edit_channel_admin_permissions": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
UUID targetUserId = UUID.fromString(requestJson.getString("target_user_id"));
|
||||
JSONObject permissions = requestJson.optJSONObject("permissions");
|
||||
@@ -700,6 +733,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "edit_group_info": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
try {
|
||||
UUID groupUUID = UUID.fromString(requestJson.getString("group_id")); // internal_uuid
|
||||
String newGroupId = requestJson.getString("new_group_id").trim(); // شناسه نمایشی جدید
|
||||
@@ -740,6 +777,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "view_channel_admins": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
|
||||
|
||||
@@ -754,6 +795,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "add_admin_to_group": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
UUID targetUserId = UUID.fromString(requestJson.getString("user_id"));
|
||||
JSONObject permissions = requestJson.optJSONObject("permissions");
|
||||
@@ -787,6 +832,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "edit_group_admin_permissions": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
UUID targetUserId = UUID.fromString(requestJson.getString("target_user_id"));
|
||||
JSONObject permissions = requestJson.optJSONObject("permissions");
|
||||
@@ -808,6 +857,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "remove_admin_from_channel": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
UUID targetUserUUID = UUID.fromString(requestJson.getString("target_user_id"));
|
||||
|
||||
@@ -850,6 +903,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "add_member_to_group": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
UUID targetUserId = UUID.fromString(requestJson.getString("user_id"));
|
||||
|
||||
@@ -887,6 +944,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "remove_member_from_group": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
UUID targetUserId = UUID.fromString(requestJson.getString("user_id"));
|
||||
|
||||
@@ -927,6 +988,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "view_group_admins": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
|
||||
String role = GroupDatabase.getGroupRole(groupId, currentUser.getInternal_uuid());
|
||||
@@ -944,6 +1009,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "get_messages": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
try {
|
||||
String receiverId = requestJson.getString("receiver_id");
|
||||
String receiverType = requestJson.getString("receiver_type");
|
||||
@@ -1009,6 +1078,10 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
|
||||
case "toggle_block": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
try {
|
||||
UUID userUUID = UUID.fromString(requestJson.getString("user_id"));
|
||||
UUID targetUUID = UUID.fromString(requestJson.getString("target_id"));
|
||||
@@ -1035,6 +1108,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "transfer_group_ownership": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
UUID newOwnerUserId = UUID.fromString(requestJson.getString("new_owner_user_id"));
|
||||
|
||||
@@ -1072,6 +1149,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "view_group_members" : {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
|
||||
JSONArray members = GroupDatabase.getGroupMembers(groupId);
|
||||
@@ -1089,6 +1170,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "delete_private_chat" : {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID targetId = UUID.fromString(requestJson.getString("target_id"));
|
||||
boolean both = requestJson.getBoolean("both");
|
||||
|
||||
@@ -1105,6 +1190,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "get_group_permissions": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
userId = currentUser.getInternal_uuid();
|
||||
|
||||
@@ -1116,6 +1205,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "leave_chat": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
String chatType = requestJson.getString("chat_type");
|
||||
UUID chatId = UUID.fromString(requestJson.getString("chat_id"));
|
||||
userId = UUID.fromString(requestJson.getString("user_id"));
|
||||
@@ -1154,6 +1247,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "delete_group": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
|
||||
if (!GroupDatabase.isOwner(groupId, currentUser.getInternal_uuid())) {
|
||||
@@ -1177,6 +1274,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "get_channel_permissions": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
try {
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
userId = currentUser.getInternal_uuid();
|
||||
@@ -1192,6 +1293,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "view_channel_subscribers": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
|
||||
boolean isOwner = ChannelDatabase.isOwner(channelId, currentUser.getInternal_uuid());
|
||||
@@ -1216,6 +1321,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "add_subscriber_to_channel": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
UUID targetUserId = UUID.fromString(requestJson.getString("user_id"));
|
||||
|
||||
@@ -1251,6 +1360,10 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
|
||||
case "remove_subscriber_from_channel": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
UUID targetUserId = UUID.fromString(requestJson.getString("user_id"));
|
||||
|
||||
@@ -1291,6 +1404,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "edit_channel_info": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
try {
|
||||
UUID channelUUID = UUID.fromString(requestJson.getString("channel_id")); // internal_uuid
|
||||
String newChannelId = requestJson.getString("new_channel_id").trim();
|
||||
@@ -1330,6 +1447,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "delete_channel": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
|
||||
if (!ChannelDatabase.isOwner(channelId, currentUser.getInternal_uuid())) {
|
||||
@@ -1353,6 +1474,10 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
|
||||
case "remove_admin_from_group": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID groupId = UUID.fromString(requestJson.getString("group_id"));
|
||||
UUID targetUserId = UUID.fromString(requestJson.getString("target_user_id"));
|
||||
|
||||
@@ -1402,6 +1527,10 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
|
||||
case "transfer_channel_ownership": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
UUID channelId = UUID.fromString(requestJson.getString("channel_id"));
|
||||
String newOwnerUserIdStr = requestJson.getString("new_owner_user_id");
|
||||
|
||||
@@ -1442,6 +1571,8 @@ public class ClientHandler implements Runnable {
|
||||
responseJson.put("message", response.getMessage());
|
||||
responseJson.put("data", response.getData() != null ? response.getData() : JSONObject.NULL);
|
||||
out.println(responseJson.toString());
|
||||
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Connection with client lost.");
|
||||
|
||||
Reference in New Issue
Block a user