View profile

This commit is contained in:
2025-07-22 18:08:18 +03:30
parent 42c92c5a2d
commit 4a849ea022
3 changed files with 183 additions and 51 deletions
@@ -730,6 +730,10 @@ public class ActionHandler {
break; break;
} }
System.out.println("\n📍 Entered Chat:");
System.out.println("🔷 Name: " + chat.getName());
System.out.println("────────────────────────────────────────────");
switch (chat.getType().trim().toLowerCase()) { switch (chat.getType().trim().toLowerCase()) {
case "private" -> stayInChat = showPrivateChatMenu(chat); case "private" -> stayInChat = showPrivateChatMenu(chat);
case "group" -> stayInChat = showGroupChatMenu(chat); case "group" -> stayInChat = showGroupChatMenu(chat);
@@ -759,11 +763,34 @@ public class ActionHandler {
System.out.println("🚪 Exiting chat due to real-time update."); System.out.println("🚪 Exiting chat due to real-time update.");
return false; return false;
} }
JSONObject req = new JSONObject();
req.put("action", "view_profile");
req.put("target_id", chat.getId()); // internal UUID
JSONObject res = sendWithResponse(req);
if (res.getString("status").equals("success")) {
JSONObject data = res.getJSONObject("data");
System.out.println("\n💬 Private Chat with: " + data.getString("profile_name"));
if (data.getBoolean("is_online")) {
System.out.println("✅ Status: Online");
} else {
System.out.println("📅 Last seen: " + data.getString("last_seen"));
}
System.out.println("────────────────────────────────────────────");
}
System.out.println("1. Send message"); System.out.println("1. Send message");
System.out.println("2. Block/Unblock"); System.out.println("2. Block/Unblock");
System.out.println("3. Delete chat (one-sided)"); System.out.println("3. Delete chat (one-sided)");
System.out.println("4. Delete chat (both sides)"); System.out.println("4. Delete chat (both sides)");
System.out.println("5. Back"); System.out.println("5. View profile");
System.out.println("6. Back");
String input = scanner.nextLine(); String input = scanner.nextLine();
@@ -779,6 +806,32 @@ public class ActionHandler {
return false; return false;
} }
case "5" -> { case "5" -> {
JSONObject reqProfile = new JSONObject();
reqProfile.put("action", "view_profile");
reqProfile.put("target_id", chat.getId());
JSONObject resProfile = sendWithResponse(reqProfile);
if (resProfile.getString("status").equals("success")) {
JSONObject profile = resProfile.getJSONObject("data");
System.out.println("\n👤 Profile Info:");
System.out.println("🔷 Name: " + profile.optString("profile_name", "Unknown"));
System.out.println("📄 Bio: " + profile.optString("bio", "No bio set"));
System.out.println("🖼️ Image URL: " + profile.optString("image_url", "N/A"));
if (profile.getBoolean("is_online")) {
System.out.println("✅ Status: Online");
} else {
System.out.println("📅 Last seen: " + profile.optString("last_seen", "Unknown"));
}
System.out.println("────────────────────────────────────────────");
} else {
System.out.println("❌ Could not load profile.");
}
return true;
}
case "6" -> {
return false; return false;
} }
default -> System.out.println("Invalid choice."); default -> System.out.println("Invalid choice.");
@@ -836,6 +889,7 @@ public class ActionHandler {
} else { } else {
System.out.println("9. Leave Group"); System.out.println("9. Leave Group");
} }
System.out.println("10. View Profile");
System.out.println("0. Back to Chat List"); System.out.println("0. Back to Chat List");
@@ -877,6 +931,9 @@ public class ActionHandler {
} }
return false; return false;
} }
case "10" ->{
GroupInfo(chat.getId());
}
case "0" -> { case "0" -> {
return false; return false;
@@ -939,6 +996,7 @@ public class ActionHandler {
System.out.println("8. Leave Channel"); System.out.println("8. Leave Channel");
} }
System.out.println("10. View Profile");
System.out.println("0. Back to Chat List"); System.out.println("0. Back to Chat List");
String input = scanner.nextLine(); String input = scanner.nextLine();
@@ -1009,6 +1067,9 @@ public class ActionHandler {
System.out.println("❌ You don't have permission."); System.out.println("❌ You don't have permission.");
} }
} }
case "10"->{
ChannelInfo(chat.getId());
}
case "0" -> { case "0" -> {
return false; return false;
} }
@@ -1693,6 +1754,34 @@ public class ActionHandler {
} }
} }
private void GroupInfo(UUID groupId){
JSONObject req = new JSONObject();
req.put("action", "get_chat_info");
req.put("receiver_id", groupId.toString());
req.put("receiver_type", "group");
JSONObject res = sendWithResponse(req);
if (res == null || !res.getString("status").equals("success")) {
System.out.println("❌ Failed to fetch group info.");
return;
}
JSONObject data = res.getJSONObject("data");
String currentId = data.getString("id");
String currentName = data.getString("name");
String currentDesc = data.optString("description", null);
String currentImage = data.optString("image_url", null);
System.out.println("\n--- Group Info ---");
System.out.println("1. Group ID: " + currentId);
System.out.println("2. Name: " + currentName);
System.out.println("3. Description: " + currentDesc);
System.out.println("4. Image URL: " + currentImage);
System.out.println("────────────────────────────────────────────");
}
private void editGroupInfo(UUID groupId) { private void editGroupInfo(UUID groupId) {
JSONObject req = new JSONObject(); JSONObject req = new JSONObject();
@@ -1792,7 +1881,33 @@ public class ActionHandler {
} }
} }
private void ChannelInfo(UUID channelInternalId){
JSONObject req = new JSONObject();
req.put("action", "get_chat_info");
req.put("receiver_id", channelInternalId.toString());
req.put("receiver_type", "channel");
JSONObject res = sendWithResponse(req);
if (res == null || !res.getString("status").equals("success")) {
System.out.println("❌ Failed to fetch channel info.");
return;
}
JSONObject data = res.getJSONObject("data");
String currentId = data.getString("id");
String currentName = data.getString("name");
String currentDesc = data.optString("description", null);
String currentImage = data.optString("image_url", null);
System.out.println("\n--- Channel Info ---");
System.out.println("1. Channel ID: " + currentId);
System.out.println("2. Name: " + currentName);
System.out.println("3. Description: " + currentDesc);
System.out.println("4. Image URL: " + currentImage);
System.out.println("────────────────────────────────────────────");
}
private void editChannelInfo(UUID channelInternalId) { private void editChannelInfo(UUID channelInternalId) {
@@ -1896,55 +2011,6 @@ 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 addAdminToEntity(String type, UUID entityId) { public void addAdminToEntity(String type, UUID entityId) {
System.out.print("Enter user ID to promote to admin: "); System.out.print("Enter user ID to promote to admin: ");
@@ -9,13 +9,58 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
public class userDatabase { public class userDatabase {
public userDatabase() { public userDatabase() {
} }
private Connection getConnection() throws SQLException { public static boolean isUserOnline(UUID userId) {
String sql = "SELECT status FROM users WHERE internal_uuid = ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
String status = rs.getString("status");
return "online".equalsIgnoreCase(status);
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private static Connection getConnection() throws SQLException {
return ConnectionDb.connect(); return ConnectionDb.connect();
} }
public static String getLastSeen(UUID userId) {
String sql = "SELECT last_seen FROM users WHERE internal_uuid = ?";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setObject(1, userId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
Timestamp lastSeen = rs.getTimestamp("last_seen");
if (lastSeen != null) {
return lastSeen.toLocalDateTime().toString();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return "Unknown";
}
public User findByUserId(String userId) { public User findByUserId(String userId) {
String query = "SELECT * FROM users WHERE user_id = ?"; String query = "SELECT * FROM users WHERE user_id = ?";
@@ -1608,6 +1608,27 @@ public class ClientHandler implements Runnable {
break; break;
} }
case "view_profile": {
UUID targetId = UUID.fromString(requestJson.getString("target_id"));
User user = userDatabase.findByInternalUUID(targetId);
if (user == null) {
response = new ResponseModel("error", "User not found.");
break;
}
JSONObject data = new JSONObject();
data.put("profile_name", user.getProfile_name());
data.put("user_id", user.getUser_id());
data.put("bio", user.getBio());
data.put("image_url", user.getImage_url());
data.put("is_online", userDatabase.isUserOnline(user.getInternal_uuid()));
data.put("last_seen", userDatabase.getLastSeen(user.getInternal_uuid()));
response = new ResponseModel("success", "Profile data", data);
break;
}
default: default: