Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last connection #160

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
32 changes: 32 additions & 0 deletions backend/matcha/db/last_connection.py
Original file line number Diff line number Diff line change
@@ -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,))
7 changes: 7 additions & 0 deletions backend/matcha/services/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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,
)
11 changes: 3 additions & 8 deletions backend/matcha/websocket/main_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion backend/matcha/websocket/socket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ commit_preprocessors = [
commit_parsers = [
{ message = "^front", group = "<!-- 0 --> Frontend" },
{ message = "^back", group = "<!-- 0 --> Backend" },
{ message = "^psql", group = "<!-- 0 --> PostgreSQL" },
{ message = "^db", group = "<!-- 0 --> Database" },
{ message = "^feat", group = "<!-- 0 --> Features" },
{ message = "^fix", group = "<!-- 1 --> Bug Fixes" },
Expand Down
1 change: 1 addition & 0 deletions postgres-init/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);

Expand Down