Server: - Split into views, routes, helpers, models modules - Merged /ws/talk and /ws/update into single /ws/chat endpoint - Replaced polling with push-based broadcast model - Added username uniqueness validation on connect - Fixed run_server arguments bug (workers parameter) - Removed deprecated loop argument from Sanic listeners - Replaced datetime.utcnow() with timezone-aware datetime.now(timezone.utc) Client: - Rewrote client as single-file module - Migrated from websocket-client to websockets (asyncio) - Fixed websocket-client conflict with asyncio event loop on Windows - Added progress indicators for key generation, exchange, connection - Added animated 3D spinning cube in UI - Updated RSA key from 512 to 2048 bits CLI: - Removed unnecessary asyncio.run() wrapper - Simplified entry point
32 lines
964 B
Python
32 lines
964 B
Python
from dataclasses import dataclass, field
|
|
from uuid import uuid4
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
|
|
@dataclass
|
|
class Message:
|
|
id: str = field(default_factory=lambda: str(uuid4()))
|
|
text: str = ""
|
|
timestamp: str = field(default_factory=lambda: datetime.utcnow().isoformat())
|
|
user_ip: str = ""
|
|
username: str = ""
|
|
|
|
|
|
@dataclass
|
|
class UserSession:
|
|
user_id: str
|
|
ip: str
|
|
username: str = "unknown"
|
|
fernet_key: Optional[bytes] = None
|
|
created_at: str = field(default_factory=lambda: datetime.utcnow().isoformat())
|
|
last_activity: str = field(default_factory=lambda: datetime.utcnow().isoformat())
|
|
active: bool = True
|
|
|
|
def update_activity(self):
|
|
self.last_activity = datetime.utcnow().isoformat()
|
|
|
|
def is_stale(self, timeout_seconds: int = 3600) -> bool:
|
|
last = datetime.fromisoformat(self.last_activity)
|
|
return (datetime.utcnow() - last).total_seconds() > timeout_seconds
|