This repository was archived by the owner on Jan 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_member.py
More file actions
38 lines (28 loc) · 1.56 KB
/
chat_member.py
File metadata and controls
38 lines (28 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from datetime import UTC, datetime
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, Text
from sqlalchemy.orm import Mapped, mapped_column
from python_chat.database import Base, db
class ChatMember(Base):
"""Association model between users and chats."""
__tablename__ = "chat_members"
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("users.id", ondelete="CASCADE"), primary_key=True)
chat_id: Mapped[int] = mapped_column(Integer, ForeignKey("chats.id", ondelete="CASCADE"), primary_key=True)
is_moderator: Mapped[bool] = mapped_column(Boolean, default=False)
joined_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
is_banned: Mapped[bool] = mapped_column(Boolean, default=False)
banned_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
banned_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
user = db.relationship("User", back_populates="chats")
chat = db.relationship("Chat", back_populates="members")
def ban(self, reason=None) -> None:
"""Ban a user from this chat."""
self.is_banned = True
self.banned_at = datetime.now(UTC)
self.banned_reason = reason
def unban(self) -> None:
"""Unban a user from this chat."""
self.is_banned = False
self.banned_at = None
self.banned_reason = None
def __repr__(self) -> str:
return f"<ChatMember user_id={self.user_id} chat_id={self.chat_id} is_moderator={self.is_moderator} is_banned={self.is_banned}>"