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
6 changes: 4 additions & 2 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ flowchart LR
subgraph CF["lilka-catalog-api · Cloudflare Worker (api/)"]
API["API routes<br/>/auth · /apps · /mod · /scan"]
CAT["/catalog/…<br/>/catalog/export.json"]
D1[("D1<br/>users · sessions · apps<br/>versions · reports")]
D1[("D1<br/>users · sessions · apps<br/>versions · ratings · reports")]
R2[("R2<br/>&lt;id&gt;-&lt;name&gt;/v/&lt;version&gt;/&lt;file&gt;")]
end

Expand Down Expand Up @@ -171,7 +171,7 @@ All bodies are JSON unless noted. Authenticated calls send
| GET | `/auth/login?return=<url>` | anyone | Redirects to GitHub. `return` must be on `ALLOWED_ORIGINS`; the OAuth state is signed, nothing is stored |
| GET | `/auth/callback` | GitHub | Redirects to `return#lilka_token=<token>` |
| POST | `/auth/logout` | user | Ends the session |
| GET | `/auth/me` | user | Current user |
| GET | `/auth/me` | user | Current user and `ratings` (slug → stars) they gave, so the site shows them without a request per app |
| GET | `/catalog/{apps,wallpapers,mods}/index_<n>.json` | mirror job | Ids of published items, 12 per page |
| GET | `/catalog/<dir>/<id>/index.json`, `index_short.json` | mirror job | Manifests in build.py's format |
| GET | `/catalog/<dir>/<id>/static/<file>`, `package.zip` | mirror job | Files |
Expand All @@ -183,6 +183,8 @@ All bodies are JSON unless noted. Authenticated calls send
| POST | `/apps` | user | Create draft; `kind`: `app`, `wallpaper` or `mod` |
| PATCH | `/apps/:slug` | owner, mod | Edit metadata, `status: hidden \| published`; moderators also `author`, `owner` (login) |
| POST | `/apps/:slug/versions` | owner, mod | Multipart: `version`, `keira_version`, `entry_type` (`lua`, `js`, `binary`, `archive`), `changelog`, files `entry`, `files[]`, `icon`, `screenshots[]`. Mods: no entry, model files as `files[]` |
| PUT | `/apps/:slug/rating` | user (not owner) | `{ "stars": 1..5 }` → `{ my_rating, average, count }`; sending the same stars again writes nothing |
| DELETE | `/apps/:slug/rating` | user | Removes your rating |
| POST | `/apps/:slug/reports` | user | `{ "reason": "..." }` |
| 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 |
Expand Down
4 changes: 4 additions & 0 deletions api/migrations/0006_ratings_by_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- /auth/me returns the signed-in user's own ratings with the session check,
-- so the site needs no request per app to show them; this keeps that lookup
-- to the user's own rows instead of a scan of every rating.
CREATE INDEX ratings_user ON ratings (user_id);
25 changes: 23 additions & 2 deletions api/scripts/smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ call 404 "approved file is not served to strangers" "${fan[@]}" "$API/files/smok
call 200 "owner gets the approved file" "${user[@]}" "$API/files/smoke-hello/$version_id/hello.lua"
call 200 "HEAD for the owner" -I "${user[@]}" "$API/files/smoke-hello/$version_id/hello.lua"

call 401 "rating needs a session" "${json[@]}" -X PUT "$API/apps/smoke-hello/rating" -d '{"stars":5}'
call 403 "owner cannot rate own app" "${user[@]}" "${json[@]}" -X PUT "$API/apps/smoke-hello/rating" -d '{"stars":5}'
call 400 "stars must be 1..5" "${fan[@]}" "${json[@]}" -X PUT "$API/apps/smoke-hello/rating" -d '{"stars":6}'
call 200 "rate" "${fan[@]}" "${json[@]}" -X PUT "$API/apps/smoke-hello/rating" -d '{"stars":2}'
call 200 "change the rating" "${fan[@]}" "${json[@]}" -X PUT "$API/apps/smoke-hello/rating" -d '{"stars":4}'
check "rating answer has the fresh average" '{"my_rating":4,"average":4,"count":1}' "$(jq -c . "$work/body")"
call 200 "moderator rates too" "${mod[@]}" "${json[@]}" -X PUT "$API/apps/smoke-hello/rating" -d '{"stars":5}'
check "average of two" "4.5 2" "$(jq -r '"\(.average) \(.count)"' "$work/body")"
call 200 "own ratings come with the session check" "${fan[@]}" "$API/auth/me"
check "/auth/me carries the user's ratings" 4 "$(jq -r '.ratings["smoke-hello"]' "$work/body")"
call 200 "remove a rating" "${mod[@]}" -X DELETE "$API/apps/smoke-hello/rating"
check "removed rating is gone" null "$(curl -sS "${mod[@]}" "$API/auth/me" | jq -r '.ratings["smoke-hello"]')"
call 404 "no rating for a draft" "${fan[@]}" "${json[@]}" -X PUT "$API/apps/smoke-mod-x/rating" -d '{"stars":3}'
call 201 "report" "${fan[@]}" "${json[@]}" -X POST "$API/apps/smoke-hello/reports" -d '{"reason":"smoke"}'
call 200 "open reports" "${mod[@]}" "$API/mod/reports"

Expand All @@ -113,9 +126,9 @@ jq -e '."@smoke-dev"[0].path == "smoke-hello"' "$work/body" >/dev/null || { echo
call 404 "catalog unknown kind" "${mirror[@]}" "$API/catalog/games/index_0.json"
call 401 "listing.json needs the mirror secret" "$API/catalog/apps/listing.json"
call 200 "listing.json for the site" "${mirror[@]}" "$API/catalog/apps/listing.json"
jq -e '.items[] | select(.path == "smoke-hello") | (.added != null) and (.updated != null) and .author == "@smoke-dev"' \
jq -e '.items[] | select(.path == "smoke-hello") | (.added != null) and (.updated != null) and .rating.average == 4 and .rating.count == 1 and .author == "@smoke-dev"' \
"$work/body" >/dev/null && li=ok || li="$(jq -c '.items[] | select(.path == "smoke-hello")' "$work/body")"
check "listing has dates and author" ok "$li"
check "listing has dates, rating and author" ok "$li"
pub=$(npx wrangler d1 execute lilka-catalog --local --json --command "SELECT published_at FROM apps WHERE slug = 'smoke-hello'" 2>/dev/null | jq -r '.[0].results[0].published_at')
check "published_at set on first approval" ok "$([ -n "$pub" ] && [ "$pub" != null ] && echo ok || echo missing)"

Expand Down Expand Up @@ -245,6 +258,14 @@ sig=${state#*.}; first=${sig:0:1}; [ "$first" = A ] && swap=B || swap=A
call 400 "tampered OAuth state refused" "$API/auth/callback?code=x&state=${state%%.*}.$swap${sig:1}"
call 400 "made-up OAuth state refused" "$API/auth/callback?code=x&state=eyJyIjoiaHR0cHM6Ly9ldmlsLmV4YW1wbGUvIn0.AAAA"

npx wrangler d1 execute lilka-catalog --local --command "
INSERT INTO users (id, login, role) VALUES (5, 'smoke-troll', 'user');
INSERT INTO sessions VALUES ('$(sha troll-token)', 5, datetime('now', '+1 day'));" >/dev/null
call 200 "troll rates" -H "Authorization: Bearer troll-token" "${json[@]}" -X PUT "$API/apps/smoke-hello/rating" -d '{"stars":1}'
call 200 "moderator bans the troll" "${mod[@]}" -X POST "$API/mod/users/smoke-troll/ban"
trolls=$(npx wrangler d1 execute lilka-catalog --local --json --command "SELECT COUNT(*) AS n FROM ratings WHERE user_id = 5" 2>/dev/null | jq -r '.[0].results[0].n')
check "a ban removes the user's ratings" 0 "$trolls"

# Last, because it uses up this IP's budget for a minute
limited=$(seq 1 330 | xargs -P 30 -I{} curl -s -o /dev/null -w '%{http_code}\n' "$API/" | grep -c '^429$' || true)
check "per-IP rate limit kicks in" yes "$([ "$limited" -gt 0 ] && echo yes || echo "no 429 in 330 requests")"
Expand Down
43 changes: 43 additions & 0 deletions api/src/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export const APP_SELECT = `
SELECT a.*, u.login AS owner_login, u.avatar_url AS owner_avatar
FROM apps a JOIN users u ON u.id = a.owner_id`;

/** The rating block of listing.json and of PUT /apps/:slug/rating. */
export function ratingSummary(count: number, total: number | null) {
return { average: count && total !== null ? Math.round((total / count) * 10) / 10 : null, count };
}

export function fileUrl(env: Env, slug: string, versionId: number, name: string): string {
return `${env.PUBLIC_URL.replace(/\/$/, '')}/files/${slug}/${versionId}/${encodeURIComponent(name)}`;
}
Expand Down Expand Up @@ -491,6 +496,44 @@ apps.post('/:slug/versions', async (c) => {
return c.json(serializeVersion(c.env, app.slug, created!, true), 201);
});

/**
* One rating per user and app, changed by sending it again.
*
* Costs one app lookup, one upsert and one read of this app's ratings, and
* nothing is dispatched: the averages reach listing.json with the next hourly
* mirror run, while the site shows the new vote at once from this response.
*/
apps.put('/:slug/rating', async (c) => {
const user = requireUser(c);
const { stars } = await readJson<{ stars?: unknown }>(c);
if (typeof stars !== 'number' || !Number.isInteger(stars) || stars < 1 || stars > 5) {
fail(400, 'stars must be an integer from 1 to 5');
}
const app = await c.env.DB.prepare(`SELECT id, owner_id, status FROM apps WHERE slug = ?`)
.bind(c.req.param('slug'))
.first<{ id: number; owner_id: number; status: string }>();
if (!app || app.status !== 'published') fail(404, 'app not found');
if (app.owner_id === user.id) fail(403, 'cannot rate your own app');
const [, summary] = await c.env.DB.batch<{ n: number; total: number | null }>([
c.env.DB.prepare(
`INSERT INTO ratings (app_id, user_id, stars) VALUES (?, ?, ?)
ON CONFLICT (app_id, user_id) DO UPDATE SET stars = excluded.stars, updated_at = datetime('now')
WHERE ratings.stars != excluded.stars`,
).bind(app.id, user.id, stars),
c.env.DB.prepare(`SELECT COUNT(*) AS n, SUM(stars) AS total FROM ratings WHERE app_id = ?`).bind(app.id),
]);
const row = summary.results[0];
return c.json({ my_rating: stars, ...ratingSummary(row?.n ?? 0, row?.total ?? null) });
});

apps.delete('/:slug/rating', async (c) => {
const user = requireUser(c);
await c.env.DB.prepare(`DELETE FROM ratings WHERE user_id = ? AND app_id = (SELECT id FROM apps WHERE slug = ?)`)
.bind(user.id, c.req.param('slug'))
.run();
return c.json({ ok: true });
});

apps.post('/:slug/reports', async (c) => {
const user = requireUser(c);
const app = await findApp(c.env, c.req.param('slug'));
Expand Down
14 changes: 13 additions & 1 deletion api/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,19 @@ auth.post('/logout', async (c) => {
return c.json({ ok: true });
});

auth.get('/me', (c) => c.json({ user: requireUser(c) }));
/**
* The session check the site makes once per page load. It also carries the
* user's own ratings, so showing "your rating" costs no request per app.
*/
auth.get('/me', async (c) => {
const user = requireUser(c);
const { results } = await c.env.DB.prepare(
`SELECT a.slug, r.stars FROM ratings r JOIN apps a ON a.id = r.app_id WHERE r.user_id = ? LIMIT 2000`,
)
.bind(user.id)
.all<{ slug: string; stars: number }>();
return c.json({ user, ratings: Object.fromEntries(results.map((r) => [r.slug, r.stars])) });
});

/**
* Creates or refreshes the user and opens a session.
Expand Down
11 changes: 8 additions & 3 deletions api/src/catalog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hono, type Context } from 'hono';
import type { AppEnv, Env } from './env';
import { displayAuthor, kindDir, type AppRow } from './apps';
import { displayAuthor, kindDir, ratingSummary, type AppRow } from './apps';
import { contentTypeOf, fail, parseJsonArray, sha256Hex } from './util';
import { objectKey } from './versions';

Expand Down Expand Up @@ -48,6 +48,8 @@ interface CatalogRow extends AppRow {
owner_blog: string | null;
owner_location: string | null;
owner_company: string | null;
rating_count: number;
rating_sum: number | null;
}

/** Published apps with their latest approved version, sorted like Python's sorted(os.listdir()). */
Expand All @@ -67,10 +69,12 @@ async function published(env: Env, kind?: AppRow['kind'], slug?: string): Promis
u.bio AS owner_bio, u.blog AS owner_blog, u.location AS owner_location, u.company AS owner_company,
v.id AS v_id, v.keira_version, v.entry_type, v.entry_file, v.files, v.files_meta, v.icon, v.icon_min,
v.screenshots, v.changelog, v.changelog_en, v.package, v.security, v.storage_prefix,
v.approved_at
v.approved_at, COALESCE(r.n, 0) AS rating_count, r.total AS rating_sum
FROM apps a
JOIN users u ON u.id = a.owner_id
JOIN versions v ON v.id = a.latest_version_id
-- Every rating once per export (the hourly mirror run), not per vote
LEFT JOIN (SELECT app_id, COUNT(*) AS n, SUM(stars) AS total FROM ratings GROUP BY app_id) r ON r.app_id = a.id
WHERE ${where.join(' AND ')}
ORDER BY a.slug COLLATE BINARY`,
)
Expand Down Expand Up @@ -349,7 +353,7 @@ function authorsInfoDoc(rows: CatalogRow[]) {
* listing.json: everything the website's list needs for one kind, in one file.
*
* Not part of the old build.py layout and not read by Keira, so it can carry
* what the catalog list sorts by (dates, author) without touching the
* what the catalog list sorts by (dates, author, rating) without touching the
* manifests devices parse. It also turns a list page from 1 index plus 12
* manifests into one request.
*/
Expand All @@ -365,6 +369,7 @@ function listingDoc(rows: CatalogRow[]) {
icon: row.icon ?? '',
added: row.published_at ?? row.approved_at,
updated: row.approved_at,
rating: ratingSummary(row.rating_count, row.rating_sum),
};
if (Object.keys(loc).length) {
entry.languages = Object.keys(loc);
Expand Down
3 changes: 2 additions & 1 deletion api/src/moderation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ moderation.get('/apps', async (c) => {
});
});

/** A ban also hides every app of the user and ends their sessions. */
/** A ban also hides every app of the user, drops their ratings and ends their sessions. */
moderation.post('/users/:login/ban', async (c) => {
const user = await c.env.DB.prepare(`SELECT id, role FROM users WHERE login = ? COLLATE NOCASE`)
.bind(c.req.param('login'))
Expand All @@ -200,6 +200,7 @@ moderation.post('/users/:login/ban', async (c) => {
c.env.DB.prepare(`UPDATE apps SET status = 'hidden', updated_at = datetime('now') WHERE owner_id = ?`).bind(
user.id,
),
c.env.DB.prepare(`DELETE FROM ratings WHERE user_id = ?`).bind(user.id),
c.env.DB.prepare(`DELETE FROM sessions WHERE user_id = ?`).bind(user.id),
]);
c.executionCtx.waitUntil(dispatch(c.env, c.env.CATALOG_REPO, 'api-sync', {}));
Expand Down
Loading
Loading