transfer owner automatically when chat owner removes himself
parent
c3459acde5
commit
b2098a78bf
|
@ -10,10 +10,16 @@ from files.helpers.lazy import lazy
|
|||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
id = Column(Integer, primary_key=True)
|
||||
owner_id = Column(Integer, ForeignKey("users.id"))
|
||||
name = Column(String)
|
||||
created_utc = Column(Integer)
|
||||
|
||||
memberships = relationship("ChatMembership", order_by="ChatMembership.created_utc")
|
||||
|
||||
@property
|
||||
@lazy
|
||||
def owner_id(self):
|
||||
return self.memberships[0].user_id
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time())
|
||||
super().__init__(*args, **kwargs)
|
||||
|
|
|
@ -30,8 +30,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(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()
|
||||
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()
|
||||
if existing:
|
||||
return redirect(f"/chat/{existing.id}")
|
||||
|
||||
|
|
Loading…
Reference in New Issue