diff --git a/backend/matcha/db/last_connection.py b/backend/matcha/db/last_connection.py new file mode 100644 index 00000000..b4e4236a --- /dev/null +++ b/backend/matcha/db/last_connection.py @@ -0,0 +1,32 @@ +from matcha.db.utils import ( + db_fetchone, + db_query, +) + + +def db_get_last_connection(id_user: int) -> str: + query = """ + SELECT last_connection_at + FROM users + WHERE + id = %s + ; + """ + + return db_fetchone(query, (id_user,))[0].timestamp() + + +def db_update_last_connection(id_user: int) -> str: + query = """ + UPDATE + users + SET + last_connection_at + = + now() + WHERE + id = %s + ; + """ + + return db_query(query, (id_user,)) diff --git a/backend/matcha/services/profile.py b/backend/matcha/services/profile.py index 95f5b18d..b898be58 100644 --- a/backend/matcha/services/profile.py +++ b/backend/matcha/services/profile.py @@ -7,10 +7,14 @@ from matcha.utils import check_request_json +from matcha.db.last_connection import db_update_last_connection + from matcha.db.pictures import db_get_user_images, db_get_url_profile from matcha.db.visit import db_put_visit, db_get_visit +from matcha.db.last_connection import db_get_last_connection + from matcha.db.db import ( db_get_interests, db_set_user_profile_data, @@ -69,6 +73,8 @@ def services_profile(id_user, request): elif "error" in profile_picture: profile_url = url = profile_picture["error"] + db_update_last_connection(id_user) + return ( jsonify( username=user_db[0], @@ -121,6 +127,7 @@ def services_profile(id_user, request): urlProfile=profile_url, isLiked=is_liked, connected=SocketManager.is_connected(user_db[0]), + lastConnection=db_get_last_connection(user_db[0]), ), 200, ) diff --git a/backend/matcha/websocket/main_namespace.py b/backend/matcha/websocket/main_namespace.py index d5f5e1d4..f51a3aa4 100644 --- a/backend/matcha/websocket/main_namespace.py +++ b/backend/matcha/websocket/main_namespace.py @@ -4,15 +4,9 @@ from matcha.utils import flaskprint -from matcha.websocket.socket_manager import SocketManager - +from matcha.db.last_connection import db_update_last_connection -class ConnectedUser: - def __init__(self): - self.sid_userid = {} - - def insert(self, sid: str, user_id: int): - self.sid_userid.update({sid: user_id}) +from matcha.websocket.socket_manager import SocketManager class MainNamespace(Namespace): @@ -41,6 +35,7 @@ def on_connect(self, auth): def on_disconnect(self): try: + db_update_last_connection(SocketManager.get_user_id(request.sid)) SocketManager.remove_session(request.sid) except Exception as e: flaskprint("Error handling message:" + str(e)) diff --git a/backend/matcha/websocket/socket_manager.py b/backend/matcha/websocket/socket_manager.py index 212ddbb5..838ee3c5 100644 --- a/backend/matcha/websocket/socket_manager.py +++ b/backend/matcha/websocket/socket_manager.py @@ -29,7 +29,7 @@ def add_session(cls, sid, user_id): @classmethod def remove_session(cls, sid): user_id = cls.sid_userid.pop(sid, None) - cls.userid_sid.pop(userid, None) + cls.userid_sid.pop(user_id, None) @classmethod def __str__(cls): diff --git a/cliff.toml b/cliff.toml index 1e00ec9e..a0bd8122 100644 --- a/cliff.toml +++ b/cliff.toml @@ -55,6 +55,7 @@ commit_preprocessors = [ commit_parsers = [ { message = "^front", group = " Frontend" }, { message = "^back", group = " Backend" }, + { message = "^psql", group = " PostgreSQL" }, { message = "^db", group = " Database" }, { message = "^feat", group = " Features" }, { message = "^fix", group = " Bug Fixes" }, diff --git a/postgres-init/init.sql b/postgres-init/init.sql index c6b8a0fb..061b2705 100644 --- a/postgres-init/init.sql +++ b/postgres-init/init.sql @@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS users ( country VARCHAR DEFAULT NULL, latitude DOUBLE PRECISION DEFAULT NULL, longitude DOUBLE PRECISION DEFAULT NULL, + last_connection_at TIMESTAMP WITH TIME ZONE DEFAULT now(), created_at TIMESTAMP WITH TIME ZONE DEFAULT now() );