Files
Module-uo/server/router/admin/uoLink.controller.js
wtclaude 582a55dde5
All checks were successful
PR Checks / server-tests (pull_request) Successful in 1m0s
PR Checks / client-build (pull_request) Successful in 1m0s
PR Checks / frozen-manifest (pull_request) Successful in 1m20s
feat(account): unlink game accounts on the shard before an account is deleted (MODULE_API 1.20.0)
ACCOUNT_DELETION.md §4.4 (AD10, AD11). api.onUserDeleting queues every linked
account in the new shard_pending_unlinks table, then calls the sidecar's
DELETE /link/{account} (actor web:account-deletion) and drops the mirror on 200/404.
A shard that is away leaves the row pending, retried on WS reconnect and on a live
server.hello; a protected account (403) is refused and listed on Admin -> Shard as
still linked on the shard. A live mob.login without the deleted user's webId clears
the row. coreApi ^1.20.0; core-ref pinned to website#232's merge (7bd93b8).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E14m6SuuY6i1vASFeGDBeY
2026-10-29 20:38:33 -05:00

158 lines
6.5 KiB
JavaScript

// ── Admin: uo-link sidecar control ─────────────────────────────────────────
//
// Configure the connection to the uo-link sidecar (base/ws URL, shared-secret
// token, protocol pin, enabled) and drive the town crier. SECURITY: the token
// is write-only over this API — stored encrypted, NEVER returned; responses
// expose only `hasToken` (same convention as the Discord bot token). Saving
// (re)starts the WS ingest client so a change takes effect with no redeploy.
const uoLinkConfig = require('../../model/uoLinkConfig/uoLinkConfig.model')
const uoLinkClient = require('../../utils/uoLinkClient')
const uoLinkSocket = require('../../utils/uoLinkSocket')
const shardBroadcast = require('../../utils/shardBroadcast')
const gameSignup = require('../../utils/gameSignup')
const accountDeletion = require('../../utils/accountDeletion')
const { activity } = require('../../core')
const log = require('../../core').logger('admin-uolink')
// Assemble the masked config + live health + ingestion stats for the panel, and
// the game accounts a website account's deletion left linked on the shard
// (ACCOUNT_DELETION.md §4.4): `pending` waits for the shard, `refused` is a
// protected staff account the shard's staff must clear by hand.
async function buildStatus() {
const config = await uoLinkConfig.getSafe()
const health = await uoLinkClient.health()
return {
...config,
health: health.ok ? health.data : { ok: false, error: health.error || `status ${health.status}` },
ingest: uoLinkSocket.getState(),
sse: shardBroadcast.stats(),
pendingUnlinks: await accountDeletion.listForAdmin(),
}
}
// GET /admin/uo-link/config — masked config + live status + ingestion stats.
async function getConfig(req, res) {
try {
return res.json(await buildStatus())
} catch (err) {
log.error('uoLink.getConfig', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// PUT /admin/uo-link/config — save connection settings + (re)start the socket.
async function saveConfig(req, res) {
const { baseUrl, wsUrl, token, protocol, enabled } = req.body
try {
const current = await uoLinkConfig.getSafe()
const willHaveToken = Boolean(token) || current.hasToken
if (enabled && !willHaveToken) {
return res.status(400).json({ message: 'An auth token is required before enabling.' })
}
await uoLinkConfig.save({
baseUrl,
wsUrl,
token,
protocol: protocol !== undefined ? Number(protocol) : undefined,
enabled,
updatedBy: req.user.id,
})
// Drop the client's cached config so the health check below uses the new values.
uoLinkClient.invalidateConfig()
// (Re)start or stop the ingest socket to match the new enabled/URL/token.
const saved = await uoLinkConfig.getSafe()
if (saved.enabled && saved.hasToken) {
await uoLinkSocket.start()
} else {
uoLinkSocket.stop()
await uoLinkConfig.recordStatus({ status: 'disconnected', pluginConnected: false })
}
await activity.log({ req, action: 'uoLink.config.update', detail: { baseUrl: saved.baseUrl, enabled: saved.enabled } })
log.info('uo-link config updated', { by: req.user.username, enabled: saved.enabled })
return res.json(await buildStatus())
} catch (err) {
log.error('uoLink.saveConfig', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// GET /admin/uo-link/signup-mode — whether this site creates game accounts.
//
// Core's Site Settings carried this field until slice 3, with help text naming
// Bridge.cfg. It reads as UO policy because it is: the site's mode and the
// shard's own SignupMode have to agree, and only one of those two is core's.
async function getSignupMode(req, res) {
try {
return res.json({ mode: await gameSignup.getMode(), modes: gameSignup.MODES })
} catch (err) {
log.error('uoLink.getSignupMode', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// PUT /admin/uo-link/signup-mode
async function saveSignupMode(req, res) {
const { mode } = req.body
try {
await gameSignup.setMode(mode, req.user.id)
await activity.log({ req, action: 'uoLink.signupMode.update', detail: { mode } })
log.info('game-signup mode updated', { by: req.user.username, mode })
return res.json({ mode })
} catch (err) {
log.error('uoLink.saveSignupMode', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// POST /admin/uo-link/towncrier — publish/replace a town-crier message.
async function postTownCrier(req, res) {
const { id, lines, durationSec } = req.body
try {
const result = await uoLinkClient.postTownCrier({ id, lines, durationSec })
if (result.ok) {
await activity.log({ req, action: 'uoLink.towncrier.post', detail: { id } })
return res.json(result.data || { ok: true, id })
}
if (result.status === 400) return res.status(400).json({ message: 'The shard rejected that message (over the line/duration caps?).' })
if (result.status === 503 || result.status === 0) {
return res.status(503).json({ message: 'The shard is unavailable right now.' })
}
return res.status(502).json({ message: 'Could not reach the shard.' })
} catch (err) {
log.error('uoLink.postTownCrier', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// DELETE /admin/uo-link/towncrier/:id — remove a town-crier message.
async function deleteTownCrier(req, res) {
const { id } = req.params
try {
const result = await uoLinkClient.deleteTownCrier(id)
if (result.ok) {
await activity.log({ req, action: 'uoLink.towncrier.delete', detail: { id } })
return res.json(result.data || { ok: true, id })
}
if (result.status === 404) return res.status(404).json({ message: 'No town-crier message with that id.' })
if (result.status === 503 || result.status === 0) {
return res.status(503).json({ message: 'The shard is unavailable right now.' })
}
return res.status(502).json({ message: 'Could not reach the shard.' })
} catch (err) {
log.error('uoLink.deleteTownCrier', err)
return res.status(500).json({ message: 'Internal Server Error' })
}
}
// GET /admin/uo-link/stream — the full live feed (incl. audit/cheat), staff only.
function stream(req, res) {
shardBroadcast.subscribe(req, res, 'admin')
}
module.exports = { getConfig, saveConfig, getSignupMode, saveSignupMode, postTownCrier, deleteTownCrier, stream }