107 lines
3.8 KiB
Python
107 lines
3.8 KiB
Python
"""User-related database models."""
|
|
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import (
|
|
BigInteger,
|
|
Boolean,
|
|
DateTime,
|
|
Float,
|
|
ForeignKey,
|
|
String,
|
|
Text,
|
|
UniqueConstraint,
|
|
)
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from .base import Base, utc_now
|
|
|
|
if TYPE_CHECKING:
|
|
from .conversation import Conversation, Message
|
|
from .guild import GuildMember
|
|
from .living_ai import ScheduledEvent, UserCommunicationStyle, UserRelationship
|
|
|
|
|
|
class User(Base):
|
|
"""Discord user tracking."""
|
|
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
discord_id: Mapped[int] = mapped_column(BigInteger, unique=True, index=True)
|
|
discord_username: Mapped[str] = mapped_column(String(255))
|
|
discord_display_name: Mapped[str | None] = mapped_column(String(255))
|
|
custom_name: Mapped[str | None] = mapped_column(String(255))
|
|
first_seen_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
|
|
last_seen_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
|
|
# Relationships
|
|
preferences: Mapped[list["UserPreference"]] = relationship(
|
|
back_populates="user", cascade="all, delete-orphan"
|
|
)
|
|
facts: Mapped[list["UserFact"]] = relationship(
|
|
back_populates="user", cascade="all, delete-orphan"
|
|
)
|
|
conversations: Mapped[list["Conversation"]] = relationship(
|
|
back_populates="user", cascade="all, delete-orphan"
|
|
)
|
|
messages: Mapped[list["Message"]] = relationship(back_populates="user")
|
|
guild_memberships: Mapped[list["GuildMember"]] = relationship(
|
|
back_populates="user", cascade="all, delete-orphan"
|
|
)
|
|
|
|
# Living AI relationships
|
|
relationships: Mapped[list["UserRelationship"]] = relationship(
|
|
back_populates="user", cascade="all, delete-orphan"
|
|
)
|
|
communication_style: Mapped["UserCommunicationStyle | None"] = relationship(
|
|
back_populates="user", cascade="all, delete-orphan", uselist=False
|
|
)
|
|
scheduled_events: Mapped[list["ScheduledEvent"]] = relationship(
|
|
back_populates="user", cascade="all, delete-orphan"
|
|
)
|
|
|
|
@property
|
|
def display_name(self) -> str:
|
|
"""Get the name to use when addressing this user."""
|
|
return self.custom_name or self.discord_display_name or self.discord_username
|
|
|
|
|
|
class UserPreference(Base):
|
|
"""Per-user preferences/settings."""
|
|
|
|
__tablename__ = "user_preferences"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
|
|
preference_key: Mapped[str] = mapped_column(String(100))
|
|
preference_value: Mapped[str | None] = mapped_column(Text)
|
|
|
|
# Relationships
|
|
user: Mapped["User"] = relationship(back_populates="preferences")
|
|
|
|
__table_args__ = (UniqueConstraint("user_id", "preference_key"),)
|
|
|
|
|
|
class UserFact(Base):
|
|
"""Facts the bot has learned about users."""
|
|
|
|
__tablename__ = "user_facts"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
|
|
fact_type: Mapped[str] = mapped_column(String(50), index=True) # general, preference, hobby
|
|
fact_content: Mapped[str] = mapped_column(Text)
|
|
confidence: Mapped[float] = mapped_column(Float, default=1.0)
|
|
source: Mapped[str] = mapped_column(String(50), default="conversation")
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True, index=True)
|
|
learned_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
|
|
last_referenced_at: Mapped[datetime | None] = mapped_column(
|
|
DateTime(timezone=True), default=None
|
|
)
|
|
|
|
# Relationships
|
|
user: Mapped["User"] = relationship(back_populates="facts")
|