Update app.py
Browse files
app.py
CHANGED
|
@@ -310,6 +310,7 @@ NAV_HTML = '''
|
|
| 310 |
{% else %}
|
| 311 |
<a href="{{ url_for('login') }}" class="nav-link"><span>🔑</span> Login</a>
|
| 312 |
<a href="{{ url_for('register') }}" class="nav-link"><span>✨</span> Register</a>
|
|
|
|
| 313 |
{% endif %}
|
| 314 |
</nav>
|
| 315 |
</aside>
|
|
@@ -1502,19 +1503,18 @@ def upload():
|
|
| 1502 |
# Chat
|
| 1503 |
@app.route('/chat', methods=['GET', 'POST'])
|
| 1504 |
def chat():
|
| 1505 |
-
if 'username' not in session:
|
| 1506 |
-
flash('Login to use chat!')
|
| 1507 |
-
return redirect(url_for('login'))
|
| 1508 |
-
|
| 1509 |
data = load_data()
|
| 1510 |
-
username = session['username']
|
| 1511 |
is_authenticated = 'username' in session
|
|
|
|
| 1512 |
|
| 1513 |
-
# Get user's chats
|
| 1514 |
-
|
| 1515 |
-
|
| 1516 |
-
|
| 1517 |
-
|
|
|
|
|
|
|
|
|
|
| 1518 |
if 'new_chat' in request.form:
|
| 1519 |
recipient = request.form.get('recipient')
|
| 1520 |
if recipient and recipient in data['users'] and recipient != username:
|
|
@@ -1676,21 +1676,21 @@ def chat():
|
|
| 1676 |
<div class="container">
|
| 1677 |
<div class="chat-list">
|
| 1678 |
<h1>Chats</h1>
|
| 1679 |
-
|
| 1680 |
-
<
|
| 1681 |
-
|
| 1682 |
-
|
|
|
|
|
|
|
| 1683 |
{% for chat_id, chat in user_chats.items() %}
|
| 1684 |
<div class="chat-item" onclick="window.location='{{ url_for('chat', chat_id=chat_id) }}'">
|
| 1685 |
-
{
|
| 1686 |
-
{{ participant }}
|
| 1687 |
-
{% endfor %}
|
| 1688 |
</div>
|
| 1689 |
{% endfor %}
|
| 1690 |
</div>
|
| 1691 |
<div class="chat-area">
|
| 1692 |
{% if selected_chat %}
|
| 1693 |
-
<h1>Chat
|
| 1694 |
<div class="messages">
|
| 1695 |
{% for message in selected_chat['messages'] %}
|
| 1696 |
<div class="message">
|
|
@@ -1714,20 +1714,24 @@ def chat():
|
|
| 1714 |
</div>
|
| 1715 |
{% endfor %}
|
| 1716 |
</div>
|
| 1717 |
-
|
| 1718 |
-
<
|
| 1719 |
-
|
| 1720 |
-
|
| 1721 |
-
|
| 1722 |
-
<
|
| 1723 |
-
|
| 1724 |
-
|
| 1725 |
-
|
| 1726 |
-
|
| 1727 |
-
|
| 1728 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1729 |
{% else %}
|
| 1730 |
-
<h1>Select a chat to
|
| 1731 |
{% endif %}
|
| 1732 |
</div>
|
| 1733 |
</div>
|
|
@@ -1760,7 +1764,7 @@ def chat():
|
|
| 1760 |
</body>
|
| 1761 |
</html>
|
| 1762 |
'''
|
| 1763 |
-
user_posts = [p for p in data['posts'] if p['uploader'] == username]
|
| 1764 |
return render_template_string(html,
|
| 1765 |
user_chats=user_chats,
|
| 1766 |
selected_chat=selected_chat,
|
|
|
|
| 310 |
{% else %}
|
| 311 |
<a href="{{ url_for('login') }}" class="nav-link"><span>🔑</span> Login</a>
|
| 312 |
<a href="{{ url_for('register') }}" class="nav-link"><span>✨</span> Register</a>
|
| 313 |
+
<a href="{{ url_for('chat') }}" class="nav-link"><span>💬</span> Chat (View Only)</a>
|
| 314 |
{% endif %}
|
| 315 |
</nav>
|
| 316 |
</aside>
|
|
|
|
| 1503 |
# Chat
|
| 1504 |
@app.route('/chat', methods=['GET', 'POST'])
|
| 1505 |
def chat():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1506 |
data = load_data()
|
|
|
|
| 1507 |
is_authenticated = 'username' in session
|
| 1508 |
+
username = session.get('username', None)
|
| 1509 |
|
| 1510 |
+
# Get user's chats if authenticated, otherwise show all chats
|
| 1511 |
+
if is_authenticated:
|
| 1512 |
+
user_chats = {chat_id: chat for chat_id, chat in data['chats'].items()
|
| 1513 |
+
if username in chat['participants']}
|
| 1514 |
+
else:
|
| 1515 |
+
user_chats = data['chats']
|
| 1516 |
+
|
| 1517 |
+
if request.method == 'POST' and is_authenticated:
|
| 1518 |
if 'new_chat' in request.form:
|
| 1519 |
recipient = request.form.get('recipient')
|
| 1520 |
if recipient and recipient in data['users'] and recipient != username:
|
|
|
|
| 1676 |
<div class="container">
|
| 1677 |
<div class="chat-list">
|
| 1678 |
<h1>Chats</h1>
|
| 1679 |
+
{% if is_authenticated %}
|
| 1680 |
+
<form method="POST">
|
| 1681 |
+
<input type="text" name="recipient" placeholder="Enter username" required>
|
| 1682 |
+
<button type="submit" name="new_chat" class="btn">New Chat</button>
|
| 1683 |
+
</form>
|
| 1684 |
+
{% endif %}
|
| 1685 |
{% for chat_id, chat in user_chats.items() %}
|
| 1686 |
<div class="chat-item" onclick="window.location='{{ url_for('chat', chat_id=chat_id) }}'">
|
| 1687 |
+
{{ chat['participants']|join(' & ') }}
|
|
|
|
|
|
|
| 1688 |
</div>
|
| 1689 |
{% endfor %}
|
| 1690 |
</div>
|
| 1691 |
<div class="chat-area">
|
| 1692 |
{% if selected_chat %}
|
| 1693 |
+
<h1>Chat between {{ selected_chat['participants']|join(' & ') }}</h1>
|
| 1694 |
<div class="messages">
|
| 1695 |
{% for message in selected_chat['messages'] %}
|
| 1696 |
<div class="message">
|
|
|
|
| 1714 |
</div>
|
| 1715 |
{% endfor %}
|
| 1716 |
</div>
|
| 1717 |
+
{% if is_authenticated %}
|
| 1718 |
+
<form method="POST" enctype="multipart/form-data" class="message-form">
|
| 1719 |
+
<input type="hidden" name="chat_id" value="{{ selected_chat_id }}">
|
| 1720 |
+
<textarea name="message" placeholder="Type a message" rows="3"></textarea>
|
| 1721 |
+
<input type="file" name="file" accept="video/*,image/*">
|
| 1722 |
+
<select name="post_id" class="post-select">
|
| 1723 |
+
<option value="">Share a post (optional)</option>
|
| 1724 |
+
{% for post in user_posts %}
|
| 1725 |
+
<option value="{{ post['id'] }}">{{ post['title'] }}</option>
|
| 1726 |
+
{% endfor %}
|
| 1727 |
+
</select>
|
| 1728 |
+
<button type="submit" name="send_message" class="btn">Send</button>
|
| 1729 |
+
</form>
|
| 1730 |
+
{% else %}
|
| 1731 |
+
<p style="margin-top: 20px;"><a href="{{ url_for('login') }}">Login</a> to send messages.</p>
|
| 1732 |
+
{% endif %}
|
| 1733 |
{% else %}
|
| 1734 |
+
<h1>Select a chat to view messages</h1>
|
| 1735 |
{% endif %}
|
| 1736 |
</div>
|
| 1737 |
</div>
|
|
|
|
| 1764 |
</body>
|
| 1765 |
</html>
|
| 1766 |
'''
|
| 1767 |
+
user_posts = [p for p in data['posts'] if p['uploader'] == username] if is_authenticated else []
|
| 1768 |
return render_template_string(html,
|
| 1769 |
user_chats=user_chats,
|
| 1770 |
selected_chat=selected_chat,
|