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