Add the trust and safety subsystem and the App Store compliance work
Implements the moderation and consent obligations a social platform carries, so the web version and any client that speaks to it enforce the same rules. Moderation core (services/moderation/, database/moderation.py): a reportable target registry, the content filter and its choke points, the report queue with atomic resolution, enforcement actions, consent tracking, maturity gating, and account deletion with a grace window. Surfaces: POST /reports plus the member report list, /admin/moderation and the per-report admin view, /workspaces, terms acceptance at /auth/terms, consent and account deletion under /profile, the report button and dialog partials, the maturity gate, and the moderation stylesheet and ReportDialog client. Every user-generated surface stays reportable by construction: new content tables are registered in REPORTABLE_TARGETS or listed in UNREPORTABLE_TABLES with a reason, and the registry test fails the suite on anything left unclassified. Docs: community guidelines, content moderation, intellectual property, privacy, terms, contact, and the admin-only moderation operations page, plus the moderation API group and the Devii moderation actions. Compliance record: applecomp.md is the requirement register, applechanges.md the gap analysis against this codebase, and appleimpl.md the implementation design they resolve to. Tests cover the report flow, admin moderation, consent, account deletion, terms acceptance, workspaces, and the registry invariant across the unit, api, and e2e tiers.
This commit is contained in:
@@ -22,6 +22,13 @@ logger = logging.getLogger(__name__)
|
||||
APP_REFERENCE_PATTERN = re.compile(r"^[a-zA-Z0-9_.-]{1,30}$")
|
||||
DEFAULT_APP_REFERENCE = "default"
|
||||
|
||||
USER_CONTENT_OWNER_KINDS = ("user", "admin")
|
||||
|
||||
CONSENT_REQUIRED_MESSAGE = (
|
||||
"Third-party AI processing of your content requires consent. "
|
||||
"Grant it under Privacy on your profile."
|
||||
)
|
||||
|
||||
|
||||
def _validate_app_reference(value: str) -> str:
|
||||
stripped = (value or "").strip()
|
||||
@@ -515,6 +522,43 @@ class GatewayService(BaseService):
|
||||
return (kind, user.get("uid") or "unknown")
|
||||
return ("anonymous", "anonymous")
|
||||
|
||||
def user_content_owner(self, owner: tuple) -> str:
|
||||
if owner[0] in USER_CONTENT_OWNER_KINDS and owner[1]:
|
||||
return owner[1]
|
||||
return ""
|
||||
|
||||
def consent_denied(self, owner: tuple) -> bool:
|
||||
from devplacepy.database import consent_granted
|
||||
|
||||
owner_uid = self.user_content_owner(owner)
|
||||
if not owner_uid:
|
||||
return False
|
||||
return not consent_granted("user", owner_uid, "ai_third_party")
|
||||
|
||||
def _audit_consent_denied(self, owner: tuple, app_reference: str) -> None:
|
||||
from devplacepy.services.audit import record as audit
|
||||
from devplacepy.services.openai_gateway.usage import audit_actor_for
|
||||
|
||||
actor_kind, actor_uid, actor_role = audit_actor_for(owner[0], owner[1])
|
||||
audit.record_system(
|
||||
"ai.consent.denied",
|
||||
actor_kind=actor_kind,
|
||||
actor_uid=actor_uid,
|
||||
actor_role=actor_role,
|
||||
origin="api",
|
||||
result="denied",
|
||||
summary=(
|
||||
f"AI gateway call by {owner[0]}/{owner[1]} blocked - "
|
||||
f"third-party AI consent not granted"
|
||||
),
|
||||
metadata={
|
||||
"owner_kind": owner[0],
|
||||
"owner_id": owner[1],
|
||||
"app_reference": app_reference,
|
||||
"consent": "ai_third_party",
|
||||
},
|
||||
)
|
||||
|
||||
def _audit_quota_exceeded(
|
||||
self,
|
||||
owner_kind: str,
|
||||
@@ -584,6 +628,13 @@ class GatewayService(BaseService):
|
||||
)
|
||||
if subpath == "models" and request.method == "GET":
|
||||
return self._models_response()
|
||||
if self.consent_denied(owner):
|
||||
self.log(
|
||||
f"Rejected {owner[0]}:{owner[1]} app={app_reference}: "
|
||||
f"third-party AI consent not granted"
|
||||
)
|
||||
self._audit_consent_denied(owner, app_reference)
|
||||
raise HTTPException(status_code=403, detail=CONSENT_REQUIRED_MESSAGE)
|
||||
limit, scope, rule = quota.resolve(owner[0], owner[1], app_reference, cfg)
|
||||
if limit > 0:
|
||||
spent = quota.spent_24h(*scope)
|
||||
|
||||
Reference in New Issue
Block a user