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

Fixes #1 #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/http"
"os"
"time"

socketio "github.com/googollee/go-socket.io"
"github.com/googollee/go-socket.io/engineio"
Expand Down Expand Up @@ -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"`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More suitable datatype is *time.Time

}


Expand Down Expand Up @@ -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)
})
}
Expand Down
26 changes: 26 additions & 0 deletions server/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
FLASK SOCKETIO SERVER
"""

from datetime import datetime
import os
import secrets

Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down
24 changes: 24 additions & 0 deletions server/python/tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Tests database module
"""

import datetime
import unittest

from database import DataBase
Expand All @@ -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()