99 lines
3.4 KiB
Python
Raw Normal View History

# retoor <retoor@molodetz.nl>
from __future__ import annotations
from ..spec import Action
from ._shared import ATTACHMENTS, body, confirm, path, query
POSTS_ACTIONS: tuple[Action, ...] = (
Action(
name="view_feed",
method="GET",
path="/feed",
summary="View the activity feed",
requires_auth=False,
params=(
query("tab", "Feed tab to view."),
query("topic", "Filter by topic."),
query("search", "Search post title, content, and author username."),
query("before", "Pagination cursor."),
),
),
Action(
name="create_post",
method="POST",
path="/posts/create",
summary="Create a new post",
params=(
body("content", "Post body text.", required=True),
body("title", "Optional post title."),
body("topic", "Optional topic."),
body("project_uid", "Attach the post to a project uid."),
body("attachment_uids", ATTACHMENTS),
body("poll_question", "Optional poll question."),
body(
"poll_options",
"Poll options as a JSON array of strings, or one option per line, or comma separated. At least two are required for the poll to be created.",
),
Add Opinion Wars: week-long two-faction battles attached to posts A new post attachment type beside polls: the composer gains a Start Opinion War builder (same disabled-inputs opt-in as the poll builder) that names exactly two factions; the battle runs for exactly 7 days from post creation. Members join a side, may defect at any time (damage already dealt stays with the faction it was dealt to), and fight once per 24 hours per battle. A fight spends 25 Code Farm coins and deals deterministic level-weighted damage: 100 + 10 * min(level, 20) HP, so a newcomer deals 110 and a veteran caps at 300 - no randomness anywhere. The battle renders on the post card as a CSS pixel-art battlefield (box-shadow sprites: castles, faction flags, marching soldiers, a flickering campfire; steps() animation, disabled under reduced motion) with live HP bars, a countdown, the viewer's faction strip, top contributors and an event ticker. Live frames ride pub/sub on public.battle.{uid} via a relay on the service-lock owner, with the durable opinion_war_events trail (per-war atomic seq) as the source of truth and a 15s incremental poller as fallback. /battles lists battles with active/ended/mine filters, search and pagination. Every mutation is a conditional UPDATE via conditional_update_row: the fight sequence claims the cooldown first, then spends coins, then lands the damage, compensating earlier steps on any later refusal so a crash costs a turn, never coins. Resolution is lazy on read (no cron): an exactly-once CAS computes the winner in the statement, awards XP (participation, winner bonus, top damage dealer bonus; draws pay participation only), emits the result event and notifies fighters. The OpinionWarService backstop resolves unviewed wars and sends fight-ready notifications, exactly-once via a marker CAS. Fan-out: battle notification type, four badges, audit keys (battle.create/join/switch/fight/resolve), Devii actions (join/fight confirm-gated), API docs group, docs prose page, sitemap and topnav entries, REPORTABLE_TARGETS registration, post-delete cascades, README and nested CLAUDE.md documentation. Verified with the four-layer procedure: property checks over the full damage domain, 1200-step stateful fuzz (hp-sum invariant, coins never negative, resolved totals frozen), and real 8-process races proving exactly-once semantics for concurrent fights, double-spends across two wars, resolution XP and double-joins. Persisted tests in tests/unit/services/opinionwar, tests/api/battles, tests/e2e/battles and tests/api/posts/create.py. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-20 23:30:02 +02:00
body(
"war_faction_a",
"Optional Opinion War faction A name (max 30 chars). Both faction names are required to start the week-long battle.",
),
body(
"war_faction_b",
"Optional Opinion War faction B name (max 30 chars). Must differ from faction A.",
),
),
),
Action(
name="view_post",
method="GET",
path="/posts/{post_slug}",
summary="View a single post by slug",
requires_auth=False,
params=(
path(
"post_slug",
"Exact post slug copied from a /posts/... link in a feed or listing response; do not build it from the title.",
),
),
),
Action(
name="edit_post",
method="POST",
path="/posts/edit/{post_slug}",
summary="Edit an existing post",
params=(
path(
"post_slug",
"Exact post slug copied from a /posts/... link in a feed or listing response; do not build it from the title.",
),
body("content", "Updated post body.", required=True),
body("title", "Updated title."),
body("topic", "Updated topic."),
body(
"poll_question",
"Optional poll question. Adds a poll to a post that does not already have one.",
),
body(
"poll_options",
"Poll options as a JSON array of strings, or one option per line, or comma separated. At least two are required for the poll to be created.",
),
),
),
Action(
name="delete_post",
method="POST",
path="/posts/delete/{post_slug}",
summary="Delete a post (your own, or any post when you are an administrator). Soft delete, confirmation required",
params=(
path(
"post_slug",
"Exact post slug copied from a /posts/... link in a feed or listing response; do not build it from the title.",
),
confirm(),
),
),
)