web: moderate accounts, accept 12MB uploads, put SAVE under CREATE
- /admin User account rows gain BLOCK/UNBLOCK, REMOVE/RESTORE and DELETE. Blocked = cannot sign in (sessions swept), removed = hidden from the strip and cannot sign in, both reversible; DELETE drops the account with its photos and recipes and unlinks the files. An allowlisted account is never a target, so an admin cannot moderate or delete itself. - Photo uploads move from a 3MB API cap / 4m nginx cap to 12MB / 16m, and the browser shrinks an oversized still before sending it (2048px JPEG, avatars 512px) so the declared type still matches the sniffed bytes. - The studio SAVE leaves the top bar and sits under the CREATE RECIPES tab, labelled SAVE RECIPES.
This commit is contained in:
+92
-12
@@ -6,9 +6,13 @@ import { randomBytes, scryptSync, timingSafeEqual } from 'node:crypto';
|
||||
export const SESSION_COOKIE = 'rc_session';
|
||||
export const SESSION_MAX_AGE_S = 30 * 24 * 60 * 60; // 30 days
|
||||
export const MAX_RECIPE_BYTES = 256 * 1024;
|
||||
// Under nginx's `client_max_body_size 4m`, so an over-limit upload is rejected
|
||||
// with our JSON error instead of nginx's HTML 413.
|
||||
export const MAX_PHOTO_BYTES = 3 * 1024 * 1024;
|
||||
// A phone's 12MP JPEG lands around 4-8MB, so 3MB rejected real photos with a
|
||||
// 413. The client downscales to 2048px before uploading (see shrinkForUpload),
|
||||
// which keeps normal uploads well under this; the cap stays generous for a
|
||||
// full-size PNG or a photo that arrived from elsewhere. Under nginx's
|
||||
// `client_max_body_size 16m`, so an over-limit upload is still rejected with
|
||||
// our JSON error instead of nginx's HTML 413.
|
||||
export const MAX_PHOTO_BYTES = 12 * 1024 * 1024;
|
||||
export const MAX_PHOTOS_PER_USER = 12;
|
||||
|
||||
const DATA_DIR = process.env.DATA_DIR || './data';
|
||||
@@ -86,8 +90,28 @@ export const isPhotoSlot = (v: unknown): v is PhotoSlot =>
|
||||
}
|
||||
}
|
||||
|
||||
// Moderation state, added after the first accounts existed:
|
||||
// blocked — may not sign in (or stay signed in); the row is kept whole.
|
||||
// deleted_at — "removed" from the site: hidden from the strip, cannot sign
|
||||
// in, but restorable. A hard DELETE is the separate, final act.
|
||||
{
|
||||
const cols = db.prepare('PRAGMA table_info(users)').all() as { name: string }[];
|
||||
if (!cols.some((c) => c.name === 'blocked')) {
|
||||
db.exec(`ALTER TABLE users ADD COLUMN blocked INTEGER NOT NULL DEFAULT 0`);
|
||||
}
|
||||
if (!cols.some((c) => c.name === 'deleted_at')) {
|
||||
db.exec(`ALTER TABLE users ADD COLUMN deleted_at TEXT`);
|
||||
}
|
||||
}
|
||||
|
||||
// `avatar` is the stored file name, or null for "no picture".
|
||||
export type User = { id: number; email: string; avatar: string | null };
|
||||
export type User = {
|
||||
id: number;
|
||||
email: string;
|
||||
avatar: string | null;
|
||||
blocked: number;
|
||||
deletedAt: string | null;
|
||||
};
|
||||
export type Recipe = {
|
||||
id: number;
|
||||
name: string;
|
||||
@@ -123,7 +147,7 @@ export function createUser(email: string, password: string): User | null {
|
||||
const info = db
|
||||
.prepare('INSERT INTO users (email, password_hash, created_at) VALUES (?, ?, ?)')
|
||||
.run(email, hashPassword(password), now());
|
||||
return { id: Number(info.lastInsertRowid), email, avatar: null };
|
||||
return { id: Number(info.lastInsertRowid), email, avatar: null, blocked: 0, deletedAt: null };
|
||||
} catch (err) {
|
||||
if ((err as { code?: string }).code === 'SQLITE_CONSTRAINT_UNIQUE') return null;
|
||||
throw err;
|
||||
@@ -132,12 +156,16 @@ export function createUser(email: string, password: string): User | null {
|
||||
|
||||
export function findUserByEmail(email: string): (User & { password_hash: string }) | undefined {
|
||||
return db
|
||||
.prepare('SELECT id, email, avatar, password_hash FROM users WHERE email = ?')
|
||||
.prepare(
|
||||
'SELECT id, email, avatar, blocked, deleted_at AS deletedAt, password_hash FROM users WHERE email = ?',
|
||||
)
|
||||
.get(email) as (User & { password_hash: string }) | undefined;
|
||||
}
|
||||
|
||||
export function findUserById(id: number): User | undefined {
|
||||
return db.prepare('SELECT id, email, avatar FROM users WHERE id = ?').get(id) as User | undefined;
|
||||
return db
|
||||
.prepare('SELECT id, email, avatar, blocked, deleted_at AS deletedAt FROM users WHERE id = ?')
|
||||
.get(id) as User | undefined;
|
||||
}
|
||||
|
||||
// Swaps the picture and hands back the file it replaced, so the caller can
|
||||
@@ -176,7 +204,10 @@ export function sessionUser(token: string): User | undefined {
|
||||
db.prepare('DELETE FROM sessions WHERE token = ?').run(row.token); // lazy cleanup
|
||||
return undefined;
|
||||
}
|
||||
return findUserById(row.userId);
|
||||
const user = findUserById(row.userId);
|
||||
// Belt to the braces of the session sweep in setUserBlocked/setUserRemoved.
|
||||
if (!user || user.blocked || user.deletedAt) return undefined;
|
||||
return user;
|
||||
}
|
||||
|
||||
export function deleteSession(token: string): void {
|
||||
@@ -231,7 +262,12 @@ export type AdminPhoto = Photo & { userId: number; email: string; mime: string;
|
||||
|
||||
export function listPhotos(): Photo[] {
|
||||
return db
|
||||
.prepare('SELECT id, created_at AS createdAt, slot FROM photos ORDER BY id DESC')
|
||||
.prepare(
|
||||
`SELECT photos.id AS id, photos.created_at AS createdAt, photos.slot AS slot
|
||||
FROM photos JOIN users ON users.id = photos.user_id
|
||||
WHERE users.deleted_at IS NULL
|
||||
ORDER BY photos.id DESC`,
|
||||
)
|
||||
.all() as Photo[];
|
||||
}
|
||||
|
||||
@@ -247,14 +283,24 @@ export function listPhotosWithOwner(): AdminPhoto[] {
|
||||
.all() as AdminPhoto[];
|
||||
}
|
||||
|
||||
// Admin listing: one row per account with how many photos it owns.
|
||||
export type AdminUser = { id: number; email: string; createdAt: string; photos: number; avatar: string | null };
|
||||
// Admin listing: one row per account with how many photos it owns. Blocked and
|
||||
// removed accounts stay listed — a removed one has to be findable to restore it.
|
||||
export type AdminUser = {
|
||||
id: number;
|
||||
email: string;
|
||||
createdAt: string;
|
||||
photos: number;
|
||||
avatar: string | null;
|
||||
blocked: number;
|
||||
deletedAt: string | null;
|
||||
};
|
||||
|
||||
export function listUsersWithCounts(): AdminUser[] {
|
||||
return db
|
||||
.prepare(
|
||||
`SELECT users.id AS id, users.email AS email, users.created_at AS createdAt,
|
||||
users.avatar AS avatar, COUNT(photos.id) AS photos
|
||||
users.avatar AS avatar, users.blocked AS blocked,
|
||||
users.deleted_at AS deletedAt, COUNT(photos.id) AS photos
|
||||
FROM users LEFT JOIN photos ON photos.user_id = users.id
|
||||
GROUP BY users.id
|
||||
ORDER BY users.id`,
|
||||
@@ -262,6 +308,40 @@ export function listUsersWithCounts(): AdminUser[] {
|
||||
.all() as AdminUser[];
|
||||
}
|
||||
|
||||
// ---- moderation -------------------------------------------------------------
|
||||
// Blocking and removing both drop the account's live sessions: the state has to
|
||||
// take effect on the next request, not whenever the cookie happens to expire.
|
||||
export function setUserBlocked(id: number, blocked: boolean): boolean {
|
||||
const info = db.prepare('UPDATE users SET blocked = ? WHERE id = ?').run(blocked ? 1 : 0, id);
|
||||
if (info.changes > 0 && blocked) db.prepare('DELETE FROM sessions WHERE user_id = ?').run(id);
|
||||
return info.changes > 0;
|
||||
}
|
||||
|
||||
export function setUserRemoved(id: number, removed: boolean): boolean {
|
||||
const info = db
|
||||
.prepare('UPDATE users SET deleted_at = ? WHERE id = ?')
|
||||
.run(removed ? now() : null, id);
|
||||
if (info.changes > 0 && removed) db.prepare('DELETE FROM sessions WHERE user_id = ?').run(id);
|
||||
return info.changes > 0;
|
||||
}
|
||||
|
||||
// The final act: the row and everything hanging off it. Returns the files the
|
||||
// caller has to unlink — the rows are the only index of what is on disk.
|
||||
export function deleteUser(id: number): { photos: string[]; avatar: string | null } | undefined {
|
||||
const row = db.prepare('SELECT avatar FROM users WHERE id = ?').get(id) as
|
||||
| { avatar: string | null }
|
||||
| undefined;
|
||||
if (!row) return undefined;
|
||||
const photos = (db.prepare('SELECT file FROM photos WHERE user_id = ?').all(id) as { file: string }[]).map(
|
||||
(r) => r.file,
|
||||
);
|
||||
if (db.prepare('DELETE FROM users WHERE id = ?').run(id).changes === 0) return undefined;
|
||||
db.prepare('DELETE FROM photos WHERE user_id = ?').run(id);
|
||||
db.prepare('DELETE FROM recipes WHERE user_id = ?').run(id);
|
||||
db.prepare('DELETE FROM sessions WHERE user_id = ?').run(id);
|
||||
return { photos, avatar: row.avatar };
|
||||
}
|
||||
|
||||
// Profile edits. The email column is UNIQUE, so a taken address comes back as
|
||||
// false rather than a thrown constraint; the password uses the same hash the
|
||||
// sign-up path writes.
|
||||
|
||||
Reference in New Issue
Block a user