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:
@@ -146,6 +146,23 @@ The profile **Media** tab (`/profile/{username}?tab=media`, public) is a paginat
|
||||
- **Devii:** `list_media` (public) and `delete_media` (auth, in `CONFIRM_REQUIRED`) in the catalog.
|
||||
- **Docs visibility (deliberate):** members and guests must never be told this is a *soft* delete. The public prose page `docs/media-gallery.html` (General) and the member-facing `media-delete` API endpoint (Profiles group) describe deletion as a plain "remove" - no soft-delete, restore, trash, or purge language. All moderation mechanics live on the admin-only `docs/media-moderation` prose page (`admin: True`) and in the admin API group (`media-restore`, `admin-media`, `admin-media-purge`, all `auth="admin"`), which `docs_search` excludes from member results and `routers/docs/` package 404s for non-admins. Because `docs_search._strip` keeps the text *inside* `{% if %}` blocks, admin content must live on a separate `admin: True` page, never inline-gated on a public page (a public page may only carry an admin-gated *link*). The member `MediaItemOut` schema omits `deleted_at`; the admin-only `AdminMediaItemOut` adds it.
|
||||
|
||||
## Moderation, consent and maturity tables (`database/moderation.py`)
|
||||
|
||||
Four soft-deletable tables carry the trust-and-safety layer; the full subsystem is documented in `devplacepy/services/moderation/CLAUDE.md`.
|
||||
|
||||
| Table | Shape | Notes |
|
||||
|---|---|---|
|
||||
| `content_reports` | `(target_type, target_uid)` + `reporter_uid` + `owner_uid` | The one queue. `owner_uid` is denormalised at insert so the admin list never N+1s. Indexes `(status, created_at)` for the queue and SLA scan, `(target_type, target_uid)` for duplicate detection, `(reporter_uid)`, `(owner_uid)` |
|
||||
| `moderation_actions` | one row per moderator decision, linked to its report | Queryable moderation state with its own lifecycle - deliberately separate from the append-only audit log, the same way `workspace_flags` is |
|
||||
| `content_maturity` | `(target_type, target_uid)` -> `level` | Polymorphic age label. Read through the batch helper `get_maturity_by_targets`, never per row. **Absence of a row means `general`**, so nothing needed backfilling |
|
||||
| `user_consents` | `(owner_kind, owner_id, kind)` | Append-only in effect: withdrawing stamps `withdrawn_at` on the current row and inserts a new one, so the history is provable |
|
||||
|
||||
`REPORTABLE_TARGETS` (target type -> table) is the registry every consumer reads, exactly like `VOTABLE_TARGETS`. `UNREPORTABLE_TABLES` is its explicit counterpart: each entry names a soft-deletable table and **why** it carries no reportable content. `tests/unit/database/moderation.py` asserts the two partition `SOFT_DELETE_TABLES`, so a new user-generated table cannot be added without classifying it.
|
||||
|
||||
The `users` columns added alongside are ensured in `backfill_api_keys()` like every other non-signup column: `terms_version`, `terms_accepted_at`, `age_band`, `age_declared_at`, `mature_opt_in`, `suspended_until`, `suspension_reason`, `deletion_requested_at`. `mature_opt_in` is normalised from NULL to `0` in the same `with db:` block that fixes the AI-modifier defaults, because it is read as a flag. **No date of birth is ever stored** - only the derived `age_band`.
|
||||
|
||||
Two atomic conditional updates protect this data and must never become read-then-write: `queue.claim_open` (report resolution) and `deletion.claim_deletion` (the account-deletion cascade). The latter's precondition is `COALESCE(deletion_requested_at, '') = ''` because the column is SQL `NULL` on rows that predate it - the exact `NULL = 0` trap recorded above.
|
||||
|
||||
## Role-based visibility (generic + DRY)
|
||||
|
||||
- **One source of truth for role/visibility checks**, registered as Jinja globals in `templating.py` - never hand-roll `user.get('role') == 'Admin'` or `user['uid'] == x['user_uid']` in a template again:
|
||||
@@ -192,6 +209,15 @@ Site settings are seeded on startup (`site_settings` table):
|
||||
| `maintenance_message` | scheduled-maintenance text | Body shown on the maintenance 503 page |
|
||||
| `customization_enabled` | `"1"` | When `"0"`, `custom_css_tag`/`custom_js_tag` inject nothing (feature off) |
|
||||
| `customization_js_enabled` | `"1"` | When `"0"`, custom CSS still serves but custom JS is suppressed |
|
||||
| `moderation_sla_hours` | `"24"` | The published moderation response window; the admin queue badge turns red past it |
|
||||
| `moderation_filter_mode` | `"review"` | `off`/`label`/`review`/`block` - how the content filter acts on a match |
|
||||
| `moderation_filter_review_score` | `"2"` | Rule score at which a match becomes a report rather than a label |
|
||||
| `moderation_minimum_age` | `"16"` | Signup floor; only the derived age band is stored |
|
||||
| `moderation_mature_default_hidden` | `"1"` | Hide mature-labelled content behind an interstitial by default |
|
||||
| `account_deletion_grace_hours` | `"24"` | Reversible window before a deleted account is purged |
|
||||
| `contact_email` / `contact_phone` / `contact_address` | `""` | Published contact details, rendered on `/docs/contact.html` |
|
||||
| `terms_version` / `privacy_version` / `guidelines_version` | `"1"` | Bumping `terms_version` forces re-acceptance before the next write. **Every reader uses `get_setting(key, "1") or "1"`** - an empty stored value must read as the default or the gate 403s every write |
|
||||
| `ai_third_party_provider` | `""` | Named in the consent copy and the privacy policy |
|
||||
| `extra_head` | `""` | Raw HTML emitted verbatim into every page `<head>` by `templating.extra_head_tag()`; site-wide trusted-admin input, not sanitized |
|
||||
|
||||
Besides the site/news/upload keys above, the **Operational** group is admin-editable at `/admin/settings`: `site_url` (public origin for absolute links incl. container ingress; resolved by `seo.public_base_url()` = setting -> `DEVPLACE_SITE_URL` env -> request origin), `rate_limit_per_minute`, `rate_limit_window_seconds`, `news_service_interval`, `session_max_age_days`, `session_remember_days`, `registration_open`, `maintenance_mode`, `maintenance_message`, `docs_search_mode` (`agent`|`bm25`, default `agent` - picks the `/docs/search.html` surface: the in-page Devii chat or the classic BM25 list; a viewer over their daily AI limit, or a guest when Devii is disabled, auto-falls-back to BM25 via `routers/docs/views.py` `_agent_search_state`), `outbound_proxy_url` (empty by default - when set, every `stealth.stealth_async_client`/`stealth_sync_client` call across the whole app routes through it via `stealth.configured_proxy_url()`; validated as `http(s)://`/`socks5(h)://` with a host in `AdminSettingsForm`; falls back to `DEVPLACE_OUTBOUND_PROXY_URL` when unset - see the "Outbound HTTP" note in the root `CLAUDE.md`). The **Custom Code** key `extra_head` is the sole key in the settings handler's `CLEARABLE_SETTINGS` set, so saving an empty textarea removes it (the default loop skips empty values).
|
||||
|
||||
@@ -36,6 +36,43 @@ from .forks import record_fork, get_fork_parent, count_forks, soft_delete_fork_r
|
||||
from .follows import get_follow_counts, get_follow_list, get_following_among
|
||||
from .deepsearch import _ds_now, create_deepsearch_session, update_deepsearch_session, get_deepsearch_session, add_deepsearch_message, get_deepsearch_messages, get_cached_deepsearch_url, upsert_deepsearch_url_cache
|
||||
from .ranking import VOTABLE_TARGETS, STAR_TARGETS, _authors_cache, _ranked_authors, _rank_map, get_top_authors, get_leaderboard, get_user_rank, get_user_stars, clear_user_stars, update_target_stars, soft_delete_engagement, delete_engagement, get_target_owner_uid
|
||||
from .moderation import (
|
||||
ACTIONS_TABLE,
|
||||
ADULT_AGE,
|
||||
AGE_BANDS,
|
||||
CONSENTS_TABLE,
|
||||
CONSENT_KINDS,
|
||||
CONSENT_STATES,
|
||||
MATURITY_LEVELS,
|
||||
MATURITY_SOURCES,
|
||||
MATURITY_TABLE,
|
||||
MATURITY_TARGETS,
|
||||
MODERATION_ACTIONS,
|
||||
MODERATION_TABLES,
|
||||
REPORTABLE_TARGETS,
|
||||
REPORTS_TABLE,
|
||||
REPORT_OPEN_STATUSES,
|
||||
REPORT_ORIGINS,
|
||||
REPORT_REASONS,
|
||||
REPORT_SEVERITIES,
|
||||
REPORT_STATUSES,
|
||||
SYSTEM_ACTOR,
|
||||
UNREPORTABLE_TABLES,
|
||||
age_band_for,
|
||||
band_allows_mature,
|
||||
band_allows_restricted,
|
||||
consent_granted,
|
||||
consent_state,
|
||||
get_maturity,
|
||||
get_maturity_by_targets,
|
||||
list_consents,
|
||||
minimum_age,
|
||||
report_reason_options,
|
||||
set_consent,
|
||||
set_maturity,
|
||||
suspension_active,
|
||||
years_between,
|
||||
)
|
||||
from .comments import _drop_blocked, _build_comment_items, load_comments, get_recent_comments_by_target_uids, get_recent_comments_by_post_uids, load_comments_by_target_uids
|
||||
from .content import resolve_by_slug, resolve_object_url, get_uids_by_username_match, text_search_clause, get_daily_topic, get_featured_news, get_trending_topics
|
||||
from .attachments_data import get_attachments, get_attachments_by_type, get_news_images_by_uids, delete_attachment_record, delete_attachments, _delete_attachment_file, get_user_media, get_user_attachments, get_user_attachment, get_deleted_media
|
||||
@@ -217,6 +254,40 @@ __all__ = [
|
||||
"soft_delete_engagement",
|
||||
"delete_engagement",
|
||||
"get_target_owner_uid",
|
||||
"ACTIONS_TABLE",
|
||||
"ADULT_AGE",
|
||||
"AGE_BANDS",
|
||||
"CONSENTS_TABLE",
|
||||
"CONSENT_KINDS",
|
||||
"CONSENT_STATES",
|
||||
"MATURITY_LEVELS",
|
||||
"MATURITY_SOURCES",
|
||||
"MATURITY_TABLE",
|
||||
"MATURITY_TARGETS",
|
||||
"MODERATION_ACTIONS",
|
||||
"MODERATION_TABLES",
|
||||
"REPORTABLE_TARGETS",
|
||||
"REPORTS_TABLE",
|
||||
"REPORT_OPEN_STATUSES",
|
||||
"REPORT_ORIGINS",
|
||||
"REPORT_REASONS",
|
||||
"REPORT_SEVERITIES",
|
||||
"REPORT_STATUSES",
|
||||
"SYSTEM_ACTOR",
|
||||
"UNREPORTABLE_TABLES",
|
||||
"age_band_for",
|
||||
"band_allows_mature",
|
||||
"band_allows_restricted",
|
||||
"consent_granted",
|
||||
"consent_state",
|
||||
"get_maturity",
|
||||
"get_maturity_by_targets",
|
||||
"list_consents",
|
||||
"report_reason_options",
|
||||
"set_consent",
|
||||
"set_maturity",
|
||||
"suspension_active",
|
||||
"years_between",
|
||||
"_drop_blocked",
|
||||
"_build_comment_items",
|
||||
"load_comments",
|
||||
|
||||
@@ -56,6 +56,44 @@ def resolve_object_url(target_type: str, target_uid: str) -> str:
|
||||
receiver = get_table("users").find_one(uid=award.get("receiver_uid", ""))
|
||||
if receiver:
|
||||
return f"/profile/{receiver['username']}?tab=awards#award-{award.get('slug', '')}"
|
||||
return "/feed"
|
||||
if target_type == "user":
|
||||
person = get_table("users").find_one(uid=target_uid)
|
||||
return f"/profile/{person['username']}" if person else "/feed"
|
||||
if target_type == "project_file":
|
||||
node = get_table("project_files").find_one(uid=target_uid)
|
||||
if not node:
|
||||
return "/projects"
|
||||
project = get_table("projects").find_one(uid=node.get("project_uid", ""))
|
||||
if not project:
|
||||
return "/projects"
|
||||
slug = project.get("slug") or project["uid"]
|
||||
return f"/projects/{slug}/files?path={node.get('path', '')}"
|
||||
if target_type == "attachment":
|
||||
attachment = get_table("attachments").find_one(uid=target_uid)
|
||||
if not attachment:
|
||||
return "/feed"
|
||||
parent_type = attachment.get("target_type") or ""
|
||||
parent_uid = attachment.get("target_uid") or ""
|
||||
if parent_type and parent_uid:
|
||||
return resolve_object_url(parent_type, parent_uid)
|
||||
owner = get_table("users").find_one(uid=attachment.get("user_uid", ""))
|
||||
return f"/profile/{owner['username']}?tab=media" if owner else "/feed"
|
||||
if target_type == "message":
|
||||
message = get_table("messages").find_one(uid=target_uid)
|
||||
if not message:
|
||||
return "/messages"
|
||||
return f"/messages?with_uid={message.get('sender_uid', '')}"
|
||||
if target_type == "poll":
|
||||
poll = get_table("polls").find_one(uid=target_uid)
|
||||
if not poll:
|
||||
return "/feed"
|
||||
return resolve_object_url("post", poll.get("post_uid", ""))
|
||||
if target_type == "workspace":
|
||||
instance = get_table("instances").find_one(uid=target_uid)
|
||||
return f"/admin/containers/{instance['uid']}" if instance else "/admin/containers"
|
||||
if target_type == "devii_output":
|
||||
return "/devii"
|
||||
return "/feed"
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from datetime import date, datetime, timezone
|
||||
|
||||
from .core import _in_clause, _now_iso, db, get_table
|
||||
from .settings import get_int_setting
|
||||
|
||||
|
||||
REPORTABLE_TARGETS: dict[str, str] = {
|
||||
"post": "posts",
|
||||
"comment": "comments",
|
||||
"gist": "gists",
|
||||
"project": "projects",
|
||||
"project_file": "project_files",
|
||||
"news": "news",
|
||||
"attachment": "attachments",
|
||||
"message": "messages",
|
||||
"quiz": "quizzes",
|
||||
"poll": "polls",
|
||||
"award": "awards",
|
||||
"user": "users",
|
||||
"issue": "issue_tickets",
|
||||
"workspace": "instances",
|
||||
"devii_output": "devii_conversations",
|
||||
}
|
||||
|
||||
|
||||
MATURITY_TARGETS: set[str] = {
|
||||
"post",
|
||||
"comment",
|
||||
"gist",
|
||||
"project",
|
||||
"news",
|
||||
"attachment",
|
||||
"quiz",
|
||||
}
|
||||
|
||||
|
||||
UNREPORTABLE_TABLES: dict[str, str] = {
|
||||
"news_images": "child rows of a reportable news article",
|
||||
"poll_options": "child rows of a reportable poll",
|
||||
"quiz_questions": "child rows of a reportable quiz",
|
||||
"quiz_options": "child rows of a reportable quiz",
|
||||
"tunnels": "child rows of a reportable workspace instance",
|
||||
"issue_comment_authors": "authorship index for reportable issue comments",
|
||||
"votes": "engagement counters, carry no authored content",
|
||||
"reactions": "engagement counters, carry no authored content",
|
||||
"bookmarks": "private to the owner",
|
||||
"follows": "relationship rows, carry no authored content",
|
||||
"poll_votes": "private ballots",
|
||||
"quiz_attempts": "private to the participant",
|
||||
"quiz_answers": "private to the participant",
|
||||
"sessions": "authentication state",
|
||||
"access_tokens": "authentication state",
|
||||
"devrant_tokens": "authentication state",
|
||||
"user_relations": "private block and mute lists",
|
||||
"notification_preferences": "private to the owner",
|
||||
"user_customizations": "runs only in the owner's own browser",
|
||||
"devii_tasks": "private to the owner",
|
||||
"devii_lessons": "private to the owner",
|
||||
"devii_virtual_tools": "private to the owner",
|
||||
"deepsearch_sessions": "private to the owner",
|
||||
"deepsearch_messages": "private to the owner",
|
||||
"isslop_analyses": "generated from a public URL, not authored content",
|
||||
"email_accounts": "private mailbox credentials",
|
||||
"instance_schedules": "child rows of a reportable workspace instance",
|
||||
"workspace_flags": "moderation records, not authored content",
|
||||
"content_reports": "moderation records, readable only by the reporter and moderators",
|
||||
"moderation_actions": "moderation records, not authored content",
|
||||
"content_maturity": "moderation labels, not authored content",
|
||||
"user_consents": "private consent history of the account holder",
|
||||
"backup_schedules": "operator configuration",
|
||||
"project_forks": "lineage index for reportable projects",
|
||||
"seo_metadata": "generated metadata for reportable content",
|
||||
}
|
||||
|
||||
|
||||
REPORT_REASONS: dict[str, str] = {
|
||||
"hate": "Hate speech or discriminatory content",
|
||||
"violence": "Realistic violence or threats",
|
||||
"weapons": "Weapons or dangerous instructions",
|
||||
"sexual": "Sexual or pornographic content",
|
||||
"religious": "Content targeting religion or belief",
|
||||
"misinformation": "False or misleading information",
|
||||
"exploitative": "Content exploiting a person",
|
||||
"harassment": "Harassment or bullying",
|
||||
"spam": "Spam or unwanted promotion",
|
||||
"intellectual_property": "Copyright or trademark infringement",
|
||||
"self_harm": "Self-harm or suicide",
|
||||
"illegal": "Illegal activity",
|
||||
"other": "Something else",
|
||||
}
|
||||
|
||||
|
||||
def report_reason_options() -> list[dict[str, str]]:
|
||||
return [{"key": key, "label": label} for key, label in REPORT_REASONS.items()]
|
||||
|
||||
|
||||
REPORT_STATUSES: tuple[str, ...] = ("open", "acknowledged", "actioned", "dismissed")
|
||||
REPORT_OPEN_STATUSES: tuple[str, ...] = ("open", "acknowledged")
|
||||
REPORT_SEVERITIES: tuple[str, ...] = ("info", "warn", "critical")
|
||||
REPORT_ORIGINS: tuple[str, ...] = ("member", "filter")
|
||||
|
||||
MODERATION_ACTIONS: tuple[str, ...] = (
|
||||
"remove_content",
|
||||
"restore_content",
|
||||
"warn",
|
||||
"suspend",
|
||||
"ban",
|
||||
"lift",
|
||||
"dismiss",
|
||||
"escalate",
|
||||
)
|
||||
|
||||
MATURITY_LEVELS: tuple[str, ...] = ("general", "mature", "restricted")
|
||||
MATURITY_SOURCES: tuple[str, ...] = ("author", "filter", "moderator")
|
||||
|
||||
CONSENT_KINDS: dict[str, str] = {
|
||||
"terms": "Terms of Service and Community Guidelines",
|
||||
"privacy": "Privacy Policy",
|
||||
"ai_third_party": "Processing of your content by a third-party AI provider",
|
||||
"activity_recording": "Recording of your presence and session activity",
|
||||
"container_credentials": (
|
||||
"Sharing your DevPlace credentials with software another member runs "
|
||||
"in a container"
|
||||
),
|
||||
}
|
||||
CONSENT_STATES: tuple[str, ...] = ("granted", "withdrawn")
|
||||
|
||||
AGE_BANDS: tuple[str, ...] = ("under_min", "13_15", "16_17", "adult")
|
||||
|
||||
ADULT_AGE = 18
|
||||
TEEN_AGE = 16
|
||||
YOUNG_TEEN_AGE = 13
|
||||
|
||||
MINIMUM_AGE_FLOOR = YOUNG_TEEN_AGE
|
||||
DEFAULT_MINIMUM_AGE = TEEN_AGE
|
||||
|
||||
SYSTEM_ACTOR = "system"
|
||||
|
||||
REPORTS_TABLE = "content_reports"
|
||||
ACTIONS_TABLE = "moderation_actions"
|
||||
MATURITY_TABLE = "content_maturity"
|
||||
CONSENTS_TABLE = "user_consents"
|
||||
|
||||
MODERATION_TABLES: tuple[str, ...] = (
|
||||
REPORTS_TABLE,
|
||||
ACTIONS_TABLE,
|
||||
MATURITY_TABLE,
|
||||
CONSENTS_TABLE,
|
||||
)
|
||||
|
||||
|
||||
def years_between(born: date, today: date) -> int:
|
||||
years = today.year - born.year
|
||||
if (today.month, today.day) < (born.month, born.day):
|
||||
years -= 1
|
||||
return years
|
||||
|
||||
|
||||
def minimum_age() -> int:
|
||||
return max(
|
||||
MINIMUM_AGE_FLOOR,
|
||||
get_int_setting("moderation_minimum_age", DEFAULT_MINIMUM_AGE),
|
||||
)
|
||||
|
||||
|
||||
def age_band_for(age: int) -> str:
|
||||
if age >= ADULT_AGE:
|
||||
return "adult"
|
||||
if age >= TEEN_AGE:
|
||||
return "16_17"
|
||||
if age >= YOUNG_TEEN_AGE:
|
||||
return "13_15"
|
||||
return "under_min"
|
||||
|
||||
|
||||
def band_allows_mature(band: str) -> bool:
|
||||
return band == "adult"
|
||||
|
||||
|
||||
def band_allows_restricted(band: str) -> bool:
|
||||
return band == "adult"
|
||||
|
||||
|
||||
def get_maturity_by_targets(target_type: str, uids: list[str]) -> dict[str, dict]:
|
||||
uids = [uid for uid in (uids or []) if uid]
|
||||
if not uids or MATURITY_TABLE not in db.tables:
|
||||
return {}
|
||||
placeholders, params = _in_clause(uids)
|
||||
params["tt"] = target_type
|
||||
rows = db.query(
|
||||
f"SELECT target_uid, level, source FROM {MATURITY_TABLE} "
|
||||
f"WHERE target_type = :tt AND target_uid IN ({placeholders}) "
|
||||
f"AND deleted_at IS NULL",
|
||||
**params,
|
||||
)
|
||||
return {
|
||||
row["target_uid"]: {"level": row["level"], "source": row["source"]}
|
||||
for row in rows
|
||||
}
|
||||
|
||||
|
||||
def get_maturity(target_type: str, target_uid: str) -> dict:
|
||||
found = get_maturity_by_targets(target_type, [target_uid])
|
||||
return found.get(target_uid, {"level": "general", "source": ""})
|
||||
|
||||
|
||||
def set_maturity(
|
||||
target_type: str, target_uid: str, level: str, source: str, set_by: str
|
||||
) -> dict | None:
|
||||
if target_type not in MATURITY_TARGETS or level not in MATURITY_LEVELS:
|
||||
return None
|
||||
from devplacepy.utils import generate_uid
|
||||
|
||||
table = get_table(MATURITY_TABLE)
|
||||
existing = table.find_one(target_type=target_type, target_uid=target_uid)
|
||||
now = _now_iso()
|
||||
if existing:
|
||||
table.update(
|
||||
{
|
||||
"id": existing["id"],
|
||||
"level": level,
|
||||
"source": source,
|
||||
"set_by": set_by,
|
||||
"updated_at": now,
|
||||
"deleted_at": None,
|
||||
"deleted_by": None,
|
||||
},
|
||||
["id"],
|
||||
)
|
||||
return table.find_one(id=existing["id"])
|
||||
uid = generate_uid()
|
||||
table.insert(
|
||||
{
|
||||
"uid": uid,
|
||||
"target_type": target_type,
|
||||
"target_uid": target_uid,
|
||||
"level": level,
|
||||
"source": source,
|
||||
"set_by": set_by,
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
"deleted_at": None,
|
||||
"deleted_by": None,
|
||||
}
|
||||
)
|
||||
return table.find_one(uid=uid)
|
||||
|
||||
|
||||
def list_consents(owner_kind: str, owner_id: str) -> list[dict]:
|
||||
if not owner_id or CONSENTS_TABLE not in db.tables:
|
||||
return []
|
||||
return list(
|
||||
get_table(CONSENTS_TABLE).find(
|
||||
owner_kind=owner_kind,
|
||||
owner_id=owner_id,
|
||||
deleted_at=None,
|
||||
order_by=["-created_at"],
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def consent_state(owner_kind: str, owner_id: str, kind: str) -> dict | None:
|
||||
if not owner_id or CONSENTS_TABLE not in db.tables:
|
||||
return None
|
||||
rows = list(
|
||||
get_table(CONSENTS_TABLE).find(
|
||||
owner_kind=owner_kind,
|
||||
owner_id=owner_id,
|
||||
kind=kind,
|
||||
deleted_at=None,
|
||||
order_by=["-created_at", "-id"],
|
||||
_limit=1,
|
||||
)
|
||||
)
|
||||
return rows[0] if rows else None
|
||||
|
||||
|
||||
def consent_granted(owner_kind: str, owner_id: str, kind: str) -> bool:
|
||||
row = consent_state(owner_kind, owner_id, kind)
|
||||
return bool(row and row.get("state") == "granted")
|
||||
|
||||
|
||||
def set_consent(
|
||||
owner_kind: str, owner_id: str, kind: str, granted: bool, version: str = "1"
|
||||
) -> dict | None:
|
||||
if kind not in CONSENT_KINDS or not owner_id:
|
||||
return None
|
||||
from devplacepy.utils import generate_uid
|
||||
|
||||
table = get_table(CONSENTS_TABLE)
|
||||
now = _now_iso()
|
||||
current = consent_state(owner_kind, owner_id, kind)
|
||||
if current and not granted and current.get("state") == "granted":
|
||||
table.update({"id": current["id"], "withdrawn_at": now}, ["id"])
|
||||
uid = generate_uid()
|
||||
table.insert(
|
||||
{
|
||||
"uid": uid,
|
||||
"owner_kind": owner_kind,
|
||||
"owner_id": owner_id,
|
||||
"kind": kind,
|
||||
"version": version,
|
||||
"state": "granted" if granted else "withdrawn",
|
||||
"granted_at": now if granted else "",
|
||||
"withdrawn_at": "" if granted else now,
|
||||
"created_at": now,
|
||||
"deleted_at": None,
|
||||
"deleted_by": None,
|
||||
}
|
||||
)
|
||||
return table.find_one(uid=uid)
|
||||
|
||||
|
||||
def suspension_active(user: dict | None) -> bool:
|
||||
if not user:
|
||||
return False
|
||||
until = (user.get("suspended_until") or "").strip()
|
||||
if not until:
|
||||
return False
|
||||
try:
|
||||
expiry = datetime.fromisoformat(until)
|
||||
except ValueError:
|
||||
return False
|
||||
if expiry.tzinfo is None:
|
||||
expiry = expiry.replace(tzinfo=timezone.utc)
|
||||
return expiry > datetime.now(timezone.utc)
|
||||
@@ -20,6 +20,7 @@ NOTIFICATION_TYPES = [
|
||||
{"key": "award", "label": "Awards", "description": "Someone gives you an award on your profile"},
|
||||
{"key": "quiz_attempt", "label": "Quiz attempts", "description": "Someone completes one of your quizzes"},
|
||||
{"key": "workspace", "label": "Dev workspaces", "description": "Idle, quota, retention and moderation notices for your dev workspaces"},
|
||||
{"key": "moderation", "label": "Moderation", "description": "Acknowledgements for reports you file and decisions taken on your content"},
|
||||
{"key": "system", "label": "System alerts", "description": "Platform infrastructure alerts (e.g. the AI gateway going down)"},
|
||||
]
|
||||
|
||||
|
||||
@@ -277,7 +277,7 @@ def init_db():
|
||||
defaults = {
|
||||
"site_name": "DevPlace",
|
||||
"site_description": "The Developer Social Network",
|
||||
"site_tagline": "Track industry shifts. Discover bold releases. Share what you are building in an open, uncensored environment.",
|
||||
"site_tagline": "Track industry shifts. Discover bold releases. Share what you are building in an open environment built by developers, for developers.",
|
||||
}
|
||||
for key, value in defaults.items():
|
||||
existing = db["site_settings"].find_one(key=key)
|
||||
@@ -558,6 +558,9 @@ def init_db():
|
||||
("slug", ""),
|
||||
("name", ""),
|
||||
("status", ""),
|
||||
("created_at", ""),
|
||||
("owner_uid", ""),
|
||||
("created_by", ""),
|
||||
("desired_state", ""),
|
||||
("container_id", ""),
|
||||
("ingress_slug", ""),
|
||||
@@ -1550,6 +1553,8 @@ def init_db():
|
||||
["user_uid", "created_at"],
|
||||
)
|
||||
|
||||
_ensure_moderation_tables()
|
||||
|
||||
for table in db.tables:
|
||||
_uid_index(db, table)
|
||||
|
||||
@@ -1688,6 +1693,19 @@ def init_db():
|
||||
"outbound_proxy_url": "",
|
||||
"devii_lessons_max_per_owner": "500",
|
||||
"devii_lessons_max_age_days": "90",
|
||||
"moderation_sla_hours": "24",
|
||||
"moderation_filter_mode": "review",
|
||||
"moderation_filter_review_score": "2",
|
||||
"moderation_minimum_age": "16",
|
||||
"moderation_mature_default_hidden": "1",
|
||||
"contact_email": "",
|
||||
"contact_phone": "",
|
||||
"contact_address": "",
|
||||
"terms_version": "1",
|
||||
"privacy_version": "1",
|
||||
"guidelines_version": "1",
|
||||
"ai_third_party_provider": "",
|
||||
"account_deletion_grace_hours": "24",
|
||||
}
|
||||
for key, value in operational_defaults.items():
|
||||
existing = db["site_settings"].find_one(key=key)
|
||||
@@ -1768,6 +1786,83 @@ def init_db():
|
||||
_refresh_query_planner_stats()
|
||||
|
||||
|
||||
MODERATION_COLUMNS: dict[str, tuple[tuple[str, object], ...]] = {
|
||||
"content_reports": (
|
||||
("uid", ""),
|
||||
("reporter_uid", ""),
|
||||
("target_type", ""),
|
||||
("target_uid", ""),
|
||||
("owner_uid", ""),
|
||||
("reason", ""),
|
||||
("detail", ""),
|
||||
("severity", "warn"),
|
||||
("status", "open"),
|
||||
("origin", "member"),
|
||||
("categories", ""),
|
||||
("resolved_by", ""),
|
||||
("resolved_at", ""),
|
||||
("created_at", ""),
|
||||
("updated_at", ""),
|
||||
),
|
||||
"moderation_actions": (
|
||||
("uid", ""),
|
||||
("report_uid", ""),
|
||||
("actor_uid", ""),
|
||||
("action", ""),
|
||||
("target_type", ""),
|
||||
("target_uid", ""),
|
||||
("subject_uid", ""),
|
||||
("reason", ""),
|
||||
("notes", ""),
|
||||
("expires_at", ""),
|
||||
("created_at", ""),
|
||||
),
|
||||
"content_maturity": (
|
||||
("uid", ""),
|
||||
("target_type", ""),
|
||||
("target_uid", ""),
|
||||
("level", "general"),
|
||||
("source", ""),
|
||||
("set_by", ""),
|
||||
("created_at", ""),
|
||||
("updated_at", ""),
|
||||
),
|
||||
"user_consents": (
|
||||
("uid", ""),
|
||||
("owner_kind", ""),
|
||||
("owner_id", ""),
|
||||
("kind", ""),
|
||||
("version", "1"),
|
||||
("state", ""),
|
||||
("granted_at", ""),
|
||||
("withdrawn_at", ""),
|
||||
("created_at", ""),
|
||||
),
|
||||
}
|
||||
|
||||
MODERATION_INDEXES: tuple[tuple[str, str, list[str]], ...] = (
|
||||
("content_reports", "idx_content_reports_queue", ["status", "created_at"]),
|
||||
("content_reports", "idx_content_reports_target", ["target_type", "target_uid"]),
|
||||
("content_reports", "idx_content_reports_reporter", ["reporter_uid"]),
|
||||
("content_reports", "idx_content_reports_owner", ["owner_uid"]),
|
||||
("moderation_actions", "idx_moderation_actions_report", ["report_uid"]),
|
||||
("moderation_actions", "idx_moderation_actions_subject", ["subject_uid"]),
|
||||
("moderation_actions", "idx_moderation_actions_created", ["created_at"]),
|
||||
("content_maturity", "idx_content_maturity_target", ["target_type", "target_uid"]),
|
||||
("user_consents", "idx_user_consents_owner", ["owner_kind", "owner_id", "kind"]),
|
||||
)
|
||||
|
||||
|
||||
def _ensure_moderation_tables() -> None:
|
||||
for table_name, columns in MODERATION_COLUMNS.items():
|
||||
table = get_table(table_name)
|
||||
for column, example in columns:
|
||||
if not table.has_column(column):
|
||||
table.create_column_by_example(column, example)
|
||||
for table_name, index_name, columns in MODERATION_INDEXES:
|
||||
_index(db, table_name, index_name, columns)
|
||||
|
||||
|
||||
def _refresh_query_planner_stats() -> None:
|
||||
try:
|
||||
has_stats = bool(
|
||||
@@ -1857,6 +1952,22 @@ def backfill_api_keys() -> int:
|
||||
users.create_column_by_example("last_award_slug", "")
|
||||
if not users.has_column("last_award_uid"):
|
||||
users.create_column_by_example("last_award_uid", "")
|
||||
if not users.has_column("terms_version"):
|
||||
users.create_column_by_example("terms_version", "")
|
||||
if not users.has_column("terms_accepted_at"):
|
||||
users.create_column_by_example("terms_accepted_at", "")
|
||||
if not users.has_column("age_band"):
|
||||
users.create_column_by_example("age_band", "")
|
||||
if not users.has_column("age_declared_at"):
|
||||
users.create_column_by_example("age_declared_at", "")
|
||||
if not users.has_column("mature_opt_in"):
|
||||
users.create_column_by_example("mature_opt_in", 0)
|
||||
if not users.has_column("suspended_until"):
|
||||
users.create_column_by_example("suspended_until", "")
|
||||
if not users.has_column("suspension_reason"):
|
||||
users.create_column_by_example("suspension_reason", "")
|
||||
if not users.has_column("deletion_requested_at"):
|
||||
users.create_column_by_example("deletion_requested_at", "")
|
||||
with db:
|
||||
db.query(
|
||||
"UPDATE users SET ai_modifier_enabled = 1 WHERE ai_modifier_enabled IS NULL"
|
||||
@@ -1871,6 +1982,7 @@ def backfill_api_keys() -> int:
|
||||
"UPDATE users SET interactions_enabled = -1 "
|
||||
"WHERE interactions_enabled IS NULL"
|
||||
)
|
||||
db.query("UPDATE users SET mature_opt_in = 0 WHERE mature_opt_in IS NULL")
|
||||
import uuid_utils
|
||||
|
||||
updated = 0
|
||||
|
||||
@@ -49,6 +49,10 @@ SOFT_DELETE_TABLES = [
|
||||
"quiz_options",
|
||||
"quiz_attempts",
|
||||
"quiz_answers",
|
||||
"content_reports",
|
||||
"moderation_actions",
|
||||
"content_maturity",
|
||||
"user_consents",
|
||||
]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user