diff --git a/files/classes/private_chats.py b/files/classes/private_chats.py index 19b0b5652..2459fd591 100644 --- a/files/classes/private_chats.py +++ b/files/classes/private_chats.py @@ -14,8 +14,6 @@ class Chat(Base): name = Column(String) created_utc = Column(Integer) - memberships = relationship("ChatMembership") - def __init__(self, *args, **kwargs): if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time()) super().__init__(*args, **kwargs) diff --git a/files/routes/private_chats.py b/files/routes/private_chats.py index a6a78d530..867af17ec 100644 --- a/files/routes/private_chats.py +++ b/files/routes/private_chats.py @@ -25,8 +25,8 @@ def chat_user(v, username): abort(403, f"@{user.username} is muting notifications from you, so you can't chat with them!") - sq = g.db.query(Chat.id).join(Chat.memberships).filter(ChatMembership.user_id.in_((v.id, user.id))).group_by(Chat.id).having(func.count(Chat.id) == 2).subquery() - existing = g.db.query(Chat.id).join(Chat.memberships).filter(Chat.id == sq.c.id).group_by(Chat.id).having(func.count(Chat.id) == 2).one_or_none() + sq = g.db.query(Chat.id).join(ChatMembership, ChatMembership.chat_id == Chat.id).filter(ChatMembership.user_id.in_((v.id, user.id))).group_by(Chat.id).having(func.count(Chat.id) == 2).subquery() + existing = g.db.query(Chat.id).join(ChatMembership, ChatMembership.chat_id == Chat.id).filter(Chat.id == sq.c.id).group_by(Chat.id).having(func.count(Chat.id) == 2).one_or_none() if existing: return redirect(f"/chat/{existing.id}") @@ -71,7 +71,10 @@ def private_chat(v, chat_id): g.db.add(membership) g.db.commit() #to clear notif count - return render_template("private_chat.html", v=v, messages=displayed_messages, chat=chat) + query = g.db.query(ChatMembership).filter_by(chat_id=chat.id) + sorted_memberships = [query.filter_by(user_id=chat.owner_id).one()] + query.filter(ChatMembership.user_id != chat.owner_id).join(ChatMembership.user).order_by(func.lower(User.username)).all() + + return render_template("private_chat.html", v=v, messages=displayed_messages, chat=chat, sorted_memberships=sorted_memberships) @app.post("/chat//name") diff --git a/files/templates/header.html b/files/templates/header.html index 8530dd683..4de60b725 100644 --- a/files/templates/header.html +++ b/files/templates/header.html @@ -532,9 +532,9 @@







{% elif request.path.startswith('/chat/') %}
-
Members ({{chat.memberships|length}})
+
Members ({{sorted_memberships|length}})
-
Members ({{chat.memberships|length}})
+
Members ({{sorted_memberships|length}})
- {% for membership in chat.memberships %} + {% for membership in sorted_memberships %} {% set user = membership.user %} {% set patron = '' %} {% if user.patron %}