News post → town crier + Discord announcement pipeline

Replace the fire-and-forget Discord-only announce on publish with a
retry-safe, two-leg pipeline. When a post transitions into published-news
(false→true publish while in news, or category→news while published), an
announce_jobs row is enqueued with two INDEPENDENT delivery legs:

  • town crier — sidecar POST /towncrier via uoLinkClient (stable id
    `post-<id>` so a retry replaces rather than duplicates)
  • discord    — bot POST /internal/announce via botInternalClient
    (single source of truth for the #news channel stays in the bot)

An in-process poller (utils/announceWorker) sweeps the table every
ANNOUNCE_POLL_MS and dispatches each due leg with its own exponential
backoff (30s→2h, 6 attempts). A leg is retried on transient failures
(503/504/network) and failed fast on data/config errors (400 over-cap,
401/409). Publishing never blocks on the sidecar or Discord — enqueue is
local DB only. Parent `status` is a done/partial/failed rollup of the two
legs; posts.announced_at is stamped once both deliver.

Admin visibility: GET /admin/posts/:id/announce + a per-leg Retry
(POST .../announce/retry) surfaced in the PostEditor for news posts.

Pure decisions (text build/caps, classification, backoff, rollup) live in
announceJobs.logic and are unit-tested (server/test/announceJobs.test.js,
10 tests). The old manual /admin/uo-link/towncrier form is untouched.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0114TpmrNW4wNXsHq5CR72jQ
This commit is contained in:
2026-07-11 16:25:25 -05:00
parent a2590812e0
commit 986a8d5d86
16 changed files with 790 additions and 35 deletions

View File

@@ -112,6 +112,10 @@ export const api = {
deletePost: (id) => req(`/admin/posts/${id}`, { method: 'DELETE' }),
publishPost: (id, published) =>
req(`/admin/posts/${id}/publish`, { method: 'PATCH', body: { published } }),
// News announcement pipeline (town crier + Discord) status + per-leg retry.
getAnnounce: (id) => req(`/admin/posts/${id}/announce`),
retryAnnounceLeg: (id, leg) =>
req(`/admin/posts/${id}/announce/retry`, { method: 'POST', body: { leg } }),
uploadImage: (file) => {
const fd = new FormData()
fd.append('image', file)

View File

@@ -1,4 +1,4 @@
import { lazy, Suspense, useState } from 'react'
import { lazy, Suspense, useEffect, useState } from 'react'
import Modal from '../../../components/Modal.jsx'
import { api } from '../../../api/client.js'
@@ -105,6 +105,8 @@ export default function PostEditor({ post, onClose, onSaved }) {
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
{error && <p className="sans" style={{ margin: 0, color: '#d98b84', fontSize: '0.85rem' }}>{error}</p>}
{isEdit && post.category === 'news' && <AnnouncePanel postId={post.id} />}
<div style={{ display: 'flex', gap: 14, flexWrap: 'wrap' }}>
<label style={{ flex: '1 1 200px' }}>
<span className="field-label">Category</span>
@@ -173,3 +175,94 @@ const delStyle = {
cursor: 'pointer',
marginRight: 'auto',
}
// ── Announcement status panel ────────────────────────────────────────────────
// Shows the town-crier + Discord delivery state for a published news post and
// offers a per-leg retry (useful after fixing the sidecar / news channel without
// re-publishing). Only rendered for news posts in edit mode; renders nothing
// until the post has actually been announced (no job row yet → nothing to show).
const LEG_META = {
towncrier: { label: 'In-game town crier' },
discord: { label: 'Discord #news' },
}
const STATUS_STYLE = {
done: { color: '#7bbf8f', label: 'delivered' },
pending: { color: '#d9b84a', label: 'pending' },
failed: { color: '#d98b84', label: 'failed' },
}
function AnnouncePanel({ postId }) {
const [job, setJob] = useState(null)
const [loading, setLoading] = useState(true)
const [retrying, setRetrying] = useState('')
async function load() {
try {
setJob(await api.admin.getAnnounce(postId))
} catch {
setJob(null)
} finally {
setLoading(false)
}
}
useEffect(() => {
load()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [postId])
async function retry(leg) {
setRetrying(leg)
try {
setJob(await api.admin.retryAnnounceLeg(postId, leg))
} catch {
// leave the current state; the row simply didn't change
} finally {
setRetrying('')
}
}
if (loading || !job) return null
return (
<div style={panelStyle}>
<span className="field-label" style={{ marginBottom: 2 }}>Announcement</span>
{['towncrier', 'discord'].map((leg) => {
const status = job[`${leg}_status`]
const err = job[`${leg}_last_error`]
const s = STATUS_STYLE[status] || STATUS_STYLE.pending
return (
<div key={leg} style={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<span className="sans" style={{ fontSize: '0.85rem', minWidth: 140 }}>{LEG_META[leg].label}</span>
<span className="sans" style={{ fontSize: '0.8rem', color: s.color, fontWeight: 600 }}> {s.label}</span>
{status !== 'done' && (
<button
onClick={() => retry(leg)}
disabled={Boolean(retrying)}
className="pill"
style={{ marginLeft: 'auto', fontSize: '0.75rem', padding: '3px 12px' }}
>
{retrying === leg ? 'Retrying…' : 'Retry'}
</button>
)}
</div>
{status === 'failed' && err && (
<span className="sans" style={{ fontSize: '0.75rem', color: '#d98b84', paddingLeft: 148 }}>{err}</span>
)}
</div>
)
})}
</div>
)
}
const panelStyle = {
display: 'flex',
flexDirection: 'column',
gap: 8,
padding: '12px 14px',
borderRadius: 8,
border: '1px solid var(--line)',
background: 'rgba(255,255,255,0.02)',
}