more efficient chat code
parent
7f67931347
commit
524a81f2e7
|
@ -1,4 +1,5 @@
|
|||
import time
|
||||
from flask import g
|
||||
|
||||
from sqlalchemy import Column, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
|
@ -14,14 +15,11 @@ class Chat(Base):
|
|||
name = Column(String)
|
||||
created_utc = Column(Integer)
|
||||
|
||||
memberships = relationship("ChatMembership", order_by="ChatMembership.created_utc")
|
||||
|
||||
@property
|
||||
@lazy
|
||||
def owner_id(self):
|
||||
if not self.memberships:
|
||||
return AUTOJANNY_ID
|
||||
return self.memberships[0].user_id
|
||||
return g.db.query(ChatMembership.user_id).filter_by(chat_id=self.id).order_by(ChatMembership.created_utc).first()[0] or AUTOJANNY_ID
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time())
|
||||
|
|
|
@ -33,8 +33,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}")
|
||||
|
||||
|
|
Loading…
Reference in New Issue