Skip to content

Add access requests from locked gates and read-only documents #830

Open

Description

@HMarzban

Parent

#823

What to build

Enable signed-in users to request access from locked Invite only gates (PrivateDocumentGate) and request edit/comment access from public or read-only documents, notifying managers (Owner + Full access) with single-step review and a 7-day decline cooldown.

1. Prisma Schema & Notification Gating (apps/hocuspocus.server, packages/supabase)

  • Add DocumentAccessRequest model (onDelete: Cascade to DocumentMetadata.documentId):
    • id String @id @default(uuid()), documentId String, requesterUserId String, requestedRole DocumentRole (DB CHECK "requestedRole" <> 'full'), message String?, status AccessRequestStatus @default(pending), resolvedBy String?, resolvedAt DateTime?, cooldownUntil DateTime?, createdAt DateTime @default(now()), updatedAt @updatedAt.
    • Partial unique index: UNIQUE ("documentId", "requesterUserId") WHERE "status" = 'pending'.
  • Notifications without double-mailing (packages/supabase & hocuspocus-worker):
    • When a request is created (or approved), insert bell notifications for Owner + all active Full access managers (channel_id = NULL safe when a document has no channels row yet) AND enqueue/send transactional email via hocuspocus-worker.
    • Explicitly gate the notification category/type inside both enqueue_push_notification() (07-4) and queue_email_notification() (07-5) in both scripts and migration so direct worker emails do not trigger a second email from the DB notification trigger.

2. Backend Endpoints (src/modules/document-sharing/, OpenAPI covered)

(Because locked users never learn documentId on Invite only 403 responses, request creation MUST accept slug and resolve it server-side.)

  • POST /api/documents/access-requests (mounted BEFORE /:documentId/*):
    • Signed-in verified user (401 if anon/unauth); fail-closed rate limit (10/hr/user).
    • Accepts { slug, requestedRole, message? } (requestedRole ∈ {edit, comment, view}; never full). Resolves slug against DocumentMetadata.slug (and alias table once #619 lands).
    • Returns 404 for trashed/nonexistent/Open docs (ownerId === null); idempotent 200 if pending request already exists; 409 if caller already holds >= requestedRole; 429 COOLDOWN_ACTIVE (with retryAfter timestamp) if declined within the last 7 days.
  • GET /api/documents/:documentId/people & GET /api/documents/:docName:
    • GET /people includes pending requests (id, name, email, initials, requestedRole, message, createdAt) ONLY for Owner | Full access.
    • Authenticated 403 privateGateResponse returns caller-scoped { hasPendingRequest, cooldownUntil } (still zero title/owner/documentId leak).
  • POST /api/documents/:documentId/access-requests/:requestId/approve & .../decline:
    • Owner or Full access. approve accepts { role } (defaults to requestedRole, editable up to full), writes DocumentGrant + Supabase document_access_grants mirror atomically, updates request to approved, and notifies requester.
    • decline sets status = 'declined', cooldownUntil = now() + 7 days. If another manager already resolved the request, return a clean conflict ("Already handled") so the UI refreshes rather than failing generically.

3. Webapp Gate, Header & Manager Review UI (apps/webapp)

  • PrivateDocumentGate (Invite only locked page):
    • Signed-out: "This document is Invite only. Sign in to open it or request access." (zero title, owner, or member count leak).
    • Signed-in: "You don't have access to this document" + inline Request access form (role selector excluding Full access, optional personal message, Request access CTA) transitioning cleanly to "Request sent", or displaying the 7-day decline cooldown notice.
  • In-document single CTA (no duplication):
    • Pad header (desktop DocumentAccessChip / mobile 40px status substrip): show a single Request edit access (edit) button for signed-in Can comment / Can view users — do NOT duplicate this button in the desktop Share footer.
    • Can view locked chat bar (ChannelInfoSurface when Can view): offer quiet "Ask to comment" submitting requestedRole: 'comment'.
  • Manager review in Share:
    • 1 pending request: inline banner showing requester avatar, name, email, optional note, editable role dropdown (Full access..Can view), Approve (non-primary / variant="cancel" so Copy link/Copy invite link keeps sole btn-primary per P4), and Decline.
    • 2+ pending requests: compact summary banner ("{N} people requested access · Review →") opening the Access requests drill-in view on desktop or the inline-pill request banners inside mobile People with access.
  • Update CONTEXT.md Access request glossary entry (_Avoid_: "join request", "ask-in").

Maintainer rulings, 2026-11-23

  • Request submission takes { slug } (never documentId, preventing ID leakage on locked docs).
  • Limit 1 pending request per user per document, and a 7-day cooldown after decline.
  • Single Request edit access CTA lives in the pad header / mobile substrip (no Share footer duplicate); "Ask to comment" in Can view chat bar requests comment.
  • Managers can change the role right before pressing Approve.

Acceptance criteria

  • POST /api/documents/access-requests resolves by slug without revealing documentId, enforces requestedRole <> 'full', prevents duplicate pending requests, and returns 429 during the 7-day post-decline cooldown.
  • Authenticated locked gate (PrivateDocumentGate) submits access requests and survives reload (hasPendingRequest / cooldownUntil in 403 gate response without leaking title/owner).
  • Approving with an edited role grants immediate access, notifies the requester without double-mailing, and clears the banner across manager clients; concurrent double-approval returns "Already handled".
  • In-pad Can view / Can comment non-owners see a single header Request edit access CTA (desktop/mobile) and Can view chat "Ask to comment", with zero duplicate request buttons in Share footer.
  • check-grants.sql stays 0 rows and server integration tests verify cooldown, slug resolution, and no double-email delivery.

Blocked by

Agent brief

Type: AFK

Where to start (read from code, not measured, as of 2026-11-23):

  • Supabase: packages/supabase/scripts/{07-4-push-notifications-pgmq.sql,07-5-email-notifications-pgmq.sql,10-functions.sql}
  • Server: apps/hocuspocus.server/src/api/controllers/documents.controller.ts (privateGateResponse), src/modules/document-sharing/, src/lib/email/
  • Webapp: apps/webapp/src/components/pages/document/components/PrivateDocumentGate.tsx, DocumentAccessChip.tsx, MobilePadTitle.tsx, SharePopover / ShareSheet, ChannelInfoSurface.tsx.

Deploy order: Supabase SQL → hocuspocus-worker → rest-api → webapp

Verify: Server integration tests + supabase_back check/test + webapp typecheck/lint/build + manual test of locked gate request, cooldown rejection, and 1-request vs 2+ request review.

Out of scope

Activity

  1. added this to the v2.4.0 milestone on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions