add viewing user profile.

This commit is contained in:
Asal Lotfi
2025-07-27 22:47:11 +03:30
parent 6f5a835224
commit 5825373f2c
5 changed files with 277 additions and 4 deletions
@@ -703,7 +703,8 @@ public class ActionHandler {
System.out.println("2. Search"); System.out.println("2. Search");
System.out.println("3. Create Channel"); System.out.println("3. Create Channel");
System.out.println("4. Create group"); System.out.println("4. Create group");
System.out.println("5. Logout"); System.out.println("5. Show sidebar menu");
System.out.println("6. Logout");
System.out.print("Choose an option: "); System.out.print("Choose an option: ");
String choice = scanner.nextLine(); String choice = scanner.nextLine();
@@ -716,7 +717,8 @@ public class ActionHandler {
case "2" -> search(); case "2" -> search();
case "3" -> createChannel(); case "3" -> createChannel();
case "4" -> createGroup(); case "4" -> createGroup();
case "5" -> { case "5" -> showSidebarMenu();
case "6" -> {
logout(); logout();
return; return;
} }
@@ -2712,8 +2714,50 @@ public class ActionHandler {
} }
} }
private void showSidebarMenu() {
System.out.println("\n--- Sidebar Menu ---");
System.out.println("1. View Profile");
System.out.println("2. New Group");
System.out.println("3. New Channel");
System.out.println("4. View Contacts");
System.out.println("5. Saved messages");
System.out.println("6. Settings");
System.out.println("7. Telegram features");
System.out.println("8. Telegram Q&A");
System.out.println("0. Back");
System.out.println("Choose your action: (0 to 8)");
// Handle invalid input
int action;
while (true) {
if (scanner.hasNextInt()) {
action = scanner.nextInt();
scanner.nextLine(); // Clear newline
if (action >= 0 && action <= 8) {
break;
}
System.out.println("Invalid input. Please enter a number between 0 and 8.");
} else {
scanner.nextLine();
System.out.println("Invalid input. Try again.");
}
}
SidebarHandler sidebarHandler = new SidebarHandler(scanner);
switch (action) {
case 0 -> {
return;
}
case 1 -> sidebarHandler.handleSidebarAction(SidebarAction.MY_PROFILE);
case 2 -> sidebarHandler.handleSidebarAction(SidebarAction.NEW_GROUP);
case 3 -> sidebarHandler.handleSidebarAction(SidebarAction.NEW_CHANNEL);
case 4 -> sidebarHandler.handleSidebarAction(SidebarAction.CONTACTS);
case 5 -> sidebarHandler.handleSidebarAction(SidebarAction.SAVED_MESSAGES);
case 6 -> sidebarHandler.handleSidebarAction(SidebarAction.SETTINGS);
case 7 -> sidebarHandler.handleSidebarAction(SidebarAction.FEATURES);
case 8 -> sidebarHandler.handleSidebarAction(SidebarAction.Q_AND_A);
default -> System.out.println("Invalid action.");
}
}
} }
@@ -0,0 +1,12 @@
package org.to.telegramfinalproject.Client;
public enum SidebarAction {
MY_PROFILE,
NEW_GROUP,
NEW_CHANNEL,
CONTACTS,
SAVED_MESSAGES,
SETTINGS,
FEATURES,
Q_AND_A
}
@@ -0,0 +1,148 @@
package org.to.telegramfinalproject.Client;
import org.json.JSONObject;
import java.util.Scanner;
public class SidebarHandler {
private final Scanner scanner;
public SidebarHandler(Scanner scanner) {
this.scanner = scanner;
}
public void handleSidebarAction(SidebarAction action) {
switch (action) {
case MY_PROFILE:
openUserProfile();
break;
case NEW_GROUP:
createNewGroup();
break;
case NEW_CHANNEL:
createNewChannel();
break;
case CONTACTS:
showContacts();
break;
case SAVED_MESSAGES:
showSavedMessages();
break;
case SETTINGS:
openSettings();
break;
case FEATURES:
showTelegramFeatures();
break;
case Q_AND_A:
showTelegramQA();
break;
}
}
private void openUserProfile() {
// Step 1: Request profile info from the server
JSONObject request = new JSONObject();
request.put("action", "get_user_profile");
JSONObject response = ActionHandler.sendWithResponse(request);
if (response == null || !response.optString("status", "fail").equals("success")) {
System.out.println("Failed to load profile information.");
return;
}
JSONObject profile = response.getJSONObject("data");
String profileName = profile.optString("profile_name", "NOT-AVAILABLE");
String userId = profile.optString("user_id", "NOT-AVAILABLE");
String status = profile.optString("status", "NOT-AVAILABLE");
String bio = profile.optString("bio", "NOT-AVAILABLE");
String profilePictureUrl = profile.optString("profile_picture_url", "NOT-AVAILABLE");
// Step 2: Show profile info
System.out.println("===== My Profile =====");
System.out.println("Profile Picture URL : " + profilePictureUrl);
System.out.println("Profile Name : " + profileName);
System.out.println("User ID : " + userId);
System.out.println("Status : " + status);
System.out.println("Bio : " + bio);
System.out.println("======================");
// Step 3: Ask if user wants to edit anything
System.out.println("Do you want to edit any of these? (yes/no)");
while (true) {
String choice = scanner.nextLine().trim().toLowerCase();
if (choice.equals("no")) return;
if (!choice.equals("yes")) {
System.out.println("Invalid input. Please enter 'yes' or 'no'.");
continue;
}
// Step 4: Show editable fields
System.out.println("Which field do you want to edit?");
System.out.println("1. Profile Name");
System.out.println("2. User ID");
System.out.println("3. Bio");
System.out.println("4. Profile Picture URL");
System.out.println("0. Cancel");
String option = scanner.nextLine().trim();
switch (option) {
case "1" -> editProfileName();
case "2" -> editUserId();
case "3" -> editBio();
case "4" -> editProfilePictureUrl();
case "0" -> { return; }
default -> System.out.println("Invalid choice. Try again.");
}
// Re-fetch and re-display profile after editing
openUserProfile();
return;
}
}
private void editProfileName() {
}
private void editUserId() {
}
private void editBio() {
}
private void editProfilePictureUrl() {
}
private void createNewGroup() {
System.out.println("👥 Creating a new group...");
}
private void createNewChannel() {
System.out.println("📢 Creating a new channel...");
}
private void showContacts() {
System.out.println("📇 Showing contacts...");
}
private void showSavedMessages() {
System.out.println("💾 Showing saved messages...");
}
private void openSettings() {
System.out.println("⚙️ Opening settings...");
}
private void showTelegramFeatures() {
System.out.println("🌟 Showing Telegram features...");
}
private void showTelegramQA() {
System.out.println("❓ Showing Q&A...");
}
}
@@ -1707,8 +1707,20 @@ public class ClientHandler implements Runnable {
// break; // break;
// } // }
// //
case "get_user_profile": {
// Get the current user's user_id
String user_id = this.currentUser.getUser_id();
// Get the profile JSON from SidebarService
JSONObject data = SidebarService.getUserProfile(user_id);
if (data == null) {
response = new ResponseModel("error", "Failed to retrieve user profile.");
} else {
response = new ResponseModel("success", "User profile retrieved successfully.", data);
}
break;
}
default: default:
response = new ResponseModel("error", "Unknown action: " + action); response = new ResponseModel("error", "Unknown action: " + action);
@@ -0,0 +1,57 @@
package org.to.telegramfinalproject.Server;
import org.json.JSONObject;
import org.to.telegramfinalproject.Database.userDatabase;
import org.to.telegramfinalproject.Models.User;
public class SidebarService {
private static userDatabase userDB = new userDatabase();
// Returns current user's profile data (name, bio, status, etc.)
public static JSONObject getUserProfile(String userId) {
User user = userDB.findByUserId(userId);
if (user == null) {
return null;
}
JSONObject profile = new JSONObject();
profile.put("user_id", user.getUser_id());
profile.put("profile_name", user.getProfile_name());
profile.put("bio", user.getBio() != null ? user.getBio() : "");
profile.put("status", "ONLINE");
profile.put("profile_picture_url", user.getImage_url());
return profile;
}
// Updates one or more profile fields for the user
public static JSONObject updateUserProfile(String userId, JSONObject updateData) {
return null;
}
// Changes the user's profile picture
public static JSONObject changeProfilePicture(String userId, byte[] imageData) {
return null;
}
// Changes user-id
public static JSONObject updateUserId(String userId, String newUserId) {
return null;
}
// (Optional) Changes username
public static JSONObject updateUserName(String userId, String newUserName) {
return null;
}
// Changes user's profile name
public static JSONObject updateProfileName(String userId, String newProfileName) {
return null;
}
// Changes or sets the user's date of birth
public static JSONObject updateDateOfBirth(String userId, String newDateOfBirth) {
return null;
}
}