712d21e746
docs/planning/social.md US-5 (task 13). Models + Alembic migration only, per the sibling-dependency boundary with the Groups API task. - groups: id, name, description, owner_id (FK users, ON DELETE SET NULL -- a group is a durable multi-member community, so it and its full roster survive the owner deleting their account; the API task handles surfacing an ownerless group for reassignment). - group_members: group_id/user_id (both FK, ON DELETE CASCADE -- pure join-table rows with no meaning without either side), role (owner/admin/member native enum, default member), joined_at, unique constraint on (group_id, user_id). - User gets owned_groups / group_memberships relationships. - tests/conftest.py TEST_TABLES updated for truncation between tests. - New tests/test_group_models.py: role assignment, unique-membership rejection, group-delete cascade, member-delete cascade, and owner-delete SET NULL survival -- all verified against real Postgres. Verified: upgrade/downgrade/upgrade round-trip, \d-inspected schema matches spec (FKs, cascade behavior, unique constraint, indexes), full backend suite 166 passed / 0 regressions. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y2FESCaMXeJpC9gSctiEeD
98 lines
3.3 KiB
Python
98 lines
3.3 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",
|
|
)
|
|
|
|
# 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
|