§22 S2: collection-scoped list/get/propose endpoints

Refactor the project-scoped serve/propose internals into collection-grained
helpers (_list_rfcs_for_collection, _get_rfc_for_collection,
_propose_into_collection); add routes under
/api/projects/<id>/collections/<cid>/rfcs[/<slug>|/propose]. Propose writes
the entry under the target collection's <subfolder>/rfcs via a new rfcs_dir
param on bot.open_idea_pr. Default-collection routes preserved as wrappers.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ben Stull
2026-06-05 13:05:58 -07:00
parent 91b0fb358c
commit f57d4080dc
3 changed files with 149 additions and 22 deletions
+78 -19
View File
@@ -717,15 +717,15 @@ def make_router(
# second project's corpus renders under /p/<id>/.
# ---------------------------------------------------------------
@router.get("/api/projects/{project_id}/rfcs")
async def list_project_rfcs(
project_id: str, request: Request, unreviewed: str | None = None
def _require_collection_in_project(collection_id: str, project_id: str) -> None:
# §22 S2: a collection-scoped route 404s when the collection does not
# belong to the project in the path (shape matches an unknown id).
if collections_mod.project_of_collection(collection_id) != project_id:
raise HTTPException(404, "Not found")
def _list_rfcs_for_collection(
collection_id: str, viewer, unreviewed: str | None
) -> dict[str, Any]:
viewer = auth.current_user(request)
# §22.5 read gate: a gated project's catalog 404s to a non-member.
auth.require_project_readable(viewer, project_id)
# §22 S1: serve the project's default collection (the corpus grain).
collection_id = collections_mod.default_collection_id(project_id)
viewer_id = viewer.user_id if viewer else None
unreviewed_clause = ""
if unreviewed is not None and unreviewed.lower() in ("1", "true", "yes"):
@@ -769,11 +769,7 @@ def make_router(
]
return {"items": items}
@router.get("/api/projects/{project_id}/rfcs/{slug}")
async def get_project_rfc(project_id: str, slug: str, request: Request) -> dict[str, Any]:
viewer = auth.current_user(request)
auth.require_project_readable(viewer, project_id)
collection_id = collections_mod.default_collection_id(project_id)
def _get_rfc_for_collection(collection_id: str, slug: str, viewer) -> dict[str, Any]:
row = db.conn().execute(
"SELECT * FROM cached_rfcs WHERE collection_id = ? AND slug = ?",
(collection_id, slug),
@@ -794,6 +790,46 @@ def make_router(
payload["proposed_use_case"] = uc["use_case"] if uc else None
return payload
@router.get("/api/projects/{project_id}/rfcs")
async def list_project_rfcs(
project_id: str, request: Request, unreviewed: str | None = None
) -> dict[str, Any]:
viewer = auth.current_user(request)
# §22.5 read gate: a gated project's catalog 404s to a non-member.
auth.require_project_readable(viewer, project_id)
# §22 S1: the project-scoped route serves the default collection.
collection_id = collections_mod.default_collection_id(project_id)
return _list_rfcs_for_collection(collection_id, viewer, unreviewed)
@router.get("/api/projects/{project_id}/rfcs/{slug}")
async def get_project_rfc(project_id: str, slug: str, request: Request) -> dict[str, Any]:
viewer = auth.current_user(request)
auth.require_project_readable(viewer, project_id)
collection_id = collections_mod.default_collection_id(project_id)
return _get_rfc_for_collection(collection_id, slug, viewer)
# §22 S2: collection-scoped serve + propose. The catalog/entry views read
# these under /p/<project>/c/<collection>/; the project-scoped routes above
# stay as the default-collection compat surface.
@router.get("/api/projects/{project_id}/collections/{collection_id}/rfcs")
async def list_collection_rfcs(
project_id: str, collection_id: str, request: Request,
unreviewed: str | None = None,
) -> dict[str, Any]:
viewer = auth.current_user(request)
auth.require_project_readable(viewer, project_id)
_require_collection_in_project(collection_id, project_id)
return _list_rfcs_for_collection(collection_id, viewer, unreviewed)
@router.get("/api/projects/{project_id}/collections/{collection_id}/rfcs/{slug}")
async def get_collection_rfc(
project_id: str, collection_id: str, slug: str, request: Request
) -> dict[str, Any]:
viewer = auth.current_user(request)
auth.require_project_readable(viewer, project_id)
_require_collection_in_project(collection_id, project_id)
return _get_rfc_for_collection(collection_id, slug, viewer)
# ---------------------------------------------------------------
# §22.4c: mark-reviewed — clear an active entry's `unreviewed` flag
# ---------------------------------------------------------------
@@ -957,6 +993,15 @@ def make_router(
# ---------------------------------------------------------------
async def _propose_into_project(project_id: str, payload: ProposeBody, user) -> dict[str, Any]:
# Default-collection wrapper (§22 S1/S2): resolve the project's default
# collection and delegate. Keeps the project-scoped propose routes intact.
return await _propose_into_collection(
project_id, collections_mod.default_collection_id(project_id), payload, user
)
async def _propose_into_collection(
project_id: str, collection_id: str, payload: ProposeBody, user
) -> dict[str, Any]:
# §22.6/§22.7: proposing a new entry requires project-level contribute
# standing in the *target* project. On the public default project the
# implicit-public baseline preserves the pre-multi-project flow.
@@ -970,7 +1015,6 @@ def make_router(
# We re-check atomically here even though the client also checks
# on every keystroke, since a concurrent submission could land
# between dialog-open and submit.
collection_id = collections_mod.default_collection_id(project_id)
clash = db.conn().execute(
"SELECT 1 FROM cached_rfcs WHERE slug = ? AND collection_id = ?", (slug, collection_id)
).fetchone()
@@ -984,11 +1028,12 @@ def make_router(
if idea_clash:
raise HTTPException(409, f"Slug `{slug}` is already reserved by an open proposal")
# §22.4b: the target project's landing state. Through Plan A every
# entry lands in the default project; M3-frontend routing carries a
# non-default target later.
target_project = project_id
landing_state = "active" if projects_mod.project_initial_state(target_project) == "active" else "super-draft"
# §22.4b: the target collection's landing state (the per-corpus field
# moved down to the collection in migration 029).
landing_state = (
"active" if collections_mod.collection_initial_state(collection_id) == "active"
else "super-draft"
)
entry = entry_mod.Entry(
slug=slug,
@@ -1019,6 +1064,9 @@ def make_router(
f"**Topic:** {entry.title}\n\n"
f"{payload.pitch.strip()}"
)
# §22 S2: write the entry under the target collection's <subfolder>/rfcs.
subfolder = collections_mod.subfolder_of(collection_id)
rfcs_dir = f"{subfolder}/rfcs" if subfolder else "rfcs"
try:
pr = await bot.open_idea_pr(
user.as_actor(),
@@ -1028,6 +1076,7 @@ def make_router(
file_contents=contents,
pr_title=pr_title,
pr_description=pr_description,
rfcs_dir=rfcs_dir,
)
except GiteaError as e:
raise HTTPException(502, f"Gitea: {e.detail}")
@@ -1076,6 +1125,16 @@ def make_router(
auth.require_project_readable(user, project_id)
return await _propose_into_project(project_id, payload, user)
@router.post("/api/projects/{project_id}/collections/{collection_id}/rfcs/propose")
async def propose_collection_rfc(
project_id: str, collection_id: str, payload: ProposeBody, request: Request
) -> dict[str, Any]:
# §22 S2: propose a new entry into a specific collection of a project.
user = auth.require_contributor(request)
auth.require_project_readable(user, project_id)
_require_collection_in_project(collection_id, project_id)
return await _propose_into_collection(project_id, collection_id, payload, user)
# ---------------------------------------------------------------
# §9.1 Slice 2 (roadmap #27): Claude Haiku tag suggestions as the
# propose-RFC fields fill in. The modal debounce-posts the partial