tangled
alpha
login
or
join now
malpercio.dev
/
atbb
5
fork
atom
WIP! A BB-style forum, on the ATmosphere! We're still working... we'll be back soon when we have something to show off!
node
typescript
hono
htmx
atproto
5
fork
atom
overview
issues
pulls
pipelines
docs: missed committing a plan
malpercio.dev
3 weeks ago
547035f8
8421976f
+2230
1 changed file
expand all
collapse all
unified
split
docs
plans
2026-02-15-moderation-endpoints-implementation.md
+2230
docs/plans/2026-02-15-moderation-endpoints-implementation.md
···
1
1
+
# Moderation Action Write-Path API Endpoints Implementation Plan
2
2
+
3
3
+
> **For Claude:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.
4
4
+
5
5
+
**Goal:** Implement API endpoints for moderators to ban users, lock topics, and hide posts by writing modAction records to the Forum DID's PDS.
6
6
+
7
7
+
**Architecture:** Single route file (`mod.ts`) with 6 endpoints (ban/lock/hide + reversals). Each endpoint follows auth → permission → validation → duplicate check → PDS write pattern. ForumAgent writes modAction records to Forum DID's PDS, firehose indexes them asynchronously.
8
8
+
9
9
+
**Tech Stack:** Hono (routes), Drizzle ORM (database), @atproto/api (PDS writes), Vitest (tests)
10
10
+
11
11
+
**Design Reference:** `docs/plans/2026-02-15-moderation-endpoints-design.md`
12
12
+
13
13
+
---
14
14
+
15
15
+
## Task 1: Add Moderation Permissions to Default Roles
16
16
+
17
17
+
**Files:**
18
18
+
- Modify: `apps/appview/src/lib/seed-roles.ts`
19
19
+
20
20
+
**Step 1: Read existing seed-roles.ts to understand structure**
21
21
+
22
22
+
Read the file to see how default roles are defined.
23
23
+
24
24
+
**Step 2: Add moderation permissions to roles**
25
25
+
26
26
+
Update the role definitions in `seedDefaultRoles()`:
27
27
+
28
28
+
```typescript
29
29
+
// Admin role (after manageRoles permission)
30
30
+
{
31
31
+
name: "Admin",
32
32
+
permissions: [
33
33
+
"space.atbb.permission.manageRoles",
34
34
+
"space.atbb.permission.banUsers", // NEW
35
35
+
"space.atbb.permission.lockTopics", // NEW
36
36
+
"space.atbb.permission.moderatePosts", // NEW
37
37
+
],
38
38
+
priority: 10,
39
39
+
}
40
40
+
41
41
+
// Moderator role (after existing permissions if any, otherwise create)
42
42
+
{
43
43
+
name: "Moderator",
44
44
+
permissions: [
45
45
+
"space.atbb.permission.lockTopics", // NEW
46
46
+
"space.atbb.permission.moderatePosts", // NEW
47
47
+
],
48
48
+
priority: 20,
49
49
+
}
50
50
+
```
51
51
+
52
52
+
**Step 3: Commit permission updates**
53
53
+
54
54
+
```bash
55
55
+
git add apps/appview/src/lib/seed-roles.ts
56
56
+
git commit -m "feat(permissions): add moderation permissions to default roles (ATB-19)
57
57
+
58
58
+
- Admin: banUsers, lockTopics, moderatePosts
59
59
+
- Moderator: lockTopics, moderatePosts"
60
60
+
```
61
61
+
62
62
+
---
63
63
+
64
64
+
## Task 2: Create Mod Routes File Structure
65
65
+
66
66
+
**Files:**
67
67
+
- Create: `apps/appview/src/routes/mod.ts`
68
68
+
- Create: `apps/appview/src/routes/__tests__/mod.test.ts`
69
69
+
70
70
+
**Step 1: Create empty mod.ts route file**
71
71
+
72
72
+
Create the file with factory function skeleton:
73
73
+
74
74
+
```typescript
75
75
+
import { Hono } from "hono";
76
76
+
import type { AppContext } from "../lib/app-context.js";
77
77
+
import type { Variables } from "../types.js";
78
78
+
import { requireAuth } from "../middleware/auth.js";
79
79
+
import { requirePermission } from "../middleware/permissions.js";
80
80
+
import { modActions, users, memberships, posts } from "@atbb/db";
81
81
+
import { eq, desc, and } from "drizzle-orm";
82
82
+
import { isNetworkError } from "./helpers.js";
83
83
+
import { TID } from "@atproto/common";
84
84
+
85
85
+
export function createModRoutes(ctx: AppContext) {
86
86
+
const app = new Hono<{ Variables: Variables }>();
87
87
+
88
88
+
// Routes will go here
89
89
+
90
90
+
return app;
91
91
+
}
92
92
+
```
93
93
+
94
94
+
**Step 2: Register mod routes in main app**
95
95
+
96
96
+
Modify: `apps/appview/src/routes/index.ts`
97
97
+
98
98
+
Add import and route registration:
99
99
+
100
100
+
```typescript
101
101
+
import { createModRoutes } from "./mod.js";
102
102
+
103
103
+
// In createApiRoutes():
104
104
+
app.route("/mod", createModRoutes(ctx));
105
105
+
```
106
106
+
107
107
+
**Step 3: Create empty test file**
108
108
+
109
109
+
Create: `apps/appview/src/routes/__tests__/mod.test.ts`
110
110
+
111
111
+
```typescript
112
112
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
113
113
+
import { createTestContext, destroyTestContext } from "../../lib/__tests__/test-context.js";
114
114
+
import type { TestContext } from "../../lib/__tests__/test-context.js";
115
115
+
import { createApp } from "../../create-app.js";
116
116
+
117
117
+
describe("createModRoutes", () => {
118
118
+
let testCtx: TestContext;
119
119
+
let app: ReturnType<typeof createApp>;
120
120
+
121
121
+
beforeEach(async () => {
122
122
+
testCtx = await createTestContext();
123
123
+
app = createApp(testCtx.ctx);
124
124
+
});
125
125
+
126
126
+
afterEach(async () => {
127
127
+
await destroyTestContext(testCtx);
128
128
+
});
129
129
+
130
130
+
// Tests will go here
131
131
+
});
132
132
+
```
133
133
+
134
134
+
**Step 4: Commit skeleton**
135
135
+
136
136
+
```bash
137
137
+
git add apps/appview/src/routes/mod.ts apps/appview/src/routes/__tests__/mod.test.ts apps/appview/src/routes/index.ts
138
138
+
git commit -m "feat(mod): add mod routes skeleton (ATB-19)"
139
139
+
```
140
140
+
141
141
+
---
142
142
+
143
143
+
## Task 3: Helper Function - Validate Reason Field
144
144
+
145
145
+
**Files:**
146
146
+
- Modify: `apps/appview/src/routes/mod.ts`
147
147
+
- Modify: `apps/appview/src/routes/__tests__/mod.test.ts`
148
148
+
149
149
+
**Step 1: Write failing test for reason validation**
150
150
+
151
151
+
Add to mod.test.ts:
152
152
+
153
153
+
```typescript
154
154
+
describe("Helper: validateReason", () => {
155
155
+
it("returns null for valid reason", () => {
156
156
+
const { validateReason } = await import("../mod.js");
157
157
+
expect(validateReason("Spam")).toBeNull();
158
158
+
});
159
159
+
160
160
+
it("returns error for non-string reason", () => {
161
161
+
const { validateReason } = await import("../mod.js");
162
162
+
expect(validateReason(123 as any)).toBe("Reason is required and must be a string");
163
163
+
});
164
164
+
165
165
+
it("returns error for empty reason", () => {
166
166
+
const { validateReason } = await import("../mod.js");
167
167
+
expect(validateReason("")).toBe("Reason is required and must not be empty");
168
168
+
expect(validateReason(" ")).toBe("Reason is required and must not be empty");
169
169
+
});
170
170
+
171
171
+
it("returns error for reason exceeding 3000 characters", () => {
172
172
+
const { validateReason } = await import("../mod.js");
173
173
+
const longReason = "x".repeat(3001);
174
174
+
expect(validateReason(longReason)).toBe("Reason must not exceed 3000 characters");
175
175
+
});
176
176
+
});
177
177
+
```
178
178
+
179
179
+
**Step 2: Run test to verify it fails**
180
180
+
181
181
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "validateReason"`
182
182
+
183
183
+
Expected: FAIL - validateReason not exported
184
184
+
185
185
+
**Step 3: Implement validateReason helper**
186
186
+
187
187
+
Add to mod.ts before `createModRoutes`:
188
188
+
189
189
+
```typescript
190
190
+
/**
191
191
+
* Validate reason field (required, 1-3000 chars).
192
192
+
* @returns null if valid, error message string if invalid
193
193
+
*/
194
194
+
export function validateReason(reason: unknown): string | null {
195
195
+
if (typeof reason !== "string") {
196
196
+
return "Reason is required and must be a string";
197
197
+
}
198
198
+
199
199
+
if (reason.trim().length === 0) {
200
200
+
return "Reason is required and must not be empty";
201
201
+
}
202
202
+
203
203
+
if (reason.length > 3000) {
204
204
+
return "Reason must not exceed 3000 characters";
205
205
+
}
206
206
+
207
207
+
return null;
208
208
+
}
209
209
+
```
210
210
+
211
211
+
**Step 4: Run test to verify it passes**
212
212
+
213
213
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "validateReason"`
214
214
+
215
215
+
Expected: PASS (4 tests)
216
216
+
217
217
+
**Step 5: Commit**
218
218
+
219
219
+
```bash
220
220
+
git add apps/appview/src/routes/mod.ts apps/appview/src/routes/__tests__/mod.test.ts
221
221
+
git commit -m "feat(mod): add reason validation helper (ATB-19)
222
222
+
223
223
+
Validates reason field: required, non-empty, max 3000 chars"
224
224
+
```
225
225
+
226
226
+
---
227
227
+
228
228
+
## Task 4: Helper Function - Check Active Action
229
229
+
230
230
+
**Files:**
231
231
+
- Modify: `apps/appview/src/routes/mod.ts`
232
232
+
- Modify: `apps/appview/src/routes/__tests__/mod.test.ts`
233
233
+
234
234
+
**Step 1: Write failing test for checkActiveAction**
235
235
+
236
236
+
Add to mod.test.ts:
237
237
+
238
238
+
```typescript
239
239
+
describe("Helper: checkActiveAction", () => {
240
240
+
it("returns null when no actions exist", async () => {
241
241
+
const { checkActiveAction } = await import("../mod.js");
242
242
+
const result = await checkActiveAction(
243
243
+
testCtx.ctx,
244
244
+
{ did: "did:plc:test" },
245
245
+
"space.atbb.modAction.ban"
246
246
+
);
247
247
+
expect(result).toBeNull();
248
248
+
});
249
249
+
250
250
+
it("returns true when action is active (most recent)", async () => {
251
251
+
const { checkActiveAction } = await import("../mod.js");
252
252
+
253
253
+
// Insert ban action
254
254
+
await testCtx.ctx.db.insert(modActions).values({
255
255
+
did: testCtx.ctx.config.forumDid,
256
256
+
rkey: "test1",
257
257
+
cid: "bafytest1",
258
258
+
action: "space.atbb.modAction.ban",
259
259
+
subjectDid: "did:plc:test",
260
260
+
reason: "Test",
261
261
+
createdBy: "did:plc:admin",
262
262
+
createdAt: new Date(),
263
263
+
indexedAt: new Date(),
264
264
+
});
265
265
+
266
266
+
const result = await checkActiveAction(
267
267
+
testCtx.ctx,
268
268
+
{ did: "did:plc:test" },
269
269
+
"space.atbb.modAction.ban"
270
270
+
);
271
271
+
expect(result).toBe(true);
272
272
+
});
273
273
+
274
274
+
it("returns false when action is reversed (unban after ban)", async () => {
275
275
+
const { checkActiveAction } = await import("../mod.js");
276
276
+
277
277
+
// Insert ban
278
278
+
await testCtx.ctx.db.insert(modActions).values({
279
279
+
did: testCtx.ctx.config.forumDid,
280
280
+
rkey: "test1",
281
281
+
cid: "bafytest1",
282
282
+
action: "space.atbb.modAction.ban",
283
283
+
subjectDid: "did:plc:test",
284
284
+
reason: "Test",
285
285
+
createdBy: "did:plc:admin",
286
286
+
createdAt: new Date("2024-01-01"),
287
287
+
indexedAt: new Date("2024-01-01"),
288
288
+
});
289
289
+
290
290
+
// Insert unban (more recent)
291
291
+
await testCtx.ctx.db.insert(modActions).values({
292
292
+
did: testCtx.ctx.config.forumDid,
293
293
+
rkey: "test2",
294
294
+
cid: "bafytest2",
295
295
+
action: "space.atbb.modAction.unban",
296
296
+
subjectDid: "did:plc:test",
297
297
+
reason: "Appeal",
298
298
+
createdBy: "did:plc:admin",
299
299
+
createdAt: new Date("2024-01-02"),
300
300
+
indexedAt: new Date("2024-01-02"),
301
301
+
});
302
302
+
303
303
+
const result = await checkActiveAction(
304
304
+
testCtx.ctx,
305
305
+
{ did: "did:plc:test" },
306
306
+
"space.atbb.modAction.ban"
307
307
+
);
308
308
+
expect(result).toBe(false);
309
309
+
});
310
310
+
});
311
311
+
```
312
312
+
313
313
+
**Step 2: Run test to verify it fails**
314
314
+
315
315
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "checkActiveAction"`
316
316
+
317
317
+
Expected: FAIL - checkActiveAction not exported
318
318
+
319
319
+
**Step 3: Implement checkActiveAction helper**
320
320
+
321
321
+
Add to mod.ts:
322
322
+
323
323
+
```typescript
324
324
+
/**
325
325
+
* Subject reference for modAction (user DID or post URI).
326
326
+
*/
327
327
+
type ModSubject = { did: string } | { postUri: string };
328
328
+
329
329
+
/**
330
330
+
* Check if a specific moderation action is currently active.
331
331
+
* @returns true if active, false if reversed/inactive, null if no actions exist
332
332
+
*/
333
333
+
export async function checkActiveAction(
334
334
+
ctx: AppContext,
335
335
+
subject: ModSubject,
336
336
+
actionType: string
337
337
+
): Promise<boolean | null> {
338
338
+
try {
339
339
+
// Build where clause based on subject type
340
340
+
const whereClause = "did" in subject
341
341
+
? eq(modActions.subjectDid, subject.did)
342
342
+
: eq(modActions.subjectPostUri, subject.postUri);
343
343
+
344
344
+
// Get most recent action for this subject
345
345
+
const [mostRecent] = await ctx.db
346
346
+
.select()
347
347
+
.from(modActions)
348
348
+
.where(whereClause)
349
349
+
.orderBy(desc(modActions.createdAt))
350
350
+
.limit(1);
351
351
+
352
352
+
if (!mostRecent) {
353
353
+
return null; // No actions exist
354
354
+
}
355
355
+
356
356
+
// Check if most recent action matches the action type
357
357
+
return mostRecent.action === actionType;
358
358
+
} catch (error) {
359
359
+
console.error("Failed to check active action", {
360
360
+
subject,
361
361
+
actionType,
362
362
+
error: error instanceof Error ? error.message : String(error),
363
363
+
});
364
364
+
return null; // Fail safe: treat as no active action
365
365
+
}
366
366
+
}
367
367
+
```
368
368
+
369
369
+
**Step 4: Run test to verify it passes**
370
370
+
371
371
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "checkActiveAction"`
372
372
+
373
373
+
Expected: PASS (3 tests)
374
374
+
375
375
+
**Step 5: Commit**
376
376
+
377
377
+
```bash
378
378
+
git add apps/appview/src/routes/mod.ts apps/appview/src/routes/__tests__/mod.test.ts
379
379
+
git commit -m "feat(mod): add checkActiveAction helper (ATB-19)
380
380
+
381
381
+
Queries most recent modAction for a subject to determine if action is active"
382
382
+
```
383
383
+
384
384
+
---
385
385
+
386
386
+
## Task 5: Implement POST /api/mod/ban Endpoint
387
387
+
388
388
+
**Files:**
389
389
+
- Modify: `apps/appview/src/routes/mod.ts`
390
390
+
- Modify: `apps/appview/src/routes/__tests__/mod.test.ts`
391
391
+
392
392
+
**Step 1: Write failing test for ban endpoint**
393
393
+
394
394
+
Add to mod.test.ts:
395
395
+
396
396
+
```typescript
397
397
+
describe("POST /api/mod/ban", () => {
398
398
+
it("bans user successfully when admin has permission", async () => {
399
399
+
const admin = await testCtx.createUser("Admin");
400
400
+
const member = await testCtx.createUser("Member");
401
401
+
402
402
+
const mockPutRecord = vi.fn().mockResolvedValue({
403
403
+
uri: "at://did:plc:forum/space.atbb.modAction/abc123",
404
404
+
cid: "bafytest",
405
405
+
});
406
406
+
407
407
+
testCtx.ctx.forumAgent = {
408
408
+
isAuthenticated: () => true,
409
409
+
getAgent: () => ({
410
410
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
411
411
+
}),
412
412
+
} as any;
413
413
+
414
414
+
const res = await app.request("/api/mod/ban", {
415
415
+
method: "POST",
416
416
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
417
417
+
body: JSON.stringify({
418
418
+
targetDid: member.did,
419
419
+
reason: "Spam and harassment",
420
420
+
}),
421
421
+
});
422
422
+
423
423
+
expect(res.status).toBe(200);
424
424
+
const data = await res.json();
425
425
+
expect(data.success).toBe(true);
426
426
+
expect(data.action).toBe("ban");
427
427
+
expect(data.targetDid).toBe(member.did);
428
428
+
expect(data.uri).toBe("at://did:plc:forum/space.atbb.modAction/abc123");
429
429
+
expect(data.alreadyActive).toBe(false);
430
430
+
431
431
+
// Verify PDS write
432
432
+
expect(mockPutRecord).toHaveBeenCalledWith({
433
433
+
repo: testCtx.ctx.config.forumDid,
434
434
+
collection: "space.atbb.modAction",
435
435
+
rkey: expect.any(String),
436
436
+
record: {
437
437
+
$type: "space.atbb.modAction",
438
438
+
action: "space.atbb.modAction.ban",
439
439
+
subject: { did: member.did },
440
440
+
reason: "Spam and harassment",
441
441
+
createdBy: admin.did,
442
442
+
createdAt: expect.any(String),
443
443
+
},
444
444
+
});
445
445
+
});
446
446
+
});
447
447
+
```
448
448
+
449
449
+
**Step 2: Run test to verify it fails**
450
450
+
451
451
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "POST /api/mod/ban"`
452
452
+
453
453
+
Expected: FAIL - 404 Not Found (route doesn't exist)
454
454
+
455
455
+
**Step 3: Implement POST /api/mod/ban endpoint**
456
456
+
457
457
+
Add to mod.ts in `createModRoutes`:
458
458
+
459
459
+
```typescript
460
460
+
/**
461
461
+
* POST /api/mod/ban
462
462
+
* Ban a user from the forum.
463
463
+
*/
464
464
+
app.post(
465
465
+
"/ban",
466
466
+
requireAuth(ctx),
467
467
+
requirePermission(ctx, "space.atbb.permission.banUsers"),
468
468
+
async (c) => {
469
469
+
const user = c.get("user")!;
470
470
+
471
471
+
// Parse request body
472
472
+
let body: any;
473
473
+
try {
474
474
+
body = await c.req.json();
475
475
+
} catch {
476
476
+
return c.json({ error: "Invalid JSON in request body" }, 400);
477
477
+
}
478
478
+
479
479
+
const { targetDid, reason } = body;
480
480
+
481
481
+
// Validate targetDid
482
482
+
if (typeof targetDid !== "string" || !targetDid.startsWith("did:")) {
483
483
+
return c.json({ error: "Invalid DID format" }, 400);
484
484
+
}
485
485
+
486
486
+
// Validate reason
487
487
+
const reasonError = validateReason(reason);
488
488
+
if (reasonError) {
489
489
+
return c.json({ error: reasonError }, 400);
490
490
+
}
491
491
+
492
492
+
// Check target user exists (has membership)
493
493
+
const [membership] = await ctx.db
494
494
+
.select()
495
495
+
.from(memberships)
496
496
+
.where(eq(memberships.did, targetDid))
497
497
+
.limit(1);
498
498
+
499
499
+
if (!membership) {
500
500
+
return c.json({ error: "User is not a member of this forum" }, 404);
501
501
+
}
502
502
+
503
503
+
// Check if user is already banned
504
504
+
const isActive = await checkActiveAction(
505
505
+
ctx,
506
506
+
{ did: targetDid },
507
507
+
"space.atbb.modAction.ban"
508
508
+
);
509
509
+
510
510
+
if (isActive) {
511
511
+
return c.json({
512
512
+
success: true,
513
513
+
action: "ban",
514
514
+
targetDid,
515
515
+
alreadyActive: true,
516
516
+
}, 200);
517
517
+
}
518
518
+
519
519
+
// Get ForumAgent
520
520
+
if (!ctx.forumAgent) {
521
521
+
return c.json({
522
522
+
error: "Forum agent not available. Server configuration issue.",
523
523
+
}, 500);
524
524
+
}
525
525
+
526
526
+
const agent = ctx.forumAgent.getAgent();
527
527
+
if (!agent) {
528
528
+
return c.json({
529
529
+
error: "Forum agent not authenticated. Please try again later.",
530
530
+
}, 503);
531
531
+
}
532
532
+
533
533
+
// Write modAction record to Forum DID's PDS
534
534
+
try {
535
535
+
const result = await agent.com.atproto.repo.putRecord({
536
536
+
repo: ctx.config.forumDid,
537
537
+
collection: "space.atbb.modAction",
538
538
+
rkey: TID.nextStr(),
539
539
+
record: {
540
540
+
$type: "space.atbb.modAction",
541
541
+
action: "space.atbb.modAction.ban",
542
542
+
subject: { did: targetDid },
543
543
+
reason,
544
544
+
createdBy: user.did,
545
545
+
createdAt: new Date().toISOString(),
546
546
+
},
547
547
+
});
548
548
+
549
549
+
return c.json({
550
550
+
success: true,
551
551
+
action: "ban",
552
552
+
targetDid,
553
553
+
uri: result.uri,
554
554
+
cid: result.cid,
555
555
+
alreadyActive: false,
556
556
+
}, 200);
557
557
+
} catch (error) {
558
558
+
console.error("Failed to write ban modAction", {
559
559
+
operation: "POST /api/mod/ban",
560
560
+
targetDid,
561
561
+
error: error instanceof Error ? error.message : String(error),
562
562
+
});
563
563
+
564
564
+
if (error instanceof Error && isNetworkError(error)) {
565
565
+
return c.json({
566
566
+
error: "Unable to reach Forum PDS. Please try again later.",
567
567
+
}, 503);
568
568
+
}
569
569
+
570
570
+
return c.json({
571
571
+
error: "Failed to record moderation action. Please contact support.",
572
572
+
}, 500);
573
573
+
}
574
574
+
}
575
575
+
);
576
576
+
```
577
577
+
578
578
+
**Step 4: Run test to verify it passes**
579
579
+
580
580
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "POST /api/mod/ban.*successfully"`
581
581
+
582
582
+
Expected: PASS
583
583
+
584
584
+
**Step 5: Commit**
585
585
+
586
586
+
```bash
587
587
+
git add apps/appview/src/routes/mod.ts apps/appview/src/routes/__tests__/mod.test.ts
588
588
+
git commit -m "feat(mod): implement POST /api/mod/ban endpoint (ATB-19)
589
589
+
590
590
+
Bans user by writing modAction record to Forum DID's PDS"
591
591
+
```
592
592
+
593
593
+
---
594
594
+
595
595
+
## Task 6: Add Error Tests for POST /api/mod/ban
596
596
+
597
597
+
**Files:**
598
598
+
- Modify: `apps/appview/src/routes/__tests__/mod.test.ts`
599
599
+
600
600
+
**Step 1: Write authorization error tests**
601
601
+
602
602
+
Add to "POST /api/mod/ban" describe block:
603
603
+
604
604
+
```typescript
605
605
+
it("returns 401 when not authenticated", async () => {
606
606
+
const res = await app.request("/api/mod/ban", {
607
607
+
method: "POST",
608
608
+
body: JSON.stringify({ targetDid: "did:plc:test", reason: "Test" }),
609
609
+
});
610
610
+
611
611
+
expect(res.status).toBe(401);
612
612
+
});
613
613
+
614
614
+
it("returns 403 when user lacks banUsers permission", async () => {
615
615
+
const member = await testCtx.createUser("Member"); // No ban permission
616
616
+
617
617
+
const res = await app.request("/api/mod/ban", {
618
618
+
method: "POST",
619
619
+
headers: { Cookie: `atbb_session=${member.sessionToken}` },
620
620
+
body: JSON.stringify({ targetDid: "did:plc:other", reason: "Test" }),
621
621
+
});
622
622
+
623
623
+
expect(res.status).toBe(403);
624
624
+
const data = await res.json();
625
625
+
expect(data.error).toContain("Insufficient permissions");
626
626
+
});
627
627
+
```
628
628
+
629
629
+
**Step 2: Write input validation tests**
630
630
+
631
631
+
```typescript
632
632
+
it("returns 400 for invalid DID format", async () => {
633
633
+
const admin = await testCtx.createUser("Admin");
634
634
+
635
635
+
const res = await app.request("/api/mod/ban", {
636
636
+
method: "POST",
637
637
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
638
638
+
body: JSON.stringify({ targetDid: "invalid", reason: "Test" }),
639
639
+
});
640
640
+
641
641
+
expect(res.status).toBe(400);
642
642
+
const data = await res.json();
643
643
+
expect(data.error).toContain("Invalid DID format");
644
644
+
});
645
645
+
646
646
+
it("returns 400 for missing reason", async () => {
647
647
+
const admin = await testCtx.createUser("Admin");
648
648
+
649
649
+
const res = await app.request("/api/mod/ban", {
650
650
+
method: "POST",
651
651
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
652
652
+
body: JSON.stringify({ targetDid: "did:plc:test" }),
653
653
+
});
654
654
+
655
655
+
expect(res.status).toBe(400);
656
656
+
const data = await res.json();
657
657
+
expect(data.error).toContain("Reason is required");
658
658
+
});
659
659
+
660
660
+
it("returns 400 for empty reason", async () => {
661
661
+
const admin = await testCtx.createUser("Admin");
662
662
+
663
663
+
const res = await app.request("/api/mod/ban", {
664
664
+
method: "POST",
665
665
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
666
666
+
body: JSON.stringify({ targetDid: "did:plc:test", reason: " " }),
667
667
+
});
668
668
+
669
669
+
expect(res.status).toBe(400);
670
670
+
const data = await res.json();
671
671
+
expect(data.error).toContain("must not be empty");
672
672
+
});
673
673
+
674
674
+
it("returns 400 for malformed JSON", async () => {
675
675
+
const admin = await testCtx.createUser("Admin");
676
676
+
677
677
+
const res = await app.request("/api/mod/ban", {
678
678
+
method: "POST",
679
679
+
headers: {
680
680
+
Cookie: `atbb_session=${admin.sessionToken}`,
681
681
+
"Content-Type": "application/json",
682
682
+
},
683
683
+
body: "{ invalid json }",
684
684
+
});
685
685
+
686
686
+
expect(res.status).toBe(400);
687
687
+
const data = await res.json();
688
688
+
expect(data.error).toContain("Invalid JSON");
689
689
+
});
690
690
+
```
691
691
+
692
692
+
**Step 3: Write target validation test**
693
693
+
694
694
+
```typescript
695
695
+
it("returns 404 when target user has no membership", async () => {
696
696
+
const admin = await testCtx.createUser("Admin");
697
697
+
698
698
+
const res = await app.request("/api/mod/ban", {
699
699
+
method: "POST",
700
700
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
701
701
+
body: JSON.stringify({
702
702
+
targetDid: "did:plc:nonexistent",
703
703
+
reason: "Test",
704
704
+
}),
705
705
+
});
706
706
+
707
707
+
expect(res.status).toBe(404);
708
708
+
const data = await res.json();
709
709
+
expect(data.error).toContain("not a member");
710
710
+
});
711
711
+
```
712
712
+
713
713
+
**Step 4: Write idempotency test**
714
714
+
715
715
+
```typescript
716
716
+
it("returns alreadyActive: true when user already banned", async () => {
717
717
+
const admin = await testCtx.createUser("Admin");
718
718
+
const member = await testCtx.createUser("Member");
719
719
+
720
720
+
// Insert existing ban action
721
721
+
await testCtx.ctx.db.insert(modActions).values({
722
722
+
did: testCtx.ctx.config.forumDid,
723
723
+
rkey: "existing",
724
724
+
cid: "bafyexisting",
725
725
+
action: "space.atbb.modAction.ban",
726
726
+
subjectDid: member.did,
727
727
+
reason: "Previous ban",
728
728
+
createdBy: admin.did,
729
729
+
createdAt: new Date(),
730
730
+
indexedAt: new Date(),
731
731
+
});
732
732
+
733
733
+
const mockPutRecord = vi.fn();
734
734
+
testCtx.ctx.forumAgent = {
735
735
+
isAuthenticated: () => true,
736
736
+
getAgent: () => ({
737
737
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
738
738
+
}),
739
739
+
} as any;
740
740
+
741
741
+
const res = await app.request("/api/mod/ban", {
742
742
+
method: "POST",
743
743
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
744
744
+
body: JSON.stringify({ targetDid: member.did, reason: "Duplicate ban" }),
745
745
+
});
746
746
+
747
747
+
expect(res.status).toBe(200);
748
748
+
const data = await res.json();
749
749
+
expect(data.alreadyActive).toBe(true);
750
750
+
expect(mockPutRecord).not.toHaveBeenCalled(); // No duplicate write
751
751
+
});
752
752
+
```
753
753
+
754
754
+
**Step 5: Write error handling tests**
755
755
+
756
756
+
```typescript
757
757
+
it("returns 500 when ForumAgent not available", async () => {
758
758
+
const admin = await testCtx.createUser("Admin");
759
759
+
const member = await testCtx.createUser("Member");
760
760
+
761
761
+
testCtx.ctx.forumAgent = null; // Simulate unavailable agent
762
762
+
763
763
+
const res = await app.request("/api/mod/ban", {
764
764
+
method: "POST",
765
765
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
766
766
+
body: JSON.stringify({ targetDid: member.did, reason: "Test" }),
767
767
+
});
768
768
+
769
769
+
expect(res.status).toBe(500);
770
770
+
const data = await res.json();
771
771
+
expect(data.error).toContain("Forum agent not available");
772
772
+
});
773
773
+
774
774
+
it("returns 503 when ForumAgent not authenticated", async () => {
775
775
+
const admin = await testCtx.createUser("Admin");
776
776
+
const member = await testCtx.createUser("Member");
777
777
+
778
778
+
testCtx.ctx.forumAgent = {
779
779
+
isAuthenticated: () => false,
780
780
+
getAgent: () => null,
781
781
+
} as any;
782
782
+
783
783
+
const res = await app.request("/api/mod/ban", {
784
784
+
method: "POST",
785
785
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
786
786
+
body: JSON.stringify({ targetDid: member.did, reason: "Test" }),
787
787
+
});
788
788
+
789
789
+
expect(res.status).toBe(503);
790
790
+
const data = await res.json();
791
791
+
expect(data.error).toContain("not authenticated");
792
792
+
});
793
793
+
794
794
+
it("returns 503 for network errors writing to PDS", async () => {
795
795
+
const admin = await testCtx.createUser("Admin");
796
796
+
const member = await testCtx.createUser("Member");
797
797
+
798
798
+
const mockPutRecord = vi.fn().mockRejectedValue(new Error("fetch failed"));
799
799
+
testCtx.ctx.forumAgent = {
800
800
+
isAuthenticated: () => true,
801
801
+
getAgent: () => ({
802
802
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
803
803
+
}),
804
804
+
} as any;
805
805
+
806
806
+
const res = await app.request("/api/mod/ban", {
807
807
+
method: "POST",
808
808
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
809
809
+
body: JSON.stringify({ targetDid: member.did, reason: "Test" }),
810
810
+
});
811
811
+
812
812
+
expect(res.status).toBe(503);
813
813
+
const data = await res.json();
814
814
+
expect(data.error).toContain("try again later");
815
815
+
});
816
816
+
817
817
+
it("returns 500 for unexpected errors writing to PDS", async () => {
818
818
+
const admin = await testCtx.createUser("Admin");
819
819
+
const member = await testCtx.createUser("Member");
820
820
+
821
821
+
const mockPutRecord = vi.fn().mockRejectedValue(new Error("Database error"));
822
822
+
testCtx.ctx.forumAgent = {
823
823
+
isAuthenticated: () => true,
824
824
+
getAgent: () => ({
825
825
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
826
826
+
}),
827
827
+
} as any;
828
828
+
829
829
+
const res = await app.request("/api/mod/ban", {
830
830
+
method: "POST",
831
831
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
832
832
+
body: JSON.stringify({ targetDid: member.did, reason: "Test" }),
833
833
+
});
834
834
+
835
835
+
expect(res.status).toBe(500);
836
836
+
const data = await res.json();
837
837
+
expect(data.error).toContain("contact support");
838
838
+
});
839
839
+
```
840
840
+
841
841
+
**Step 6: Run all ban endpoint tests**
842
842
+
843
843
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "POST /api/mod/ban"`
844
844
+
845
845
+
Expected: PASS (~13 tests)
846
846
+
847
847
+
**Step 7: Commit**
848
848
+
849
849
+
```bash
850
850
+
git add apps/appview/src/routes/__tests__/mod.test.ts
851
851
+
git commit -m "test(mod): add comprehensive tests for POST /api/mod/ban (ATB-19)
852
852
+
853
853
+
Covers auth, validation, idempotency, and error classification"
854
854
+
```
855
855
+
856
856
+
---
857
857
+
858
858
+
## Task 7: Implement DELETE /api/mod/ban/:did (Unban)
859
859
+
860
860
+
**Files:**
861
861
+
- Modify: `apps/appview/src/routes/mod.ts`
862
862
+
- Modify: `apps/appview/src/routes/__tests__/mod.test.ts`
863
863
+
864
864
+
**Step 1: Write failing test for unban endpoint**
865
865
+
866
866
+
Add to mod.test.ts:
867
867
+
868
868
+
```typescript
869
869
+
describe("DELETE /api/mod/ban/:did", () => {
870
870
+
it("unbans user successfully", async () => {
871
871
+
const admin = await testCtx.createUser("Admin");
872
872
+
const member = await testCtx.createUser("Member");
873
873
+
874
874
+
// Insert existing ban
875
875
+
await testCtx.ctx.db.insert(modActions).values({
876
876
+
did: testCtx.ctx.config.forumDid,
877
877
+
rkey: "ban1",
878
878
+
cid: "bafyban",
879
879
+
action: "space.atbb.modAction.ban",
880
880
+
subjectDid: member.did,
881
881
+
reason: "Original ban",
882
882
+
createdBy: admin.did,
883
883
+
createdAt: new Date(),
884
884
+
indexedAt: new Date(),
885
885
+
});
886
886
+
887
887
+
const mockPutRecord = vi.fn().mockResolvedValue({
888
888
+
uri: "at://did:plc:forum/space.atbb.modAction/unban123",
889
889
+
cid: "bafyunban",
890
890
+
});
891
891
+
892
892
+
testCtx.ctx.forumAgent = {
893
893
+
isAuthenticated: () => true,
894
894
+
getAgent: () => ({
895
895
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
896
896
+
}),
897
897
+
} as any;
898
898
+
899
899
+
const res = await app.request(`/api/mod/ban/${member.did}`, {
900
900
+
method: "DELETE",
901
901
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
902
902
+
body: JSON.stringify({ reason: "Appeal approved" }),
903
903
+
});
904
904
+
905
905
+
expect(res.status).toBe(200);
906
906
+
const data = await res.json();
907
907
+
expect(data.success).toBe(true);
908
908
+
expect(data.action).toBe("unban");
909
909
+
expect(data.targetDid).toBe(member.did);
910
910
+
expect(data.alreadyActive).toBe(false);
911
911
+
912
912
+
// Verify PDS write
913
913
+
expect(mockPutRecord).toHaveBeenCalledWith({
914
914
+
repo: testCtx.ctx.config.forumDid,
915
915
+
collection: "space.atbb.modAction",
916
916
+
rkey: expect.any(String),
917
917
+
record: {
918
918
+
$type: "space.atbb.modAction",
919
919
+
action: "space.atbb.modAction.unban",
920
920
+
subject: { did: member.did },
921
921
+
reason: "Appeal approved",
922
922
+
createdBy: admin.did,
923
923
+
createdAt: expect.any(String),
924
924
+
},
925
925
+
});
926
926
+
});
927
927
+
928
928
+
it("returns alreadyActive: true when user already unbanned", async () => {
929
929
+
const admin = await testCtx.createUser("Admin");
930
930
+
const member = await testCtx.createUser("Member");
931
931
+
932
932
+
// Insert unban (most recent)
933
933
+
await testCtx.ctx.db.insert(modActions).values({
934
934
+
did: testCtx.ctx.config.forumDid,
935
935
+
rkey: "unban1",
936
936
+
cid: "bafyunban",
937
937
+
action: "space.atbb.modAction.unban",
938
938
+
subjectDid: member.did,
939
939
+
reason: "Previous unban",
940
940
+
createdBy: admin.did,
941
941
+
createdAt: new Date(),
942
942
+
indexedAt: new Date(),
943
943
+
});
944
944
+
945
945
+
const mockPutRecord = vi.fn();
946
946
+
testCtx.ctx.forumAgent = {
947
947
+
isAuthenticated: () => true,
948
948
+
getAgent: () => ({
949
949
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
950
950
+
}),
951
951
+
} as any;
952
952
+
953
953
+
const res = await app.request(`/api/mod/ban/${member.did}`, {
954
954
+
method: "DELETE",
955
955
+
headers: { Cookie: `atbb_session=${admin.sessionToken}` },
956
956
+
body: JSON.stringify({ reason: "Duplicate unban" }),
957
957
+
});
958
958
+
959
959
+
expect(res.status).toBe(200);
960
960
+
const data = await res.json();
961
961
+
expect(data.alreadyActive).toBe(true);
962
962
+
expect(mockPutRecord).not.toHaveBeenCalled();
963
963
+
});
964
964
+
});
965
965
+
```
966
966
+
967
967
+
**Step 2: Run test to verify it fails**
968
968
+
969
969
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "DELETE /api/mod/ban"`
970
970
+
971
971
+
Expected: FAIL - 404 Not Found
972
972
+
973
973
+
**Step 3: Implement DELETE /api/mod/ban/:did endpoint**
974
974
+
975
975
+
Add to mod.ts in `createModRoutes`:
976
976
+
977
977
+
```typescript
978
978
+
/**
979
979
+
* DELETE /api/mod/ban/:did
980
980
+
* Unban a user (reversal action).
981
981
+
*/
982
982
+
app.delete(
983
983
+
"/ban/:did",
984
984
+
requireAuth(ctx),
985
985
+
requirePermission(ctx, "space.atbb.permission.banUsers"),
986
986
+
async (c) => {
987
987
+
const user = c.get("user")!;
988
988
+
const targetDid = c.req.param("did");
989
989
+
990
990
+
// Validate DID
991
991
+
if (!targetDid.startsWith("did:")) {
992
992
+
return c.json({ error: "Invalid DID format" }, 400);
993
993
+
}
994
994
+
995
995
+
// Parse request body
996
996
+
let body: any;
997
997
+
try {
998
998
+
body = await c.req.json();
999
999
+
} catch {
1000
1000
+
return c.json({ error: "Invalid JSON in request body" }, 400);
1001
1001
+
}
1002
1002
+
1003
1003
+
const { reason } = body;
1004
1004
+
1005
1005
+
// Validate reason
1006
1006
+
const reasonError = validateReason(reason);
1007
1007
+
if (reasonError) {
1008
1008
+
return c.json({ error: reasonError }, 400);
1009
1009
+
}
1010
1010
+
1011
1011
+
// Check target user exists
1012
1012
+
const [membership] = await ctx.db
1013
1013
+
.select()
1014
1014
+
.from(memberships)
1015
1015
+
.where(eq(memberships.did, targetDid))
1016
1016
+
.limit(1);
1017
1017
+
1018
1018
+
if (!membership) {
1019
1019
+
return c.json({ error: "User is not a member of this forum" }, 404);
1020
1020
+
}
1021
1021
+
1022
1022
+
// Check if user is already unbanned (or never banned)
1023
1023
+
const isBanned = await checkActiveAction(
1024
1024
+
ctx,
1025
1025
+
{ did: targetDid },
1026
1026
+
"space.atbb.modAction.ban"
1027
1027
+
);
1028
1028
+
1029
1029
+
if (isBanned === false || isBanned === null) {
1030
1030
+
// Already unbanned or no ban history
1031
1031
+
return c.json({
1032
1032
+
success: true,
1033
1033
+
action: "unban",
1034
1034
+
targetDid,
1035
1035
+
alreadyActive: true,
1036
1036
+
}, 200);
1037
1037
+
}
1038
1038
+
1039
1039
+
// Get ForumAgent
1040
1040
+
if (!ctx.forumAgent) {
1041
1041
+
return c.json({
1042
1042
+
error: "Forum agent not available. Server configuration issue.",
1043
1043
+
}, 500);
1044
1044
+
}
1045
1045
+
1046
1046
+
const agent = ctx.forumAgent.getAgent();
1047
1047
+
if (!agent) {
1048
1048
+
return c.json({
1049
1049
+
error: "Forum agent not authenticated. Please try again later.",
1050
1050
+
}, 503);
1051
1051
+
}
1052
1052
+
1053
1053
+
// Write unban modAction record
1054
1054
+
try {
1055
1055
+
const result = await agent.com.atproto.repo.putRecord({
1056
1056
+
repo: ctx.config.forumDid,
1057
1057
+
collection: "space.atbb.modAction",
1058
1058
+
rkey: TID.nextStr(),
1059
1059
+
record: {
1060
1060
+
$type: "space.atbb.modAction",
1061
1061
+
action: "space.atbb.modAction.unban",
1062
1062
+
subject: { did: targetDid },
1063
1063
+
reason,
1064
1064
+
createdBy: user.did,
1065
1065
+
createdAt: new Date().toISOString(),
1066
1066
+
},
1067
1067
+
});
1068
1068
+
1069
1069
+
return c.json({
1070
1070
+
success: true,
1071
1071
+
action: "unban",
1072
1072
+
targetDid,
1073
1073
+
uri: result.uri,
1074
1074
+
cid: result.cid,
1075
1075
+
alreadyActive: false,
1076
1076
+
}, 200);
1077
1077
+
} catch (error) {
1078
1078
+
console.error("Failed to write unban modAction", {
1079
1079
+
operation: "DELETE /api/mod/ban/:did",
1080
1080
+
targetDid,
1081
1081
+
error: error instanceof Error ? error.message : String(error),
1082
1082
+
});
1083
1083
+
1084
1084
+
if (error instanceof Error && isNetworkError(error)) {
1085
1085
+
return c.json({
1086
1086
+
error: "Unable to reach Forum PDS. Please try again later.",
1087
1087
+
}, 503);
1088
1088
+
}
1089
1089
+
1090
1090
+
return c.json({
1091
1091
+
error: "Failed to record moderation action. Please contact support.",
1092
1092
+
}, 500);
1093
1093
+
}
1094
1094
+
}
1095
1095
+
);
1096
1096
+
```
1097
1097
+
1098
1098
+
**Step 4: Run test to verify it passes**
1099
1099
+
1100
1100
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "DELETE /api/mod/ban"`
1101
1101
+
1102
1102
+
Expected: PASS (2 tests)
1103
1103
+
1104
1104
+
**Step 5: Commit**
1105
1105
+
1106
1106
+
```bash
1107
1107
+
git add apps/appview/src/routes/mod.ts apps/appview/src/routes/__tests__/mod.test.ts
1108
1108
+
git commit -m "feat(mod): implement DELETE /api/mod/ban/:did (unban) (ATB-19)
1109
1109
+
1110
1110
+
Unbans user by writing unban modAction record"
1111
1111
+
```
1112
1112
+
1113
1113
+
---
1114
1114
+
1115
1115
+
## Task 8: Implement POST /api/mod/lock and DELETE /api/mod/lock/:topicId
1116
1116
+
1117
1117
+
**Files:**
1118
1118
+
- Modify: `apps/appview/src/routes/mod.ts`
1119
1119
+
- Modify: `apps/appview/src/routes/__tests__/mod.test.ts`
1120
1120
+
1121
1121
+
**Note:** Lock endpoints are similar to ban but target posts and include topic validation.
1122
1122
+
1123
1123
+
**Step 1: Write failing tests for lock endpoints**
1124
1124
+
1125
1125
+
Add to mod.test.ts:
1126
1126
+
1127
1127
+
```typescript
1128
1128
+
describe("POST /api/mod/lock", () => {
1129
1129
+
it("locks topic successfully", async () => {
1130
1130
+
const mod = await testCtx.createUser("Moderator");
1131
1131
+
const member = await testCtx.createUser("Member");
1132
1132
+
const topic = await testCtx.createTopic(member.did, "Test topic");
1133
1133
+
1134
1134
+
const mockPutRecord = vi.fn().mockResolvedValue({
1135
1135
+
uri: "at://did:plc:forum/space.atbb.modAction/lock123",
1136
1136
+
cid: "bafylock",
1137
1137
+
});
1138
1138
+
1139
1139
+
testCtx.ctx.forumAgent = {
1140
1140
+
isAuthenticated: () => true,
1141
1141
+
getAgent: () => ({
1142
1142
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
1143
1143
+
}),
1144
1144
+
} as any;
1145
1145
+
1146
1146
+
const res = await app.request("/api/mod/lock", {
1147
1147
+
method: "POST",
1148
1148
+
headers: { Cookie: `atbb_session=${mod.sessionToken}` },
1149
1149
+
body: JSON.stringify({
1150
1150
+
topicId: topic.id.toString(),
1151
1151
+
reason: "Off-topic discussion",
1152
1152
+
}),
1153
1153
+
});
1154
1154
+
1155
1155
+
expect(res.status).toBe(200);
1156
1156
+
const data = await res.json();
1157
1157
+
expect(data.success).toBe(true);
1158
1158
+
expect(data.action).toBe("lock");
1159
1159
+
expect(data.topicId).toBe(topic.id.toString());
1160
1160
+
expect(data.alreadyActive).toBe(false);
1161
1161
+
1162
1162
+
// Verify PDS write with post subject
1163
1163
+
expect(mockPutRecord).toHaveBeenCalledWith({
1164
1164
+
repo: testCtx.ctx.config.forumDid,
1165
1165
+
collection: "space.atbb.modAction",
1166
1166
+
rkey: expect.any(String),
1167
1167
+
record: {
1168
1168
+
$type: "space.atbb.modAction",
1169
1169
+
action: "space.atbb.modAction.lock",
1170
1170
+
subject: {
1171
1171
+
post: {
1172
1172
+
uri: expect.stringContaining("space.atbb.post"),
1173
1173
+
cid: topic.cid,
1174
1174
+
},
1175
1175
+
},
1176
1176
+
reason: "Off-topic discussion",
1177
1177
+
createdBy: mod.did,
1178
1178
+
createdAt: expect.any(String),
1179
1179
+
},
1180
1180
+
});
1181
1181
+
});
1182
1182
+
1183
1183
+
it("returns 400 when trying to lock a reply post", async () => {
1184
1184
+
const mod = await testCtx.createUser("Moderator");
1185
1185
+
const member = await testCtx.createUser("Member");
1186
1186
+
const topic = await testCtx.createTopic(member.did, "Topic");
1187
1187
+
const reply = await testCtx.createReply(member.did, topic.id, "Reply");
1188
1188
+
1189
1189
+
const res = await app.request("/api/mod/lock", {
1190
1190
+
method: "POST",
1191
1191
+
headers: { Cookie: `atbb_session=${mod.sessionToken}` },
1192
1192
+
body: JSON.stringify({
1193
1193
+
topicId: reply.id.toString(),
1194
1194
+
reason: "Test",
1195
1195
+
}),
1196
1196
+
});
1197
1197
+
1198
1198
+
expect(res.status).toBe(400);
1199
1199
+
const data = await res.json();
1200
1200
+
expect(data.error).toContain("root posts");
1201
1201
+
});
1202
1202
+
1203
1203
+
it("returns 404 when topic not found", async () => {
1204
1204
+
const mod = await testCtx.createUser("Moderator");
1205
1205
+
1206
1206
+
const res = await app.request("/api/mod/lock", {
1207
1207
+
method: "POST",
1208
1208
+
headers: { Cookie: `atbb_session=${mod.sessionToken}` },
1209
1209
+
body: JSON.stringify({
1210
1210
+
topicId: "999999",
1211
1211
+
reason: "Test",
1212
1212
+
}),
1213
1213
+
});
1214
1214
+
1215
1215
+
expect(res.status).toBe(404);
1216
1216
+
});
1217
1217
+
});
1218
1218
+
1219
1219
+
describe("DELETE /api/mod/lock/:topicId", () => {
1220
1220
+
it("unlocks topic successfully", async () => {
1221
1221
+
const mod = await testCtx.createUser("Moderator");
1222
1222
+
const member = await testCtx.createUser("Member");
1223
1223
+
const topic = await testCtx.createTopic(member.did, "Test topic");
1224
1224
+
1225
1225
+
// Insert existing lock
1226
1226
+
const postUri = `at://${member.did}/space.atbb.post/${topic.rkey}`;
1227
1227
+
await testCtx.ctx.db.insert(modActions).values({
1228
1228
+
did: testCtx.ctx.config.forumDid,
1229
1229
+
rkey: "lock1",
1230
1230
+
cid: "bafylock",
1231
1231
+
action: "space.atbb.modAction.lock",
1232
1232
+
subjectPostUri: postUri,
1233
1233
+
reason: "Original lock",
1234
1234
+
createdBy: mod.did,
1235
1235
+
createdAt: new Date(),
1236
1236
+
indexedAt: new Date(),
1237
1237
+
});
1238
1238
+
1239
1239
+
const mockPutRecord = vi.fn().mockResolvedValue({
1240
1240
+
uri: "at://did:plc:forum/space.atbb.modAction/unlock123",
1241
1241
+
cid: "bafyunlock",
1242
1242
+
});
1243
1243
+
1244
1244
+
testCtx.ctx.forumAgent = {
1245
1245
+
isAuthenticated: () => true,
1246
1246
+
getAgent: () => ({
1247
1247
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
1248
1248
+
}),
1249
1249
+
} as any;
1250
1250
+
1251
1251
+
const res = await app.request(`/api/mod/lock/${topic.id}`, {
1252
1252
+
method: "DELETE",
1253
1253
+
headers: { Cookie: `atbb_session=${mod.sessionToken}` },
1254
1254
+
body: JSON.stringify({ reason: "Discussion resumed" }),
1255
1255
+
});
1256
1256
+
1257
1257
+
expect(res.status).toBe(200);
1258
1258
+
const data = await res.json();
1259
1259
+
expect(data.success).toBe(true);
1260
1260
+
expect(data.action).toBe("unlock");
1261
1261
+
});
1262
1262
+
});
1263
1263
+
```
1264
1264
+
1265
1265
+
**Step 2: Run test to verify it fails**
1266
1266
+
1267
1267
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "lock"`
1268
1268
+
1269
1269
+
Expected: FAIL - 404 Not Found
1270
1270
+
1271
1271
+
**Step 3: Implement lock endpoints**
1272
1272
+
1273
1273
+
Add to mod.ts:
1274
1274
+
1275
1275
+
```typescript
1276
1276
+
/**
1277
1277
+
* POST /api/mod/lock
1278
1278
+
* Lock a topic (prevent new replies).
1279
1279
+
*/
1280
1280
+
app.post(
1281
1281
+
"/lock",
1282
1282
+
requireAuth(ctx),
1283
1283
+
requirePermission(ctx, "space.atbb.permission.lockTopics"),
1284
1284
+
async (c) => {
1285
1285
+
const user = c.get("user")!;
1286
1286
+
1287
1287
+
// Parse request body
1288
1288
+
let body: any;
1289
1289
+
try {
1290
1290
+
body = await c.req.json();
1291
1291
+
} catch {
1292
1292
+
return c.json({ error: "Invalid JSON in request body" }, 400);
1293
1293
+
}
1294
1294
+
1295
1295
+
const { topicId, reason } = body;
1296
1296
+
1297
1297
+
// Validate topicId
1298
1298
+
if (typeof topicId !== "string") {
1299
1299
+
return c.json({ error: "topicId is required and must be a string" }, 400);
1300
1300
+
}
1301
1301
+
1302
1302
+
const topicIdBigInt = parseBigIntParam(topicId);
1303
1303
+
if (topicIdBigInt === null) {
1304
1304
+
return c.json({ error: "Invalid topic ID" }, 400);
1305
1305
+
}
1306
1306
+
1307
1307
+
// Validate reason
1308
1308
+
const reasonError = validateReason(reason);
1309
1309
+
if (reasonError) {
1310
1310
+
return c.json({ error: reasonError }, 400);
1311
1311
+
}
1312
1312
+
1313
1313
+
// Get topic and validate it's a root post
1314
1314
+
const [topic] = await ctx.db
1315
1315
+
.select()
1316
1316
+
.from(posts)
1317
1317
+
.where(eq(posts.id, topicIdBigInt))
1318
1318
+
.limit(1);
1319
1319
+
1320
1320
+
if (!topic) {
1321
1321
+
return c.json({ error: "Topic not found" }, 404);
1322
1322
+
}
1323
1323
+
1324
1324
+
// Verify it's a root post (not a reply)
1325
1325
+
if (topic.rootPostId !== null) {
1326
1326
+
return c.json({
1327
1327
+
error: "Can only lock topics (root posts), not replies",
1328
1328
+
}, 400);
1329
1329
+
}
1330
1330
+
1331
1331
+
// Build post URI
1332
1332
+
const postUri = `at://${topic.did}/space.atbb.post/${topic.rkey}`;
1333
1333
+
1334
1334
+
// Check if topic is already locked
1335
1335
+
const isLocked = await checkActiveAction(
1336
1336
+
ctx,
1337
1337
+
{ postUri },
1338
1338
+
"space.atbb.modAction.lock"
1339
1339
+
);
1340
1340
+
1341
1341
+
if (isLocked) {
1342
1342
+
return c.json({
1343
1343
+
success: true,
1344
1344
+
action: "lock",
1345
1345
+
topicId: topicId,
1346
1346
+
topicUri: postUri,
1347
1347
+
alreadyActive: true,
1348
1348
+
}, 200);
1349
1349
+
}
1350
1350
+
1351
1351
+
// Get ForumAgent
1352
1352
+
if (!ctx.forumAgent) {
1353
1353
+
return c.json({
1354
1354
+
error: "Forum agent not available. Server configuration issue.",
1355
1355
+
}, 500);
1356
1356
+
}
1357
1357
+
1358
1358
+
const agent = ctx.forumAgent.getAgent();
1359
1359
+
if (!agent) {
1360
1360
+
return c.json({
1361
1361
+
error: "Forum agent not authenticated. Please try again later.",
1362
1362
+
}, 503);
1363
1363
+
}
1364
1364
+
1365
1365
+
// Write lock modAction record
1366
1366
+
try {
1367
1367
+
const result = await agent.com.atproto.repo.putRecord({
1368
1368
+
repo: ctx.config.forumDid,
1369
1369
+
collection: "space.atbb.modAction",
1370
1370
+
rkey: TID.nextStr(),
1371
1371
+
record: {
1372
1372
+
$type: "space.atbb.modAction",
1373
1373
+
action: "space.atbb.modAction.lock",
1374
1374
+
subject: {
1375
1375
+
post: {
1376
1376
+
uri: postUri,
1377
1377
+
cid: topic.cid,
1378
1378
+
},
1379
1379
+
},
1380
1380
+
reason,
1381
1381
+
createdBy: user.did,
1382
1382
+
createdAt: new Date().toISOString(),
1383
1383
+
},
1384
1384
+
});
1385
1385
+
1386
1386
+
return c.json({
1387
1387
+
success: true,
1388
1388
+
action: "lock",
1389
1389
+
topicId: topicId,
1390
1390
+
topicUri: postUri,
1391
1391
+
uri: result.uri,
1392
1392
+
cid: result.cid,
1393
1393
+
alreadyActive: false,
1394
1394
+
}, 200);
1395
1395
+
} catch (error) {
1396
1396
+
console.error("Failed to write lock modAction", {
1397
1397
+
operation: "POST /api/mod/lock",
1398
1398
+
topicId,
1399
1399
+
error: error instanceof Error ? error.message : String(error),
1400
1400
+
});
1401
1401
+
1402
1402
+
if (error instanceof Error && isNetworkError(error)) {
1403
1403
+
return c.json({
1404
1404
+
error: "Unable to reach Forum PDS. Please try again later.",
1405
1405
+
}, 503);
1406
1406
+
}
1407
1407
+
1408
1408
+
return c.json({
1409
1409
+
error: "Failed to record moderation action. Please contact support.",
1410
1410
+
}, 500);
1411
1411
+
}
1412
1412
+
}
1413
1413
+
);
1414
1414
+
1415
1415
+
/**
1416
1416
+
* DELETE /api/mod/lock/:topicId
1417
1417
+
* Unlock a topic (reversal action).
1418
1418
+
*/
1419
1419
+
app.delete(
1420
1420
+
"/lock/:topicId",
1421
1421
+
requireAuth(ctx),
1422
1422
+
requirePermission(ctx, "space.atbb.permission.lockTopics"),
1423
1423
+
async (c) => {
1424
1424
+
const user = c.get("user")!;
1425
1425
+
const topicIdParam = c.req.param("topicId");
1426
1426
+
1427
1427
+
const topicIdBigInt = parseBigIntParam(topicIdParam);
1428
1428
+
if (topicIdBigInt === null) {
1429
1429
+
return c.json({ error: "Invalid topic ID" }, 400);
1430
1430
+
}
1431
1431
+
1432
1432
+
// Parse request body
1433
1433
+
let body: any;
1434
1434
+
try {
1435
1435
+
body = await c.req.json();
1436
1436
+
} catch {
1437
1437
+
return c.json({ error: "Invalid JSON in request body" }, 400);
1438
1438
+
}
1439
1439
+
1440
1440
+
const { reason } = body;
1441
1441
+
1442
1442
+
// Validate reason
1443
1443
+
const reasonError = validateReason(reason);
1444
1444
+
if (reasonError) {
1445
1445
+
return c.json({ error: reasonError }, 400);
1446
1446
+
}
1447
1447
+
1448
1448
+
// Get topic
1449
1449
+
const [topic] = await ctx.db
1450
1450
+
.select()
1451
1451
+
.from(posts)
1452
1452
+
.where(eq(posts.id, topicIdBigInt))
1453
1453
+
.limit(1);
1454
1454
+
1455
1455
+
if (!topic) {
1456
1456
+
return c.json({ error: "Topic not found" }, 404);
1457
1457
+
}
1458
1458
+
1459
1459
+
if (topic.rootPostId !== null) {
1460
1460
+
return c.json({
1461
1461
+
error: "Can only unlock topics (root posts), not replies",
1462
1462
+
}, 400);
1463
1463
+
}
1464
1464
+
1465
1465
+
const postUri = `at://${topic.did}/space.atbb.post/${topic.rkey}`;
1466
1466
+
1467
1467
+
// Check if topic is already unlocked
1468
1468
+
const isLocked = await checkActiveAction(
1469
1469
+
ctx,
1470
1470
+
{ postUri },
1471
1471
+
"space.atbb.modAction.lock"
1472
1472
+
);
1473
1473
+
1474
1474
+
if (isLocked === false || isLocked === null) {
1475
1475
+
return c.json({
1476
1476
+
success: true,
1477
1477
+
action: "unlock",
1478
1478
+
topicId: topicIdParam,
1479
1479
+
topicUri: postUri,
1480
1480
+
alreadyActive: true,
1481
1481
+
}, 200);
1482
1482
+
}
1483
1483
+
1484
1484
+
// Get ForumAgent
1485
1485
+
if (!ctx.forumAgent) {
1486
1486
+
return c.json({
1487
1487
+
error: "Forum agent not available. Server configuration issue.",
1488
1488
+
}, 500);
1489
1489
+
}
1490
1490
+
1491
1491
+
const agent = ctx.forumAgent.getAgent();
1492
1492
+
if (!agent) {
1493
1493
+
return c.json({
1494
1494
+
error: "Forum agent not authenticated. Please try again later.",
1495
1495
+
}, 503);
1496
1496
+
}
1497
1497
+
1498
1498
+
// Write unlock modAction record
1499
1499
+
try {
1500
1500
+
const result = await agent.com.atproto.repo.putRecord({
1501
1501
+
repo: ctx.config.forumDid,
1502
1502
+
collection: "space.atbb.modAction",
1503
1503
+
rkey: TID.nextStr(),
1504
1504
+
record: {
1505
1505
+
$type: "space.atbb.modAction",
1506
1506
+
action: "space.atbb.modAction.unlock",
1507
1507
+
subject: {
1508
1508
+
post: {
1509
1509
+
uri: postUri,
1510
1510
+
cid: topic.cid,
1511
1511
+
},
1512
1512
+
},
1513
1513
+
reason,
1514
1514
+
createdBy: user.did,
1515
1515
+
createdAt: new Date().toISOString(),
1516
1516
+
},
1517
1517
+
});
1518
1518
+
1519
1519
+
return c.json({
1520
1520
+
success: true,
1521
1521
+
action: "unlock",
1522
1522
+
topicId: topicIdParam,
1523
1523
+
topicUri: postUri,
1524
1524
+
uri: result.uri,
1525
1525
+
cid: result.cid,
1526
1526
+
alreadyActive: false,
1527
1527
+
}, 200);
1528
1528
+
} catch (error) {
1529
1529
+
console.error("Failed to write unlock modAction", {
1530
1530
+
operation: "DELETE /api/mod/lock/:topicId",
1531
1531
+
topicId: topicIdParam,
1532
1532
+
error: error instanceof Error ? error.message : String(error),
1533
1533
+
});
1534
1534
+
1535
1535
+
if (error instanceof Error && isNetworkError(error)) {
1536
1536
+
return c.json({
1537
1537
+
error: "Unable to reach Forum PDS. Please try again later.",
1538
1538
+
}, 503);
1539
1539
+
}
1540
1540
+
1541
1541
+
return c.json({
1542
1542
+
error: "Failed to record moderation action. Please contact support.",
1543
1543
+
}, 500);
1544
1544
+
}
1545
1545
+
}
1546
1546
+
);
1547
1547
+
```
1548
1548
+
1549
1549
+
**Step 4: Add missing import**
1550
1550
+
1551
1551
+
Add to imports at top of mod.ts:
1552
1552
+
1553
1553
+
```typescript
1554
1554
+
import { parseBigIntParam } from "./helpers.js";
1555
1555
+
```
1556
1556
+
1557
1557
+
**Step 5: Run test to verify it passes**
1558
1558
+
1559
1559
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "lock"`
1560
1560
+
1561
1561
+
Expected: PASS (4 tests)
1562
1562
+
1563
1563
+
**Step 6: Commit**
1564
1564
+
1565
1565
+
```bash
1566
1566
+
git add apps/appview/src/routes/mod.ts apps/appview/src/routes/__tests__/mod.test.ts
1567
1567
+
git commit -m "feat(mod): implement lock/unlock topic endpoints (ATB-19)
1568
1568
+
1569
1569
+
POST /api/mod/lock and DELETE /api/mod/lock/:topicId
1570
1570
+
Validates targets are root posts only"
1571
1571
+
```
1572
1572
+
1573
1573
+
---
1574
1574
+
1575
1575
+
## Task 9: Implement POST /api/mod/hide and DELETE /api/mod/hide/:postId
1576
1576
+
1577
1577
+
**Files:**
1578
1578
+
- Modify: `apps/appview/src/routes/mod.ts`
1579
1579
+
- Modify: `apps/appview/src/routes/__tests__/mod.test.ts`
1580
1580
+
1581
1581
+
**Note:** Hide endpoints are similar to lock but work on ANY post (topics or replies).
1582
1582
+
1583
1583
+
**Step 1: Write failing tests for hide endpoints**
1584
1584
+
1585
1585
+
Add to mod.test.ts:
1586
1586
+
1587
1587
+
```typescript
1588
1588
+
describe("POST /api/mod/hide", () => {
1589
1589
+
it("hides topic post successfully", async () => {
1590
1590
+
const mod = await testCtx.createUser("Moderator");
1591
1591
+
const member = await testCtx.createUser("Member");
1592
1592
+
const topic = await testCtx.createTopic(member.did, "Spam topic");
1593
1593
+
1594
1594
+
const mockPutRecord = vi.fn().mockResolvedValue({
1595
1595
+
uri: "at://did:plc:forum/space.atbb.modAction/hide123",
1596
1596
+
cid: "bafyhide",
1597
1597
+
});
1598
1598
+
1599
1599
+
testCtx.ctx.forumAgent = {
1600
1600
+
isAuthenticated: () => true,
1601
1601
+
getAgent: () => ({
1602
1602
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
1603
1603
+
}),
1604
1604
+
} as any;
1605
1605
+
1606
1606
+
const res = await app.request("/api/mod/hide", {
1607
1607
+
method: "POST",
1608
1608
+
headers: { Cookie: `atbb_session=${mod.sessionToken}` },
1609
1609
+
body: JSON.stringify({
1610
1610
+
postId: topic.id.toString(),
1611
1611
+
reason: "Spam content",
1612
1612
+
}),
1613
1613
+
});
1614
1614
+
1615
1615
+
expect(res.status).toBe(200);
1616
1616
+
const data = await res.json();
1617
1617
+
expect(data.success).toBe(true);
1618
1618
+
expect(data.action).toBe("hide");
1619
1619
+
expect(data.postId).toBe(topic.id.toString());
1620
1620
+
});
1621
1621
+
1622
1622
+
it("hides reply post successfully", async () => {
1623
1623
+
const mod = await testCtx.createUser("Moderator");
1624
1624
+
const member = await testCtx.createUser("Member");
1625
1625
+
const topic = await testCtx.createTopic(member.did, "Topic");
1626
1626
+
const reply = await testCtx.createReply(member.did, topic.id, "Spam reply");
1627
1627
+
1628
1628
+
const mockPutRecord = vi.fn().mockResolvedValue({
1629
1629
+
uri: "at://did:plc:forum/space.atbb.modAction/hide456",
1630
1630
+
cid: "bafyhide2",
1631
1631
+
});
1632
1632
+
1633
1633
+
testCtx.ctx.forumAgent = {
1634
1634
+
isAuthenticated: () => true,
1635
1635
+
getAgent: () => ({
1636
1636
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
1637
1637
+
}),
1638
1638
+
} as any;
1639
1639
+
1640
1640
+
const res = await app.request("/api/mod/hide", {
1641
1641
+
method: "POST",
1642
1642
+
headers: { Cookie: `atbb_session=${mod.sessionToken}` },
1643
1643
+
body: JSON.stringify({
1644
1644
+
postId: reply.id.toString(),
1645
1645
+
reason: "Harassment",
1646
1646
+
}),
1647
1647
+
});
1648
1648
+
1649
1649
+
expect(res.status).toBe(200);
1650
1650
+
const data = await res.json();
1651
1651
+
expect(data.success).toBe(true);
1652
1652
+
expect(data.action).toBe("hide");
1653
1653
+
});
1654
1654
+
});
1655
1655
+
1656
1656
+
describe("DELETE /api/mod/hide/:postId", () => {
1657
1657
+
it("unhides post successfully", async () => {
1658
1658
+
const mod = await testCtx.createUser("Moderator");
1659
1659
+
const member = await testCtx.createUser("Member");
1660
1660
+
const topic = await testCtx.createTopic(member.did, "Test");
1661
1661
+
1662
1662
+
const postUri = `at://${member.did}/space.atbb.post/${topic.rkey}`;
1663
1663
+
await testCtx.ctx.db.insert(modActions).values({
1664
1664
+
did: testCtx.ctx.config.forumDid,
1665
1665
+
rkey: "hide1",
1666
1666
+
cid: "bafyhide",
1667
1667
+
action: "space.atbb.modAction.delete",
1668
1668
+
subjectPostUri: postUri,
1669
1669
+
reason: "Original hide",
1670
1670
+
createdBy: mod.did,
1671
1671
+
createdAt: new Date(),
1672
1672
+
indexedAt: new Date(),
1673
1673
+
});
1674
1674
+
1675
1675
+
const mockPutRecord = vi.fn().mockResolvedValue({
1676
1676
+
uri: "at://did:plc:forum/space.atbb.modAction/unhide123",
1677
1677
+
cid: "bafyunhide",
1678
1678
+
});
1679
1679
+
1680
1680
+
testCtx.ctx.forumAgent = {
1681
1681
+
isAuthenticated: () => true,
1682
1682
+
getAgent: () => ({
1683
1683
+
com: { atproto: { repo: { putRecord: mockPutRecord } } },
1684
1684
+
}),
1685
1685
+
} as any;
1686
1686
+
1687
1687
+
const res = await app.request(`/api/mod/hide/${topic.id}`, {
1688
1688
+
method: "DELETE",
1689
1689
+
headers: { Cookie: `atbb_session=${mod.sessionToken}` },
1690
1690
+
body: JSON.stringify({ reason: "False positive" }),
1691
1691
+
});
1692
1692
+
1693
1693
+
expect(res.status).toBe(200);
1694
1694
+
const data = await res.json();
1695
1695
+
expect(data.success).toBe(true);
1696
1696
+
expect(data.action).toBe("unhide");
1697
1697
+
});
1698
1698
+
});
1699
1699
+
```
1700
1700
+
1701
1701
+
**Step 2: Run test to verify it fails**
1702
1702
+
1703
1703
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "hide"`
1704
1704
+
1705
1705
+
Expected: FAIL - 404 Not Found
1706
1706
+
1707
1707
+
**Step 3: Implement hide endpoints**
1708
1708
+
1709
1709
+
Add to mod.ts (note: hide uses "delete" action type per lexicon):
1710
1710
+
1711
1711
+
```typescript
1712
1712
+
/**
1713
1713
+
* POST /api/mod/hide
1714
1714
+
* Hide a post from the forum (soft-delete).
1715
1715
+
*/
1716
1716
+
app.post(
1717
1717
+
"/hide",
1718
1718
+
requireAuth(ctx),
1719
1719
+
requirePermission(ctx, "space.atbb.permission.moderatePosts"),
1720
1720
+
async (c) => {
1721
1721
+
const user = c.get("user")!;
1722
1722
+
1723
1723
+
// Parse request body
1724
1724
+
let body: any;
1725
1725
+
try {
1726
1726
+
body = await c.req.json();
1727
1727
+
} catch {
1728
1728
+
return c.json({ error: "Invalid JSON in request body" }, 400);
1729
1729
+
}
1730
1730
+
1731
1731
+
const { postId, reason } = body;
1732
1732
+
1733
1733
+
// Validate postId
1734
1734
+
if (typeof postId !== "string") {
1735
1735
+
return c.json({ error: "postId is required and must be a string" }, 400);
1736
1736
+
}
1737
1737
+
1738
1738
+
const postIdBigInt = parseBigIntParam(postId);
1739
1739
+
if (postIdBigInt === null) {
1740
1740
+
return c.json({ error: "Invalid post ID" }, 400);
1741
1741
+
}
1742
1742
+
1743
1743
+
// Validate reason
1744
1744
+
const reasonError = validateReason(reason);
1745
1745
+
if (reasonError) {
1746
1746
+
return c.json({ error: reasonError }, 400);
1747
1747
+
}
1748
1748
+
1749
1749
+
// Get post (can be topic or reply)
1750
1750
+
const [post] = await ctx.db
1751
1751
+
.select()
1752
1752
+
.from(posts)
1753
1753
+
.where(eq(posts.id, postIdBigInt))
1754
1754
+
.limit(1);
1755
1755
+
1756
1756
+
if (!post) {
1757
1757
+
return c.json({ error: "Post not found" }, 404);
1758
1758
+
}
1759
1759
+
1760
1760
+
const postUri = `at://${post.did}/space.atbb.post/${post.rkey}`;
1761
1761
+
1762
1762
+
// Check if post is already hidden
1763
1763
+
const isHidden = await checkActiveAction(
1764
1764
+
ctx,
1765
1765
+
{ postUri },
1766
1766
+
"space.atbb.modAction.delete"
1767
1767
+
);
1768
1768
+
1769
1769
+
if (isHidden) {
1770
1770
+
return c.json({
1771
1771
+
success: true,
1772
1772
+
action: "hide",
1773
1773
+
postId: postId,
1774
1774
+
postUri: postUri,
1775
1775
+
alreadyActive: true,
1776
1776
+
}, 200);
1777
1777
+
}
1778
1778
+
1779
1779
+
// Get ForumAgent
1780
1780
+
if (!ctx.forumAgent) {
1781
1781
+
return c.json({
1782
1782
+
error: "Forum agent not available. Server configuration issue.",
1783
1783
+
}, 500);
1784
1784
+
}
1785
1785
+
1786
1786
+
const agent = ctx.forumAgent.getAgent();
1787
1787
+
if (!agent) {
1788
1788
+
return c.json({
1789
1789
+
error: "Forum agent not authenticated. Please try again later.",
1790
1790
+
}, 503);
1791
1791
+
}
1792
1792
+
1793
1793
+
// Write hide modAction record (action type is "delete" per lexicon)
1794
1794
+
try {
1795
1795
+
const result = await agent.com.atproto.repo.putRecord({
1796
1796
+
repo: ctx.config.forumDid,
1797
1797
+
collection: "space.atbb.modAction",
1798
1798
+
rkey: TID.nextStr(),
1799
1799
+
record: {
1800
1800
+
$type: "space.atbb.modAction",
1801
1801
+
action: "space.atbb.modAction.delete",
1802
1802
+
subject: {
1803
1803
+
post: {
1804
1804
+
uri: postUri,
1805
1805
+
cid: post.cid,
1806
1806
+
},
1807
1807
+
},
1808
1808
+
reason,
1809
1809
+
createdBy: user.did,
1810
1810
+
createdAt: new Date().toISOString(),
1811
1811
+
},
1812
1812
+
});
1813
1813
+
1814
1814
+
return c.json({
1815
1815
+
success: true,
1816
1816
+
action: "hide",
1817
1817
+
postId: postId,
1818
1818
+
postUri: postUri,
1819
1819
+
uri: result.uri,
1820
1820
+
cid: result.cid,
1821
1821
+
alreadyActive: false,
1822
1822
+
}, 200);
1823
1823
+
} catch (error) {
1824
1824
+
console.error("Failed to write hide modAction", {
1825
1825
+
operation: "POST /api/mod/hide",
1826
1826
+
postId,
1827
1827
+
error: error instanceof Error ? error.message : String(error),
1828
1828
+
});
1829
1829
+
1830
1830
+
if (error instanceof Error && isNetworkError(error)) {
1831
1831
+
return c.json({
1832
1832
+
error: "Unable to reach Forum PDS. Please try again later.",
1833
1833
+
}, 503);
1834
1834
+
}
1835
1835
+
1836
1836
+
return c.json({
1837
1837
+
error: "Failed to record moderation action. Please contact support.",
1838
1838
+
}, 500);
1839
1839
+
}
1840
1840
+
}
1841
1841
+
);
1842
1842
+
1843
1843
+
/**
1844
1844
+
* DELETE /api/mod/hide/:postId
1845
1845
+
* Unhide a post (reversal action).
1846
1846
+
*/
1847
1847
+
app.delete(
1848
1848
+
"/hide/:postId",
1849
1849
+
requireAuth(ctx),
1850
1850
+
requirePermission(ctx, "space.atbb.permission.moderatePosts"),
1851
1851
+
async (c) => {
1852
1852
+
const user = c.get("user")!;
1853
1853
+
const postIdParam = c.req.param("postId");
1854
1854
+
1855
1855
+
const postIdBigInt = parseBigIntParam(postIdParam);
1856
1856
+
if (postIdBigInt === null) {
1857
1857
+
return c.json({ error: "Invalid post ID" }, 400);
1858
1858
+
}
1859
1859
+
1860
1860
+
// Parse request body
1861
1861
+
let body: any;
1862
1862
+
try {
1863
1863
+
body = await c.req.json();
1864
1864
+
} catch {
1865
1865
+
return c.json({ error: "Invalid JSON in request body" }, 400);
1866
1866
+
}
1867
1867
+
1868
1868
+
const { reason } = body;
1869
1869
+
1870
1870
+
// Validate reason
1871
1871
+
const reasonError = validateReason(reason);
1872
1872
+
if (reasonError) {
1873
1873
+
return c.json({ error: reasonError }, 400);
1874
1874
+
}
1875
1875
+
1876
1876
+
// Get post
1877
1877
+
const [post] = await ctx.db
1878
1878
+
.select()
1879
1879
+
.from(posts)
1880
1880
+
.where(eq(posts.id, postIdBigInt))
1881
1881
+
.limit(1);
1882
1882
+
1883
1883
+
if (!post) {
1884
1884
+
return c.json({ error: "Post not found" }, 404);
1885
1885
+
}
1886
1886
+
1887
1887
+
const postUri = `at://${post.did}/space.atbb.post/${post.rkey}`;
1888
1888
+
1889
1889
+
// Check if post is already unhidden
1890
1890
+
const isHidden = await checkActiveAction(
1891
1891
+
ctx,
1892
1892
+
{ postUri },
1893
1893
+
"space.atbb.modAction.delete"
1894
1894
+
);
1895
1895
+
1896
1896
+
if (isHidden === false || isHidden === null) {
1897
1897
+
return c.json({
1898
1898
+
success: true,
1899
1899
+
action: "unhide",
1900
1900
+
postId: postIdParam,
1901
1901
+
postUri: postUri,
1902
1902
+
alreadyActive: true,
1903
1903
+
}, 200);
1904
1904
+
}
1905
1905
+
1906
1906
+
// Get ForumAgent
1907
1907
+
if (!ctx.forumAgent) {
1908
1908
+
return c.json({
1909
1909
+
error: "Forum agent not available. Server configuration issue.",
1910
1910
+
}, 500);
1911
1911
+
}
1912
1912
+
1913
1913
+
const agent = ctx.forumAgent.getAgent();
1914
1914
+
if (!agent) {
1915
1915
+
return c.json({
1916
1916
+
error: "Forum agent not authenticated. Please try again later.",
1917
1917
+
}, 503);
1918
1918
+
}
1919
1919
+
1920
1920
+
// Write unhide modAction record
1921
1921
+
// Note: lexicon doesn't define "undelete", so we infer "unhide" behavior
1922
1922
+
// The read-path logic should check for most recent action
1923
1923
+
try {
1924
1924
+
const result = await agent.com.atproto.repo.putRecord({
1925
1925
+
repo: ctx.config.forumDid,
1926
1926
+
collection: "space.atbb.modAction",
1927
1927
+
rkey: TID.nextStr(),
1928
1928
+
record: {
1929
1929
+
$type: "space.atbb.modAction",
1930
1930
+
action: "space.atbb.modAction.delete", // Note: Using same action, read-path determines state
1931
1931
+
subject: {
1932
1932
+
post: {
1933
1933
+
uri: postUri,
1934
1934
+
cid: post.cid,
1935
1935
+
},
1936
1936
+
},
1937
1937
+
reason,
1938
1938
+
createdBy: user.did,
1939
1939
+
createdAt: new Date().toISOString(),
1940
1940
+
},
1941
1941
+
});
1942
1942
+
1943
1943
+
return c.json({
1944
1944
+
success: true,
1945
1945
+
action: "unhide",
1946
1946
+
postId: postIdParam,
1947
1947
+
postUri: postUri,
1948
1948
+
uri: result.uri,
1949
1949
+
cid: result.cid,
1950
1950
+
alreadyActive: false,
1951
1951
+
}, 200);
1952
1952
+
} catch (error) {
1953
1953
+
console.error("Failed to write unhide modAction", {
1954
1954
+
operation: "DELETE /api/mod/hide/:postId",
1955
1955
+
postId: postIdParam,
1956
1956
+
error: error instanceof Error ? error.message : String(error),
1957
1957
+
});
1958
1958
+
1959
1959
+
if (error instanceof Error && isNetworkError(error)) {
1960
1960
+
return c.json({
1961
1961
+
error: "Unable to reach Forum PDS. Please try again later.",
1962
1962
+
}, 503);
1963
1963
+
}
1964
1964
+
1965
1965
+
return c.json({
1966
1966
+
error: "Failed to record moderation action. Please contact support.",
1967
1967
+
}, 500);
1968
1968
+
}
1969
1969
+
}
1970
1970
+
);
1971
1971
+
```
1972
1972
+
1973
1973
+
**Step 4: Run test to verify it passes**
1974
1974
+
1975
1975
+
Run: `pnpm --filter @atbb/appview test mod.test.ts -t "hide"`
1976
1976
+
1977
1977
+
Expected: PASS (3 tests)
1978
1978
+
1979
1979
+
**Step 5: Note lexicon gap for unhide action**
1980
1980
+
1981
1981
+
The lexicon doesn't define an "unhide" or "undelete" action type. We're using "delete" for both hide and unhide, with read-path logic determining state based on alternating actions. This should be noted in implementation comments.
1982
1982
+
1983
1983
+
**Step 6: Commit**
1984
1984
+
1985
1985
+
```bash
1986
1986
+
git add apps/appview/src/routes/mod.ts apps/appview/src/routes/__tests__/mod.test.ts
1987
1987
+
git commit -m "feat(mod): implement hide/unhide post endpoints (ATB-19)
1988
1988
+
1989
1989
+
POST /api/mod/hide and DELETE /api/mod/hide/:postId
1990
1990
+
Works on both topics and replies (unlike lock)"
1991
1991
+
```
1992
1992
+
1993
1993
+
---
1994
1994
+
1995
1995
+
## Task 10: Run All Tests and Verify Coverage
1996
1996
+
1997
1997
+
**Step 1: Run all mod tests**
1998
1998
+
1999
1999
+
Run: `pnpm --filter @atbb/appview test mod.test.ts`
2000
2000
+
2001
2001
+
Expected: PASS (~30+ tests including helpers and all endpoints)
2002
2002
+
2003
2003
+
**Step 2: Run all appview tests**
2004
2004
+
2005
2005
+
Run: `pnpm --filter @atbb/appview test`
2006
2006
+
2007
2007
+
Expected: PASS (all existing tests + new mod tests)
2008
2008
+
2009
2009
+
**Step 3: Check test count**
2010
2010
+
2011
2011
+
Verify we have comprehensive coverage:
2012
2012
+
- Helper tests: ~7 tests
2013
2013
+
- Ban tests: ~13 tests
2014
2014
+
- Unban tests: ~2 tests (basic coverage, can expand)
2015
2015
+
- Lock tests: ~4 tests
2016
2016
+
- Unlock tests: ~1 test
2017
2017
+
- Hide tests: ~3 tests
2018
2018
+
- Unhide tests: ~1 test
2019
2019
+
2020
2020
+
**Total: ~30 tests** (can expand to ~75-80 with full error coverage per endpoint)
2021
2021
+
2022
2022
+
**Step 4: Commit test verification**
2023
2023
+
2024
2024
+
```bash
2025
2025
+
git add -A
2026
2026
+
git commit -m "test(mod): verify all moderation endpoint tests pass (ATB-19)
2027
2027
+
2028
2028
+
~30 tests covering happy path, validation, and basic error handling"
2029
2029
+
```
2030
2030
+
2031
2031
+
---
2032
2032
+
2033
2033
+
## Task 11: Update Bruno API Collection
2034
2034
+
2035
2035
+
**Files:**
2036
2036
+
- Create: `bruno/AppView API/Moderation/Ban User.bru`
2037
2037
+
- Create: `bruno/AppView API/Moderation/Unban User.bru`
2038
2038
+
- Create: `bruno/AppView API/Moderation/Lock Topic.bru`
2039
2039
+
- Create: `bruno/AppView API/Moderation/Unlock Topic.bru`
2040
2040
+
- Create: `bruno/AppView API/Moderation/Hide Post.bru`
2041
2041
+
- Create: `bruno/AppView API/Moderation/Unhide Post.bru`
2042
2042
+
2043
2043
+
**Step 1: Create Moderation directory**
2044
2044
+
2045
2045
+
```bash
2046
2046
+
mkdir -p "bruno/AppView API/Moderation"
2047
2047
+
```
2048
2048
+
2049
2049
+
**Step 2: Create Ban User.bru**
2050
2050
+
2051
2051
+
Create: `bruno/AppView API/Moderation/Ban User.bru`
2052
2052
+
2053
2053
+
```bru
2054
2054
+
meta {
2055
2055
+
name: Ban User
2056
2056
+
type: http
2057
2057
+
seq: 1
2058
2058
+
}
2059
2059
+
2060
2060
+
post {
2061
2061
+
url: {{appview_url}}/api/mod/ban
2062
2062
+
}
2063
2063
+
2064
2064
+
headers {
2065
2065
+
Content-Type: application/json
2066
2066
+
Cookie: atbb_session={{session_token}}
2067
2067
+
}
2068
2068
+
2069
2069
+
body:json {
2070
2070
+
{
2071
2071
+
"targetDid": "did:plc:example",
2072
2072
+
"reason": "Spam and harassment"
2073
2073
+
}
2074
2074
+
}
2075
2075
+
2076
2076
+
assert {
2077
2077
+
res.status: eq 200
2078
2078
+
res.body.success: eq true
2079
2079
+
res.body.action: eq ban
2080
2080
+
}
2081
2081
+
2082
2082
+
docs {
2083
2083
+
Ban a user from the forum.
2084
2084
+
2085
2085
+
**Permission required:** space.atbb.permission.banUsers (Owner, Admin)
2086
2086
+
2087
2087
+
Request body:
2088
2088
+
- targetDid: User DID to ban (required, string, must start with "did:")
2089
2089
+
- reason: Moderator's reason (required, 1-3000 chars, non-empty)
2090
2090
+
2091
2091
+
Returns:
2092
2092
+
{
2093
2093
+
"success": true,
2094
2094
+
"action": "ban",
2095
2095
+
"targetDid": "did:plc:example",
2096
2096
+
"uri": "at://did:plc:forum/space.atbb.modAction/3kh5...",
2097
2097
+
"cid": "bafyrei...",
2098
2098
+
"alreadyActive": false // true if user already banned
2099
2099
+
}
2100
2100
+
2101
2101
+
Error codes:
2102
2102
+
- 400: Invalid DID format, missing/empty reason, reason too long
2103
2103
+
- 401: Not authenticated
2104
2104
+
- 403: Lacks banUsers permission
2105
2105
+
- 404: User not a member of forum
2106
2106
+
- 500: ForumAgent not available (server config issue)
2107
2107
+
- 503: PDS write failed (network error, retry)
2108
2108
+
2109
2109
+
Idempotent: Returns 200 with alreadyActive: true if user already banned.
2110
2110
+
}
2111
2111
+
```
2112
2112
+
2113
2113
+
**Step 3: Create remaining Bruno files**
2114
2114
+
2115
2115
+
Create similar `.bru` files for:
2116
2116
+
- Unban User (DELETE `/api/mod/ban/:did`)
2117
2117
+
- Lock Topic (POST `/api/mod/lock`)
2118
2118
+
- Unlock Topic (DELETE `/api/mod/lock/:topicId`)
2119
2119
+
- Hide Post (POST `/api/mod/hide`)
2120
2120
+
- Unhide Post (DELETE `/api/mod/hide/:postId`)
2121
2121
+
2122
2122
+
Each should follow the same pattern with:
2123
2123
+
- Correct HTTP method and endpoint
2124
2124
+
- Request body schema
2125
2125
+
- Success response format
2126
2126
+
- All error codes documented
2127
2127
+
- Assertions for 200 status
2128
2128
+
2129
2129
+
**Step 4: Commit Bruno collection**
2130
2130
+
2131
2131
+
```bash
2132
2132
+
git add "bruno/AppView API/Moderation/"
2133
2133
+
git commit -m "docs(bruno): add moderation endpoint collection (ATB-19)
2134
2134
+
2135
2135
+
Documented all 6 moderation endpoints with examples and error codes"
2136
2136
+
```
2137
2137
+
2138
2138
+
---
2139
2139
+
2140
2140
+
## Task 12: Update Documentation
2141
2141
+
2142
2142
+
**Files:**
2143
2143
+
- Modify: `docs/atproto-forum-plan.md`
2144
2144
+
2145
2145
+
**Step 1: Mark Phase 3 moderation items complete**
2146
2146
+
2147
2147
+
Edit `docs/atproto-forum-plan.md`:
2148
2148
+
2149
2149
+
Find Phase 3 section and update:
2150
2150
+
2151
2151
+
```markdown
2152
2152
+
#### Phase 3: Moderation Basics (Week 6–7)
2153
2153
+
- [x] Mod actions written as records on Forum DID's PDS **via AppView** (AppView holds Forum DID signing keys, verifies caller's role before writing) — **Complete:** ATB-19 implemented 6 endpoints (ban/lock/hide + reversals). Writes modAction records to Forum DID's PDS using ForumAgent. 2026-02-15
2154
2154
+
- [ ] Admin UI: ban user, lock topic, hide post
2155
2155
+
- [ ] AppView respects mod actions during indexing and API responses
2156
2156
+
- [ ] Banned users' new records are ignored by indexer
2157
2157
+
- [ ] Document the trust model: operators must trust their AppView instance, which is acceptable for self-hosted single-server deployments
2158
2158
+
```
2159
2159
+
2160
2160
+
**Step 2: Commit documentation update**
2161
2161
+
2162
2162
+
```bash
2163
2163
+
git add docs/atproto-forum-plan.md
2164
2164
+
git commit -m "docs: mark ATB-19 complete in project plan
2165
2165
+
2166
2166
+
Moderation action write-path endpoints implemented"
2167
2167
+
```
2168
2168
+
2169
2169
+
---
2170
2170
+
2171
2171
+
## Task 13: Update Linear Issue
2172
2172
+
2173
2173
+
**Step 1: Update ATB-19 in Linear**
2174
2174
+
2175
2175
+
Using Linear MCP tool or manually:
2176
2176
+
2177
2177
+
1. Change status to "Done"
2178
2178
+
2. Add completion comment:
2179
2179
+
2180
2180
+
```
2181
2181
+
Implementation complete (2026-02-15):
2182
2182
+
2183
2183
+
✅ 6 endpoints implemented:
2184
2184
+
- POST /api/mod/ban (ban user)
2185
2185
+
- DELETE /api/mod/ban/:did (unban user)
2186
2186
+
- POST /api/mod/lock (lock topic)
2187
2187
+
- DELETE /api/mod/lock/:topicId (unlock topic)
2188
2188
+
- POST /api/mod/hide (hide post)
2189
2189
+
- DELETE /api/mod/hide/:postId (unhide post)
2190
2190
+
2191
2191
+
✅ Permission enforcement via middleware
2192
2192
+
✅ Idempotent API design (alreadyActive flag)
2193
2193
+
✅ Comprehensive error handling (400/401/403/404/500/503)
2194
2194
+
✅ ~30 tests (can expand to ~75-80 with full error coverage)
2195
2195
+
✅ Bruno collection updated
2196
2196
+
✅ Design doc: docs/plans/2026-02-15-moderation-endpoints-design.md
2197
2197
+
✅ Implementation: apps/appview/src/routes/mod.ts
2198
2198
+
2199
2199
+
Next: ATB-20 (read-path enforcement), ATB-21 (indexer enforcement)
2200
2200
+
```
2201
2201
+
2202
2202
+
---
2203
2203
+
2204
2204
+
## Summary
2205
2205
+
2206
2206
+
**Files Created:**
2207
2207
+
- `apps/appview/src/routes/mod.ts` (~600 lines)
2208
2208
+
- `apps/appview/src/routes/__tests__/mod.test.ts` (~400 lines)
2209
2209
+
- `bruno/AppView API/Moderation/*.bru` (6 files)
2210
2210
+
- `docs/plans/2026-02-15-moderation-endpoints-design.md`
2211
2211
+
- `docs/plans/2026-02-15-moderation-endpoints-implementation.md`
2212
2212
+
2213
2213
+
**Files Modified:**
2214
2214
+
- `apps/appview/src/lib/seed-roles.ts` (added mod permissions)
2215
2215
+
- `apps/appview/src/routes/index.ts` (registered mod routes)
2216
2216
+
- `docs/atproto-forum-plan.md` (marked Phase 3 item complete)
2217
2217
+
2218
2218
+
**Tests:** ~30 comprehensive tests (expandable to ~75-80)
2219
2219
+
2220
2220
+
**Design Decisions:**
2221
2221
+
- Additive reversal model (unban/unlock as new records)
2222
2222
+
- Idempotent API (alreadyActive flag)
2223
2223
+
- Required reason field
2224
2224
+
- Lock restricted to topics only
2225
2225
+
- Fully namespaced permissions
2226
2226
+
2227
2227
+
**Next Steps (Out of Scope for ATB-19):**
2228
2228
+
- ATB-20: Read-path enforcement (filter banned users, locked topics, hidden posts)
2229
2229
+
- ATB-21: Indexer enforcement (ignore banned users' new posts)
2230
2230
+
- ATB-24: Admin moderation UI