Files
devplacepy/devplacepy/routers/news.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

164 lines
4.9 KiB
Python

# retoor <retoor@molodetz.nl>
import logging
from datetime import datetime, timedelta, timezone
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse
from devplacepy.database import (
get_maturity,
get_table,
db,
load_comments,
resolve_by_slug,
get_news_images_by_uids,
get_recent_comments_by_target_uids,
get_user_bookmarks,
paginate,
resolve_object_url,
mark_notifications_read_by_target,
)
from devplacepy.utils import get_current_user, time_ago, not_found
from devplacepy.content import canonical_redirect
from devplacepy.seo import (
base_seo_context,
website_schema,
site_url,
news_article_schema,
list_page_seo,
next_page_url,
)
from devplacepy.responses import respond
from devplacepy.schemas import NewsListOut, NewsDetailOut
logger = logging.getLogger(__name__)
router = APIRouter()
NEWS_MAX_AGE_DAYS = 4
@router.get("", response_class=HTMLResponse)
async def news_page(request: Request, before: str = None):
user = get_current_user(request)
cutoff = (
datetime.now(timezone.utc) - timedelta(days=NEWS_MAX_AGE_DAYS)
).isoformat()
news_table = get_table("news")
articles, next_cursor = paginate(
news_table,
before=before,
order=["-grade", "-synced_at"],
cursor_field="synced_at",
status="published",
synced_at={">=": cutoff},
)
article_uids = [a["uid"] for a in articles]
images_by_news = get_news_images_by_uids(article_uids)
recent_comments = get_recent_comments_by_target_uids(
"news", article_uids, 3, user
)
enriched = []
for a in articles:
enriched.append(
{
"article": a,
"time_ago": time_ago(a["synced_at"]),
"image_url": a.get("image_url", "") or images_by_news.get(a["uid"]),
"grade": a.get("grade", 0),
"featured": a.get("featured", 0),
"recent_comments": recent_comments.get(a["uid"], []),
}
)
seo_ctx = list_page_seo(
request,
title="Developer News",
description="Curated developer news and industry signals. Stay ahead with hand-picked articles.",
breadcrumbs=[
{"name": "Home", "url": "/feed"},
{"name": "News", "url": "/news"},
],
next_url=next_page_url(request, next_cursor),
)
return respond(
request,
"news.html",
{
**seo_ctx,
"request": request,
"user": user,
"articles": enriched,
"next_cursor": next_cursor,
},
model=NewsListOut,
)
@router.get("/{news_slug}", response_class=HTMLResponse)
async def news_detail_page(request: Request, news_slug: str):
user = get_current_user(request)
news_table = get_table("news")
article = resolve_by_slug(news_table, news_slug)
if not article:
raise not_found("News article not found")
redirect = canonical_redirect("news", article, news_slug)
if redirect:
return redirect
if user:
mark_notifications_read_by_target(
user["uid"], resolve_object_url("news", article["uid"])
)
image_url = article.get("image_url", "") or ""
if not image_url and "news_images" in db.tables:
img = get_table("news_images").find_one(
news_uid=article["uid"], deleted_at=None, is_placeholder=0
)
if img:
image_url = img["url"]
canonical_slug = article.get("slug", "") or article["uid"]
comments = load_comments("news", article["uid"], user)
bookmarked = bool(user) and article["uid"] in get_user_bookmarks(
user["uid"], "news", [article["uid"]]
)
base = site_url(request)
page_url = f"{base}/news/{canonical_slug}"
seo_ctx = base_seo_context(
request,
title=article.get("title", "News Article"),
description=article.get("description", "") or "",
seo_target=("news", article["uid"]),
og_type="article",
og_image=image_url or None,
breadcrumbs=[
{"name": "Home", "url": "/feed"},
{"name": "News", "url": "/news"},
{"name": article.get("title", "")[:60], "url": page_url},
],
schemas=[website_schema(base), news_article_schema(article, base, image_url)],
)
return respond(
request,
"news_detail.html",
{
**seo_ctx,
"request": request,
"user": user,
"article": article,
"canonical_slug": canonical_slug,
"image_url": image_url,
"grade": article.get("grade", 0),
"time_ago": time_ago(article["synced_at"]),
"comments": comments,
"bookmarked": bookmarked,
"maturity": get_maturity("news", article["uid"])["level"],
},
model=NewsDetailOut,
)