""" Sharing models — cross-user access to heaps and folders. HeapShare grants another user read or read+write access to a heap. FolderShare does the same for a folder (or source root). """ import uuid from sqlalchemy import ( Column, DateTime, ForeignKey, Index, String, UniqueConstraint, func, ) from app.database import Base class HeapShare(Base): __tablename__ = "heap_shares" id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4())) heap_id = Column( String, ForeignKey("heaps.id", ondelete="CASCADE"), nullable=False, ) # Denormalized from heap.user_id for fast "shares I own" lookups. owner_id = Column(String, ForeignKey("users.id"), nullable=False) shared_with_id = Column(String, ForeignKey("users.id"), nullable=False) permission = Column(String, nullable=False, default="read") # 'read' | 'write' # Lifecycle: 'pending' while the recipient hasn't acted, 'accepted' # once they've Accept'd in the notification bell. Decline deletes the # row outright — see migration 0014 for the backfill of pre-existing # rows to 'accepted' so nothing vanishes from existing sidebars. status = Column(String, nullable=False, default="pending") created_at = Column(DateTime, server_default=func.now()) accepted_at = Column(DateTime, nullable=True) __table_args__ = ( UniqueConstraint("heap_id", "shared_with_id", name="uq_heap_share"), Index("ix_heap_shares_shared_with", "shared_with_id"), Index("ix_heap_shares_heap_id", "heap_id"), Index("ix_heap_shares_shared_with_status", "shared_with_id", "status"), ) class FolderShare(Base): __tablename__ = "folder_shares" id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4())) # Can reference either a Folder.id or a SourceRoot.id. folder_id = Column(String, nullable=False) folder_type = Column(String, nullable=False, default="folder") # 'folder' | 'source_root' owner_id = Column(String, ForeignKey("users.id"), nullable=False) shared_with_id = Column(String, ForeignKey("users.id"), nullable=False) permission = Column(String, nullable=False, default="read") # 'read' | 'write' status = Column(String, nullable=False, default="pending") created_at = Column(DateTime, server_default=func.now()) accepted_at = Column(DateTime, nullable=True) __table_args__ = ( UniqueConstraint("folder_id", "shared_with_id", name="uq_folder_share"), Index("ix_folder_shares_shared_with", "shared_with_id"), Index("ix_folder_shares_folder_id", "folder_id"), Index("ix_folder_shares_shared_with_status", "shared_with_id", "status"), )