Eluza133 commited on
Commit
6fa8354
·
verified ·
1 Parent(s): 5403bcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +327 -115
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  import logging
5
  import threading
6
  import time
7
- from datetime import datetime
8
  from huggingface_hub import HfApi, hf_hub_download
9
  from werkzeug.utils import secure_filename
10
  import random
@@ -35,12 +35,14 @@ def load_data():
35
  if 'general_chat' not in data:
36
  data['general_chat'] = []
37
  if 'private_chats' not in data:
38
- data['private_chats'] = {} # Dictionary for private chats
39
  for user in data['users']:
40
  if 'last_chat_visit' not in data['users'][user]:
41
  data['users'][user]['last_chat_visit'] = '1970-01-01 00:00:00'
42
  if 'last_private_visit' not in data['users'][user]:
43
- data['users'][user]['last_private_visit'] = '1970-01-01 00:00:00' # For private messages
 
 
44
  logging.info("Data loaded successfully")
45
  return data
46
  except Exception as e:
@@ -111,6 +113,17 @@ def get_private_unread_count(data, username):
111
  unread += sum(1 for msg in messages if datetime.strptime(msg['time'], '%Y-%m-%d %H:%M:%S') > last_visit and msg['sender'] != username)
112
  return unread
113
 
 
 
 
 
 
 
 
 
 
 
 
114
  # Updated base style
115
  BASE_STYLE = '''
116
  :root {
@@ -126,6 +139,8 @@ BASE_STYLE = '''
126
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
127
  --glass-bg: rgba(255, 255, 255, 0.15);
128
  --transition: all 0.4s ease-in-out;
 
 
129
  }
130
  * { margin: 0; padding: 0; box-sizing: border-box; }
131
  body {
@@ -327,6 +342,19 @@ BASE_STYLE = '''
327
  background: var(--accent);
328
  color: white;
329
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
330
  @keyframes zoomIn {
331
  from { opacity: 0; transform: scale(0.9); }
332
  to { opacity: 1; transform: scale(1); }
@@ -379,7 +407,7 @@ NAV_HTML = '''
379
  <nav class="nav-links">
380
  <a href="{{ url_for('feed') }}" class="nav-link"><span>📜</span> Feed</a>
381
  {% if is_authenticated %}
382
- <a href="{{ url_for('profile') }}" class="nav-link"><span>👤</span> Profile ({{ username }})</a>
383
  <a href="{{ url_for('upload') }}" class="nav-link"><span>⬆️</span> Upload</a>
384
  <a href="{{ url_for('chat') }}" class="nav-link"><span>💬</span> Chat {% if unread_count > 0 %}<span class="badge">{{ unread_count }}</span>{% endif %}</a>
385
  <a href="{{ url_for('users') }}" class="nav-link"><span>👥</span> Users <span class="badge">{{ user_count }}</span></a>
@@ -407,7 +435,15 @@ def register():
407
  flash('User already exists!')
408
  return redirect(url_for('register'))
409
 
410
- data['users'][username] = {'password': password, 'bio': '', 'link': '', 'avatar': None, 'last_chat_visit': '1970-01-01 00:00:00', 'last_private_visit': '1970-01-01 00:00:00'}
 
 
 
 
 
 
 
 
411
  save_data(data)
412
  flash('Registration successful! Please log in.')
413
  return redirect(url_for('login'))
@@ -418,6 +454,7 @@ def register():
418
  unread_count = get_unread_count(data, username) if is_authenticated else 0
419
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
420
  user_count = len(data['users'])
 
421
 
422
  html = '''
423
  <!DOCTYPE html>
@@ -507,29 +544,32 @@ def register():
507
  </body>
508
  </html>
509
  '''
510
- return render_template_string(html, is_authenticated=is_authenticated, username=username, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
511
 
512
  # Login
513
  @app.route('/login', methods=['GET', 'POST'])
514
  def login():
 
515
  if request.method == 'POST':
516
  username = request.form.get('username')
517
  password = request.form.get('password')
518
- data = load_data()
519
 
520
  if username in data['users'] and data['users'][username]['password'] == password:
521
  session['username'] = username
522
  session.permanent = True
 
523
  return redirect(url_for('feed'))
524
  flash('Invalid username or password!')
525
  return redirect(url_for('login'))
526
 
527
  is_authenticated = 'username' in session
528
  username = session.get('username', None)
529
- data = load_data()
 
530
  unread_count = get_unread_count(data, username) if is_authenticated else 0
531
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
532
  user_count = len(data['users'])
 
533
 
534
  html = '''
535
  <!DOCTYPE html>
@@ -618,11 +658,15 @@ def login():
618
  </body>
619
  </html>
620
  '''
621
- return render_template_string(html, is_authenticated=is_authenticated, username=username, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
622
 
623
  # Logout
624
  @app.route('/logout')
625
  def logout():
 
 
 
 
626
  session.pop('username', None)
627
  return redirect(url_for('feed'))
628
 
@@ -641,12 +685,15 @@ def increment_view(post_id):
641
  @app.route('/', methods=['GET', 'POST'])
642
  def feed():
643
  data = load_data()
 
 
 
644
  posts = sorted(data.get('posts', []), key=lambda x: datetime.strptime(x['upload_date'], '%Y-%m-%d %H:%M:%S'), reverse=True)
645
  is_authenticated = 'username' in session
646
- username = session.get('username', None)
647
  unread_count = get_unread_count(data, username) if is_authenticated else 0
648
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
649
  user_count = len(data['users'])
 
650
 
651
  search_query = request.form.get('search', '').strip().lower() if request.method == 'POST' else request.args.get('search', '').strip().lower()
652
  if search_query:
@@ -780,7 +827,7 @@ def feed():
780
  {% endif %}
781
  <h2>{{ post['title'] }}</h2>
782
  <p>{{ post['description'] }}</p>
783
- <p>By: <a href="{{ url_for('user_profile', username=post['uploader']) }}" class="username-link">{{ post['uploader'] }}</a> | {{ post['upload_date'] }}</p>
784
  <p class="stats">Views: {{ post['views'] }} | Likes: {{ post['likes']|length }}</p>
785
  </a>
786
  {% endfor %}
@@ -825,21 +872,24 @@ def feed():
825
  </body>
826
  </html>
827
  '''
828
- return render_template_string(html, posts=posts, is_authenticated=is_authenticated, username=username, repo_id=REPO_ID, search_query=search_query, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
829
 
830
  # Post page
831
  @app.route('/post/<post_id>', methods=['GET', 'POST'])
832
  def post_page(post_id):
833
  data = load_data()
 
 
 
834
  post = next((p for p in data['posts'] if p['id'] == post_id), None)
835
  if not post:
836
  return "Post not found", 404
837
 
838
  is_authenticated = 'username' in session
839
- username = session.get('username', None)
840
  unread_count = get_unread_count(data, username) if is_authenticated else 0
841
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
842
  user_count = len(data['users'])
 
843
  post['views'] = post.get('views', 0) + 1
844
  save_data(data)
845
 
@@ -938,7 +988,7 @@ def post_page(post_id):
938
  <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ post['type'] }}s/{{ post['filename'] }}" alt="{{ post['title'] }}" onclick="openModal(this.src)">
939
  {% endif %}
940
  <p>{{ post['description'] }}</p>
941
- <p>By: <a href="{{ url_for('user_profile', username=post['uploader']) }}" class="username-link">{{ post['uploader'] }}</a> | {{ post['upload_date'] }}</p>
942
  <p>Views: {{ post['views'] }} | Likes: {{ post['likes']|length }}</p>
943
  {% if is_authenticated %}
944
  <form method="POST" style="display: inline;">
@@ -954,7 +1004,7 @@ def post_page(post_id):
954
  <h3 style="margin-top: 35px; font-size: 1.6em;">Comments</h3>
955
  {% for comment in post.get('comments', []) %}
956
  <div class="comment">
957
- <p><strong><a href="{{ url_for('user_profile', username=comment['user']) }}" class="username-link">{{ comment['user'] }}</a></strong> ({{ comment['date'] }}): {{ comment['text'] }}</p>
958
  </div>
959
  {% endfor %}
960
  <a href="{{ url_for('feed') }}" class="btn">Back to Feed</a>
@@ -991,7 +1041,7 @@ def post_page(post_id):
991
  </body>
992
  </html>
993
  '''
994
- return render_template_string(html, post=post, repo_id=REPO_ID, is_authenticated=is_authenticated, username=username, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
995
 
996
  # User profile
997
  @app.route('/profile', methods=['GET', 'POST'])
@@ -1002,11 +1052,13 @@ def profile():
1002
 
1003
  data = load_data()
1004
  username = session['username']
 
1005
  user_posts = sorted([p for p in data['posts'] if p['uploader'] == username], key=lambda x: datetime.strptime(x['upload_date'], '%Y-%m-%d %H:%M:%S'), reverse=True)
1006
  is_authenticated = 'username' in session
1007
  unread_count = get_unread_count(data, username)
1008
  private_unread_count = get_private_unread_count(data, username)
1009
  user_count = len(data['users'])
 
1010
 
1011
  total_views = sum(post.get('views', 0) for post in user_posts)
1012
  total_likes = sum(len(post.get('likes', [])) for post in user_posts)
@@ -1014,6 +1066,7 @@ def profile():
1014
  bio = user_data.get('bio', '')
1015
  link = user_data.get('link', '')
1016
  avatar = user_data.get('avatar', None)
 
1017
 
1018
  if request.method == 'POST':
1019
  if 'delete_post' in request.form:
@@ -1163,7 +1216,10 @@ def profile():
1163
  <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ avatar }}" alt="Avatar" class="avatar">
1164
  {% endif %}
1165
  <div class="profile-info">
1166
- <h1>{{ username }}</h1>
 
 
 
1167
  <p>{{ bio }}</p>
1168
  {% if link %}
1169
  <p><a href="{{ link }}" target="_blank" style="color: var(--primary);">{{ link }}</a></p>
@@ -1252,27 +1308,32 @@ def profile():
1252
  </body>
1253
  </html>
1254
  '''
1255
- return render_template_string(html, username=username, user_posts=user_posts, total_views=total_views, total_likes=total_likes, bio=bio, link=link, avatar=avatar, repo_id=REPO_ID, is_authenticated=is_authenticated, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
1256
 
1257
  # Other user's profile
1258
  @app.route('/profile/<username>')
1259
  def user_profile(username):
1260
  data = load_data()
 
 
 
1261
  if username not in data['users']:
1262
  return "User not found", 404
1263
 
1264
  user_posts = sorted([p for p in data['posts'] if p['uploader'] == username], key=lambda x: datetime.strptime(x['upload_date'], '%Y-%m-%d %H:%M:%S'), reverse=True)
1265
  is_authenticated = 'username' in session
1266
- session_username = session.get('username', None)
1267
  unread_count = get_unread_count(data, session_username) if is_authenticated else 0
1268
  private_unread_count = get_private_unread_count(data, session_username) if is_authenticated else 0
1269
  user_count = len(data['users'])
 
1270
  total_views = sum(post.get('views', 0) for post in user_posts)
1271
  total_likes = sum(len(post.get('likes', [])) for post in user_posts)
1272
  user_data = data['users'].get(username, {})
1273
  bio = user_data.get('bio', '')
1274
  link = user_data.get('link', '')
1275
  avatar = user_data.get('avatar', None)
 
 
1276
 
1277
  html = '''
1278
  <!DOCTYPE html>
@@ -1382,7 +1443,10 @@ def user_profile(username):
1382
  <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ avatar }}" alt="Avatar" class="avatar">
1383
  {% endif %}
1384
  <div class="profile-info">
1385
- <h1>{{ username }}</h1>
 
 
 
1386
  <p>{{ bio }}</p>
1387
  {% if link %}
1388
  <p><a href="{{ link }}" target="_blank" style="color: var(--primary);">{{ link }}</a></p>
@@ -1459,7 +1523,7 @@ def user_profile(username):
1459
  </body>
1460
  </html>
1461
  '''
1462
- return render_template_string(html, username=username, user_posts=user_posts, total_views=total_views, total_likes=total_likes, bio=bio, link=link, avatar=avatar, repo_id=REPO_ID, is_authenticated=is_authenticated, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
1463
 
1464
  # Upload content
1465
  @app.route('/upload', methods=['GET', 'POST'])
@@ -1470,9 +1534,11 @@ def upload():
1470
 
1471
  data = load_data()
1472
  username = session.get('username', None)
 
1473
  unread_count = get_unread_count(data, username)
1474
  private_unread_count = get_private_unread_count(data, username)
1475
  user_count = len(data['users'])
 
1476
 
1477
  if request.method == 'POST':
1478
  title = request.form.get('title')
@@ -1623,14 +1689,16 @@ def upload():
1623
  </body>
1624
  </html>
1625
  '''
1626
- return render_template_string(html, username=username, is_authenticated=is_authenticated, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
1627
 
1628
  # General Chat
1629
  @app.route('/chat', methods=['GET', 'POST'])
1630
  def chat():
1631
  data = load_data()
1632
- is_authenticated = 'username' in session
1633
  username = session.get('username', None)
 
 
 
1634
  chat_messages = data['general_chat']
1635
 
1636
  if is_authenticated:
@@ -1640,6 +1708,7 @@ def chat():
1640
  unread_count = get_unread_count(data, username) if is_authenticated else 0
1641
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
1642
  user_count = len(data['users'])
 
1643
 
1644
  if request.method == 'POST' and is_authenticated:
1645
  message = request.form.get('message')
@@ -1697,7 +1766,8 @@ def chat():
1697
  <title>General Chat - Content Hub</title>
1698
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
1699
  <style>
1700
- ''' + BASE_STYLE + '''
 
1701
  .container {
1702
  max-width: 900px;
1703
  background: var(--card-bg-light);
@@ -1799,6 +1869,7 @@ def chat():
1799
  {% for message in chat_messages %}
1800
  <div class="message">
1801
  <a href="{{ url_for('user_profile', username=message['sender']) }}" class="sender">{{ message['sender'] }}</a>
 
1802
  <span class="time">({{ message['time'] }})</span>:
1803
  {% if 'text' in message %}
1804
  <p>{{ message['text'] }}</p>
@@ -1812,63 +1883,63 @@ def chat():
1812
  <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" alt="Chat file" onclick="openModal(this.src)">
1813
  {% endif %}
1814
  {% endif %}
1815
- {% if 'post_id' in message %}
1816
- {% with post = posts|selectattr('id', 'equalto', message['post_id'])|first %}
1817
- {% if post %}
1818
- <p>Shared: <a href="{{ url_for('post_page', post_id=message['post_id']) }}">{{ post['title'] }}</a></p>
 
 
1819
  {% endif %}
1820
- {% endwith %}
1821
- {% endif %}
1822
  </div>
1823
- {% endfor %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1824
  </div>
1825
- {% if is_authenticated %}
1826
- <form method="POST" enctype="multipart/form-data" class="message-form">
1827
- <textarea name="message" placeholder="Type a message" rows="2"></textarea>
1828
- <input type="file" name="file" accept="video/*,image/*">
1829
- <select name="post_id" class="post-select">
1830
- <option value="">Share a post (optional)</option>
1831
- {% for post in user_posts %}
1832
- <option value="{{ post['id'] }}">{{ post['title'] }}</option>
1833
- {% endfor %}
1834
- </select>
1835
- <button type="submit" class="btn">Send</button>
1836
- </form>
1837
- {% else %}
1838
- <p style="margin-top: 15px;"><a href="{{ url_for('login') }}">Login</a> to send messages.</p>
1839
- {% endif %}
1840
- </div>
1841
- <div class="modal" id="imageModal" onclick="closeModal(event)">
1842
- <img id="modalImage" src="">
1843
- </div>
1844
- <script>
1845
- function toggleSidebar() {
1846
- document.getElementById('sidebar').classList.toggle('active');
1847
- }
1848
- function toggleTheme() {
1849
- document.body.classList.toggle('dark');
1850
- localStorage.setItem('theme', document.body.classList.contains('dark') ? 'dark' : 'light');
1851
- }
1852
- function openModal(src) {
1853
- const modal = document.getElementById('imageModal');
1854
- const modalImg = document.getElementById('modalImage');
1855
- modal.style.display = 'flex';
1856
- modalImg.src = src;
1857
- }
1858
- function closeModal(event) {
1859
- if (event.target.tagName !== 'IMG') {
1860
- document.getElementById('imageModal').style.display = 'none';
1861
  }
1862
- }
1863
- window.onload = () => {
1864
- if (localStorage.getItem('theme') === 'dark') document.body.classList.add('dark');
1865
- const messagesDiv = document.getElementById('messages');
1866
- messagesDiv.scrollTop = messagesDiv.scrollHeight;
1867
- };
1868
- </script>
1869
- </body>
1870
- </html>
1871
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1872
  user_posts = [p for p in data['posts'] if p['uploader'] == username] if is_authenticated else []
1873
  return render_template_string(html,
1874
  chat_messages=chat_messages,
@@ -1879,18 +1950,27 @@ def chat():
1879
  user_posts=user_posts,
1880
  unread_count=unread_count,
1881
  user_count=user_count,
1882
- private_unread_count=private_unread_count)
 
 
1883
 
1884
  # Users page
1885
- @app.route('/users', methods=['GET'])
1886
  def users():
1887
  data = load_data()
1888
- is_authenticated = 'username' in session
1889
  username = session.get('username', None)
 
 
 
1890
  unread_count = get_unread_count(data, username) if is_authenticated else 0
1891
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
1892
  user_count = len(data['users'])
1893
- user_list = [(u, data['users'][u].get('avatar')) for u in data['users']]
 
 
 
 
 
1894
 
1895
  html = '''
1896
  <!DOCTYPE html>
@@ -1922,6 +2002,33 @@ def users():
1922
  -webkit-background-clip: text;
1923
  color: transparent;
1924
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1925
  .user-grid {
1926
  display: grid;
1927
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
@@ -1978,8 +2085,14 @@ def users():
1978
  <button class="theme-toggle" onclick="toggleTheme()">🌙</button>
1979
  <div class="container">
1980
  <h1>Users ({{ user_count }})</h1>
 
 
 
 
 
 
1981
  <div class="user-grid">
1982
- {% for user, avatar in user_list %}
1983
  {% if user != username %}
1984
  <div class="user-item">
1985
  {% if avatar %}
@@ -1989,6 +2102,7 @@ def users():
1989
  {% endif %}
1990
  <div class="user-info">
1991
  <a href="{{ url_for('user_profile', username=user) }}">{{ user }}</a>
 
1992
  {% if is_authenticated %}
1993
  <br><a href="{{ url_for('private_chat', recipient=user) }}" class="btn message-btn">Send Message</a>
1994
  {% endif %}
@@ -2013,7 +2127,7 @@ def users():
2013
  </body>
2014
  </html>
2015
  '''
2016
- return render_template_string(html, user_list=user_list, username=username, is_authenticated=is_authenticated, repo_id=REPO_ID, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
2017
 
2018
  # Messages page (list of private dialogs)
2019
  @app.route('/messages', methods=['GET'])
@@ -2024,10 +2138,12 @@ def messages():
2024
 
2025
  data = load_data()
2026
  username = session['username']
 
2027
  is_authenticated = 'username' in session
2028
  unread_count = get_unread_count(data, username)
2029
  private_unread_count = get_private_unread_count(data, username)
2030
  user_count = len(data['users'])
 
2031
 
2032
  dialogs = {}
2033
  for chat_key, messages in data['private_chats'].items():
@@ -2035,7 +2151,12 @@ def messages():
2035
  other_user = user1 if user2 == username else user2
2036
  last_message = messages[-1] if messages else None
2037
  unread = sum(1 for msg in messages if datetime.strptime(msg['time'], '%Y-%m-%d %H:%M:%S') > datetime.strptime(data['users'][username]['last_private_visit'], '%Y-%m-%d %H:%M:%S') and msg['sender'] != username)
2038
- dialogs[other_user] = {'last_message': last_message, 'unread': unread}
 
 
 
 
 
2039
 
2040
  html = '''
2041
  <!DOCTYPE html>
@@ -2080,8 +2201,8 @@ def messages():
2080
  box-shadow: var(--shadow);
2081
  transition: var(--transition);
2082
  display: flex;
2083
- justify-content: space-between;
2084
  align-items: center;
 
2085
  }
2086
  body.dark .dialog-item {
2087
  background: var(--card-bg-dark);
@@ -2089,6 +2210,16 @@ def messages():
2089
  .dialog-item:hover {
2090
  transform: translateY(-5px);
2091
  }
 
 
 
 
 
 
 
 
 
 
2092
  .dialog-info a {
2093
  color: var(--primary);
2094
  font-weight: 600;
@@ -2116,8 +2247,14 @@ def messages():
2116
  <div class="dialog-list">
2117
  {% for other_user, info in dialogs.items() %}
2118
  <div class="dialog-item">
 
 
 
 
 
2119
  <div class="dialog-info">
2120
  <a href="{{ url_for('private_chat', recipient=other_user) }}">{{ other_user }}</a>
 
2121
  {% if info.last_message %}
2122
  <p style="font-size: 0.9em; color: rgba(0, 0, 0, 0.6);">{{ info.last_message['time'] }}:
2123
  {% if 'text' in info.last_message %}{{ info.last_message['text']|truncate(30) }}{% elif 'file' in info.last_message %}File{% elif 'post_id' in info.last_message %}Post{% endif %}</p>
@@ -2148,7 +2285,7 @@ def messages():
2148
  </body>
2149
  </html>
2150
  '''
2151
- return render_template_string(html, dialogs=dialogs, username=username, is_authenticated=is_authenticated, repo_id=REPO_ID, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
2152
 
2153
  # Private Chat
2154
  @app.route('/chat/<recipient>', methods=['GET', 'POST'])
@@ -2159,6 +2296,7 @@ def private_chat(recipient):
2159
 
2160
  data = load_data()
2161
  username = session['username']
 
2162
  if recipient not in data['users']:
2163
  return "User not found", 404
2164
 
@@ -2166,6 +2304,7 @@ def private_chat(recipient):
2166
  unread_count = get_unread_count(data, username)
2167
  private_unread_count = get_private_unread_count(data, username)
2168
  user_count = len(data['users'])
 
2169
 
2170
  # Update last_private_visit
2171
  data['users'][username]['last_private_visit'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
@@ -2222,6 +2361,9 @@ def private_chat(recipient):
2222
 
2223
  return redirect(url_for('private_chat', recipient=recipient))
2224
 
 
 
 
2225
  html = '''
2226
  <!DOCTYPE html>
2227
  <html lang="en">
@@ -2251,6 +2393,16 @@ def private_chat(recipient):
2251
  background: linear-gradient(135deg, var(--primary), var(--accent));
2252
  -webkit-background-clip: text;
2253
  color: transparent;
 
 
 
 
 
 
 
 
 
 
2254
  }
2255
  .messages {
2256
  max-height: 500px;
@@ -2262,19 +2414,40 @@ def private_chat(recipient):
2262
  scroll-behavior: smooth;
2263
  }
2264
  .message {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2265
  padding: 12px 18px;
2266
  background: var(--card-bg-light);
2267
  border-radius: 12px;
2268
- margin-bottom: 12px;
2269
  font-size: 1em;
2270
  transition: var(--transition);
 
2271
  }
2272
- body.dark .message {
2273
  background: var(--card-bg-dark);
2274
  }
2275
- .message:hover {
 
 
 
 
 
 
 
 
2276
  transform: translateY(-2px);
2277
- box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
2278
  }
2279
  .message .sender {
2280
  color: var(--primary);
@@ -2288,9 +2461,10 @@ def private_chat(recipient):
2288
  .message .time {
2289
  font-size: 0.85em;
2290
  color: rgba(0, 0, 0, 0.6);
2291
- margin-left: 10px;
 
2292
  }
2293
- body.dark .message .time {
2294
  color: rgba(255, 255, 255, 0.6);
2295
  }
2296
  .message img, .message video {
@@ -2322,6 +2496,13 @@ def private_chat(recipient):
2322
  body.dark .post-select {
2323
  color: var(--text-dark);
2324
  }
 
 
 
 
 
 
 
2325
  </style>
2326
  </head>
2327
  <body>
@@ -2329,31 +2510,54 @@ def private_chat(recipient):
2329
  ''' + NAV_HTML + '''
2330
  <button class="theme-toggle" onclick="toggleTheme()">🌙</button>
2331
  <div class="container">
2332
- <h1>Chat with {{ recipient }}</h1>
 
 
 
 
 
 
 
2333
  <div class="messages" id="messages">
2334
  {% for message in private_messages %}
2335
- <div class="message">
2336
- <a href="{{ url_for('user_profile', username=message['sender']) }}" class="sender">{{ message['sender'] }}</a>
2337
- <span class="time">({{ message['time'] }})</span>:
2338
- {% if 'text' in message %}
2339
- <p>{{ message['text'] }}</p>
2340
- {% endif %}
2341
- {% if 'file' in message %}
2342
- {% if message['file_type'] == 'video' %}
2343
- <video controls>
2344
- <source src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" type="video/mp4">
2345
- </video>
2346
  {% else %}
2347
- <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" alt="Chat file" onclick="openModal(this.src)">
 
 
 
 
 
 
2348
  {% endif %}
2349
  {% endif %}
2350
- {% if 'post_id' in message %}
2351
- {% with post = posts|selectattr('id', 'equalto', message['post_id'])|first %}
2352
- {% if post %}
2353
- <p>Shared: <a href="{{ url_for('post_page', post_id=message['post_id']) }}">{{ post['title'] }}</a></p>
 
 
 
 
 
 
 
 
 
2354
  {% endif %}
2355
- {% endwith %}
2356
- {% endif %}
 
 
 
 
 
 
 
 
2357
  </div>
2358
  {% endfor %}
2359
  </div>
@@ -2412,18 +2616,26 @@ def private_chat(recipient):
2412
  user_posts=user_posts,
2413
  unread_count=unread_count,
2414
  user_count=user_count,
2415
- private_unread_count=private_unread_count)
 
 
 
 
 
2416
 
2417
  # Admin panel
2418
  @app.route('/admhosto', methods=['GET', 'POST'])
2419
  def admin_panel():
2420
  data = load_data()
 
 
 
2421
  posts = sorted(data.get('posts', []), key=lambda x: datetime.strptime(x['upload_date'], '%Y-%m-%d %H:%M:%S'), reverse=True)
2422
  is_authenticated = 'username' in session
2423
- username = session.get('username', None)
2424
  unread_count = get_unread_count(data, username) if is_authenticated else 0
2425
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
2426
  user_count = len(data['users'])
 
2427
 
2428
  search_query = request.form.get('search', '').strip().lower() if request.method == 'POST' and 'search' in request.form else request.args.get('search', '').strip().lower()
2429
  if search_query:
@@ -2560,7 +2772,7 @@ def admin_panel():
2560
  <h2>{{ post['title'] }}</h2>
2561
  </a>
2562
  <p>{{ post['description'] }}</p>
2563
- <p>By: <a href="{{ url_for('user_profile', username=post['uploader']) }}" class="username-link">{{ post['uploader'] }}</a> | {{ post['upload_date'] }}</p>
2564
  <form method="POST">
2565
  <input type="hidden" name="post_id" value="{{ post['id'] }}">
2566
  <button type="submit" name="delete" class="btn delete-btn">Delete</button>
@@ -2611,7 +2823,7 @@ def admin_panel():
2611
  </body>
2612
  </html>
2613
  '''
2614
- return render_template_string(html, posts=posts, is_authenticated=is_authenticated, username=username, repo_id=REPO_ID, search_query=search_query, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count)
2615
 
2616
  if __name__ == '__main__':
2617
  backup_thread = threading.Thread(target=periodic_backup, daemon=True)
 
4
  import logging
5
  import threading
6
  import time
7
+ from datetime import datetime, timedelta
8
  from huggingface_hub import HfApi, hf_hub_download
9
  from werkzeug.utils import secure_filename
10
  import random
 
35
  if 'general_chat' not in data:
36
  data['general_chat'] = []
37
  if 'private_chats' not in data:
38
+ data['private_chats'] = {}
39
  for user in data['users']:
40
  if 'last_chat_visit' not in data['users'][user]:
41
  data['users'][user]['last_chat_visit'] = '1970-01-01 00:00:00'
42
  if 'last_private_visit' not in data['users'][user]:
43
+ data['users'][user]['last_private_visit'] = '1970-01-01 00:00:00'
44
+ if 'last_seen' not in data['users'][user]:
45
+ data['users'][user]['last_seen'] = '1970-01-01 00:00:00' # Track last activity
46
  logging.info("Data loaded successfully")
47
  return data
48
  except Exception as e:
 
113
  unread += sum(1 for msg in messages if datetime.strptime(msg['time'], '%Y-%m-%d %H:%M:%S') > last_visit and msg['sender'] != username)
114
  return unread
115
 
116
+ def is_user_online(data, username):
117
+ if username not in data['users']:
118
+ return False
119
+ last_seen = datetime.strptime(data['users'][username]['last_seen'], '%Y-%m-%d %H:%M:%S')
120
+ return (datetime.now() - last_seen).total_seconds() < 300 # Online if active within 5 minutes
121
+
122
+ def update_last_seen(data, username):
123
+ if username in data['users']:
124
+ data['users'][username]['last_seen'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
125
+ save_data(data)
126
+
127
  # Updated base style
128
  BASE_STYLE = '''
129
  :root {
 
139
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
140
  --glass-bg: rgba(255, 255, 255, 0.15);
141
  --transition: all 0.4s ease-in-out;
142
+ --online: #34c759; /* Green for online */
143
+ --offline: #ff3b30; /* Red for offline */
144
  }
145
  * { margin: 0; padding: 0; box-sizing: border-box; }
146
  body {
 
342
  background: var(--accent);
343
  color: white;
344
  }
345
+ .status-dot {
346
+ width: 10px;
347
+ height: 10px;
348
+ border-radius: 50%;
349
+ display: inline-block;
350
+ margin-left: 5px;
351
+ }
352
+ .online {
353
+ background: var(--online);
354
+ }
355
+ .offline {
356
+ background: var(--offline);
357
+ }
358
  @keyframes zoomIn {
359
  from { opacity: 0; transform: scale(0.9); }
360
  to { opacity: 1; transform: scale(1); }
 
407
  <nav class="nav-links">
408
  <a href="{{ url_for('feed') }}" class="nav-link"><span>📜</span> Feed</a>
409
  {% if is_authenticated %}
410
+ <a href="{{ url_for('profile') }}" class="nav-link"><span>👤</span> Profile ({{ username }}) <span class="status-dot {{ 'online' if is_online else 'offline' }}"></span></a>
411
  <a href="{{ url_for('upload') }}" class="nav-link"><span>⬆️</span> Upload</a>
412
  <a href="{{ url_for('chat') }}" class="nav-link"><span>💬</span> Chat {% if unread_count > 0 %}<span class="badge">{{ unread_count }}</span>{% endif %}</a>
413
  <a href="{{ url_for('users') }}" class="nav-link"><span>👥</span> Users <span class="badge">{{ user_count }}</span></a>
 
435
  flash('User already exists!')
436
  return redirect(url_for('register'))
437
 
438
+ data['users'][username] = {
439
+ 'password': password,
440
+ 'bio': '',
441
+ 'link': '',
442
+ 'avatar': None,
443
+ 'last_chat_visit': '1970-01-01 00:00:00',
444
+ 'last_private_visit': '1970-01-01 00:00:00',
445
+ 'last_seen': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
446
+ }
447
  save_data(data)
448
  flash('Registration successful! Please log in.')
449
  return redirect(url_for('login'))
 
454
  unread_count = get_unread_count(data, username) if is_authenticated else 0
455
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
456
  user_count = len(data['users'])
457
+ is_online = is_user_online(data, username) if is_authenticated else False
458
 
459
  html = '''
460
  <!DOCTYPE html>
 
544
  </body>
545
  </html>
546
  '''
547
+ return render_template_string(html, is_authenticated=is_authenticated, username=username, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online)
548
 
549
  # Login
550
  @app.route('/login', methods=['GET', 'POST'])
551
  def login():
552
+ data = load_data()
553
  if request.method == 'POST':
554
  username = request.form.get('username')
555
  password = request.form.get('password')
 
556
 
557
  if username in data['users'] and data['users'][username]['password'] == password:
558
  session['username'] = username
559
  session.permanent = True
560
+ update_last_seen(data, username)
561
  return redirect(url_for('feed'))
562
  flash('Invalid username or password!')
563
  return redirect(url_for('login'))
564
 
565
  is_authenticated = 'username' in session
566
  username = session.get('username', None)
567
+ if is_authenticated:
568
+ update_last_seen(data, username)
569
  unread_count = get_unread_count(data, username) if is_authenticated else 0
570
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
571
  user_count = len(data['users'])
572
+ is_online = is_user_online(data, username) if is_authenticated else False
573
 
574
  html = '''
575
  <!DOCTYPE html>
 
658
  </body>
659
  </html>
660
  '''
661
+ return render_template_string(html, is_authenticated=is_authenticated, username=username, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online)
662
 
663
  # Logout
664
  @app.route('/logout')
665
  def logout():
666
+ data = load_data()
667
+ username = session.get('username', None)
668
+ if username:
669
+ update_last_seen(data, username)
670
  session.pop('username', None)
671
  return redirect(url_for('feed'))
672
 
 
685
  @app.route('/', methods=['GET', 'POST'])
686
  def feed():
687
  data = load_data()
688
+ username = session.get('username', None)
689
+ if username:
690
+ update_last_seen(data, username)
691
  posts = sorted(data.get('posts', []), key=lambda x: datetime.strptime(x['upload_date'], '%Y-%m-%d %H:%M:%S'), reverse=True)
692
  is_authenticated = 'username' in session
 
693
  unread_count = get_unread_count(data, username) if is_authenticated else 0
694
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
695
  user_count = len(data['users'])
696
+ is_online = is_user_online(data, username) if is_authenticated else False
697
 
698
  search_query = request.form.get('search', '').strip().lower() if request.method == 'POST' else request.args.get('search', '').strip().lower()
699
  if search_query:
 
827
  {% endif %}
828
  <h2>{{ post['title'] }}</h2>
829
  <p>{{ post['description'] }}</p>
830
+ <p>By: <a href="{{ url_for('user_profile', username=post['uploader']) }}" class="username-link">{{ post['uploader'] }}</a> <span class="status-dot {{ 'online' if is_user_online(post['uploader']) else 'offline' }}"></span> | {{ post['upload_date'] }}</p>
831
  <p class="stats">Views: {{ post['views'] }} | Likes: {{ post['likes']|length }}</p>
832
  </a>
833
  {% endfor %}
 
872
  </body>
873
  </html>
874
  '''
875
+ return render_template_string(html, posts=posts, is_authenticated=is_authenticated, username=username, repo_id=REPO_ID, search_query=search_query, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online, is_user_online=lambda u: is_user_online(data, u))
876
 
877
  # Post page
878
  @app.route('/post/<post_id>', methods=['GET', 'POST'])
879
  def post_page(post_id):
880
  data = load_data()
881
+ username = session.get('username', None)
882
+ if username:
883
+ update_last_seen(data, username)
884
  post = next((p for p in data['posts'] if p['id'] == post_id), None)
885
  if not post:
886
  return "Post not found", 404
887
 
888
  is_authenticated = 'username' in session
 
889
  unread_count = get_unread_count(data, username) if is_authenticated else 0
890
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
891
  user_count = len(data['users'])
892
+ is_online = is_user_online(data, username) if is_authenticated else False
893
  post['views'] = post.get('views', 0) + 1
894
  save_data(data)
895
 
 
988
  <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ post['type'] }}s/{{ post['filename'] }}" alt="{{ post['title'] }}" onclick="openModal(this.src)">
989
  {% endif %}
990
  <p>{{ post['description'] }}</p>
991
+ <p>By: <a href="{{ url_for('user_profile', username=post['uploader']) }}" class="username-link">{{ post['uploader'] }}</a> <span class="status-dot {{ 'online' if is_user_online(post['uploader']) else 'offline' }}"></span> | {{ post['upload_date'] }}</p>
992
  <p>Views: {{ post['views'] }} | Likes: {{ post['likes']|length }}</p>
993
  {% if is_authenticated %}
994
  <form method="POST" style="display: inline;">
 
1004
  <h3 style="margin-top: 35px; font-size: 1.6em;">Comments</h3>
1005
  {% for comment in post.get('comments', []) %}
1006
  <div class="comment">
1007
+ <p><strong><a href="{{ url_for('user_profile', username=comment['user']) }}" class="username-link">{{ comment['user'] }}</a> <span class="status-dot {{ 'online' if is_user_online(comment['user']) else 'offline' }}"></span></strong> ({{ comment['date'] }}): {{ comment['text'] }}</p>
1008
  </div>
1009
  {% endfor %}
1010
  <a href="{{ url_for('feed') }}" class="btn">Back to Feed</a>
 
1041
  </body>
1042
  </html>
1043
  '''
1044
+ return render_template_string(html, post=post, repo_id=REPO_ID, is_authenticated=is_authenticated, username=username, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online, is_user_online=lambda u: is_user_online(data, u))
1045
 
1046
  # User profile
1047
  @app.route('/profile', methods=['GET', 'POST'])
 
1052
 
1053
  data = load_data()
1054
  username = session['username']
1055
+ update_last_seen(data, username)
1056
  user_posts = sorted([p for p in data['posts'] if p['uploader'] == username], key=lambda x: datetime.strptime(x['upload_date'], '%Y-%m-%d %H:%M:%S'), reverse=True)
1057
  is_authenticated = 'username' in session
1058
  unread_count = get_unread_count(data, username)
1059
  private_unread_count = get_private_unread_count(data, username)
1060
  user_count = len(data['users'])
1061
+ is_online = is_user_online(data, username)
1062
 
1063
  total_views = sum(post.get('views', 0) for post in user_posts)
1064
  total_likes = sum(len(post.get('likes', [])) for post in user_posts)
 
1066
  bio = user_data.get('bio', '')
1067
  link = user_data.get('link', '')
1068
  avatar = user_data.get('avatar', None)
1069
+ last_seen = user_data.get('last_seen', 'Never')
1070
 
1071
  if request.method == 'POST':
1072
  if 'delete_post' in request.form:
 
1216
  <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ avatar }}" alt="Avatar" class="avatar">
1217
  {% endif %}
1218
  <div class="profile-info">
1219
+ <h1>{{ username }} <span class="status-dot {{ 'online' if is_online else 'offline' }}"></span></h1>
1220
+ {% if not is_online %}
1221
+ <p style="color: rgba(0, 0, 0, 0.6);">Last seen: {{ last_seen }}</p>
1222
+ {% endif %}
1223
  <p>{{ bio }}</p>
1224
  {% if link %}
1225
  <p><a href="{{ link }}" target="_blank" style="color: var(--primary);">{{ link }}</a></p>
 
1308
  </body>
1309
  </html>
1310
  '''
1311
+ return render_template_string(html, username=username, user_posts=user_posts, total_views=total_views, total_likes=total_likes, bio=bio, link=link, avatar=avatar, repo_id=REPO_ID, is_authenticated=is_authenticated, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online, last_seen=last_seen)
1312
 
1313
  # Other user's profile
1314
  @app.route('/profile/<username>')
1315
  def user_profile(username):
1316
  data = load_data()
1317
+ session_username = session.get('username', None)
1318
+ if session_username:
1319
+ update_last_seen(data, session_username)
1320
  if username not in data['users']:
1321
  return "User not found", 404
1322
 
1323
  user_posts = sorted([p for p in data['posts'] if p['uploader'] == username], key=lambda x: datetime.strptime(x['upload_date'], '%Y-%m-%d %H:%M:%S'), reverse=True)
1324
  is_authenticated = 'username' in session
 
1325
  unread_count = get_unread_count(data, session_username) if is_authenticated else 0
1326
  private_unread_count = get_private_unread_count(data, session_username) if is_authenticated else 0
1327
  user_count = len(data['users'])
1328
+ is_online = is_user_online(data, session_username) if is_authenticated else False
1329
  total_views = sum(post.get('views', 0) for post in user_posts)
1330
  total_likes = sum(len(post.get('likes', [])) for post in user_posts)
1331
  user_data = data['users'].get(username, {})
1332
  bio = user_data.get('bio', '')
1333
  link = user_data.get('link', '')
1334
  avatar = user_data.get('avatar', None)
1335
+ profile_is_online = is_user_online(data, username)
1336
+ last_seen = user_data.get('last_seen', 'Never')
1337
 
1338
  html = '''
1339
  <!DOCTYPE html>
 
1443
  <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ avatar }}" alt="Avatar" class="avatar">
1444
  {% endif %}
1445
  <div class="profile-info">
1446
+ <h1>{{ username }} <span class="status-dot {{ 'online' if profile_is_online else 'offline' }}"></span></h1>
1447
+ {% if not profile_is_online %}
1448
+ <p style="color: rgba(0, 0, 0, 0.6);">Last seen: {{ last_seen }}</p>
1449
+ {% endif %}
1450
  <p>{{ bio }}</p>
1451
  {% if link %}
1452
  <p><a href="{{ link }}" target="_blank" style="color: var(--primary);">{{ link }}</a></p>
 
1523
  </body>
1524
  </html>
1525
  '''
1526
+ return render_template_string(html, username=username, user_posts=user_posts, total_views=total_views, total_likes=total_likes, bio=bio, link=link, avatar=avatar, repo_id=REPO_ID, is_authenticated=is_authenticated, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online, profile_is_online=profile_is_online, last_seen=last_seen)
1527
 
1528
  # Upload content
1529
  @app.route('/upload', methods=['GET', 'POST'])
 
1534
 
1535
  data = load_data()
1536
  username = session.get('username', None)
1537
+ update_last_seen(data, username)
1538
  unread_count = get_unread_count(data, username)
1539
  private_unread_count = get_private_unread_count(data, username)
1540
  user_count = len(data['users'])
1541
+ is_online = is_user_online(data, username)
1542
 
1543
  if request.method == 'POST':
1544
  title = request.form.get('title')
 
1689
  </body>
1690
  </html>
1691
  '''
1692
+ return render_template_string(html, username=username, is_authenticated=is_authenticated, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online)
1693
 
1694
  # General Chat
1695
  @app.route('/chat', methods=['GET', 'POST'])
1696
  def chat():
1697
  data = load_data()
 
1698
  username = session.get('username', None)
1699
+ if username:
1700
+ update_last_seen(data, username)
1701
+ is_authenticated = 'username' in session
1702
  chat_messages = data['general_chat']
1703
 
1704
  if is_authenticated:
 
1708
  unread_count = get_unread_count(data, username) if is_authenticated else 0
1709
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
1710
  user_count = len(data['users'])
1711
+ is_online = is_user_online(data, username) if is_authenticated else False
1712
 
1713
  if request.method == 'POST' and is_authenticated:
1714
  message = request.form.get('message')
 
1766
  <title>General Chat - Content Hub</title>
1767
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
1768
  <style>
1769
+ '''
1770
+ BASE_STYLE + '''
1771
  .container {
1772
  max-width: 900px;
1773
  background: var(--card-bg-light);
 
1869
  {% for message in chat_messages %}
1870
  <div class="message">
1871
  <a href="{{ url_for('user_profile', username=message['sender']) }}" class="sender">{{ message['sender'] }}</a>
1872
+ <span class="status-dot {{ 'online' if is_user_online(message['sender']) else 'offline' }}"></span>
1873
  <span class="time">({{ message['time'] }})</span>:
1874
  {% if 'text' in message %}
1875
  <p>{{ message['text'] }}</p>
 
1883
  <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" alt="Chat file" onclick="openModal(this.src)">
1884
  {% endif %}
1885
  {% endif %}
1886
+ {% if 'post_id' in message %}
1887
+ {% with post = posts|selectattr('id', 'equalto', message['post_id'])|first %}
1888
+ {% if post %}
1889
+ <p>Shared: <a href="{{ url_for('post_page', post_id=message['post_id']) }}">{{ post['title'] }}</a></p>
1890
+ {% endif %}
1891
+ {% endwith %}
1892
  {% endif %}
1893
+ </div>
1894
+ {% endfor %}
1895
  </div>
1896
+ {% if is_authenticated %}
1897
+ <form method="POST" enctype="multipart/form-data" class="message-form">
1898
+ <textarea name="message" placeholder="Type a message" rows="2"></textarea>
1899
+ <input type="file" name="file" accept="video/*,image/*">
1900
+ <select name="post_id" class="post-select">
1901
+ <option value="">Share a post (optional)</option>
1902
+ {% for post in user_posts %}
1903
+ <option value="{{ post['id'] }}">{{ post['title'] }}</option>
1904
+ {% endfor %}
1905
+ </select>
1906
+ <button type="submit" class="btn">Send</button>
1907
+ </form>
1908
+ {% else %}
1909
+ <p style="margin-top: 15px;"><a href="{{ url_for('login') }}">Login</a> to send messages.</p>
1910
+ {% endif %}
1911
  </div>
1912
+ <div class="modal" id="imageModal" onclick="closeModal(event)">
1913
+ <img id="modalImage" src="">
1914
+ </div>
1915
+ <script>
1916
+ function toggleSidebar() {
1917
+ document.getElementById('sidebar').classList.toggle('active');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1918
  }
1919
+ function toggleTheme() {
1920
+ document.body.classList.toggle('dark');
1921
+ localStorage.setItem('theme', document.body.classList.contains('dark') ? 'dark' : 'light');
1922
+ }
1923
+ function openModal(src) {
1924
+ const modal = document.getElementById('imageModal');
1925
+ const modalImg = document.getElementById('modalImage');
1926
+ modal.style.display = 'flex';
1927
+ modalImg.src = src;
1928
+ }
1929
+ function closeModal(event) {
1930
+ if (event.target.tagName !== 'IMG') {
1931
+ document.getElementById('imageModal').style.display = 'none';
1932
+ }
1933
+ }
1934
+ window.onload = () => {
1935
+ if (localStorage.getItem('theme') === 'dark') document.body.classList.add('dark');
1936
+ const messagesDiv = document.getElementById('messages');
1937
+ messagesDiv.scrollTop = messagesDiv.scrollHeight;
1938
+ };
1939
+ </script>
1940
+ </body>
1941
+ </html>
1942
+ '''
1943
  user_posts = [p for p in data['posts'] if p['uploader'] == username] if is_authenticated else []
1944
  return render_template_string(html,
1945
  chat_messages=chat_messages,
 
1950
  user_posts=user_posts,
1951
  unread_count=unread_count,
1952
  user_count=user_count,
1953
+ private_unread_count=private_unread_count,
1954
+ is_online=is_online,
1955
+ is_user_online=lambda u: is_user_online(data, u))
1956
 
1957
  # Users page
1958
+ @app.route('/users', methods=['GET', 'POST'])
1959
  def users():
1960
  data = load_data()
 
1961
  username = session.get('username', None)
1962
+ if username:
1963
+ update_last_seen(data, username)
1964
+ is_authenticated = 'username' in session
1965
  unread_count = get_unread_count(data, username) if is_authenticated else 0
1966
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
1967
  user_count = len(data['users'])
1968
+ is_online = is_user_online(data, username) if is_authenticated else False
1969
+ user_list = [(u, data['users'][u].get('avatar'), is_user_online(data, u)) for u in data['users']]
1970
+
1971
+ search_query = request.form.get('search', '').strip().lower() if request.method == 'POST' else ''
1972
+ if search_query:
1973
+ user_list = [u for u in user_list if search_query in u[0].lower()]
1974
 
1975
  html = '''
1976
  <!DOCTYPE html>
 
2002
  -webkit-background-clip: text;
2003
  color: transparent;
2004
  }
2005
+ .search-container {
2006
+ max-width: 700px;
2007
+ margin: 0 auto 35px;
2008
+ position: relative;
2009
+ }
2010
+ .search-input {
2011
+ padding-right: 50px;
2012
+ background: var(--card-bg-light);
2013
+ border-radius: 12px;
2014
+ box-shadow: var(--shadow);
2015
+ }
2016
+ body.dark .search-input {
2017
+ background: var(--card-bg-dark);
2018
+ }
2019
+ .search-btn {
2020
+ position: absolute;
2021
+ right: 5px;
2022
+ top: 50%;
2023
+ transform: translateY(-50%);
2024
+ background: var(--primary);
2025
+ padding: 8px 16px;
2026
+ border-radius: 10px;
2027
+ box-shadow: none;
2028
+ }
2029
+ .search-btn:hover {
2030
+ background: #e6415f;
2031
+ }
2032
  .user-grid {
2033
  display: grid;
2034
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
 
2085
  <button class="theme-toggle" onclick="toggleTheme()">🌙</button>
2086
  <div class="container">
2087
  <h1>Users ({{ user_count }})</h1>
2088
+ <div class="search-container">
2089
+ <form method="POST">
2090
+ <input type="text" name="search" class="search-input" placeholder="Search users" value="{{ search_query }}">
2091
+ <button type="submit" class="search-btn">🔍</button>
2092
+ </form>
2093
+ </div>
2094
  <div class="user-grid">
2095
+ {% for user, avatar, online in user_list %}
2096
  {% if user != username %}
2097
  <div class="user-item">
2098
  {% if avatar %}
 
2102
  {% endif %}
2103
  <div class="user-info">
2104
  <a href="{{ url_for('user_profile', username=user) }}">{{ user }}</a>
2105
+ <span class="status-dot {{ 'online' if online else 'offline' }}"></span>
2106
  {% if is_authenticated %}
2107
  <br><a href="{{ url_for('private_chat', recipient=user) }}" class="btn message-btn">Send Message</a>
2108
  {% endif %}
 
2127
  </body>
2128
  </html>
2129
  '''
2130
+ return render_template_string(html, user_list=user_list, username=username, is_authenticated=is_authenticated, repo_id=REPO_ID, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online, search_query=search_query)
2131
 
2132
  # Messages page (list of private dialogs)
2133
  @app.route('/messages', methods=['GET'])
 
2138
 
2139
  data = load_data()
2140
  username = session['username']
2141
+ update_last_seen(data, username)
2142
  is_authenticated = 'username' in session
2143
  unread_count = get_unread_count(data, username)
2144
  private_unread_count = get_private_unread_count(data, username)
2145
  user_count = len(data['users'])
2146
+ is_online = is_user_online(data, username)
2147
 
2148
  dialogs = {}
2149
  for chat_key, messages in data['private_chats'].items():
 
2151
  other_user = user1 if user2 == username else user2
2152
  last_message = messages[-1] if messages else None
2153
  unread = sum(1 for msg in messages if datetime.strptime(msg['time'], '%Y-%m-%d %H:%M:%S') > datetime.strptime(data['users'][username]['last_private_visit'], '%Y-%m-%d %H:%M:%S') and msg['sender'] != username)
2154
+ dialogs[other_user] = {
2155
+ 'last_message': last_message,
2156
+ 'unread': unread,
2157
+ 'avatar': data['users'][other_user].get('avatar'),
2158
+ 'online': is_user_online(data, other_user)
2159
+ }
2160
 
2161
  html = '''
2162
  <!DOCTYPE html>
 
2201
  box-shadow: var(--shadow);
2202
  transition: var(--transition);
2203
  display: flex;
 
2204
  align-items: center;
2205
+ gap: 15px;
2206
  }
2207
  body.dark .dialog-item {
2208
  background: var(--card-bg-dark);
 
2210
  .dialog-item:hover {
2211
  transform: translateY(-5px);
2212
  }
2213
+ .dialog-avatar {
2214
+ width: 50px;
2215
+ height: 50px;
2216
+ border-radius: 50%;
2217
+ object-fit: cover;
2218
+ box-shadow: var(--shadow);
2219
+ }
2220
+ .dialog-info {
2221
+ flex-grow: 1;
2222
+ }
2223
  .dialog-info a {
2224
  color: var(--primary);
2225
  font-weight: 600;
 
2247
  <div class="dialog-list">
2248
  {% for other_user, info in dialogs.items() %}
2249
  <div class="dialog-item">
2250
+ {% if info.avatar %}
2251
+ <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ info.avatar }}" alt="{{ other_user }} Avatar" class="dialog-avatar">
2252
+ {% else %}
2253
+ <div class="dialog-avatar" style="background: var(--primary);"></div>
2254
+ {% endif %}
2255
  <div class="dialog-info">
2256
  <a href="{{ url_for('private_chat', recipient=other_user) }}">{{ other_user }}</a>
2257
+ <span class="status-dot {{ 'online' if info.online else 'offline' }}"></span>
2258
  {% if info.last_message %}
2259
  <p style="font-size: 0.9em; color: rgba(0, 0, 0, 0.6);">{{ info.last_message['time'] }}:
2260
  {% if 'text' in info.last_message %}{{ info.last_message['text']|truncate(30) }}{% elif 'file' in info.last_message %}File{% elif 'post_id' in info.last_message %}Post{% endif %}</p>
 
2285
  </body>
2286
  </html>
2287
  '''
2288
+ return render_template_string(html, dialogs=dialogs, username=username, is_authenticated=is_authenticated, repo_id=REPO_ID, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online)
2289
 
2290
  # Private Chat
2291
  @app.route('/chat/<recipient>', methods=['GET', 'POST'])
 
2296
 
2297
  data = load_data()
2298
  username = session['username']
2299
+ update_last_seen(data, username)
2300
  if recipient not in data['users']:
2301
  return "User not found", 404
2302
 
 
2304
  unread_count = get_unread_count(data, username)
2305
  private_unread_count = get_private_unread_count(data, username)
2306
  user_count = len(data['users'])
2307
+ is_online = is_user_online(data, username)
2308
 
2309
  # Update last_private_visit
2310
  data['users'][username]['last_private_visit'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
 
2361
 
2362
  return redirect(url_for('private_chat', recipient=recipient))
2363
 
2364
+ recipient_avatar = data['users'][recipient].get('avatar')
2365
+ recipient_online = is_user_online(data, recipient)
2366
+
2367
  html = '''
2368
  <!DOCTYPE html>
2369
  <html lang="en">
 
2393
  background: linear-gradient(135deg, var(--primary), var(--accent));
2394
  -webkit-background-clip: text;
2395
  color: transparent;
2396
+ display: flex;
2397
+ align-items: center;
2398
+ gap: 15px;
2399
+ }
2400
+ .recipient-avatar {
2401
+ width: 40px;
2402
+ height: 40px;
2403
+ border-radius: 50%;
2404
+ object-fit: cover;
2405
+ box-shadow: var(--shadow);
2406
  }
2407
  .messages {
2408
  max-height: 500px;
 
2414
  scroll-behavior: smooth;
2415
  }
2416
  .message {
2417
+ display: flex;
2418
+ align-items: flex-start;
2419
+ gap: 10px;
2420
+ margin-bottom: 15px;
2421
+ max-width: 70%;
2422
+ }
2423
+ .message.sent {
2424
+ margin-left: auto;
2425
+ flex-direction: row-reverse;
2426
+ }
2427
+ .message.received {
2428
+ margin-right: auto;
2429
+ }
2430
+ .message-content {
2431
  padding: 12px 18px;
2432
  background: var(--card-bg-light);
2433
  border-radius: 12px;
 
2434
  font-size: 1em;
2435
  transition: var(--transition);
2436
+ box-shadow: var(--shadow);
2437
  }
2438
+ body.dark .message-content {
2439
  background: var(--card-bg-dark);
2440
  }
2441
+ .message.sent .message-content {
2442
+ background: var(--primary);
2443
+ color: white;
2444
+ }
2445
+ .message.received .message-content {
2446
+ background: var(--secondary);
2447
+ color: var(--text-dark);
2448
+ }
2449
+ .message:hover .message-content {
2450
  transform: translateY(-2px);
 
2451
  }
2452
  .message .sender {
2453
  color: var(--primary);
 
2461
  .message .time {
2462
  font-size: 0.85em;
2463
  color: rgba(0, 0, 0, 0.6);
2464
+ margin-top: 5px;
2465
+ display: block;
2466
  }
2467
+ body.dark .message .time, .message.sent .time {
2468
  color: rgba(255, 255, 255, 0.6);
2469
  }
2470
  .message img, .message video {
 
2496
  body.dark .post-select {
2497
  color: var(--text-dark);
2498
  }
2499
+ .message-avatar {
2500
+ width: 30px;
2501
+ height: 30px;
2502
+ border-radius: 50%;
2503
+ object-fit: cover;
2504
+ box-shadow: var(--shadow);
2505
+ }
2506
  </style>
2507
  </head>
2508
  <body>
 
2510
  ''' + NAV_HTML + '''
2511
  <button class="theme-toggle" onclick="toggleTheme()">🌙</button>
2512
  <div class="container">
2513
+ <h1>
2514
+ {% if recipient_avatar %}
2515
+ <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ recipient_avatar }}" alt="{{ recipient }} Avatar" class="recipient-avatar">
2516
+ {% else %}
2517
+ <div class="recipient-avatar" style="background: var(--primary);"></div>
2518
+ {% endif %}
2519
+ Chat with {{ recipient }} <span class="status-dot {{ 'online' if recipient_online else 'offline' }}"></span>
2520
+ </h1>
2521
  <div class="messages" id="messages">
2522
  {% for message in private_messages %}
2523
+ <div class="message {{ 'sent' if message['sender'] == username else 'received' }}">
2524
+ {% if message['sender'] == username %}
2525
+ {% if avatar %}
2526
+ <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ avatar }}" alt="Your Avatar" class="message-avatar">
 
 
 
 
 
 
 
2527
  {% else %}
2528
+ <div class="message-avatar" style="background: var(--primary);"></div>
2529
+ {% endif %}
2530
+ {% else %}
2531
+ {% if recipient_avatar %}
2532
+ <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ recipient_avatar }}" alt="{{ recipient }} Avatar" class="message-avatar">
2533
+ {% else %}
2534
+ <div class="message-avatar" style="background: var(--secondary);"></div>
2535
  {% endif %}
2536
  {% endif %}
2537
+ <div class="message-content">
2538
+ <a href="{{ url_for('user_profile', username=message['sender']) }}" class="sender">{{ message['sender'] }}</a>
2539
+ <span class="status-dot {{ 'online' if is_user_online(message['sender']) else 'offline' }}"></span>
2540
+ {% if 'text' in message %}
2541
+ <p>{{ message['text'] }}</p>
2542
+ {% endif %}
2543
+ {% if 'file' in message %}
2544
+ {% if message['file_type'] == 'video' %}
2545
+ <video controls>
2546
+ <source src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" type="video/mp4">
2547
+ </video>
2548
+ {% else %}
2549
+ <img src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ message['file'] }}" alt="Chat file" onclick="openModal(this.src)">
2550
  {% endif %}
2551
+ {% endif %}
2552
+ {% if 'post_id' in message %}
2553
+ {% with post = posts|selectattr('id', 'equalto', message['post_id'])|first %}
2554
+ {% if post %}
2555
+ <p>Shared: <a href="{{ url_for('post_page', post_id=message['post_id']) }}">{{ post['title'] }}</a></p>
2556
+ {% endif %}
2557
+ {% endwith %}
2558
+ {% endif %}
2559
+ <span class="time">{{ message['time'] }}</span>
2560
+ </div>
2561
  </div>
2562
  {% endfor %}
2563
  </div>
 
2616
  user_posts=user_posts,
2617
  unread_count=unread_count,
2618
  user_count=user_count,
2619
+ private_unread_count=private_unread_count,
2620
+ is_online=is_online,
2621
+ is_user_online=lambda u: is_user_online(data, u),
2622
+ avatar=avatar,
2623
+ recipient_avatar=recipient_avatar,
2624
+ recipient_online=recipient_online)
2625
 
2626
  # Admin panel
2627
  @app.route('/admhosto', methods=['GET', 'POST'])
2628
  def admin_panel():
2629
  data = load_data()
2630
+ username = session.get('username', None)
2631
+ if username:
2632
+ update_last_seen(data, username)
2633
  posts = sorted(data.get('posts', []), key=lambda x: datetime.strptime(x['upload_date'], '%Y-%m-%d %H:%M:%S'), reverse=True)
2634
  is_authenticated = 'username' in session
 
2635
  unread_count = get_unread_count(data, username) if is_authenticated else 0
2636
  private_unread_count = get_private_unread_count(data, username) if is_authenticated else 0
2637
  user_count = len(data['users'])
2638
+ is_online = is_user_online(data, username) if is_authenticated else False
2639
 
2640
  search_query = request.form.get('search', '').strip().lower() if request.method == 'POST' and 'search' in request.form else request.args.get('search', '').strip().lower()
2641
  if search_query:
 
2772
  <h2>{{ post['title'] }}</h2>
2773
  </a>
2774
  <p>{{ post['description'] }}</p>
2775
+ <p>By: <a href="{{ url_for('user_profile', username=post['uploader']) }}" class="username-link">{{ post['uploader'] }}</a> <span class="status-dot {{ 'online' if is_user_online(post['uploader']) else 'offline' }}"></span> | {{ post['upload_date'] }}</p>
2776
  <form method="POST">
2777
  <input type="hidden" name="post_id" value="{{ post['id'] }}">
2778
  <button type="submit" name="delete" class="btn delete-btn">Delete</button>
 
2823
  </body>
2824
  </html>
2825
  '''
2826
+ return render_template_string(html, posts=posts, is_authenticated=is_authenticated, username=username, repo_id=REPO_ID, search_query=search_query, unread_count=unread_count, user_count=user_count, private_unread_count=private_unread_count, is_online=is_online, is_user_online=lambda u: is_user_online(data, u))
2827
 
2828
  if __name__ == '__main__':
2829
  backup_thread = threading.Thread(target=periodic_backup, daemon=True)