9661b9a81c
Models + Alembic migration (0007, head) only, per docs/planning/social.md US-1 -- the friends/follow API router is a separate sibling task. - friendships: requester_id/addressee_id (FK users, CASCADE), status (pending/accepted native enum), unique pair constraint, indexed FKs. - follows: follower_id/followed_id (FK users, CASCADE), unique pair. - blocks: blocker_id/blocked_id (FK users, CASCADE), unique pair. - Relationships wired onto User for both directions of each table. - tests/test_social_models.py exercises request/accept, the unique-pair constraint, follow, block, and DB-level CASCADE delete against real Postgres. Full backend suite (152 tests) still green. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y2FESCaMXeJpC9gSctiEeD
87 lines
3.0 KiB
Python
87 lines
3.0 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")
|
|
avatar_loadout = relationship(
|
|
"AvatarLoadout",
|
|
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",
|
|
)
|
|
|
|
@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
|