Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ Moderators are GitHub numeric user ids in `MODERATORS` of `api/wrangler.toml`.
They see a **Moderation** tab: versions waiting for review with code and image
preview, checksums and the antivirus result; approve, reject with a reason, ban
an author; open reports. **All items** lists every app, wallpaper and mod with
an owner selector, **Users** everyone who has signed in, with ban and unban.
an owner selector and **Manage**: edit every field including the author
line, publish or hide, review each version with its files, see and resolve
the item's reports, delete it for good. **Users** lists everyone who has
signed in; **Details** shows their activity, items and reports, and lets a
moderator mark them trusted (no item limits; still scanned and reviewed),
keep a private note, end their sessions, remove their ratings, ban or unban.

New uploads are announced in a GitHub issue labelled `moderation` (see
[api/README.md](api/README.md#lifecycle-of-an-upload)).
Expand Down
11 changes: 8 additions & 3 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,16 @@ All bodies are JSON unless noted. Authenticated calls send
| GET | `/users/:login/apps[?versions]` | user | Apps of a user (all of them, with versions, for the user themself or a moderator) |
| GET | `/files/:slug/:version/:name` | owner, mod | File by version id, for "My apps" and moderation previews |
| GET | `/mod/queue?status=` | mod | Versions by status, default `pending_review` |
| GET | `/mod/apps` | mod | Every item with its owner, drafts and hidden ones included |
| GET | `/mod/users` | mod | Everyone who has signed in, with role and item count |
| GET | `/mod/apps` | mod | Every item with owner, creator, version and pending counts, rating, open reports |
| DELETE | `/mod/apps/:slug` | mod | `{ "confirm": "<slug>" }`: deletes the item, its versions, ratings, reports and R2 files |
| GET | `/mod/users` | mod | Everyone who has signed in, with role, trusted, note flag and item count |
| GET | `/mod/users/:login` | mod | Profile, note, trusted, activity (created, ratings, sessions, last sign-in), items, reports filed |
| PATCH | `/mod/users/:login` | mod | `{ "trusted"?: bool, "note"?: string \| null }`; trusted lifts the per-account item limits |
| POST | `/mod/users/:login/sign-out` | mod | Ends every session of the user |
| DELETE | `/mod/users/:login/ratings` | mod | Removes every rating the user gave |
| POST | `/mod/versions/:id/approve` | mod | `{ "note"?: "..." }` |
| POST | `/mod/versions/:id/reject` | mod | `{ "note": "reason" }` |
| GET | `/mod/reports` | mod | Open reports |
| GET | `/mod/reports[?slug=]` | mod | Open reports, or every report on one item |
| POST | `/mod/reports/:id/resolve` | mod | |
| POST | `/mod/users/:login/ban` / `unban` | mod | Ban hides all their apps and ends sessions |
| POST | `/mod/import[?replace]` | mod | One app from the old repo (`scripts/import_catalog.py`) |
Expand Down
6 changes: 6 additions & 0 deletions api/migrations/0008_moderation_tools.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Moderator tools: a private note per user, and "trusted" for authors the
-- moderators know, which lifts the per-account item limits (3 new a day,
-- 20 in all, 5 waiting for review). Their uploads are still scanned and
-- reviewed.
ALTER TABLE users ADD COLUMN trusted INTEGER NOT NULL DEFAULT 0;
ALTER TABLE users ADD COLUMN mod_note TEXT;
38 changes: 38 additions & 0 deletions api/scripts/smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,43 @@ call 409 "no hand-over to a banned user" "${mod[@]}" "${json[@]}" -X PATCH "$API
call 200 "download name is encoded" -I "${user[@]}" "$API/files/smoke-hello/$version_id/hello.lua?download"
grep -qi "filename\*=UTF-8''hello.lua" "$work/body" && dl=ok || dl=raw
check "Content-Disposition uses filename*" ok "$dl"

# --- moderator tools ------------------------------------------------------
call 200 "all items with counts" "${mod[@]}" "$API/mod/apps"
jq -e '[.items[] | select(.slug == "smoke-hello")][0] | .versions >= 1 and .created_by == "smoke-dev" and (.rating | has("average")) and (.open_reports >= 1)' "$work/body" >/dev/null && ma=ok || ma=bad
check "all items: versions, creator, rating, reports" ok "$ma"
jq -e '[.items[] | select(.slug == "Smoke_Import")][0].created_by == null' "$work/body" >/dev/null && mi=ok || mi=bad
check "an import has no creator" ok "$mi"
call 403 "user details are for moderators" "${fan[@]}" "$API/mod/users/smoke-dev"
call 200 "moderator opens a user" "${mod[@]}" "$API/mod/users/smoke-fan"
jq -e '.stats.created == 20 and .stats.sessions == 1 and (.items | length) == 19 and .user.trusted == false' "$work/body" >/dev/null && ud=ok || ud=bad
check "user details: created 20, owns 19 (one handed away), sessions" ok "$ud"
call 400 "note too long" "${mod[@]}" "${json[@]}" -X PATCH "$API/mod/users/smoke-fan" -d "{\"note\":\"$(printf 'x%.0s' $(seq 1 2001))\"}"
call 403 "users cannot edit users" "${fan[@]}" "${json[@]}" -X PATCH "$API/mod/users/smoke-fan" -d '{"trusted":true}'
call 200 "moderator trusts the fan and leaves a note" "${mod[@]}" "${json[@]}" -X PATCH "$API/mod/users/smoke-fan" -d '{"trusted":true,"note":"knows the hardware"}'
check "a trusted author is past the 20-item cap" 1 "$(spam 1 fan-trusted)"
call 200 "moderator takes trust back" "${mod[@]}" "${json[@]}" -X PATCH "$API/mod/users/smoke-fan" -d '{"trusted":false}'
check "the cap is back" 0 "$(spam 1 fan-untrusted)"
check "the note is kept" "knows the hardware" "$(curl -sS "${mod[@]}" "$API/mod/users/smoke-fan" | jq -r .user.mod_note)"
npx wrangler d1 execute lilka-catalog --local --command "
INSERT INTO users (id, login, role) VALUES (6, 'smoke-temp', 'user');
INSERT INTO sessions VALUES ('$(sha temp-token)', 6, datetime('now', '+30 days'));" >/dev/null
temp=(-H "Authorization: Bearer temp-token")
call 200 "temp user rates" "${temp[@]}" "${json[@]}" -X PUT "$API/apps/smoke-hello/rating" -d '{"stars":2}'
call 200 "moderator removes the user's ratings" "${mod[@]}" -X DELETE "$API/mod/users/smoke-temp/ratings"
check "one rating removed" 1 "$(jq -r .ratings "$work/body")"
call 200 "moderator ends the user's sessions" "${mod[@]}" -X POST "$API/mod/users/smoke-temp/sign-out"
call 401 "the ended session no longer works" "${temp[@]}" "$API/auth/me"

call 201 "moderator makes an item to delete" "${mod[@]}" "${json[@]}" -X POST "$API/apps" \
-d '{"slug":"smoke-delete","kind":"app","name":"d","short_description":"d","sources_url":"https://github.com/a/b"}'
call 201 "with a version" "${mod[@]}" -X POST "$API/apps/smoke-delete/versions" \
-F version=1.0.0 -F keira_version=1.0.0 -F entry_type=lua -F "entry=@$work/hello.lua"
delete_prefix=$(npx wrangler d1 execute lilka-catalog --local --json --command "SELECT v.storage_prefix FROM versions v JOIN apps a ON a.id = v.app_id WHERE a.slug = 'smoke-delete'" 2>/dev/null | jq -r '.[0].results[0].storage_prefix')
call 400 "delete needs the slug repeated" "${mod[@]}" "${json[@]}" -X DELETE "$API/mod/apps/smoke-delete" -d '{"confirm":"smoke"}'
call 403 "users cannot delete items" "${user[@]}" "${json[@]}" -X DELETE "$API/mod/apps/smoke-delete" -d '{"confirm":"smoke-delete"}'
call 200 "moderator deletes the item" "${mod[@]}" "${json[@]}" -X DELETE "$API/mod/apps/smoke-delete" -d '{"confirm":"smoke-delete"}'
call 404 "deleted item is gone" "${mod[@]}" "$API/apps/smoke-delete"
r2_list() { curl -sS "$API/cdn-cgi/local/explorer/api/r2/buckets/lilka-catalog-files/objects?limit=1000" | jq -r '.result[].key'; }
# The local bucket outlives the database between runs, so look only at this run's folders
prefix_of() { npx wrangler d1 execute lilka-catalog --local --json --command "SELECT storage_prefix FROM versions WHERE id = $1" 2>/dev/null | jq -r '.[0].results[0].storage_prefix'; }
Expand All @@ -229,6 +266,7 @@ call 200 "processor clears it" "${scanner[@]}" -X POST "$API/scan/versions/$reje
call 200 "moderator rejects it" "${mod[@]}" "${json[@]}" -X POST "$API/mod/versions/$reject_id/reject" -d '{"note":"not a case"}'
sleep 1
check "rejected files are deleted" 0 "$(r2_list | grep -c "^$(prefix_of "$reject_id")/" || true)"
check "a deleted item's files are gone" 0 "$(r2_list | grep -c "^$delete_prefix/" || true)"
check "approved files are kept" 1 "$(r2_list | grep -c "^$(prefix_of "$version_id")/hello.lua$" || true)"

call 200 "owner hides app" "${user[@]}" "${json[@]}" -X PATCH "$API/apps/smoke-hello" -d '{"status":"hidden"}'
Expand Down
32 changes: 21 additions & 11 deletions api/src/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ export async function findApp(env: Env, slug: string): Promise<AppRow | null> {
return env.DB.prepare(`${APP_SELECT} WHERE a.slug = ?`).bind(slug).first<AppRow>();
}

/** Moderators, and authors a moderator marked trusted, skip the per-account item limits. */
function limitsLifted(user: User): boolean {
return user.role === 'moderator' || !!user.trusted;
}

function canManage(user: User | null, app: AppRow): boolean {
return !!user && user.role !== 'banned' && (user.id === app.owner_id || user.role === 'moderator');
}
Expand Down Expand Up @@ -324,7 +329,7 @@ apps.post('/', async (c) => {

const exists = await c.env.DB.prepare(`SELECT 1 FROM apps WHERE slug = ?`).bind(body.slug).first();
if (exists) fail(409, 'slug is taken');
if (user.role !== 'moderator') {
if (!limitsLifted(user)) {
// Counted from the table: it holds across Worker instances and over a
// whole day, which the per-minute rate limiters cannot do. Both count
// what the user created, not imports handed over to them (migration 0007)
Expand Down Expand Up @@ -380,20 +385,23 @@ apps.patch('/:slug', async (c) => {
params.push(value);
};

// Imported items were checked against the import's limits, longer than a
// new submission's; a moderator editing one must be able to save it
const mod = user.role === 'moderator';
if (body.name !== undefined) {
const name = readLocalized(body.name, 'name', 64);
const name = readLocalized(body.name, 'name', mod ? 200 : 64);
if (!name.uk && !name.en) fail(400, 'name cannot be empty');
set('name_uk', name.uk);
set('name_en', name.en);
}
if (body.short_description !== undefined) {
const short = readLocalized(body.short_description, 'short_description', 200);
const short = readLocalized(body.short_description, 'short_description', mod ? 1000 : 200);
if (!short.uk && !short.en) fail(400, 'short_description cannot be empty');
set('short_uk', short.uk);
set('short_en', short.en);
}
if (body.description !== undefined) {
const description = readLocalized(body.description, 'description', 20000);
const description = readLocalized(body.description, 'description', mod ? 100000 : 20000);
set('description_uk', description.uk);
set('description_en', description.en);
}
Expand Down Expand Up @@ -473,13 +481,15 @@ apps.post('/:slug/versions', async (c) => {
const length = Number(c.req.header('Content-Length') ?? 0);
if (length > MAX_UPLOAD_BYTES + 64 * 1024) fail(413, `upload is larger than ${MAX_UPLOAD_BYTES} bytes`);

const pending = await c.env.DB.prepare(
`SELECT COUNT(*) AS n FROM versions v JOIN apps a ON a.id = v.app_id
WHERE a.owner_id = ? AND v.status IN ('pending_scan', 'pending_review')`,
)
.bind(app.owner_id)
.first<{ n: number }>();
if ((pending?.n ?? 0) >= MAX_PENDING_PER_USER) fail(429, 'too many versions are waiting for review');
if (!limitsLifted(user)) {
const pending = await c.env.DB.prepare(
`SELECT COUNT(*) AS n FROM versions v JOIN apps a ON a.id = v.app_id
WHERE a.owner_id = ? AND v.status IN ('pending_scan', 'pending_review')`,
)
.bind(app.owner_id)
.first<{ n: number }>();
if ((pending?.n ?? 0) >= MAX_PENDING_PER_USER) fail(429, 'too many versions are waiting for review');
}

let form: FormData;
try {
Expand Down
2 changes: 1 addition & 1 deletion api/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const sessionMiddleware: MiddlewareHandler<AppEnv> = async (c, next) => {
if (match) {
const tokenHash = await sha256Hex(match[1]);
const user = await c.env.DB.prepare(
`SELECT u.id, u.login, u.name, u.avatar_url, u.role
`SELECT u.id, u.login, u.name, u.avatar_url, u.role, u.trusted
FROM sessions s JOIN users u ON u.id = s.user_id
WHERE s.token_hash = ? AND s.expires_at > datetime('now')`,
)
Expand Down
2 changes: 2 additions & 0 deletions api/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface User {
name: string | null;
avatar_url: string | null;
role: Role;
/** Set by a moderator: no per-account item limits (migration 0008). */
trusted: number;
}

export type AppEnv = {
Expand Down
Loading
Loading