sync update from LAN repo (source dhk-1 @ 3b1ec5b4)

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

No files matched your search

@@ -10,7 +10,7 @@ from __future__ import annotations
import json
from typing import Any, Literal
from fastapi import APIRouter, HTTPException, Path, Request
from fastapi import APIRouter, HTTPException, Path, Query, Request
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, field_validator
from app.gateway.deps import get_task_deeplink_detail_store
@@ -19,6 +19,7 @@ router = APIRouter(prefix="/api/task-deeplink-details", tags=["task-deeplink-det
_B5_DETAIL_TYPE = "b5-1"
_MAX_EXTRA_BYTES = 64 * 1024
_MAX_SUB_TYPE = 64
class TaskDeeplinkDetailUpsertRequest(BaseModel):
@@ -26,7 +27,9 @@ class TaskDeeplinkDetailUpsertRequest(BaseModel):
``extra`` is intentionally an object, not a bag of top-level fields, so
the stable contract stays small while integrators can attach their own
structured fields.
structured fields. ``type`` is the business type (the deep-link
``?type=``): the same task id may hold one snapshot per type; omitting it
addresses the legacy no-type partition.
"""
model_config = ConfigDict(populate_by_name=True)
@@ -42,9 +45,14 @@ class TaskDeeplinkDetailUpsertRequest(BaseModel):
validation_alias=AliasChoices("taskContent", "task_content"),
)
overview: str = Field(default="", max_length=4_000)
detail_sub_type: str = Field(
default="",
max_length=_MAX_SUB_TYPE,
validation_alias=AliasChoices("type", "taskType", "sub_type"),
)
extra: dict[str, Any] = Field(default_factory=dict)
@field_validator("task_name", "task_content", "overview")
@field_validator("task_name", "task_content", "overview", "detail_sub_type")
@classmethod
def strip_text(cls, value: str) -> str:
return value.strip()
@@ -73,6 +81,7 @@ class TaskDeeplinkDetailResponse(BaseModel):
detail_type: Literal["b5-1"] = Field(serialization_alias="type")
detail_id: str = Field(serialization_alias="id")
detail_sub_type: str = Field(default="", serialization_alias="taskType")
task_name: str = Field(serialization_alias="taskName")
task_content: str = Field(serialization_alias="taskContent")
overview: str = ""
@@ -94,6 +103,7 @@ def _detail_response(row: dict[str, Any]) -> TaskDeeplinkDetailResponse:
return TaskDeeplinkDetailResponse(
detail_type=row["detail_type"],
detail_id=row["detail_id"],
detail_sub_type=str(row.get("sub_type") or ""),
task_name=row["task_name"],
task_content=row["task_content"],
overview=row.get("overview") or "",
@@ -110,24 +120,18 @@ def _normalized_detail_id(detail_id: str) -> str:
return normalized
def _is_administrator(request: Request) -> bool:
"""The external integration key is checked by AuthMiddleware.
def _writer_id(request: Request) -> str:
"""Audit identity for a snapshot write.
Browser-authenticated writes stay available to administrators for support
and testing, but ordinary users can only read the detail after login.
The PUT endpoint is credential-free (integration systems write before the
user's browser is involved), so an unauthenticated caller is recorded as
the external integrator; a logged-in browser write keeps the user id.
"""
if getattr(request.state, "task_deeplink_external_writer", False):
return True
user = getattr(request.state, "user", None)
return user is None or getattr(user, "system_role", None) == "admin"
def _writer_id(request: Request) -> str:
if getattr(request.state, "task_deeplink_external_writer", False):
return "external:task-deeplink"
user = getattr(request.state, "user", None)
return str(getattr(user, "id", "system"))
if user is not None:
return str(getattr(user, "id", "system"))
return "external:task-deeplink"
@router.get("/{detail_type}/{detail_id}", response_model=TaskDeeplinkDetailEnvelope)
@@ -135,11 +139,20 @@ async def get_task_deeplink_detail(
request: Request,
detail_type: Literal["b5-1"],
detail_id: str = Path(min_length=1, max_length=128),
type: str = Query(default="", max_length=_MAX_SUB_TYPE),
) -> TaskDeeplinkDetailEnvelope:
"""Read one persisted snapshot for a post-login deep-link workspace."""
"""Read one persisted snapshot for a post-login deep-link workspace.
``?type=`` selects the business-type partition written via the PUT body's
``type`` field; omitted means the legacy no-type partition.
"""
store = get_task_deeplink_detail_store(request)
row = await store.get_detail(detail_type, _normalized_detail_id(detail_id))
row = await store.get_detail(
detail_type,
_normalized_detail_id(detail_id),
sub_type=type.strip(),
)
if row is None:
raise HTTPException(status_code=404, detail="Task deep-link detail not found")
return TaskDeeplinkDetailEnvelope(data=_detail_response(row))
@@ -152,14 +165,18 @@ async def upsert_task_deeplink_detail(
detail_type: Literal["b5-1"],
detail_id: str = Path(min_length=1, max_length=128),
) -> TaskDeeplinkDetailUpsertEnvelope:
"""Idempotently create or update a snapshot before ``goPath=b5-1``."""
"""Idempotently create or update a snapshot before ``goPath=b5-1``.
Open write endpoint: no login session or integration key is required
(AuthMiddleware/CSRF let the PUT through); the writer is only recorded
for audit. Reads still require a normal authenticated session.
"""
if not _is_administrator(request):
raise HTTPException(status_code=403, detail="Only administrators may write task deep-link details")
store = get_task_deeplink_detail_store(request)
row, created, changed = await store.upsert_detail(
detail_type,
_normalized_detail_id(detail_id),
sub_type=body.detail_sub_type,
task_name=body.task_name,
task_content=body.task_content,
overview=body.overview,