Edit logout logic
This commit is contained in:
@@ -177,9 +177,9 @@ public class ActionHandler {
|
||||
break;
|
||||
|
||||
case "3":
|
||||
Session.currentUser = null;
|
||||
Session.chatList = null;
|
||||
System.out.println("Logged out.");
|
||||
|
||||
logout();
|
||||
|
||||
return;
|
||||
default:
|
||||
System.out.println("Invalid choice.");
|
||||
@@ -225,5 +225,21 @@ public class ActionHandler {
|
||||
send(request);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void logout() {
|
||||
if (Session.currentUser != null && Session.currentUser.has("user_id")) {
|
||||
JSONObject request = new JSONObject();
|
||||
String userId = Session.currentUser.getString("internalUUID");
|
||||
request.put("action", "logout");
|
||||
request.put("user_id",userId);
|
||||
send(request);
|
||||
Session.currentUser = null;
|
||||
Session.chatList = null;
|
||||
} else {
|
||||
System.out.println("Not logged in.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.to.telegramfinalproject.Client;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
@@ -56,27 +58,32 @@ public class TelegramClient {
|
||||
}
|
||||
break;
|
||||
case "3":
|
||||
System.out.println(" Disconnecting...");
|
||||
System.out.println("Disconnecting...");
|
||||
|
||||
if (Session.currentUser != null && Session.currentUser.has("internalUUID")) {
|
||||
try {
|
||||
JSONObject logoutRequest = new JSONObject();
|
||||
logoutRequest.put("action", "logout");
|
||||
logoutRequest.put("user_id", Session.currentUser.getString("internalUUID"));
|
||||
out.println(logoutRequest.toString());
|
||||
in.readLine();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to notify server on logout: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (this.socket != null) {
|
||||
this.socket.close();
|
||||
}
|
||||
|
||||
if (this.in != null) {
|
||||
this.in.close();
|
||||
}
|
||||
|
||||
if (this.out != null) {
|
||||
this.out.close();
|
||||
}
|
||||
|
||||
if (socket != null) socket.close();
|
||||
if (in != null) in.close();
|
||||
if (out != null) out.close();
|
||||
System.out.println("Disconnected.");
|
||||
} catch (IOException e) {
|
||||
System.err.println(" Error closing connection: " + e.getMessage());
|
||||
System.err.println("Error closing connection: " + e.getMessage());
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
|
||||
default:
|
||||
System.out.println("Invalid choice. Please try again.");
|
||||
}
|
||||
|
||||
@@ -69,12 +69,20 @@ public class ClientHandler implements Runnable {
|
||||
} else {
|
||||
|
||||
User user = authService.login(request.getUsername(), request.getPassword());
|
||||
if (user == null) {
|
||||
|
||||
if (user == null){
|
||||
response = new ResponseModel("error", "Login failed.");
|
||||
break;
|
||||
}
|
||||
this.currentUser = user;
|
||||
|
||||
if (SessionManager.contains(user.getInternal_uuid())) {
|
||||
response = new ResponseModel("error", "You are already logged in from another device.");
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
SessionManager.addUser(user.getInternal_uuid(), this.socket);
|
||||
userDatabase.updateUserStatus(user.getInternal_uuid(), "online");
|
||||
List<Contact> contacts = ContactDatabase.getContacts(user.getInternal_uuid());
|
||||
@@ -118,16 +126,21 @@ public class ClientHandler implements Runnable {
|
||||
|
||||
case "logout": {
|
||||
String user_Id = requestJson.optString("user_id");
|
||||
if (userId != null && !user_Id.isEmpty()) {
|
||||
UUID uuid = UUID.fromString(user_Id);
|
||||
userDatabase.updateUserStatus(uuid, "offline");
|
||||
userDatabase.updateLastSeen(uuid);
|
||||
SessionManager.removeUser(uuid);
|
||||
response = new ResponseModel("success", "Logged out.");
|
||||
if (user_Id != null && !user_Id.isEmpty()) {
|
||||
try {
|
||||
UUID uuid = UUID.fromString(user_Id);
|
||||
userDatabase.updateUserStatus(uuid, "offline");
|
||||
userDatabase.updateLastSeen(uuid);
|
||||
SessionManager.removeUser(uuid);
|
||||
response = new ResponseModel("success", "Logged out.");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
response = new ResponseModel("error", "Invalid UUID format for user_id.");
|
||||
}
|
||||
} else {
|
||||
response = new ResponseModel("error", "Invalid user_id for logout.");
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
case "search": {
|
||||
|
||||
@@ -32,4 +32,12 @@ public class SessionManager {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean contains(UUID userId) {
|
||||
return onlineUsers.containsKey(userId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user