107 lines
3.6 KiB
Python
107 lines
3.6 KiB
Python
from datetime import date
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Date, Text
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.sql import func
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
username = Column(String(50), unique=True, index=True, nullable=False)
|
|
email = Column(String(120), unique=True, index=True, nullable=False)
|
|
hashed_password = Column(String(255), nullable=False)
|
|
date_of_birth = Column(Date, nullable=True)
|
|
is_active = Column(Boolean, default=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
# Presence / status (backend/app/services/presence.py, app/routers/presence.py)
|
|
last_seen_at = Column(DateTime(timezone=True), nullable=True)
|
|
status_message = Column(Text, nullable=True)
|
|
|
|
sessions = relationship("Session", back_populates="user", cascade="all, delete-orphan")
|
|
refresh_tokens = relationship("RefreshToken", back_populates="user", cascade="all, delete-orphan")
|
|
worlds = relationship("World", back_populates="owner")
|
|
assets = relationship("Asset", back_populates="owner")
|
|
asset_collections = relationship("AssetCollection", back_populates="owner", cascade="all, delete-orphan")
|
|
avatar_loadout = relationship(
|
|
"AvatarLoadout",
|
|
back_populates="user",
|
|
uselist=False,
|
|
cascade="all, delete-orphan",
|
|
)
|
|
|
|
# Profile (app/models/profile.py): bio/avatar picture/privacy + favorites.
|
|
profile = relationship(
|
|
"Profile",
|
|
back_populates="user",
|
|
uselist=False,
|
|
cascade="all, delete-orphan",
|
|
)
|
|
|
|
# Social graph (docs/planning/social.md US-1; app/models/social.py)
|
|
sent_friend_requests = relationship(
|
|
"Friendship",
|
|
foreign_keys="Friendship.requester_id",
|
|
back_populates="requester",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
received_friend_requests = relationship(
|
|
"Friendship",
|
|
foreign_keys="Friendship.addressee_id",
|
|
back_populates="addressee",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
following = relationship(
|
|
"Follow",
|
|
foreign_keys="Follow.follower_id",
|
|
back_populates="follower",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
followers = relationship(
|
|
"Follow",
|
|
foreign_keys="Follow.followed_id",
|
|
back_populates="followed",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
blocks_initiated = relationship(
|
|
"Block",
|
|
foreign_keys="Block.blocker_id",
|
|
back_populates="blocker",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
blocked_by = relationship(
|
|
"Block",
|
|
foreign_keys="Block.blocked_id",
|
|
back_populates="blocked",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
|
|
# Groups (docs/planning/social.md US-5; app/models/groups.py)
|
|
owned_groups = relationship(
|
|
"Group",
|
|
back_populates="owner",
|
|
)
|
|
group_memberships = relationship(
|
|
"GroupMember",
|
|
back_populates="user",
|
|
cascade="all, delete-orphan",
|
|
)
|
|
|
|
@property
|
|
def is_adult(self) -> bool:
|
|
"""Return True if the user is 18 or older based on date_of_birth."""
|
|
if self.date_of_birth is None:
|
|
return False
|
|
today = date.today()
|
|
age = today.year - self.date_of_birth.year
|
|
# Adjust if birthday hasn't occurred yet this year
|
|
if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
|
|
age -= 1
|
|
return age >= 18
|