diff --git a/server/go/main.go b/server/go/main.go index c423068..3374cfc 100644 --- a/server/go/main.go +++ b/server/go/main.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "os" + "time" socketio "github.com/googollee/go-socket.io" "github.com/googollee/go-socket.io/engineio" @@ -49,6 +50,7 @@ type Message struct { User string `json:"user"` Text string `json:"message,omitempty"` To string `json:"to,omitempty"` + Timstamp string `json:"timestamp,omitempty"` } @@ -120,6 +122,7 @@ func InitializeSockets() { server.OnEvent("/", "send_message", func(s socketio.Conn, m Message) { log.Println(m.To) + m.Timestamp = time.Now().Format("2006-01-02 15:04:05") server.BroadcastToRoom("/", m.To, "receive_message", m) }) } diff --git a/server/python/main.py b/server/python/main.py index c9b63e2..805ef41 100644 --- a/server/python/main.py +++ b/server/python/main.py @@ -2,6 +2,7 @@ FLASK SOCKETIO SERVER """ +from datetime import datetime import os import secrets @@ -56,6 +57,14 @@ def handle_messages(data): sends message in perticular room """ socketio.emit("receive_message", data, to=data["to"]) + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + message_with_timestamp = { + "timestamp": timestamp, + "from": data["from"], + "to": data["to"], + "message": data["message"] + } + socketio.emit("receive_message", message_with_timestamp, to=data["to"]) @socketio.on("join_room") @@ -70,6 +79,23 @@ def handle_join_room_event(data): join_room(room) socketio.emit("room_announcements", "Join Room Callback", to=room) +@socketio.on("get_user_cards") +def handle_get_user_cards(): + """ + get user cards with timestamps + """ + users = db.get_user_cards() + user_cards_with_timestamps = [] + + for user in users: + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + user_card_with_timestamp = { + "timestamp": timestamp, + "user": user + } + user_cards_with_timestamps.append(user_card_with_timestamp) + + socketio.emit("receive_user_cards", user_cards_with_timestamps) if __name__ == "__main__": PORT = os.getenv("PORT", "5000") diff --git a/server/python/tests/test_database.py b/server/python/tests/test_database.py index 5025a84..5d3b0d7 100644 --- a/server/python/tests/test_database.py +++ b/server/python/tests/test_database.py @@ -2,6 +2,7 @@ Tests database module """ +import datetime import unittest from database import DataBase @@ -22,3 +23,26 @@ def test_save_user(self): public_key = "random-public-key" user_id = self.database.save_user(public_key) self.assertEqual(public_key, self.database.get_public_key(user_id)) + + def test_get_user_cards(self): + """ + Test retrieving user cards with timestamps + """ + # Assuming the database already has some user cards + user_cards = self.database.get_user_cards() + + for user_card in user_cards: + # Check if the timestamp key exists in each user card + self.assertIn("timestamp", user_card) + + # Check if the timestamp value is a valid datetime string + try: + datetime.strptime(user_card["timestamp"], "%Y-%m-%d %H:%M:%S") + except ValueError: + self.fail("Invalid timestamp format") + + # Check if the user key exists in each user card + self.assertIn("user", user_card) + +if __name__ == "__main__": + unittest.main()