Files
devplacepy/devplacepy/database/__init__.py
T
retoor 8e9d3fad98 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.
2026-08-09 00:18:20 +02:00

331 lines
12 KiB
Python

# retoor <retoor@molodetz.nl>
from .core import dataset, logging, Path, or_, defaultdict, datetime, timedelta, timezone, TTLCache, DATABASE_URL, DEFAULT_CORRECTION_PROMPT, DEFAULT_MODIFIER_PROMPT, INTERNAL_GATEWAY_URL, ensure_data_dirs, logger, db
from .core import refresh_snapshot, _local_cache_versions, _cache_version_cache, _cache_state_ready, _ensure_cache_state, get_cache_version, bump_cache_version, sync_local_cache, _index, _drop_index, _uid_index, get_table, _in_clause, _now_iso
from .atomic import conditional_update_row
from .settings import _settings_cache, get_setting, get_int_setting, set_setting, clear_settings_cache, internal_gateway_key
from .users import get_users_by_uids, _admins_cache, invalidate_admins_cache, get_admin_uids, set_user_timezone, set_last_seen, get_online_users, get_primary_admin_uid, is_account_active, search_users_by_username
from .relations import _relations_cache, get_user_relations, get_blocked_uids, get_muted_uids, get_silenced_uids, invalidate_user_relations
from .pagination import PAGE_SIZE, paginate, interleave_by_author, paginate_diverse, get_user_post_count, clear_user_post_count, build_pagination
from .soft_delete import SOFT_DELETE_TABLES, ensure_soft_delete_columns, soft_delete, soft_delete_in, restore, purge, list_deleted, count_deleted, restore_event, purge_event
from .engagement import _comment_count_cache, get_comment_counts_by_post_uids, get_post_counts_by_user_uids, get_vote_counts, get_user_votes, get_reactions_by_targets, get_user_bookmarks, get_polls_by_post_uids, get_poll_for_post
from .usage import _add_usage, _get_usage, add_correction_usage, get_correction_usage, add_modifier_usage, get_modifier_usage, NEWS_USAGE_KEY, add_news_usage, get_news_usage, ISSUE_USAGE_KEY, add_issue_usage, get_issue_usage, SEO_USAGE_KEY, add_seo_usage, get_seo_usage, AWARD_USAGE_KEY, add_award_usage, get_award_usage
from .awards import (
AWARDS_PER_PAGE,
award_display_hours,
award_give_cooldown_hours,
award_receive_cooldown_hours,
award_is_prominent,
can_give_award,
can_receive_award,
count_published_awards,
enrich_award,
get_prominent_award,
get_user_awards,
has_giver_cooldown,
has_receiver_cooldown,
recompute_user_award_stats,
revoke_award,
)
from .seo_meta import SEO_META_TYPES, get_seo_metadata, get_seo_metadata_batch, has_fresh_seo_metadata, upsert_seo_metadata, mark_seo_metadata_stale
from .activity import record_activity, record_unique_activity, get_user_activity, _activity_cache, _ACTIVITY_TABLES, get_activity_calendar, _activity_level, get_first_activity_date, HEATMAP_WEEKS, get_activity_heatmap, get_activity_months, get_streaks
from .customization import CUSTOMIZATION_GLOBAL_SCOPE, CUSTOMIZATION_LANGS, _customizations_cache, _customization_key, CUSTOMIZATION_PREF_COLUMNS, get_customization_prefs, set_customization_pref, get_custom_overrides, get_custom_override, list_custom_overrides, set_custom_override, delete_custom_override
from .email import EMAIL_ACCOUNT_DEFAULTS, list_email_accounts, get_email_account, set_email_account, delete_email_account
from .notifications import NOTIFICATION_TYPES, NOTIFICATION_CHANNELS, _NOTIFICATION_CHANNEL_COLUMNS, _NOTIFICATION_CHANNEL_DEFAULTS, _NOTIFICATION_TYPE_KEYS, _notification_prefs_cache, _notification_default, get_notification_default, set_notification_default, _notification_overrides, notification_enabled, get_notification_prefs, set_notification_pref, reset_notification_prefs, mark_notifications_read_by_target
from .forks import record_fork, get_fork_parent, count_forks, soft_delete_fork_relations, delete_fork_relations
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
from .stats import _stats_cache, get_site_stats, _analytics_cache, get_platform_analytics, _gist_languages_cache, get_gist_languages
from .schema import BUG_TABLE_RENAMES, migrate_bug_tables_to_issue_tables, init_db, _refresh_query_planner_stats, OLD_GATEWAY_URL, migrate_ai_gateway_settings, backfill_api_keys, _backfill_gamification
__all__ = [
"dataset",
"logging",
"Path",
"or_",
"defaultdict",
"datetime",
"timedelta",
"timezone",
"TTLCache",
"DATABASE_URL",
"DEFAULT_CORRECTION_PROMPT",
"DEFAULT_MODIFIER_PROMPT",
"INTERNAL_GATEWAY_URL",
"ensure_data_dirs",
"logger",
"db",
"refresh_snapshot",
"_local_cache_versions",
"_cache_version_cache",
"_cache_state_ready",
"_ensure_cache_state",
"get_cache_version",
"bump_cache_version",
"sync_local_cache",
"_index",
"_drop_index",
"_uid_index",
"get_table",
"_in_clause",
"_now_iso",
"conditional_update_row",
"_settings_cache",
"get_setting",
"get_int_setting",
"set_setting",
"clear_settings_cache",
"internal_gateway_key",
"get_users_by_uids",
"_admins_cache",
"invalidate_admins_cache",
"get_admin_uids",
"set_user_timezone",
"set_last_seen",
"get_online_users",
"get_primary_admin_uid",
"is_account_active",
"search_users_by_username",
"_relations_cache",
"get_user_relations",
"get_blocked_uids",
"get_muted_uids",
"get_silenced_uids",
"invalidate_user_relations",
"PAGE_SIZE",
"paginate",
"interleave_by_author",
"paginate_diverse",
"get_user_post_count",
"clear_user_post_count",
"build_pagination",
"SOFT_DELETE_TABLES",
"ensure_soft_delete_columns",
"soft_delete",
"soft_delete_in",
"restore",
"purge",
"list_deleted",
"count_deleted",
"restore_event",
"purge_event",
"_comment_count_cache",
"get_comment_counts_by_post_uids",
"get_post_counts_by_user_uids",
"get_vote_counts",
"get_user_votes",
"get_reactions_by_targets",
"get_user_bookmarks",
"get_polls_by_post_uids",
"get_poll_for_post",
"_add_usage",
"_get_usage",
"add_correction_usage",
"get_correction_usage",
"add_modifier_usage",
"get_modifier_usage",
"NEWS_USAGE_KEY",
"add_news_usage",
"get_news_usage",
"ISSUE_USAGE_KEY",
"add_issue_usage",
"get_issue_usage",
"SEO_USAGE_KEY",
"add_seo_usage",
"get_seo_usage",
"SEO_META_TYPES",
"get_seo_metadata",
"get_seo_metadata_batch",
"has_fresh_seo_metadata",
"upsert_seo_metadata",
"mark_seo_metadata_stale",
"record_activity",
"record_unique_activity",
"get_user_activity",
"_activity_cache",
"_ACTIVITY_TABLES",
"get_activity_calendar",
"_activity_level",
"get_first_activity_date",
"HEATMAP_WEEKS",
"get_activity_heatmap",
"get_activity_months",
"get_streaks",
"CUSTOMIZATION_GLOBAL_SCOPE",
"CUSTOMIZATION_LANGS",
"_customizations_cache",
"_customization_key",
"CUSTOMIZATION_PREF_COLUMNS",
"get_customization_prefs",
"set_customization_pref",
"get_custom_overrides",
"get_custom_override",
"list_custom_overrides",
"set_custom_override",
"delete_custom_override",
"EMAIL_ACCOUNT_DEFAULTS",
"list_email_accounts",
"get_email_account",
"set_email_account",
"delete_email_account",
"NOTIFICATION_TYPES",
"NOTIFICATION_CHANNELS",
"_NOTIFICATION_CHANNEL_COLUMNS",
"_NOTIFICATION_CHANNEL_DEFAULTS",
"_NOTIFICATION_TYPE_KEYS",
"_notification_prefs_cache",
"_notification_default",
"get_notification_default",
"set_notification_default",
"_notification_overrides",
"notification_enabled",
"get_notification_prefs",
"set_notification_pref",
"reset_notification_prefs",
"mark_notifications_read_by_target",
"record_fork",
"get_fork_parent",
"count_forks",
"soft_delete_fork_relations",
"delete_fork_relations",
"get_follow_counts",
"get_follow_list",
"get_following_among",
"_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",
"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",
"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",
"get_recent_comments_by_target_uids",
"get_recent_comments_by_post_uids",
"load_comments_by_target_uids",
"resolve_by_slug",
"resolve_object_url",
"get_uids_by_username_match",
"text_search_clause",
"get_daily_topic",
"get_featured_news",
"get_trending_topics",
"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",
"_stats_cache",
"get_site_stats",
"_analytics_cache",
"get_platform_analytics",
"_gist_languages_cache",
"get_gist_languages",
"BUG_TABLE_RENAMES",
"migrate_bug_tables_to_issue_tables",
"init_db",
"_refresh_query_planner_stats",
"OLD_GATEWAY_URL",
"migrate_ai_gateway_settings",
"backfill_api_keys",
"_backfill_gamification",
]