feat: implement tree sharing, draft trees, and session-to-tree conversion (Issues #16, #25, #17)

Backend features:
- Tree sharing via secure tokens with expiration (Issue #16)
- Draft tree status with conditional validation (Issue #25)
- Save session as custom tree with fork tracking (Issue #17)
- Tree validation system for publish requirements
- Session-to-tree conversion preserving custom steps

Database migrations:
- 024: Tree sharing (tree_shares table, visibility field)
- 025: Tree status field (draft/published)
- 25b: Merge migration for indexes

New endpoints:
- POST /api/v1/trees/{id}/share - Generate share token
- GET /api/v1/shared/{token} - Public tree access
- POST /api/v1/trees/{id}/can-publish - Validate tree
- POST /api/v1/sessions/{id}/save-as-tree - Convert session

Test coverage:
- 20 tests for draft trees functionality
- 14 tests for session-to-tree conversion
- 15 tests for tree sharing

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Michael Chihlas
2026-02-07 23:06:13 -05:00
parent 9f92547309
commit c7b2c59ef6
16 changed files with 2141 additions and 7 deletions

View File

@@ -0,0 +1,74 @@
"""add tree sharing tables
Revision ID: 024
Revises: 023
Create Date: 2026-02-07
Adds tree sharing infrastructure:
- visibility field on trees table (private/team/link/public)
- tree_shares: Share tokens for link-based sharing with forking control
Key features:
- Default existing trees to 'team' visibility
- Share tokens are URL-safe (secrets.token_urlsafe)
- Optional expiration for time-limited shares
- allow_forking flag controls whether recipients can fork shared trees
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID
# revision identifiers, used by Alembic.
revision: str = '024'
down_revision: Union[str, None] = '023'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Add visibility field to trees with default 'team'
op.add_column('trees', sa.Column(
'visibility',
sa.String(20),
nullable=False,
server_default='team',
comment="Visibility level: private (author only), team (account members), link (share token), public (all users)"
))
op.create_index('ix_trees_visibility', 'trees', ['visibility'])
# Add CHECK constraint for visibility values
op.create_check_constraint(
'ck_trees_visibility',
'trees',
"visibility IN ('private', 'team', 'link', 'public')"
)
# Create tree_shares table
op.create_table(
'tree_shares',
sa.Column('id', UUID(as_uuid=True), primary_key=True, server_default=sa.text('gen_random_uuid()')),
sa.Column('tree_id', UUID(as_uuid=True), nullable=False),
sa.Column('share_token', sa.String(64), nullable=False, unique=True),
sa.Column('created_by', UUID(as_uuid=True), nullable=False),
sa.Column('allow_forking', sa.Boolean, nullable=False, server_default='true'),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('now()')),
sa.Column('expires_at', sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(['tree_id'], ['trees.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['created_by'], ['users.id'], ondelete='CASCADE')
)
# Create indexes for tree_shares
op.create_index('ix_tree_shares_tree_id', 'tree_shares', ['tree_id'])
op.create_index('ix_tree_shares_share_token', 'tree_shares', ['share_token'])
op.create_index('ix_tree_shares_created_by', 'tree_shares', ['created_by'])
op.create_index('ix_tree_shares_expires_at', 'tree_shares', ['expires_at'])
def downgrade() -> None:
op.drop_table('tree_shares')
op.drop_index('ix_trees_visibility', table_name='trees')
op.drop_constraint('ck_trees_visibility', 'trees', type_='check')
op.drop_column('trees', 'visibility')

View File

@@ -0,0 +1,47 @@
"""add tree status field for draft/published workflow
Revision ID: 025
Revises: 25b001abd0f7
Create Date: 2026-02-08
Adds status field to trees table for draft/published workflow:
- status: enum ('draft', 'published') - defaults to 'published' for existing trees
- Drafts allow incomplete/invalid structures for work-in-progress
- Published trees require validation before saving
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '025'
down_revision: Union[str, None] = '25b001abd0f7'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Add status field with default 'published' (for backward compatibility)
op.add_column('trees', sa.Column(
'status',
sa.String(20),
nullable=False,
server_default='published',
comment="Status: draft (work in progress) or published (validated and available)"
))
op.create_index('ix_trees_status', 'trees', ['status'])
# Add CHECK constraint for status values
op.create_check_constraint(
'ck_trees_status',
'trees',
"status IN ('draft', 'published')"
)
def downgrade() -> None:
op.drop_index('ix_trees_status', table_name='trees')
op.drop_constraint('ck_trees_status', 'trees', type_='check')
op.drop_column('trees', 'status')

View File

@@ -0,0 +1,26 @@
"""merge tree sharing and session indexes
Revision ID: 25b001abd0f7
Revises: 024, 11c8abf7ef5b
Create Date: 2026-02-07 21:43:57.354334
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '25b001abd0f7'
down_revision: Union[str, None] = ('024', '11c8abf7ef5b')
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
pass
def downgrade() -> None:
pass