Files
FabledScribe/alembic/versions/0025_add_sharing_and_notifications.py
bvandeusen c7ef709633 Multi-user sharing, groups, and in-app notifications
Backend:
- Alembic migration 0025: groups, group_memberships, project_shares,
  note_shares, notifications tables
- models: Group, GroupMembership, ProjectShare, NoteShare, Notification
- services/access.py: permission resolution (viewer/editor/admin/owner)
- services/groups.py + routes/groups.py: full group CRUD + membership
- services/sharing.py + routes/shares.py: project/note sharing API
- services/notifications.py: in-app notification create/list/mark-read
- routes/in_app_notifications.py: GET/POST notification endpoints
- routes/users.py: user search endpoint
- services/projects.py + services/notes.py: *_for_user variants
- routes updated to use *_for_user on get/list; adds permission field
- app.py: register all new blueprints

Frontend:
- api/client.ts: ShareEntry, GroupEntry, NotificationEntry types + helpers
- stores/notifications.ts: Pinia store with polling
- NotificationBell.vue: bell icon + badge + dropdown toggle + 60s poll
- NotificationsPanel.vue: unread notification list with mark-all-read
- ShareDialog.vue: teleport modal for sharing projects/notes with users/groups
- SharedWithMeView.vue: /shared route listing shared projects and notes
- AppHeader: NotificationBell, Shared nav link
- ProjectView, NoteViewerView, TaskViewerView: Share button + ShareDialog
- SettingsView: Groups admin tab with create/delete groups + member mgmt
- router: /shared route

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-11 15:37:00 -04:00

210 lines
6.1 KiB
Python

"""Add groups, group_memberships, project_shares, note_shares, notifications tables.
Revision ID: 0025
Revises: 0024
Create Date: 2026-03-11
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision = "0025"
down_revision = "0024"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table(
"groups",
sa.Column("id", sa.Integer(), primary_key=True),
sa.Column("name", sa.Text(), nullable=False, unique=True),
sa.Column("description", sa.Text()),
sa.Column(
"created_by",
sa.Integer(),
sa.ForeignKey("users.id", ondelete="SET NULL"),
),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
)
op.create_table(
"group_memberships",
sa.Column("id", sa.Integer(), primary_key=True),
sa.Column(
"group_id",
sa.Integer(),
sa.ForeignKey("groups.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"user_id",
sa.Integer(),
sa.ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column("role", sa.Text(), nullable=False, server_default="member"),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.UniqueConstraint("group_id", "user_id", name="uq_gm_group_user"),
)
op.create_index("idx_gm_user", "group_memberships", ["user_id"])
op.create_table(
"project_shares",
sa.Column("id", sa.Integer(), primary_key=True),
sa.Column(
"project_id",
sa.Integer(),
sa.ForeignKey("projects.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"shared_with_user_id",
sa.Integer(),
sa.ForeignKey("users.id", ondelete="CASCADE"),
),
sa.Column(
"shared_with_group_id",
sa.Integer(),
sa.ForeignKey("groups.id", ondelete="CASCADE"),
),
sa.Column("permission", sa.Text(), nullable=False),
sa.Column(
"invited_by",
sa.Integer(),
sa.ForeignKey("users.id"),
nullable=False,
),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.CheckConstraint(
"(shared_with_user_id IS NOT NULL) != (shared_with_group_id IS NOT NULL)",
name="ck_ps_exclusive_target",
),
)
op.create_index("idx_ps_project", "project_shares", ["project_id"])
op.execute(
"CREATE UNIQUE INDEX idx_ps_user ON project_shares(project_id, shared_with_user_id)"
" WHERE shared_with_user_id IS NOT NULL"
)
op.execute(
"CREATE UNIQUE INDEX idx_ps_group ON project_shares(project_id, shared_with_group_id)"
" WHERE shared_with_group_id IS NOT NULL"
)
op.create_table(
"note_shares",
sa.Column("id", sa.Integer(), primary_key=True),
sa.Column(
"note_id",
sa.Integer(),
sa.ForeignKey("notes.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"shared_with_user_id",
sa.Integer(),
sa.ForeignKey("users.id", ondelete="CASCADE"),
),
sa.Column(
"shared_with_group_id",
sa.Integer(),
sa.ForeignKey("groups.id", ondelete="CASCADE"),
),
sa.Column("permission", sa.Text(), nullable=False),
sa.Column(
"invited_by",
sa.Integer(),
sa.ForeignKey("users.id"),
nullable=False,
),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.Column(
"updated_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
sa.CheckConstraint(
"(shared_with_user_id IS NOT NULL) != (shared_with_group_id IS NOT NULL)",
name="ck_ns_exclusive_target",
),
)
op.execute(
"CREATE UNIQUE INDEX idx_ns_user ON note_shares(note_id, shared_with_user_id)"
" WHERE shared_with_user_id IS NOT NULL"
)
op.execute(
"CREATE UNIQUE INDEX idx_ns_group ON note_shares(note_id, shared_with_group_id)"
" WHERE shared_with_group_id IS NOT NULL"
)
op.create_table(
"notifications",
sa.Column("id", sa.Integer(), primary_key=True),
sa.Column(
"user_id",
sa.Integer(),
sa.ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column("type", sa.Text(), nullable=False),
sa.Column(
"payload",
postgresql.JSONB(),
nullable=False,
server_default="{}",
),
sa.Column("read_at", sa.DateTime(timezone=True)),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.func.now(),
nullable=False,
),
)
op.execute(
"CREATE INDEX idx_notif_user_unread ON notifications(user_id, read_at)"
" WHERE read_at IS NULL"
)
def downgrade() -> None:
op.drop_table("notifications")
op.drop_table("note_shares")
op.drop_table("project_shares")
op.drop_table("group_memberships")
op.drop_table("groups")