This commit is contained in:
rimu 2024-02-19 16:13:53 +13:00
parent a566c40913
commit eaf53f87c2
2 changed files with 32 additions and 10 deletions

View file

@ -25,7 +25,10 @@ def chat_home(conversation_id=None):
conversation_member.c.conversation_id == Conversation.id). \ conversation_member.c.conversation_id == Conversation.id). \
filter(conversation_member.c.user_id == current_user.id).order_by(desc(Conversation.updated_at)).limit(50).all() filter(conversation_member.c.user_id == current_user.id).order_by(desc(Conversation.updated_at)).limit(50).all()
if conversation_id is None: if conversation_id is None:
return redirect(url_for('chat.chat_home', conversation_id=conversations[0].id)) if conversations:
return redirect(url_for('chat.chat_home', conversation_id=conversations[0].id))
else:
return redirect(url_for('chat.empty'))
else: else:
conversation = Conversation.query.get_or_404(conversation_id) conversation = Conversation.query.get_or_404(conversation_id)
conversation.read = True conversation.read = True
@ -33,16 +36,8 @@ def chat_home(conversation_id=None):
abort(400) abort(400)
if conversations: if conversations:
messages = conversation.messages.order_by(ChatMessage.created_at).all() messages = conversation.messages.order_by(ChatMessage.created_at).all()
if messages:
if messages[0].sender_id == current_user.id:
other_party = User.query.get(messages[0].recipient_id)
else:
other_party = User.query.get(messages[0].sender_id)
else:
other_party = None
else: else:
messages = [] messages = []
other_party = None
sql = f"UPDATE notification SET read = true WHERE url = '/chat/{conversation_id}' AND user_id = {current_user.id}" sql = f"UPDATE notification SET read = true WHERE url = '/chat/{conversation_id}' AND user_id = {current_user.id}"
db.session.execute(text(sql)) db.session.execute(text(sql))
@ -50,7 +45,8 @@ def chat_home(conversation_id=None):
current_user.unread_notifications = Notification.query.filter_by(user_id=current_user.id, read=False).count() current_user.unread_notifications = Notification.query.filter_by(user_id=current_user.id, read=False).count()
db.session.commit() db.session.commit()
return render_template('chat/conversation.html', title=_('Chat with %(name)s', name=other_party.display_name()) if other_party else _('Chat'), return render_template('chat/conversation.html',
title=_('Chat with %(name)s', name=conversation.member_names(current_user.id)),
conversations=conversations, messages=messages, form=form, conversations=conversations, messages=messages, form=form,
current_conversation=conversation_id, conversation=conversation, current_conversation=conversation_id, conversation=conversation,
moderating_communities=moderating_communities(current_user.get_id()), moderating_communities=moderating_communities(current_user.get_id()),
@ -99,6 +95,12 @@ def blocked():
return render_template('chat/blocked.html') return render_template('chat/blocked.html')
@bp.route('/chat/empty', methods=['GET'])
@login_required
def empty():
return render_template('chat/empty.html')
@bp.route('/chat/<int:conversation_id>/options', methods=['GET', 'POST']) @bp.route('/chat/<int:conversation_id>/options', methods=['GET', 'POST'])
@login_required @login_required
def chat_options(conversation_id): def chat_options(conversation_id):

View file

@ -0,0 +1,20 @@
{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') %}
{% extends 'themes/' + theme() + '/base.html' %}
{% else %}
{% extends "base.html" %}
{% endif %} %}
{% block app_content %}
<div class="row">
<div class="col col-login mx-auto">
<div class="card mt-5">
<div class="card-body p-6">
<div class="card-title">{{ _('No chats') }}</div>
<div class="card-body">
<p>{{ _("There are no chats involving you, yet. Start a conversation using the \"Send message\" button on someone's profile.") }}</p>
</div>
</div>
</div>
</div>
</div>
{% endblock %}