Implement moderator management system with API endpoints for adding, removing, and checking moderators. Update server and documentation accordingly.
This commit is contained in:
1 parent
f27fcf7bf7
commit
016d998263
6 files changed
+459
-126
No files matched your search
+74
-6
@@ -62,6 +62,10 @@ Responses to API routes set permissive CORS headers (`Access-Control-Allow-Origi
|
||||
| `sessions.json` | Bearer session tokens + expiry |
|
||||
| `userdata.json` | Per-user synced state (roulette, list %, saved runs, roulette slots) |
|
||||
| `reset_tokens.json` | One-time password reset tokens + expiry |
|
||||
| `mods.json` | List of moderator usernames |
|
||||
| `bugreports.json` | Bug reports submitted via contact form |
|
||||
| `messages.json` | Private messages between users |
|
||||
| `config.json` | Server configuration (e.g., Discord webhook URL) |
|
||||
|
||||
These files are created on demand where noted below. Use `.gitignore` for `users.json`, `sessions.json`, and `userdata.json` if they contain real data.
|
||||
|
||||
@@ -88,7 +92,7 @@ Unless noted, bodies are **JSON** with `Content-Type: application/json`. Errors
|
||||
|
||||
#### `PUT /api/list`
|
||||
|
||||
- **Auth:** Admin Basic (if `ADMIN_PASSWORD` is set)
|
||||
- **Auth:** Admin Basic or Mod Bearer (if `ADMIN_PASSWORD` is set)
|
||||
- **Body:** `{ "text": "full data.txt content" }`
|
||||
- **200:** `{ "ok": true }` — writes `data.txt`, emits `list-update`
|
||||
|
||||
@@ -109,16 +113,40 @@ Run objects include (among others): `id`, `playerName`, `levelTitle`, `videoUrl`
|
||||
|
||||
#### `PUT /api/runs/:id` / `DELETE /api/runs/:id`
|
||||
|
||||
- **Auth:** Admin Basic
|
||||
- **Auth:** Admin Basic or Mod Bearer
|
||||
- **PUT body:** fields merged via `normalizeRun` with existing run
|
||||
200 / 404 / 400 as appropriate; emits `runs-update` on success
|
||||
|
||||
#### `POST /api/runs/bulk-approve`
|
||||
|
||||
- **Auth:** Admin Basic
|
||||
- **Auth:** Admin Basic or Mod Bearer
|
||||
- **Body:** `{ "playerName": "exact match", "reviewNotes?": "..." }`
|
||||
- **200:** `{ "ok": true, "approved": number, "playerName": "..." }` — approves all **pending** runs whose `playerName` matches (case-insensitive); may emit `runs-update`
|
||||
|
||||
### Mod management
|
||||
|
||||
#### `GET /api/mods`
|
||||
|
||||
- **Auth:** Mod Bearer
|
||||
- **200:** `{ "mods": [ "username", ... ] }`
|
||||
|
||||
#### `POST /api/mods`
|
||||
|
||||
- **Auth:** Mod Bearer
|
||||
- **Body:** `{ "username": "newmod" }`
|
||||
- **201:** `{ "ok": true, "mods": [ ... ] }` — adds user to mods list
|
||||
|
||||
#### `DELETE /api/mods`
|
||||
|
||||
- **Auth:** Mod Bearer
|
||||
- **Body:** `{ "username": "oldmod" }`
|
||||
- **200:** `{ "ok": true, "mods": [ ... ] }` — removes user from mods list
|
||||
|
||||
#### `GET /api/modcheck`
|
||||
|
||||
- **Auth:** Basic or Bearer
|
||||
- **200:** `{ "isMod": true/false, "username": "..." }` — checks if authenticated user is a mod
|
||||
|
||||
### Auth (FEDL user accounts)
|
||||
|
||||
#### `POST /api/auth/signup`
|
||||
@@ -194,24 +222,64 @@ Run objects include (among others): `id`, `playerName`, `levelTitle`, `videoUrl`
|
||||
- **Body:** `{ "data": { ...same fields as above... } }` — server sanitizes `savedRuns` and `rouletteSlots` (size limits and field trimming)
|
||||
- **200:** `{ "ok": true }`
|
||||
- **401 / 400**
|
||||
### Bug reports
|
||||
|
||||
#### `GET /api/bugreports`
|
||||
|
||||
- **Auth:** Admin Basic or Mod Bearer
|
||||
- **200:** `{ "items": [ bugreport, ... ] }`
|
||||
|
||||
#### `POST /api/bugreports`
|
||||
|
||||
- **Body:** `{ "category", "subject", "description", "email?" }`
|
||||
- **201:** `{ "ok": true, "item": bugreport }` — emits `bugreports-update`
|
||||
|
||||
#### `PUT /api/bugreports/:id` / `DELETE /api/bugreports/:id`
|
||||
|
||||
- **Auth:** Admin Basic or Mod Bearer
|
||||
- **PUT body:** fields to update (category, subject, description, email, status)
|
||||
- 200 / 404 / 400; emits `bugreports-update` on success
|
||||
|
||||
### Messages
|
||||
|
||||
#### `GET /api/messages`
|
||||
|
||||
- **Header:** `Authorization: Bearer <token>`
|
||||
- **200:** `{ "items": [ message, ... ] }` — user's messages
|
||||
|
||||
#### `POST /api/messages`
|
||||
|
||||
- **Header:** `Authorization: Bearer <token>`
|
||||
- **Body:** `{ "toUsername", "content" }`
|
||||
- **201:** `{ "ok": true, "item": message }` — emits `messages-update` to recipient
|
||||
|
||||
#### `GET /api/messages/conversation?with=username`
|
||||
|
||||
- **Header:** `Authorization: Bearer <token>`
|
||||
- **200:** `{ "items": [ message, ... ] }` — conversation with specified user
|
||||
|
||||
#### `POST /api/messages/search`
|
||||
|
||||
- **Header:** `Authorization: Bearer <token>`
|
||||
- **Body:** `{ "query": "username prefix" }`
|
||||
- **200:** `{ "items": [ { "username", "userId" }, ... ] }` — user search results
|
||||
### Imports (admin + external APIs)
|
||||
|
||||
#### `POST /api/import/pointercrate`
|
||||
|
||||
- **Auth:** Admin Basic
|
||||
- **Auth:** Admin Basic or Mod Bearer
|
||||
- Fetches Pointercrate records, maps to run shape, appends to `runs.json`
|
||||
- **200:** summary object with counts (see implementation)
|
||||
|
||||
#### `POST /api/import/aredl`
|
||||
|
||||
- **Auth:** Admin Basic
|
||||
- **Auth:** Admin Basic or Mod Bearer
|
||||
- Requires `AREDL_ACCESS_TOKEN` or `AREDL_API_KEY` configured
|
||||
- **200:** summary / **500** on configuration or API errors
|
||||
|
||||
#### `POST /api/import/targeted`
|
||||
|
||||
- **Auth:** Admin Basic
|
||||
- **Auth:** Admin Basic or Mod Bearer
|
||||
- **Body:** `{ "source": "pointercrate" | "aredl", "filter": "player" | "level", "query": "string" }`
|
||||
- Filters remote records and appends matching runs
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
["wolf_reaper90"]
|
||||
+128
-6
@@ -14,8 +14,26 @@ const userDataPath = path.join(__dirname, 'userdata.json');
|
||||
const resetTokensPath = path.join(__dirname, 'reset_tokens.json');
|
||||
const bugReportsPath = path.join(__dirname, 'bugreports.json');
|
||||
const messagesPath = path.join(__dirname, 'messages.json');
|
||||
const modsPath = path.join(__dirname, 'mods.json');
|
||||
const configPath = path.join(__dirname, 'config.json');
|
||||
|
||||
function readMods() {
|
||||
try {
|
||||
if (fs.existsSync(modsPath)) {
|
||||
const raw = fs.readFileSync(modsPath, 'utf8');
|
||||
const parsed = JSON.parse(raw);
|
||||
if (Array.isArray(parsed)) return parsed;
|
||||
}
|
||||
} catch (e) {}
|
||||
return [];
|
||||
}
|
||||
|
||||
function writeMods(mods) {
|
||||
fs.writeFileSync(modsPath, JSON.stringify(mods, null, 2) + '\n', 'utf8');
|
||||
}
|
||||
|
||||
let MOD_USERS = readMods();
|
||||
|
||||
const serverConfig = safeReadJsonFile(configPath, {}, 'config.json');
|
||||
const discordWebhookUrl = serverConfig.discordWebhookUrl || '';
|
||||
|
||||
@@ -153,13 +171,28 @@ function isAuthorized(req) {
|
||||
}
|
||||
}
|
||||
|
||||
function requireMod(req, res) {
|
||||
const token = getBearerToken(req);
|
||||
const sess = findSession(token);
|
||||
if (!sess) {
|
||||
setCors(res);
|
||||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Authentication required' }));
|
||||
return false;
|
||||
}
|
||||
const username = String(sess.username || '').toLowerCase();
|
||||
if (!MOD_USERS.includes(username)) {
|
||||
setCors(res);
|
||||
res.writeHead(403, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Not authorized' }));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function requireAdmin(req, res) {
|
||||
if (isAuthorized(req)) return true;
|
||||
setCors(res);
|
||||
res.setHeader('WWW-Authenticate', 'Basic realm="FEDL Admin"');
|
||||
res.writeHead(401, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||||
res.end('Authentication required');
|
||||
return false;
|
||||
return requireMod(req, res);
|
||||
}
|
||||
|
||||
function ensureRunsFile() {
|
||||
@@ -600,7 +633,6 @@ function safeReadBugReports() {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function readPosts() {
|
||||
ensurePostsFile();
|
||||
@@ -1696,6 +1728,95 @@ const server = http.createServer((req, res) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === 'DELETE' && pathname === '/api/runs/__authcheck__') {
|
||||
if (!requireAdmin(req, res)) return;
|
||||
res.writeHead(204);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === 'GET' && pathname === '/api/modcheck') {
|
||||
let username = '';
|
||||
const authHeader = String(req.headers.authorization || '');
|
||||
if (authHeader.startsWith('Basic ')) {
|
||||
try {
|
||||
const decoded = Buffer.from(authHeader.slice(6), 'base64').toString('utf8');
|
||||
const separatorIndex = decoded.indexOf(':');
|
||||
username = separatorIndex === -1 ? '' : decoded.slice(0, separatorIndex).toLowerCase();
|
||||
} catch (error) {
|
||||
sendJson(res, 400, { error: 'Invalid authorization header' });
|
||||
return;
|
||||
}
|
||||
} else if (authHeader.startsWith('Bearer ')) {
|
||||
const token = authHeader.slice(7);
|
||||
const sess = findSession(token);
|
||||
if (sess) {
|
||||
username = String(sess.username || '').toLowerCase();
|
||||
}
|
||||
}
|
||||
if (!username) {
|
||||
sendJson(res, 401, { error: 'Authorization required' });
|
||||
return;
|
||||
}
|
||||
const isMod = MOD_USERS.includes(username);
|
||||
sendJson(res, 200, { isMod: isMod, username: username });
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathname === '/api/mods') {
|
||||
if (req.method === 'GET') {
|
||||
if (!requireMod(req, res)) return;
|
||||
sendJson(res, 200, { mods: MOD_USERS });
|
||||
return;
|
||||
}
|
||||
if (req.method === 'POST') {
|
||||
if (!requireMod(req, res)) return;
|
||||
let body = '';
|
||||
req.on('data', chunk => { body += chunk; if (body.length > 65536) req.destroy(); });
|
||||
req.on('end', () => {
|
||||
try {
|
||||
const payload = JSON.parse(body || '{}');
|
||||
const newMod = String(payload.username || '').trim().toLowerCase();
|
||||
if (!newMod) {
|
||||
sendJson(res, 400, { error: 'username is required' });
|
||||
return;
|
||||
}
|
||||
if (MOD_USERS.includes(newMod)) {
|
||||
sendJson(res, 400, { error: 'User is already a mod' });
|
||||
return;
|
||||
}
|
||||
MOD_USERS.push(newMod);
|
||||
writeMods(MOD_USERS);
|
||||
sendJson(res, 201, { ok: true, mods: MOD_USERS });
|
||||
} catch (e) {
|
||||
sendJson(res, 400, { error: 'Invalid request body' });
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (req.method === 'DELETE') {
|
||||
if (!requireMod(req, res)) return;
|
||||
let body = '';
|
||||
req.on('data', chunk => { body += chunk; if (body.length > 65536) req.destroy(); });
|
||||
req.on('end', () => {
|
||||
try {
|
||||
const payload = JSON.parse(body || '{}');
|
||||
const removeMod = String(payload.username || '').trim().toLowerCase();
|
||||
if (!removeMod) {
|
||||
sendJson(res, 400, { error: 'username is required' });
|
||||
return;
|
||||
}
|
||||
MOD_USERS = MOD_USERS.filter(m => m !== removeMod);
|
||||
writeMods(MOD_USERS);
|
||||
sendJson(res, 200, { ok: true, mods: MOD_USERS });
|
||||
} catch (e) {
|
||||
sendJson(res, 400, { error: 'Invalid request body' });
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (req.method === 'POST' && pathname === '/api/runs') {
|
||||
let body = '';
|
||||
req.on('data', chunk => {
|
||||
@@ -2050,4 +2171,5 @@ server.listen(port, host, () => {
|
||||
console.log(`Legacy list fallback: ${legacyDataPath}`);
|
||||
console.log(`Using runs file: ${runsPath}`);
|
||||
console.log(`Admin password protection: ${adminPassword ? 'enabled' : 'disabled'}`);
|
||||
console.log(`Mods loaded from mods.json`);
|
||||
});
|
||||
Reference in new issue
Block a user