parthmax24 commited on
Commit
b3a9c0e
·
0 Parent(s):

intial commit

Browse files
Files changed (8) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +27 -0
  3. README.md +10 -0
  4. app.py +234 -0
  5. requirements.txt +5 -0
  6. static/main.css +908 -0
  7. static/main.js +417 -0
  8. templates/index.html +81 -0
.gitattributes ADDED
@@ -0,0 +1 @@
 
 
1
+ docs/images/*.png filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use Python 3.11 slim image
2
+ FROM python:3.11-slim
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Install system dependencies
8
+ RUN apt-get update && apt-get install -y \
9
+ gcc \
10
+ curl \
11
+ && rm -rf /var/lib/apt/lists/*
12
+
13
+ # Copy requirements first for caching
14
+ COPY requirements.txt .
15
+
16
+ # Install Python dependencies
17
+ RUN pip install --no-cache-dir -r requirements.txt
18
+
19
+ # Copy application code
20
+ COPY . .
21
+
22
+ # Create non-root user for security
23
+ RUN useradd -m appuser && chown -R appuser /app
24
+ USER appuser
25
+
26
+ # Hugging Face requires apps to run on port 7860
27
+ CMD uvicorn app:app --host 0.0.0.0 --port 7860
README.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: TriChat
3
+ emoji: 💬
4
+ colorFrom: indigo
5
+ colorTo: pink
6
+ sdk: docker
7
+ sdk_version: '1.0'
8
+ app_file: Dockerfile
9
+ pinned: false
10
+ ---
app.py ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException
2
+ from fastapi.staticfiles import StaticFiles
3
+ from fastapi.responses import HTMLResponse
4
+ from fastapi.templating import Jinja2Templates
5
+ import json
6
+ import asyncio
7
+ from datetime import datetime
8
+ from typing import Dict, List, Set
9
+ import base64
10
+ import mimetypes
11
+ import os
12
+ import uvicorn
13
+ app = FastAPI(title="Tri-Chat API", description="Real-time chat with WebSocket support")
14
+
15
+ # Serve static assets
16
+ app.mount("/static", StaticFiles(directory="static"), name="static")
17
+
18
+ # Setup templates
19
+ templates = Jinja2Templates(directory="templates")
20
+
21
+ # Connection Manager to handle WebSocket connections
22
+ class ConnectionManager:
23
+ def __init__(self):
24
+ # Store active connections by room
25
+ self.active_connections: Dict[str, List[Dict]] = {}
26
+ # Store message history by room (in-memory for demo)
27
+ self.message_history: Dict[str, List[Dict]] = {}
28
+
29
+ async def connect(self, websocket: WebSocket, room: str, username: str):
30
+ await websocket.accept()
31
+
32
+ # Initialize room if it doesn't exist
33
+ if room not in self.active_connections:
34
+ self.active_connections[room] = []
35
+ self.message_history[room] = []
36
+
37
+ # Add connection to room
38
+ connection_info = {
39
+ "websocket": websocket,
40
+ "username": username,
41
+ "joined_at": datetime.now().isoformat()
42
+ }
43
+ self.active_connections[room].append(connection_info)
44
+
45
+ # Send join notification
46
+ join_message = {
47
+ "type": "system",
48
+ "message": f"{username} joined the room",
49
+ "timestamp": datetime.now().isoformat(),
50
+ "room": room
51
+ }
52
+ await self.broadcast_to_room(room, join_message)
53
+
54
+ # Send message history to new user
55
+ for message in self.message_history[room]:
56
+ await websocket.send_text(json.dumps(message))
57
+
58
+ def disconnect(self, websocket: WebSocket, room: str):
59
+ if room in self.active_connections:
60
+ # Find and remove the connection
61
+ for conn in self.active_connections[room]:
62
+ if conn["websocket"] == websocket:
63
+ self.active_connections[room].remove(conn)
64
+ return conn["username"]
65
+ return None
66
+
67
+ async def broadcast_to_room(self, room: str, message: dict):
68
+ if room not in self.active_connections:
69
+ return
70
+
71
+ # Store message in history
72
+ self.message_history[room].append(message)
73
+
74
+ # Keep only last 100 messages per room
75
+ if len(self.message_history[room]) > 100:
76
+ self.message_history[room] = self.message_history[room][-100:]
77
+
78
+ # Send to all connections in room
79
+ disconnected = []
80
+ for connection_info in self.active_connections[room]:
81
+ try:
82
+ await connection_info["websocket"].send_text(json.dumps(message))
83
+ except:
84
+ disconnected.append(connection_info)
85
+
86
+ # Remove disconnected clients
87
+ for conn in disconnected:
88
+ self.active_connections[room].remove(conn)
89
+
90
+ def get_room_users(self, room: str) -> List[str]:
91
+ if room not in self.active_connections:
92
+ return []
93
+ return [conn["username"] for conn in self.active_connections[room]]
94
+
95
+ # Global connection manager instance
96
+ manager = ConnectionManager()
97
+
98
+ @app.get("/", response_class=HTMLResponse)
99
+ async def get_chat_page():
100
+ """Serve the chat HTML page"""
101
+ try:
102
+ with open("templates/index.html", "r", encoding="utf-8") as f:
103
+ html_content = f.read()
104
+ return HTMLResponse(content=html_content)
105
+ except FileNotFoundError:
106
+ return HTMLResponse(
107
+ content="<h1>Error: templates/index.html not found</h1><p>Please make sure the templates directory exists with index.html</p>",
108
+ status_code=404
109
+ )
110
+
111
+ @app.websocket("/ws/{room}")
112
+ async def websocket_endpoint(websocket: WebSocket, room: str, username: str):
113
+ """WebSocket endpoint for real-time chat"""
114
+
115
+ # Validate inputs
116
+ if not username or len(username.strip()) == 0:
117
+ await websocket.close(code=1008, reason="Username is required")
118
+ return
119
+
120
+ if not room or len(room.strip()) == 0:
121
+ room = "global"
122
+
123
+ # Sanitize inputs
124
+ username = username.strip()[:20] # Limit username length
125
+ room = room.strip()[:30] # Limit room name length
126
+
127
+ await manager.connect(websocket, room, username)
128
+
129
+ try:
130
+ while True:
131
+ # Receive message from client
132
+ data = await websocket.receive_text()
133
+ message_data = json.loads(data)
134
+
135
+ # Validate message type
136
+ if message_data.get("type") not in ["text", "file"]:
137
+ continue
138
+
139
+ # Process text message
140
+ if message_data["type"] == "text":
141
+ text_content = message_data.get("text", "").strip()
142
+ if len(text_content) == 0:
143
+ continue
144
+
145
+ # Sanitize and limit text length
146
+ text_content = text_content[:500]
147
+
148
+ message = {
149
+ "type": "text",
150
+ "username": username,
151
+ "text": text_content,
152
+ "timestamp": datetime.now().isoformat(),
153
+ "room": room
154
+ }
155
+
156
+ await manager.broadcast_to_room(room, message)
157
+
158
+ # Process file message
159
+ elif message_data["type"] == "file":
160
+ file_name = message_data.get("fileName", "unknown")[:100]
161
+ file_type = message_data.get("fileType", "application/octet-stream")
162
+ file_size = message_data.get("fileSize", 0)
163
+ file_data = message_data.get("fileData", "")
164
+
165
+ # Validate file size (5MB limit)
166
+ if file_size > 5 * 1024 * 1024:
167
+ await websocket.send_text(json.dumps({
168
+ "type": "error",
169
+ "message": "File size exceeds 5MB limit"
170
+ }))
171
+ continue
172
+
173
+ # Validate base64 data
174
+ try:
175
+ base64.b64decode(file_data)
176
+ except Exception:
177
+ await websocket.send_text(json.dumps({
178
+ "type": "error",
179
+ "message": "Invalid file data"
180
+ }))
181
+ continue
182
+
183
+ message = {
184
+ "type": "file",
185
+ "username": username,
186
+ "fileName": file_name,
187
+ "fileType": file_type,
188
+ "fileSize": file_size,
189
+ "fileData": file_data,
190
+ "timestamp": datetime.now().isoformat(),
191
+ "room": room
192
+ }
193
+
194
+ await manager.broadcast_to_room(room, message)
195
+
196
+ except WebSocketDisconnect:
197
+ disconnected_username = manager.disconnect(websocket, room)
198
+ if disconnected_username:
199
+ leave_message = {
200
+ "type": "system",
201
+ "message": f"{disconnected_username} left the room",
202
+ "timestamp": datetime.now().isoformat(),
203
+ "room": room
204
+ }
205
+ await manager.broadcast_to_room(room, leave_message)
206
+
207
+ @app.get("/api/rooms")
208
+ async def get_active_rooms():
209
+ """Get list of active chat rooms"""
210
+ rooms = []
211
+ for room_name, connections in manager.active_connections.items():
212
+ if connections: # Only include rooms with active connections
213
+ rooms.append({
214
+ "name": room_name,
215
+ "user_count": len(connections),
216
+ "users": [conn["username"] for conn in connections]
217
+ })
218
+ return {"rooms": rooms}
219
+
220
+ @app.get("/api/rooms/{room}/users")
221
+ async def get_room_users(room: str):
222
+ """Get list of users in a specific room"""
223
+ users = manager.get_room_users(room)
224
+ return {
225
+ "room": room,
226
+ "users": users,
227
+ "user_count": len(users)
228
+ }
229
+
230
+
231
+
232
+ if __name__ == "__main__":
233
+ port = int(os.getenv("PORT", 7860)) # Hugging Face expects 7860
234
+ uvicorn.run(app, host="0.0.0.0", port=port)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi==0.104.1
2
+ uvicorn[standard]==0.24.0
3
+ websockets==12.0
4
+ python-multipart==0.0.6
5
+ jinja2==3.1.2
static/main.css ADDED
@@ -0,0 +1,908 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :root {
2
+ --primary-bg: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
3
+ --secondary-bg: rgba(255, 255, 255, 0.8);
4
+ --card-bg: rgba(255, 255, 255, 0.9);
5
+ --text-primary: #1a202c;
6
+ --text-secondary: #4a5568;
7
+ --text-muted: #718096;
8
+ --border-color: rgba(0, 0, 0, 0.08);
9
+ --accent-primary: #3b82f6;
10
+ --accent-secondary: #60a5fa;
11
+ --accent-success: #10b981;
12
+ --accent-danger: #ef4444;
13
+ --shadow-light: 0 1px 3px rgba(0, 0, 0, 0.05);
14
+ --shadow-medium: 0 4px 12px rgba(0, 0, 0, 0.1);
15
+ --shadow-heavy: 0 8px 24px rgba(0, 0, 0, 0.12);
16
+ --blur-light: blur(10px);
17
+ --blur-medium: blur(20px);
18
+ }
19
+
20
+ * {
21
+ margin: 0;
22
+ padding: 0;
23
+ box-sizing: border-box;
24
+ }
25
+
26
+ body {
27
+ font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
28
+ background: var(--primary-bg);
29
+ color: var(--text-primary);
30
+ height: 100vh;
31
+ display: flex;
32
+ flex-direction: column;
33
+ font-size: 14px;
34
+ line-height: 1.5;
35
+ transition: all 0.3s ease;
36
+ }
37
+
38
+ .header {
39
+ background: var(--card-bg);
40
+ backdrop-filter: var(--blur-medium);
41
+ padding: 1.5rem;
42
+ border-bottom: 1px solid var(--border-color);
43
+ box-shadow: var(--shadow-light);
44
+ position: relative;
45
+ }
46
+
47
+ .header::before {
48
+ content: '';
49
+ position: absolute;
50
+ top: 0;
51
+ left: 0;
52
+ right: 0;
53
+ height: 3px;
54
+ background: linear-gradient(90deg, var(--accent-primary), var(--accent-secondary));
55
+ }
56
+
57
+ .header-top {
58
+ display: flex;
59
+ justify-content: space-between;
60
+ align-items: center;
61
+ margin-bottom: 1.5rem;
62
+ }
63
+
64
+ .logo {
65
+ display: flex;
66
+ align-items: center;
67
+ gap: 0.75rem;
68
+ }
69
+
70
+ .logo i {
71
+ font-size: 1.5rem;
72
+ color: var(--accent-primary);
73
+ }
74
+
75
+ .header h1 {
76
+ color: var(--text-primary);
77
+ font-size: 1.5rem;
78
+ font-weight: 700;
79
+ letter-spacing: -0.025em;
80
+ }
81
+
82
+ .connection-form {
83
+ display: flex;
84
+ gap: 1rem;
85
+ align-items: center;
86
+ flex-wrap: wrap;
87
+ }
88
+
89
+ .input-group {
90
+ display: flex;
91
+ flex-direction: column;
92
+ gap: 0.25rem;
93
+ min-width: 180px;
94
+ }
95
+
96
+ .input-group label {
97
+ font-size: 0.75rem;
98
+ font-weight: 500;
99
+ color: var(--text-muted);
100
+ text-transform: uppercase;
101
+ letter-spacing: 0.05em;
102
+ }
103
+
104
+ .connection-form input {
105
+ padding: 0.75rem 1rem;
106
+ border: 1px solid var(--border-color);
107
+ border-radius: 12px;
108
+ background: var(--secondary-bg);
109
+ backdrop-filter: var(--blur-light);
110
+ color: var(--text-primary);
111
+ font-size: 0.875rem;
112
+ transition: all 0.2s ease;
113
+ outline: none;
114
+ }
115
+
116
+ .connection-form input:focus {
117
+ border-color: var(--accent-primary);
118
+ box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
119
+ }
120
+
121
+ .connection-form input::placeholder {
122
+ color: var(--text-muted);
123
+ }
124
+
125
+ .btn {
126
+ padding: 0.75rem 1.5rem;
127
+ border: none;
128
+ border-radius: 12px;
129
+ background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
130
+ color: white;
131
+ cursor: pointer;
132
+ font-weight: 600;
133
+ font-size: 0.875rem;
134
+ transition: all 0.2s ease;
135
+ position: relative;
136
+ overflow: hidden;
137
+ display: flex;
138
+ align-items: center;
139
+ gap: 0.5rem;
140
+ box-shadow: var(--shadow-light);
141
+ }
142
+
143
+ .btn::before {
144
+ content: '';
145
+ position: absolute;
146
+ top: 0;
147
+ left: -100%;
148
+ width: 100%;
149
+ height: 100%;
150
+ background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
151
+ transition: left 0.5s;
152
+ }
153
+
154
+ .btn:hover::before {
155
+ left: 100%;
156
+ }
157
+
158
+ .btn:hover {
159
+ transform: translateY(-2px);
160
+ box-shadow: var(--shadow-medium);
161
+ }
162
+
163
+ .btn:active {
164
+ transform: translateY(0);
165
+ }
166
+
167
+ .btn:disabled {
168
+ background: var(--text-muted);
169
+ cursor: not-allowed;
170
+ transform: none;
171
+ opacity: 0.6;
172
+ }
173
+
174
+ .btn-secondary {
175
+ background: transparent;
176
+ border: 1px solid var(--border-color);
177
+ color: var(--text-secondary);
178
+ }
179
+
180
+ .btn-secondary:hover {
181
+ background: var(--secondary-bg);
182
+ }
183
+
184
+ .is-hidden {
185
+ display: none !important;
186
+ }
187
+
188
+ .main-content {
189
+ display: flex;
190
+ flex: 1;
191
+ overflow: hidden;
192
+ gap: 1px;
193
+ }
194
+
195
+ .sidebar {
196
+ width: 280px;
197
+ background: var(--card-bg);
198
+ backdrop-filter: var(--blur-light);
199
+ border-right: 1px solid var(--border-color);
200
+ display: flex;
201
+ flex-direction: column;
202
+ transition: all 0.3s ease;
203
+ }
204
+
205
+ .sidebar-header {
206
+ padding: 1.5rem;
207
+ border-bottom: 1px solid var(--border-color);
208
+ }
209
+
210
+ .sidebar-header h3 {
211
+ color: var(--text-primary);
212
+ font-size: 1rem;
213
+ font-weight: 600;
214
+ display: flex;
215
+ align-items: center;
216
+ gap: 0.5rem;
217
+ }
218
+
219
+ .sidebar-header i {
220
+ color: var(--accent-success);
221
+ }
222
+
223
+ .sidebar-content {
224
+ flex: 1;
225
+ padding: 1rem;
226
+ overflow-y: auto;
227
+ }
228
+
229
+ .user-list {
230
+ list-style: none;
231
+ display: flex;
232
+ flex-direction: column;
233
+ gap: 0.5rem;
234
+ }
235
+
236
+ .user-list li {
237
+ padding: 0.75rem 1rem;
238
+ color: var(--text-secondary);
239
+ background: var(--secondary-bg);
240
+ border-radius: 8px;
241
+ transition: all 0.2s ease;
242
+ display: flex;
243
+ align-items: center;
244
+ gap: 0.75rem;
245
+ }
246
+
247
+ .user-list li:hover {
248
+ background: var(--card-bg);
249
+ transform: translateX(2px);
250
+ }
251
+
252
+ .user-list li::before {
253
+ content: '';
254
+ width: 8px;
255
+ height: 8px;
256
+ border-radius: 50%;
257
+ background: var(--accent-success);
258
+ flex-shrink: 0;
259
+ }
260
+
261
+ .chat-container {
262
+ flex: 1;
263
+ display: flex;
264
+ flex-direction: column;
265
+ background: var(--secondary-bg);
266
+ backdrop-filter: var(--blur-light);
267
+ }
268
+
269
+ .messages {
270
+ flex: 1;
271
+ overflow-y: auto;
272
+ padding: 1.5rem;
273
+ display: flex;
274
+ flex-direction: column;
275
+ gap: 1rem;
276
+ scroll-behavior: smooth;
277
+ }
278
+
279
+ .messages::-webkit-scrollbar {
280
+ width: 6px;
281
+ }
282
+
283
+ .messages::-webkit-scrollbar-track {
284
+ background: transparent;
285
+ }
286
+
287
+ .messages::-webkit-scrollbar-thumb {
288
+ background: var(--border-color);
289
+ border-radius: 3px;
290
+ }
291
+
292
+ .messages::-webkit-scrollbar-thumb:hover {
293
+ background: var(--text-muted);
294
+ }
295
+
296
+ .message {
297
+ background: var(--card-bg);
298
+ backdrop-filter: var(--blur-light);
299
+ border-radius: 16px;
300
+ padding: 1rem 1.25rem;
301
+ max-width: 75%;
302
+ border: 1px solid var(--border-color);
303
+ box-shadow: var(--shadow-light);
304
+ position: relative;
305
+ animation: messageSlide 0.3s ease-out;
306
+ }
307
+
308
+ @keyframes messageSlide {
309
+ from {
310
+ opacity: 0;
311
+ transform: translateY(10px);
312
+ }
313
+ to {
314
+ opacity: 1;
315
+ transform: translateY(0);
316
+ }
317
+ }
318
+
319
+ .message.own {
320
+ align-self: flex-end;
321
+ background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
322
+ color: white;
323
+ border: none;
324
+ }
325
+
326
+ .message.own .username,
327
+ .message.own .timestamp {
328
+ color: rgba(255, 255, 255, 0.9);
329
+ }
330
+
331
+ .message.system {
332
+ align-self: center;
333
+ background: var(--secondary-bg);
334
+ border: 1px solid var(--border-color);
335
+ font-style: italic;
336
+ color: var(--text-muted);
337
+ max-width: 60%;
338
+ text-align: center;
339
+ font-size: 0.8rem;
340
+ }
341
+
342
+ .message-header {
343
+ display: flex;
344
+ justify-content: space-between;
345
+ align-items: center;
346
+ margin-bottom: 0.5rem;
347
+ font-size: 0.75rem;
348
+ }
349
+
350
+ .username {
351
+ font-weight: 600;
352
+ color: var(--accent-primary);
353
+ }
354
+
355
+ .message.own .username {
356
+ color: rgba(255, 255, 255, 0.9);
357
+ }
358
+
359
+ .timestamp {
360
+ color: var(--text-muted);
361
+ font-size: 0.7rem;
362
+ }
363
+
364
+ .message-content {
365
+ word-wrap: break-word;
366
+ line-height: 1.4;
367
+ }
368
+
369
+ .file-preview img {
370
+ max-width: 200px;
371
+ max-height: 150px;
372
+ border-radius: 8px;
373
+ margin-top: 0.75rem;
374
+ box-shadow: var(--shadow-light);
375
+ }
376
+
377
+ .file-download {
378
+ display: inline-flex;
379
+ align-items: center;
380
+ gap: 0.5rem;
381
+ margin-top: 0.75rem;
382
+ padding: 0.5rem 1rem;
383
+ background: var(--secondary-bg);
384
+ border-radius: 8px;
385
+ color: var(--accent-primary);
386
+ text-decoration: none;
387
+ font-size: 0.8rem;
388
+ transition: all 0.2s ease;
389
+ border: 1px solid var(--border-color);
390
+ }
391
+
392
+ .file-download:hover {
393
+ background: var(--card-bg);
394
+ transform: translateY(-1px);
395
+ box-shadow: var(--shadow-light);
396
+ }
397
+
398
+ .file-summary {
399
+ display: flex;
400
+ align-items: center;
401
+ gap: 0.5rem;
402
+ margin-bottom: 0.5rem;
403
+ }
404
+
405
+ .file-size {
406
+ color: var(--text-muted);
407
+ font-size: 0.8rem;
408
+ margin-bottom: 0.5rem;
409
+ }
410
+
411
+ .input-area {
412
+ padding: 1.5rem;
413
+ background: var(--card-bg);
414
+ backdrop-filter: var(--blur-medium);
415
+ border-top: 1px solid var(--border-color);
416
+ }
417
+
418
+ .input-row {
419
+ display: flex;
420
+ gap: 1rem;
421
+ align-items: flex-end;
422
+ }
423
+
424
+ #messageInput {
425
+ flex: 1;
426
+ padding: 1rem 1.25rem;
427
+ border: 1px solid var(--border-color);
428
+ border-radius: 20px;
429
+ background: var(--secondary-bg);
430
+ backdrop-filter: var(--blur-light);
431
+ color: var(--text-primary);
432
+ resize: none;
433
+ outline: none;
434
+ transition: all 0.2s ease;
435
+ font-family: inherit;
436
+ max-height: 100px;
437
+ min-height: 44px;
438
+ }
439
+
440
+ #messageInput:focus {
441
+ border-color: var(--accent-primary);
442
+ box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
443
+ }
444
+
445
+ #messageInput::placeholder {
446
+ color: var(--text-muted);
447
+ }
448
+
449
+ .file-input-wrapper {
450
+ position: relative;
451
+ }
452
+
453
+ .file-input-wrapper input[type=file] {
454
+ position: absolute;
455
+ opacity: 0;
456
+ width: 100%;
457
+ height: 100%;
458
+ cursor: pointer;
459
+ }
460
+
461
+ .file-input-label {
462
+ display: flex;
463
+ align-items: center;
464
+ justify-content: center;
465
+ width: 44px;
466
+ height: 44px;
467
+ background: var(--secondary-bg);
468
+ border: 1px solid var(--border-color);
469
+ border-radius: 50%;
470
+ cursor: pointer;
471
+ transition: all 0.2s ease;
472
+ color: var(--text-secondary);
473
+ }
474
+
475
+ .file-input-label:hover {
476
+ background: var(--accent-primary);
477
+ color: white;
478
+ transform: translateY(-2px);
479
+ box-shadow: var(--shadow-light);
480
+ }
481
+
482
+ .status {
483
+ padding: 0.75rem 1rem;
484
+ text-align: center;
485
+ font-size: 0.8rem;
486
+ border-radius: 8px;
487
+ margin-top: 1rem;
488
+ font-weight: 500;
489
+ }
490
+
491
+ .status.connected {
492
+ color: var(--accent-success);
493
+ background: rgba(16, 185, 129, 0.1);
494
+ border: 1px solid rgba(16, 185, 129, 0.2);
495
+ }
496
+
497
+ .status.disconnected {
498
+ color: var(--accent-danger);
499
+ background: rgba(239, 68, 68, 0.1);
500
+ border: 1px solid rgba(239, 68, 68, 0.2);
501
+ }
502
+
503
+ .status.neutral {
504
+ color: var(--text-muted);
505
+ background: var(--secondary-bg);
506
+ border: 1px solid var(--border-color);
507
+ }
508
+
509
+ /* Mobile optimizations */
510
+ @media (max-width: 768px) {
511
+ .header {
512
+ padding: 1rem;
513
+ }
514
+
515
+ .header-top {
516
+ margin-bottom: 1rem;
517
+ }
518
+
519
+ .header h1 {
520
+ font-size: 1.25rem;
521
+ }
522
+
523
+ .connection-form {
524
+ flex-direction: column;
525
+ gap: 0.75rem;
526
+ width: 100%;
527
+ }
528
+
529
+ .input-group {
530
+ width: 100%;
531
+ min-width: auto;
532
+ }
533
+
534
+ .connection-form input,
535
+ .btn {
536
+ width: 100%;
537
+ }
538
+
539
+ .main-content {
540
+ flex-direction: column;
541
+ height: calc(100vh - 200px);
542
+ }
543
+
544
+ .sidebar {
545
+ width: 100%;
546
+ height: 120px;
547
+ flex-direction: row;
548
+ border-right: none;
549
+ border-bottom: 1px solid var(--border-color);
550
+ }
551
+
552
+ .sidebar-header {
553
+ min-width: 120px;
554
+ border-right: 1px solid var(--border-color);
555
+ border-bottom: none;
556
+ }
557
+
558
+ .sidebar-header h3 {
559
+ font-size: 0.9rem;
560
+ }
561
+
562
+ .sidebar-content {
563
+ overflow-x: auto;
564
+ overflow-y: hidden;
565
+ }
566
+
567
+ .user-list {
568
+ flex-direction: row;
569
+ white-space: nowrap;
570
+ padding-bottom: 0.5rem;
571
+ }
572
+
573
+ .user-list li {
574
+ flex-shrink: 0;
575
+ font-size: 0.8rem;
576
+ padding: 0.5rem 0.75rem;
577
+ }
578
+
579
+ .message {
580
+ max-width: 90%;
581
+ padding: 0.75rem 1rem;
582
+ }
583
+
584
+ .messages {
585
+ padding: 1rem;
586
+ }
587
+
588
+ .input-area {
589
+ padding: 1rem;
590
+ }
591
+
592
+ .input-row {
593
+ gap: 0.75rem;
594
+ }
595
+ }
596
+
597
+ @media (max-width: 480px) {
598
+ .header {
599
+ padding: 0.75rem;
600
+ }
601
+
602
+ .messages {
603
+ padding: 0.75rem;
604
+ gap: 0.75rem;
605
+ }
606
+
607
+ .message {
608
+ padding: 0.75rem;
609
+ border-radius: 12px;
610
+ }
611
+
612
+ .input-area {
613
+ padding: 0.75rem;
614
+ }
615
+ }
616
+
617
+ * {
618
+ transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
619
+ }
620
+
621
+ :root {
622
+ --mobile-safe-bottom: env(safe-area-inset-bottom, 0px);
623
+ }
624
+
625
+ html,
626
+ body {
627
+ min-height: 100%;
628
+ overflow: hidden;
629
+ }
630
+
631
+ button,
632
+ input,
633
+ textarea {
634
+ font: inherit;
635
+ }
636
+
637
+ .mobile-users-toggle {
638
+ display: none;
639
+ }
640
+
641
+ .mobile-drawer-backdrop {
642
+ display: none;
643
+ }
644
+
645
+ @media (max-width: 768px) {
646
+ body {
647
+ height: 100dvh;
648
+ min-height: 100dvh;
649
+ }
650
+
651
+ .header {
652
+ z-index: 20;
653
+ padding: 0.85rem;
654
+ }
655
+
656
+ .header-top {
657
+ gap: 0.75rem;
658
+ margin-bottom: 0.75rem;
659
+ }
660
+
661
+ .logo {
662
+ min-width: 0;
663
+ }
664
+
665
+ .header h1 {
666
+ overflow: hidden;
667
+ text-overflow: ellipsis;
668
+ white-space: nowrap;
669
+ }
670
+
671
+ .mobile-users-toggle {
672
+ width: 42px;
673
+ height: 42px;
674
+ border-radius: 50%;
675
+ flex: 0 0 auto;
676
+ display: inline-flex;
677
+ align-items: center;
678
+ justify-content: center;
679
+ }
680
+
681
+ .mobile-users-toggle {
682
+ background: var(--secondary-bg);
683
+ border: 1px solid var(--border-color);
684
+ color: var(--text-secondary);
685
+ cursor: pointer;
686
+ transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.3s ease;
687
+ }
688
+
689
+ .mobile-users-toggle:active {
690
+ transform: scale(0.96);
691
+ }
692
+
693
+ .connection-form {
694
+ display: grid;
695
+ grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
696
+ gap: 0.65rem;
697
+ align-items: end;
698
+ }
699
+
700
+ .connection-form .btn {
701
+ min-height: 44px;
702
+ justify-content: center;
703
+ grid-column: span 2;
704
+ }
705
+
706
+ .connection-form input {
707
+ min-height: 44px;
708
+ font-size: 16px;
709
+ }
710
+
711
+ .status {
712
+ margin-top: 0.75rem;
713
+ padding: 0.6rem 0.75rem;
714
+ text-align: left;
715
+ }
716
+
717
+ .main-content {
718
+ height: auto;
719
+ min-height: 0;
720
+ flex: 1;
721
+ position: relative;
722
+ }
723
+
724
+ .chat-container {
725
+ min-width: 0;
726
+ min-height: 0;
727
+ }
728
+
729
+ .messages {
730
+ min-height: 0;
731
+ padding: 0.85rem;
732
+ padding-bottom: 1rem;
733
+ }
734
+
735
+ .message,
736
+ .message.system {
737
+ max-width: min(92%, 34rem);
738
+ border-radius: 14px;
739
+ }
740
+
741
+ .message-header {
742
+ gap: 0.75rem;
743
+ }
744
+
745
+ .message-content {
746
+ overflow-wrap: anywhere;
747
+ }
748
+
749
+ .file-preview img {
750
+ max-width: 100%;
751
+ height: auto;
752
+ }
753
+
754
+ .sidebar {
755
+ position: fixed;
756
+ top: 0;
757
+ right: 0;
758
+ bottom: 0;
759
+ z-index: 40;
760
+ width: min(84vw, 320px);
761
+ height: auto;
762
+ flex-direction: column;
763
+ transform: translateX(105%);
764
+ border-left: 1px solid var(--border-color);
765
+ border-right: 0;
766
+ border-bottom: 0;
767
+ box-shadow: var(--shadow-heavy);
768
+ transition: transform 0.25s ease;
769
+ }
770
+
771
+ body.mobile-users-open .sidebar {
772
+ transform: translateX(0);
773
+ }
774
+
775
+ .sidebar-header {
776
+ min-width: 0;
777
+ padding: 1rem;
778
+ border-right: 0;
779
+ border-bottom: 1px solid var(--border-color);
780
+ }
781
+
782
+ .sidebar-content {
783
+ overflow-y: auto;
784
+ overflow-x: hidden;
785
+ padding: 0.85rem;
786
+ }
787
+
788
+ .user-list {
789
+ flex-direction: column;
790
+ white-space: normal;
791
+ padding-bottom: 0;
792
+ }
793
+
794
+ .user-list li {
795
+ width: 100%;
796
+ min-height: 42px;
797
+ }
798
+
799
+ .mobile-drawer-backdrop {
800
+ position: fixed;
801
+ inset: 0;
802
+ z-index: 35;
803
+ display: block;
804
+ pointer-events: none;
805
+ background: rgba(15, 23, 42, 0);
806
+ transition: background-color 0.25s ease;
807
+ }
808
+
809
+ body.mobile-users-open .mobile-drawer-backdrop {
810
+ pointer-events: auto;
811
+ background: rgba(15, 23, 42, 0.42);
812
+ }
813
+
814
+ .input-area {
815
+ padding: 0.75rem;
816
+ padding-bottom: calc(0.75rem + var(--mobile-safe-bottom));
817
+ }
818
+
819
+ .input-row {
820
+ display: grid;
821
+ grid-template-columns: 44px minmax(0, 1fr) 48px;
822
+ gap: 0.55rem;
823
+ align-items: end;
824
+ }
825
+
826
+ #messageInput {
827
+ min-height: 44px;
828
+ max-height: 132px;
829
+ padding: 0.78rem 0.95rem;
830
+ border-radius: 18px;
831
+ font-size: 16px;
832
+ }
833
+
834
+ #sendBtn {
835
+ width: 48px;
836
+ height: 44px;
837
+ min-width: 48px;
838
+ padding: 0;
839
+ border-radius: 50%;
840
+ justify-content: center;
841
+ }
842
+
843
+ #sendBtn span {
844
+ display: none;
845
+ }
846
+ }
847
+
848
+ @media (max-width: 480px) {
849
+ .header {
850
+ padding: 0.7rem;
851
+ }
852
+
853
+ .connection-form {
854
+ grid-template-columns: 1fr;
855
+ }
856
+
857
+ .connection-form .btn {
858
+ grid-column: auto;
859
+ }
860
+
861
+ .input-group label {
862
+ font-size: 0.68rem;
863
+ }
864
+
865
+ .status {
866
+ font-size: 0.75rem;
867
+ }
868
+
869
+ .messages {
870
+ padding: 0.7rem;
871
+ }
872
+
873
+ .message {
874
+ max-width: 94%;
875
+ padding: 0.72rem 0.82rem;
876
+ }
877
+
878
+ .message.system {
879
+ max-width: 92%;
880
+ }
881
+
882
+ .file-download {
883
+ width: 100%;
884
+ justify-content: center;
885
+ }
886
+ }
887
+
888
+ @media (max-width: 360px) {
889
+ .logo i {
890
+ display: none;
891
+ }
892
+
893
+ .mobile-users-toggle {
894
+ width: 40px;
895
+ height: 40px;
896
+ }
897
+
898
+ .input-row {
899
+ grid-template-columns: 40px minmax(0, 1fr) 44px;
900
+ }
901
+
902
+ .file-input-label,
903
+ #sendBtn {
904
+ width: 40px;
905
+ min-width: 40px;
906
+ }
907
+ }
908
+
static/main.js ADDED
@@ -0,0 +1,417 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class TriChat {
2
+ constructor() {
3
+ this.ws = null;
4
+ this.username = '';
5
+ this.room = 'global';
6
+ this.isConnected = false;
7
+
8
+ this.initElements();
9
+ this.bindEvents();
10
+ this.setupMessageInputResize();
11
+ }
12
+
13
+ initElements() {
14
+ this.elements = {
15
+ usernameInput: document.getElementById('usernameInput'),
16
+ roomInput: document.getElementById('roomInput'),
17
+ connectBtn: document.getElementById('connectBtn'),
18
+ disconnectBtn: document.getElementById('disconnectBtn'),
19
+ status: document.getElementById('status'),
20
+ messages: document.getElementById('messages'),
21
+ messageInput: document.getElementById('messageInput'),
22
+ sendBtn: document.getElementById('sendBtn'),
23
+ fileInput: document.getElementById('fileInput'),
24
+ inputArea: document.getElementById('inputArea'),
25
+ userList: document.getElementById('userList')
26
+ };
27
+ }
28
+
29
+ setupMessageInputResize() {
30
+ this.elements.messageInput.addEventListener('input', () => {
31
+ this.elements.messageInput.style.height = 'auto';
32
+ this.elements.messageInput.style.height = Math.min(this.elements.messageInput.scrollHeight, 100) + 'px';
33
+ });
34
+ }
35
+
36
+ bindEvents() {
37
+ this.elements.connectBtn.addEventListener('click', () => this.connect());
38
+ this.elements.disconnectBtn.addEventListener('click', () => this.disconnect());
39
+ this.elements.sendBtn.addEventListener('click', () => this.sendMessage());
40
+ this.elements.fileInput.addEventListener('change', (e) => this.handleFileSelect(e));
41
+
42
+ this.elements.messageInput.addEventListener('keypress', (e) => {
43
+ if (e.key === 'Enter' && !e.shiftKey) {
44
+ e.preventDefault();
45
+ this.sendMessage();
46
+ }
47
+ });
48
+
49
+ this.elements.usernameInput.addEventListener('keypress', (e) => {
50
+ if (e.key === 'Enter') {
51
+ this.connect();
52
+ }
53
+ });
54
+ }
55
+
56
+ async connect() {
57
+ const username = this.elements.usernameInput.value.trim();
58
+ const room = this.elements.roomInput.value.trim() || 'global';
59
+
60
+ if (!username) {
61
+ this.showNotification('Please enter your name', 'error');
62
+ this.elements.usernameInput.focus();
63
+ return;
64
+ }
65
+
66
+ this.username = username;
67
+ this.room = room;
68
+
69
+ try {
70
+ this.updateStatus('Connecting...', 'neutral');
71
+
72
+ const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
73
+ const wsUrl = `${protocol}//${window.location.host}/ws/${room}?username=${encodeURIComponent(username)}`;
74
+
75
+ this.ws = new WebSocket(wsUrl);
76
+
77
+ this.ws.onopen = () => {
78
+ this.isConnected = true;
79
+ this.updateUI();
80
+ this.updateStatus(`Connected to ${room}`, 'connected');
81
+ this.showNotification('Successfully connected!', 'success');
82
+ };
83
+
84
+ this.ws.onmessage = (event) => {
85
+ const message = JSON.parse(event.data);
86
+ this.displayMessage(message);
87
+ if (message.type === 'user_list') {
88
+ this.updateUserList(message.users);
89
+ }
90
+ };
91
+
92
+ this.ws.onclose = () => {
93
+ this.isConnected = false;
94
+ this.updateUI();
95
+ this.updateStatus('Disconnected', 'disconnected');
96
+ };
97
+
98
+ this.ws.onerror = (error) => {
99
+ console.error('WebSocket error:', error);
100
+ this.updateStatus('Connection error', 'disconnected');
101
+ this.showNotification('Connection failed', 'error');
102
+ };
103
+
104
+ } catch (error) {
105
+ console.error('Connection failed:', error);
106
+ this.updateStatus('Failed to connect', 'disconnected');
107
+ this.showNotification('Failed to connect', 'error');
108
+ }
109
+ }
110
+
111
+ disconnect() {
112
+ if (this.ws) {
113
+ this.ws.close();
114
+ }
115
+ }
116
+
117
+ updateUI() {
118
+ if (this.isConnected) {
119
+ this.elements.connectBtn.classList.add('is-hidden');
120
+ this.elements.disconnectBtn.classList.remove('is-hidden');
121
+ this.elements.inputArea.classList.remove('is-hidden');
122
+ this.elements.usernameInput.disabled = true;
123
+ this.elements.roomInput.disabled = true;
124
+ this.elements.messageInput.focus();
125
+ } else {
126
+ this.elements.connectBtn.classList.remove('is-hidden');
127
+ this.elements.disconnectBtn.classList.add('is-hidden');
128
+ this.elements.inputArea.classList.add('is-hidden');
129
+ this.elements.usernameInput.disabled = false;
130
+ this.elements.roomInput.disabled = false;
131
+ this.elements.messages.innerHTML = '';
132
+ this.elements.userList.innerHTML = '';
133
+ }
134
+ }
135
+
136
+ updateStatus(text, type) {
137
+ this.elements.status.innerHTML = `
138
+ <i class="fas ${type === 'connected' ? 'fa-check-circle' : type === 'disconnected' ? 'fa-times-circle' : 'fa-info-circle'}"></i>
139
+ ${text}
140
+ `;
141
+ this.elements.status.className = 'status ' + type;
142
+ }
143
+
144
+ showNotification(message, type) {
145
+ // Simple notification - you could enhance this with a toast library
146
+ console.log(`${type.toUpperCase()}: ${message}`);
147
+ }
148
+
149
+ updateUserList(users) {
150
+ this.elements.userList.innerHTML = '';
151
+ users.forEach(user => {
152
+ const li = document.createElement('li');
153
+ li.textContent = user;
154
+ this.elements.userList.appendChild(li);
155
+ });
156
+ }
157
+
158
+ sendMessage() {
159
+ const text = this.elements.messageInput.value.trim();
160
+ if (!text || !this.isConnected) return;
161
+
162
+ const message = {
163
+ type: 'text',
164
+ username: this.username,
165
+ text: text,
166
+ timestamp: new Date().toISOString(),
167
+ room: this.room
168
+ };
169
+
170
+ this.ws.send(JSON.stringify(message));
171
+ this.elements.messageInput.value = '';
172
+ this.elements.messageInput.style.height = 'auto';
173
+ }
174
+
175
+ async handleFileSelect(event) {
176
+ const file = event.target.files[0];
177
+ if (!file) return;
178
+
179
+ if (file.size > 5 * 1024 * 1024) {
180
+ this.showNotification('File size must be less than 5MB', 'error');
181
+ return;
182
+ }
183
+
184
+ try {
185
+ const base64Data = await this.fileToBase64(file);
186
+
187
+ const message = {
188
+ type: 'file',
189
+ username: this.username,
190
+ fileName: file.name,
191
+ fileType: file.type,
192
+ fileSize: file.size,
193
+ fileData: base64Data,
194
+ timestamp: new Date().toISOString(),
195
+ room: this.room
196
+ };
197
+
198
+ this.ws.send(JSON.stringify(message));
199
+ event.target.value = '';
200
+ this.showNotification('File uploaded successfully', 'success');
201
+
202
+ } catch (error) {
203
+ console.error('File upload error:', error);
204
+ this.showNotification('Failed to upload file', 'error');
205
+ }
206
+ }
207
+
208
+ fileToBase64(file) {
209
+ return new Promise((resolve, reject) => {
210
+ const reader = new FileReader();
211
+ reader.onload = () => resolve(reader.result.split(',')[1]);
212
+ reader.onerror = reject;
213
+ reader.readAsDataURL(file);
214
+ });
215
+ }
216
+
217
+ displayMessage(message) {
218
+ const messageElement = document.createElement('div');
219
+ messageElement.className = 'message';
220
+
221
+ if (message.type === 'system') {
222
+ messageElement.className += ' system';
223
+ messageElement.innerHTML = `
224
+ <div class="message-content">
225
+ <i class="fas fa-info-circle"></i>
226
+ ${this.escapeHtml(message.message)}
227
+ </div>
228
+ `;
229
+ } else {
230
+ if (message.username === this.username) {
231
+ messageElement.className += ' own';
232
+ }
233
+
234
+ const timestamp = new Date(message.timestamp).toLocaleTimeString([], {
235
+ hour: '2-digit',
236
+ minute: '2-digit'
237
+ });
238
+
239
+ if (message.type === 'text') {
240
+ messageElement.innerHTML = `
241
+ <div class="message-header">
242
+ <span class="username">${this.escapeHtml(message.username)}</span>
243
+ <span class="timestamp">${timestamp}</span>
244
+ </div>
245
+ <div class="message-content">${this.escapeHtml(message.text)}</div>
246
+ `;
247
+ } else if (message.type === 'file') {
248
+ const downloadUrl = 'data:' + message.fileType + ';base64,' + message.fileData;
249
+ let preview = '';
250
+ let fileIcon = 'fas fa-file';
251
+
252
+ if (message.fileType.startsWith('image/')) {
253
+ preview = `<div class="file-preview"><img src="${downloadUrl}" alt="${message.fileName}"></div>`;
254
+ fileIcon = 'fas fa-image';
255
+ } else if (message.fileType.startsWith('video/')) {
256
+ fileIcon = 'fas fa-video';
257
+ } else if (message.fileType.startsWith('audio/')) {
258
+ fileIcon = 'fas fa-music';
259
+ } else if (message.fileType.includes('pdf')) {
260
+ fileIcon = 'fas fa-file-pdf';
261
+ } else if (message.fileType.includes('document') || message.fileType.includes('text')) {
262
+ fileIcon = 'fas fa-file-alt';
263
+ }
264
+
265
+ messageElement.innerHTML = `
266
+ <div class="message-header">
267
+ <span class="username">${this.escapeHtml(message.username)}</span>
268
+ <span class="timestamp">${timestamp}</span>
269
+ </div>
270
+ <div class="message-content">
271
+ <div class="file-summary">
272
+ <i class="${fileIcon}"></i>
273
+ <strong>${this.escapeHtml(message.fileName)}</strong>
274
+ </div>
275
+ <div class="file-size">
276
+ ${this.formatFileSize(message.fileSize)}
277
+ </div>
278
+ ${preview}
279
+ <a href="${downloadUrl}" download="${message.fileName}" class="file-download">
280
+ <i class="fas fa-download"></i>
281
+ Download
282
+ </a>
283
+ </div>
284
+ `;
285
+ }
286
+ }
287
+
288
+ this.elements.messages.appendChild(messageElement);
289
+ this.elements.messages.scrollTop = this.elements.messages.scrollHeight;
290
+
291
+ // Add subtle animation
292
+ messageElement.style.opacity = '0';
293
+ messageElement.style.transform = 'translateY(10px)';
294
+
295
+ requestAnimationFrame(() => {
296
+ messageElement.style.transition = 'opacity 0.3s ease, transform 0.3s ease';
297
+ messageElement.style.opacity = '1';
298
+ messageElement.style.transform = 'translateY(0)';
299
+ });
300
+ }
301
+
302
+ escapeHtml(text) {
303
+ const div = document.createElement('div');
304
+ div.textContent = text;
305
+ return div.innerHTML;
306
+ }
307
+
308
+ formatFileSize(bytes) {
309
+ if (bytes === 0) return '0 Bytes';
310
+ const k = 1024;
311
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
312
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
313
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
314
+ }
315
+ }
316
+
317
+ // Initialize the chat application
318
+ document.addEventListener('DOMContentLoaded', () => {
319
+ const triChat = new TriChat();
320
+
321
+ // Add some welcome messages for demo
322
+ if (!triChat.isConnected) {
323
+ setTimeout(() => {
324
+ const welcomeMessage = document.createElement('div');
325
+ welcomeMessage.className = 'message system';
326
+ welcomeMessage.innerHTML = `
327
+ <div class="message-content">
328
+ <i class="fas fa-rocket"></i>
329
+ Welcome to Tri-Chat! Connect with your name to start chatting.
330
+ </div>
331
+ `;
332
+ triChat.elements.messages.appendChild(welcomeMessage);
333
+ }, 500);
334
+ }
335
+ });
336
+
337
+ (function () {
338
+ const mobileQuery = window.matchMedia('(max-width: 768px)');
339
+
340
+ function setupMobileControls() {
341
+ const headerTop = document.querySelector('.header-top');
342
+ const sidebar = document.querySelector('.sidebar');
343
+ const sendBtn = document.getElementById('sendBtn');
344
+
345
+ if (!headerTop || !sidebar) return;
346
+
347
+ let usersToggle = document.querySelector('.mobile-users-toggle');
348
+ if (!usersToggle) {
349
+ usersToggle = document.createElement('button');
350
+ usersToggle.type = 'button';
351
+ usersToggle.className = 'mobile-users-toggle';
352
+ usersToggle.setAttribute('aria-label', 'Show online users');
353
+ usersToggle.setAttribute('aria-controls', 'onlineUsersPanel');
354
+ usersToggle.setAttribute('aria-expanded', 'false');
355
+ usersToggle.innerHTML = '<i class="fas fa-users"></i>';
356
+ headerTop.appendChild(usersToggle);
357
+ }
358
+
359
+ let backdrop = document.querySelector('.mobile-drawer-backdrop');
360
+ if (!backdrop) {
361
+ backdrop = document.createElement('div');
362
+ backdrop.className = 'mobile-drawer-backdrop';
363
+ document.body.appendChild(backdrop);
364
+ }
365
+
366
+ sidebar.id = 'onlineUsersPanel';
367
+
368
+ if (sendBtn && !sendBtn.querySelector('span')) {
369
+ const label = Array.from(sendBtn.childNodes).find((node) => node.nodeType === Node.TEXT_NODE && node.textContent.trim());
370
+ if (label) {
371
+ const text = label.textContent.trim();
372
+ label.textContent = '';
373
+ const span = document.createElement('span');
374
+ span.textContent = text;
375
+ sendBtn.appendChild(span);
376
+ }
377
+ }
378
+
379
+ const closeDrawer = () => {
380
+ document.body.classList.remove('mobile-users-open');
381
+ usersToggle.setAttribute('aria-expanded', 'false');
382
+ };
383
+
384
+ const toggleDrawer = () => {
385
+ const nextState = !document.body.classList.contains('mobile-users-open');
386
+ document.body.classList.toggle('mobile-users-open', nextState);
387
+ usersToggle.setAttribute('aria-expanded', String(nextState));
388
+ };
389
+
390
+ usersToggle.addEventListener('click', toggleDrawer);
391
+ backdrop.addEventListener('click', closeDrawer);
392
+ document.addEventListener('keydown', (event) => {
393
+ if (event.key === 'Escape') closeDrawer();
394
+ });
395
+
396
+ const userList = document.getElementById('userList');
397
+ if (userList) {
398
+ userList.addEventListener('click', () => {
399
+ if (mobileQuery.matches) closeDrawer();
400
+ });
401
+ }
402
+ }
403
+
404
+ function syncViewportState() {
405
+ document.body.classList.toggle('is-mobile-layout', mobileQuery.matches);
406
+ if (!mobileQuery.matches) {
407
+ document.body.classList.remove('mobile-users-open');
408
+ }
409
+ }
410
+
411
+ document.addEventListener('DOMContentLoaded', () => {
412
+ setupMobileControls();
413
+ syncViewportState();
414
+ mobileQuery.addEventListener('change', syncViewportState);
415
+ });
416
+ })();
417
+
templates/index.html ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Tri-Chat - Premium Chat Experience</title>
7
+ <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
8
+ <link href="/static/main.css" rel="stylesheet">
9
+ </head>
10
+ <body>
11
+ <div class="header">
12
+ <div class="header-top">
13
+ <div class="logo">
14
+ <i class="fas fa-comments"></i>
15
+ <h1>Tri-Chat</h1>
16
+ </div>
17
+ </div>
18
+
19
+ <div class="connection-form">
20
+ <div class="input-group">
21
+ <label for="usernameInput">Your Name</label>
22
+ <input type="text" id="usernameInput" placeholder="Enter your name" maxlength="20">
23
+ </div>
24
+ <div class="input-group">
25
+ <label for="roomInput">Room</label>
26
+ <input type="text" id="roomInput" placeholder="global" maxlength="30">
27
+ </div>
28
+ <button class="btn" id="connectBtn">
29
+ <i class="fas fa-plug"></i>
30
+ Connect
31
+ </button>
32
+ <button class="btn btn-secondary is-hidden" id="disconnectBtn">
33
+ <i class="fas fa-sign-out-alt"></i>
34
+ Disconnect
35
+ </button>
36
+ </div>
37
+
38
+ <div class="status neutral" id="status">
39
+ <i class="fas fa-info-circle"></i>
40
+ Enter your name and click Connect to start chatting
41
+ </div>
42
+ </div>
43
+
44
+ <div class="main-content">
45
+ <div class="sidebar">
46
+ <div class="sidebar-header">
47
+ <h3>
48
+ <i class="fas fa-users"></i>
49
+ Online
50
+ </h3>
51
+ </div>
52
+ <div class="sidebar-content">
53
+ <ul class="user-list" id="userList"></ul>
54
+ </div>
55
+ </div>
56
+
57
+ <div class="chat-container">
58
+ <div class="messages" id="messages"></div>
59
+
60
+ <div class="input-area is-hidden" id="inputArea">
61
+ <div class="input-row">
62
+ <div class="file-input-wrapper">
63
+ <input type="file" id="fileInput" accept="*/*">
64
+ <label for="fileInput" class="file-input-label">
65
+ <i class="fas fa-paperclip"></i>
66
+ </label>
67
+ </div>
68
+ <textarea id="messageInput" placeholder="Type your message..." maxlength="500" rows="1"></textarea>
69
+ <button class="btn" id="sendBtn">
70
+ <i class="fas fa-paper-plane"></i>
71
+ Send
72
+ </button>
73
+ </div>
74
+ </div>
75
+ </div>
76
+ </div>
77
+ <script src="/static/main.js"></script>
78
+ </body>
79
+ </html>
80
+
81
+