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
3 changes: 2 additions & 1 deletion api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ the code controls is what a request costs:
few rows per item instead of a scan per item;
- rate limits per IP (300/min), per user for writes (60/min) and uploads
(10/min) run before any database work;
- per account: 3 new items a day, 20 in all, 5 versions waiting for review
- per account: 3 new items a day, 20 in all (both count items the user
created, not imports handed over to them), 5 versions waiting for review
(moderators are exempt);
- on the free plan the limits are hard caps: a flood can stop the API until
00:00 UTC, but never costs money. Do not move to a paid plan without a
Expand Down
11 changes: 11 additions & 0 deletions api/migrations/0007_created_by.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Who created an item, as opposed to who owns it now. The per-account limits
-- (3 new items a day, 20 in all) count what a user created: imports handed
-- over to their author were created by the import, and counting them blocked
-- authors from adding anything for a day after the hand-over.
-- (0006 is ratings_by_user on a parallel branch; the order does not matter.)
ALTER TABLE apps ADD COLUMN created_by INTEGER REFERENCES users (id);
-- Imports are the apps whose version skipped review; everything else was
-- created by its current owner (nothing user-made has been handed over yet)
UPDATE apps SET created_by = owner_id
WHERE NOT EXISTS (SELECT 1 FROM versions v WHERE v.app_id = apps.id AND v.auto_approve = 1);
CREATE INDEX apps_created_by ON apps (created_by, created_at);
10 changes: 8 additions & 2 deletions api/scripts/smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ call 409 "catalog account cannot be banned" "${mod[@]}" -X POST "$API/mod/users/
call 404 "hand over needs a signed-in user" "${mod[@]}" "${json[@]}" -X PATCH "$API/apps/Smoke_Import" -d '{"owner":"nobody-here"}'
call 200 "moderator hands the import over" "${mod[@]}" "${json[@]}" -X PATCH "$API/apps/Smoke_Import" -d '{"owner":"smoke-dev"}'
check "new owner sees it in their apps" ok "$(curl -sS "${user[@]}" "$API/users/smoke-dev/apps" | jq -r '[.items[].slug] | index("Smoke_Import") | if . == null then "missing" else "ok" end')"
# The import handed over today was created by the import, not by smoke-dev:
# with their own 2 items today they may add a 3rd, and only the 4th is refused
newapp() { curl -s -o "$work/body" -w '%{http_code}' "${user[@]}" "${json[@]}" -X POST "$API/apps" \
-d "{\"slug\":\"$1\",\"kind\":\"app\",\"name\":\"s\",\"short_description\":\"s\",\"sources_url\":\"https://github.com/a/b\"}"; }
check "a handed-over import does not count as a new item" 201 "$(newapp smoke-third)"
check "the 4th own item in a day is refused" 429 "$(newapp smoke-fourth)"
call 200 "app after hand-over" "${mod[@]}" "$API/apps/Smoke_Import"
check "author line kept after hand-over" "@smoke-dev" "$(jq -r .author "$work/body")"
call 404 "import is not public before processing" "${mirror[@]}" "$API/catalog/apps/Smoke_Import/index.json"
Expand All @@ -174,8 +180,8 @@ check "daily cap says so" ok "$msg"
# Yesterday's items no longer count, but 20 per account still holds
npx wrangler d1 execute lilka-catalog --local --command "
UPDATE apps SET created_at = datetime('now', '-2 days') WHERE owner_id = 3;
INSERT INTO apps (slug, owner_id, kind, sources_url, created_at)
SELECT 'fan-old-' || value, 3, 'app', 'https://github.com/a/b', datetime('now', '-2 days')
INSERT INTO apps (slug, owner_id, created_by, kind, sources_url, created_at)
SELECT 'fan-old-' || value, 3, 3, 'app', 'https://github.com/a/b', datetime('now', '-2 days')
FROM json_each('[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]');" >/dev/null
check "a day later the fan can add again" 1 "$(spam 1 fan-next)"
check "apps per account capped at 20" 0 "$(spam 1 fan-over)"
Expand Down
25 changes: 13 additions & 12 deletions api/src/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { MAX_UPLOAD_BYTES, createVersion, objectKey, readVersionForm } from './v
const PAGE_SIZE = 24;
/** Unreviewed uploads per user: keeps one account from flooding the queue. */
const MAX_PENDING_PER_USER = 5;
/** Apps per account, drafts included: stops slug squatting and spam. */
/** Apps an account created, drafts included: stops slug squatting and spam. */
const MAX_APPS_PER_USER = 20;
/** New apps, wallpapers and mods per account in any 24 hours. */
export const MAX_NEW_ITEMS_PER_DAY = 3;
Expand Down Expand Up @@ -320,30 +320,31 @@ 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') {
const owned = await c.env.DB.prepare(`SELECT COUNT(*) AS n FROM apps WHERE owner_id = ?`)
.bind(user.id)
.first<{ n: number }>();
if ((owned?.n ?? 0) >= MAX_APPS_PER_USER) fail(429, `at most ${MAX_APPS_PER_USER} apps per account`);
// Counted from the table: it holds across Worker instances and over a
// whole day, which the per-minute rate limiters cannot do
const today = await c.env.DB.prepare(
`SELECT COUNT(*) AS n FROM apps WHERE owner_id = ? AND created_at > datetime('now', '-1 day')`,
// whole day, which the per-minute rate limiters cannot do. Both count
// what the user created, not imports handed over to them (migration 0007)
const counts = await c.env.DB.prepare(
`SELECT COUNT(*) AS total, COALESCE(SUM(created_at > datetime('now', '-1 day')), 0) AS today
FROM apps WHERE created_by = ?`,
)
.bind(user.id)
.first<{ n: number }>();
if ((today?.n ?? 0) >= MAX_NEW_ITEMS_PER_DAY) {
.first<{ total: number; today: number }>();
if ((counts?.total ?? 0) >= MAX_APPS_PER_USER) fail(429, `at most ${MAX_APPS_PER_USER} apps per account`);
// The 4th item within 24 hours is refused: 3 are allowed
if ((counts?.today ?? 0) >= MAX_NEW_ITEMS_PER_DAY) {
fail(429, `at most ${MAX_NEW_ITEMS_PER_DAY} new items per day, try again tomorrow`);
}
}

await c.env.DB.prepare(
`INSERT INTO apps (slug, owner_id, kind, name_uk, name_en, short_uk, short_en,
`INSERT INTO apps (slug, owner_id, created_by, kind, name_uk, name_en, short_uk, short_en,
description_uk, description_en, sources_type, sources_url, license)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
)
.bind(
body.slug,
user.id,
user.id,
kind,
name.uk,
name.en,
Expand Down
Loading