diff --git a/backend/migrations/008_user_preferences.sql b/backend/migrations/008_user_preferences.sql new file mode 100644 index 0000000..5566e7c --- /dev/null +++ b/backend/migrations/008_user_preferences.sql @@ -0,0 +1,47 @@ +-- 008_user_preferences.sql +-- +-- 1. Preferences d'affichage par utilisateur (theme et langue), choisies dans +-- le compte. Le rendu serveur reste en francais ; ces colonnes ne servent +-- qu'a l'interface cliente. +-- 2. Favoris par utilisateur : `files.is_starred` etait un marqueur global, +-- partage entre tous les comptes ayant acces au fichier. Une table de +-- jointure remplace ce marqueur. Les etoiles deja posees sont reprises par +-- le proprietaire du fichier. +-- 3. Suivi de lecture du chat par utilisateur, pour les pastilles de messages +-- non lus dans la vue des dossiers. +-- 4. Suppression de `folders.description`, champ jamais affiche. + +ALTER TABLE users + ADD COLUMN theme VARCHAR(10) NOT NULL DEFAULT 'system' AFTER max_file_bytes, + ADD COLUMN language VARCHAR(5) NOT NULL DEFAULT 'fr' AFTER theme, + ADD CONSTRAINT chk_users_theme CHECK (theme IN ('system', 'light', 'dark')), + ADD CONSTRAINT chk_users_language CHECK (language IN ('fr', 'en')); + +CREATE TABLE user_file_stars ( + user_id CHAR(36) NOT NULL, + file_id CHAR(36) NOT NULL, + created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + PRIMARY KEY (user_id, file_id), + KEY idx_user_file_stars_file (file_id), + CONSTRAINT fk_user_file_stars_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE, + CONSTRAINT fk_user_file_stars_file FOREIGN KEY (file_id) REFERENCES files (id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +INSERT INTO user_file_stars (user_id, file_id) +SELECT owner_id, id FROM files WHERE is_starred = 1 AND owner_id IS NOT NULL; + +DROP INDEX idx_files_starred ON files; +ALTER TABLE files DROP COLUMN is_starred; + +CREATE TABLE chat_reads ( + folder_id CHAR(36) NOT NULL, + user_id CHAR(36) NOT NULL, + last_read_message_id BIGINT UNSIGNED NOT NULL DEFAULT 0, + updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3), + PRIMARY KEY (folder_id, user_id), + KEY idx_chat_reads_user (user_id), + CONSTRAINT fk_chat_reads_folder FOREIGN KEY (folder_id) REFERENCES folders (id) ON DELETE CASCADE, + CONSTRAINT fk_chat_reads_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +ALTER TABLE folders DROP COLUMN description; diff --git a/backend/src/index.ts b/backend/src/index.ts index a66e6d7..c0e64cf 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -5,6 +5,7 @@ import { registerAccountRoutes } from "./routes/account.js"; import { registerAdminRoutes } from "./routes/admin.js"; import { registerAuthRoutes } from "./routes/auth.js"; import { registerChatRoutes } from "./routes/chat.js"; +import { registerDirectoryRoutes } from "./routes/directory.js"; import { registerFileRoutes } from "./routes/files.js"; import { registerFolderRoutes } from "./routes/folders.js"; import { registerMediaRoutes } from "./routes/media.js"; @@ -23,6 +24,7 @@ void startService({ registerAuthRoutes(app, db, config); registerAccountRoutes(app, db, config); registerChatRoutes(app, db, config); + registerDirectoryRoutes(app, db); registerFolderRoutes(app, db, config); registerFileRoutes(app, db, config); registerMediaRoutes(app, db); diff --git a/backend/src/routes/account.ts b/backend/src/routes/account.ts index 38b4890..83f6a7a 100644 --- a/backend/src/routes/account.ts +++ b/backend/src/routes/account.ts @@ -51,6 +51,11 @@ const profileInput = z.object({ last_name: z.string().trim().min(1).max(50), }); +const preferencesInput = z.object({ + theme: z.enum(["system", "light", "dark"]), + language: z.enum(["fr", "en"]), +}); + const forgotInput = z.object({ email: z.string().trim().email().max(255) }); const resetInput = z.object({ @@ -90,6 +95,27 @@ export const registerAccountRoutes = (app: App, db: Db, config: Config): void => return context.json({ ok: true, first_name: data.first_name, last_name: data.last_name }); }); + app.get("/api/cloud/account/preferences", requireAuth, async (context) => { + const user = currentUser(context); + const row = await db.one<{ theme: string; language: string }>( + "SELECT theme, language FROM users WHERE id = ? AND deleted_at IS NULL", + [user.id], + ); + if (!row) throw AppError.notFound("Compte introuvable."); + return context.json({ theme: row.theme, language: row.language }); + }); + + app.patch("/api/cloud/account/preferences", requireAuth, async (context) => { + const user = currentUser(context); + const data = parse(preferencesInput, await context.req.json().catch(() => null)); + await db.exec("UPDATE users SET theme = ?, language = ? WHERE id = ?", [ + data.theme, + data.language, + user.id, + ]); + return context.json({ theme: data.theme, language: data.language }); + }); + app.get("/api/cloud/account/sessions", requireAuth, async (context) => { const user = currentUser(context); const current = sha256(context.get("token") ?? ""); diff --git a/backend/src/routes/admin.ts b/backend/src/routes/admin.ts index 6524234..4ba56c7 100644 --- a/backend/src/routes/admin.ts +++ b/backend/src/routes/admin.ts @@ -433,21 +433,53 @@ export const registerAdminRoutes = (app: App, db: Db, config: Config): void => { return context.json({ ok: true }); }); + /** + * Facettes du journal : valeurs distinctes utilisees pour alimenter les + * listes deroulantes de filtre (actions et comptes ayant agi). Un compte + * supprime reste identifiable par son e-mail conserve dans le journal. + */ + app.get("/api/cloud/admin/audit/filters", requireAuth, async (context) => { + requireAdmin(context); + const actions = await db.query<{ action: string }>( + "SELECT DISTINCT action FROM audit_logs ORDER BY action ASC", + ); + const users = await db.query<{ user_id: string; user_email: string | null }>( + `SELECT user_id, MAX(user_email) AS user_email + FROM audit_logs + WHERE user_id IS NOT NULL + GROUP BY user_id + ORDER BY user_email ASC`, + ); + return context.json({ + actions: actions.map((row) => row.action), + users: users.map((row) => ({ id: row.user_id, email: row.user_email })), + }); + }); + app.get("/api/cloud/admin/audit", requireAuth, async (context) => { requireAdmin(context); const limit = Math.min(500, Math.max(1, Number(context.req.query("limit")) || 100)); const offset = Math.max(0, Number(context.req.query("offset")) || 0); - const action = context.req.query("action"); - const rows = action - ? await db.query( - `SELECT id, user_id, user_email, action, resource, resource_id, details, ip_address, created_at - FROM audit_logs WHERE action = ? ORDER BY created_at DESC LIMIT ${limit} OFFSET ${offset}`, - [action], - ) - : await db.query( - `SELECT id, user_id, user_email, action, resource, resource_id, details, ip_address, created_at - FROM audit_logs ORDER BY created_at DESC LIMIT ${limit} OFFSET ${offset}`, - ); + const action = context.req.query("action") || null; + const userId = context.req.query("userId") || null; + + const conditions: string[] = []; + const params: unknown[] = []; + if (action) { + conditions.push("action = ?"); + params.push(action); + } + if (userId) { + conditions.push("user_id = ?"); + params.push(userId); + } + const where = conditions.length > 0 ? ` WHERE ${conditions.join(" AND ")}` : ""; + + const rows = await db.query( + `SELECT id, user_id, user_email, action, resource, resource_id, details, ip_address, created_at + FROM audit_logs${where} ORDER BY created_at DESC LIMIT ${limit} OFFSET ${offset}`, + params, + ); return context.json({ entries: rows }); }); }; diff --git a/backend/src/routes/auth.ts b/backend/src/routes/auth.ts index b4dd8a9..1b40e3e 100644 --- a/backend/src/routes/auth.ts +++ b/backend/src/routes/auth.ts @@ -46,6 +46,8 @@ interface UserRow { max_file_bytes: number; must_change_password: number; is_active: number; + theme: string; + language: string; avatar_updated_at: Date | null; } @@ -60,6 +62,8 @@ const publicUser = (row: UserRow) => ({ used_bytes: row.used_bytes, max_file_bytes: row.max_file_bytes, must_change_password: row.must_change_password, + theme: row.theme, + language: row.language, has_avatar: row.avatar_updated_at !== null, avatar_updated_at: row.avatar_updated_at, }); @@ -68,6 +72,7 @@ const SELECT_USER = ` SELECT u.id, u.email, u.first_name, u.last_name, u.password_hash, r.slug AS role_slug, r.name AS role_name, u.quota_bytes, u.used_bytes, u.max_file_bytes, u.must_change_password, u.is_active, + u.theme, u.language, ua.updated_at AS avatar_updated_at FROM users u JOIN roles r ON r.id = u.role_id diff --git a/backend/src/routes/chat.ts b/backend/src/routes/chat.ts index e43dca8..04adf61 100644 --- a/backend/src/routes/chat.ts +++ b/backend/src/routes/chat.ts @@ -8,7 +8,7 @@ import { type Config, type Db, } from "../lib/index.js"; -import { audit, canManageFolder, canRead } from "../access.js"; +import { audit, canManageFolder, canRead, READ_SQL } from "../access.js"; /** * Chat attache a un dossier. Le salon ne se lit et ne s'ecrit qu'avec l'acces @@ -80,6 +80,51 @@ export const registerChatRoutes = (app: App, db: Db, config: Config): void => { return folder; }; + /** Reprise du curseur de lecture du salon pour la session courante. */ + const markRead = async (folderId: string, userId: string, messageId: number): Promise => { + if (messageId <= 0) return; + await db + .exec( + `INSERT INTO chat_reads (folder_id, user_id, last_read_message_id) + VALUES (?, ?, ?) + ON DUPLICATE KEY UPDATE + last_read_message_id = GREATEST(last_read_message_id, ?), + updated_at = UTC_TIMESTAMP(3)`, + [folderId, userId, messageId, messageId], + ) + .catch(() => undefined); + }; + + /** + * Pastilles de messages non lus : tous les salons accessibles a la session + * qui contiennent au moins un message qu'elle n'a pas encore lu. Les + * messages de la session ignores : il n'a rien de neuf a lire. + */ + app.get("/api/cloud/chat/unread", requireAuth, async (context) => { + const user = currentUser(context); + const rows = await db.query<{ folder_id: string; unread: number }>( + `SELECT f.id AS folder_id, COUNT(m.id) AS unread + FROM folders f + JOIN chat_messages m + ON m.folder_id = f.id + AND m.deleted_at IS NULL + AND m.user_id <> ? + WHERE f.deleted_at IS NULL + AND m.id > COALESCE ( + SELECT cr.last_read_message_id + FROM chat_reads cr + WHERE cr.folder_id = f.id AND cr.user_id = ? ) + AND ${READ_SQL} + GROUP BY f.id + HAVING unread > 0`, + [user.id, user.id, user.id, user.id], + ); + + const unread: Record = {}; + for (const row of rows) unread[row.folder_id] = Number(row.unread); + return context.json({ unread }); + }); + app.get("/api/cloud/folders/:folderId/chat/members", requireAuth, async (context) => { const user = currentUser(context); const folderId = context.req.param("folderId") ?? ""; @@ -158,6 +203,9 @@ export const registerChatRoutes = (app: App, db: Db, config: Config): void => { ) ).reverse(); + const newest = rows.reduce((max, row) => Math.max(max, row.id), 0); + await markRead(folderId, user.id, newest); + return context.json({ messages: rows.map(publicMessage) }); }); diff --git a/backend/src/routes/directory.ts b/backend/src/routes/directory.ts new file mode 100644 index 0000000..248fef2 --- /dev/null +++ b/backend/src/routes/directory.ts @@ -0,0 +1,47 @@ +import { + attachUser, + currentUser, + requireAuth, + type App, + type Db, +} from "../lib/index.js"; + +/** + * Repertoire des comptes actifs, reduit au strict necessaire pour choisir un + * destinataire lors d'un partage. La session courante est exclue : on ne se + * partage pas un dossier a soi-meme. Aucune donnee sensible n'est exposee. + */ +export const registerDirectoryRoutes = (app: App, db: Db): void => { + app.use("/api/cloud/directory", attachUser(db)); + + app.get("/api/cloud/directory", requireAuth, async (context) => { + const user = currentUser(context); + const rows = await db.query<{ + id: string; + first_name: string; + last_name: string; + email: string; + avatar_updated_at: Date | null; + }>( + `SELECT u.id, u.first_name, u.last_name, u.email, + ua.updated_at AS avatar_updated_at + FROM users u + LEFT JOIN user_avatars ua ON ua.user_id = u.id + WHERE u.deleted_at IS NULL AND u.is_active = 1 AND u.id <> ? + ORDER BY u.first_name ASC, u.last_name ASC + LIMIT 1000`, + [user.id], + ); + + return context.json({ + users: rows.map((row) => ({ + id: row.id, + first_name: row.first_name, + last_name: row.last_name, + email: row.email, + has_avatar: row.avatar_updated_at !== null, + avatar_updated_at: row.avatar_updated_at, + })), + }); + }); +}; diff --git a/backend/src/routes/files.ts b/backend/src/routes/files.ts index c031a7a..f12cf47 100644 --- a/backend/src/routes/files.ts +++ b/backend/src/routes/files.ts @@ -71,9 +71,11 @@ interface FileRow { updated_at: string; } -const SELECT_FILE = `SELECT id, folder_id, name, mime_type, size_bytes, checksum_sha256, - relative_path, owner_id, is_starred, download_count, created_at, updated_at - FROM files`; +const SELECT_FILE = `SELECT f.id, f.folder_id, f.name, f.mime_type, f.size_bytes, f.checksum_sha256, + f.relative_path, f.owner_id, + EXISTS (SELECT 1 FROM user_file_stars s WHERE s.file_id = f.id AND s.user_id = ?) AS is_starred, + f.download_count, f.created_at, f.updated_at + FROM files f`; const publicFile = (row: FileRow) => ({ id: row.id, @@ -371,7 +373,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { config.TRUST_PROXY, ); - const created = await db.required(`${SELECT_FILE} WHERE id = ?`, [id]); + const created = await db.required(`${SELECT_FILE} WHERE f.id = ?`, [user.id, id]); return context.json({ file: publicFile(created) }, 201); }, ); @@ -398,22 +400,22 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { const rows = await db.query( `${SELECT_FILE} - WHERE deleted_at IS NULL - AND (folder_id <=> ?) + WHERE f.deleted_at IS NULL + AND (f.folder_id <=> ?) AND ( ? = 1 - OR owner_id = ? - OR ( folder_id IS NOT NULL - AND EXISTS ( SELECT 1 FROM folders f - WHERE f.id = files.folder_id AND f.access_type = 'public' ) ) - OR ( folder_id IS NOT NULL + OR f.owner_id = ? + OR ( f.folder_id IS NOT NULL + AND EXISTS ( SELECT 1 FROM folders f2 + WHERE f2.id = f.folder_id AND f2.access_type = 'public' ) ) + OR ( f.folder_id IS NOT NULL AND EXISTS ( SELECT 1 FROM folder_permissions fp - WHERE fp.folder_id = files.folder_id + WHERE fp.folder_id = f.folder_id AND fp.user_id = ? AND fp.level IN ('read', 'write', 'admin') AND ( fp.expires_at IS NULL OR fp.expires_at > UTC_TIMESTAMP() ) ) ) ) - ORDER BY is_starred DESC, name`, - [folderId, 0, user.id, user.id], + ORDER BY is_starred DESC, f.name`, + [user.id, folderId, 0, user.id, user.id], ); return context.json({ folderId, files: rows.map(publicFile) }); @@ -429,12 +431,12 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { const user = currentUser(context); const rows = await db.query( `SELECT fi.id, fi.folder_id, fi.name, fi.mime_type, fi.size_bytes, fi.checksum_sha256, - fi.relative_path, fi.owner_id, fi.is_starred, fi.download_count, + fi.relative_path, fi.owner_id, 1 AS is_starred, fi.download_count, fi.created_at, fi.updated_at, fo.name AS folder_name FROM files fi + JOIN user_file_stars ufs ON ufs.file_id = fi.id AND ufs.user_id = ? LEFT JOIN folders fo ON fo.id = fi.folder_id WHERE fi.deleted_at IS NULL - AND fi.is_starred = 1 AND ( fi.owner_id = ? OR ( fi.folder_id IS NOT NULL AND EXISTS ( SELECT 1 FROM folders f @@ -446,8 +448,8 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { AND fp.level IN ('read', 'write', 'admin') AND ( fp.expires_at IS NULL OR fp.expires_at > UTC_TIMESTAMP() ) ) ) ) - ORDER BY fi.created_at DESC`, - [user.id, user.id], + ORDER BY ufs.created_at DESC`, + [user.id, user.id, user.id], ); return context.json({ @@ -460,7 +462,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { const rawId = context.req.param("id"); if (!uuid.safeParse(rawId).success) throw AppError.notFound("Fichier introuvable."); - const row = await db.one(`${SELECT_FILE} WHERE id = ? AND deleted_at IS NULL`, [rawId]); + const row = await db.one(`${SELECT_FILE} WHERE f.id = ? AND f.deleted_at IS NULL`, [user.id, rawId]); if (!row) throw AppError.notFound("Fichier introuvable."); const allowed = @@ -499,7 +501,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { const rawId = context.req.param("id"); if (!uuid.safeParse(rawId).success) throw AppError.notFound("Fichier introuvable."); - const row = await db.one(`${SELECT_FILE} WHERE id = ? AND deleted_at IS NULL`, [rawId]); + const row = await db.one(`${SELECT_FILE} WHERE f.id = ? AND f.deleted_at IS NULL`, [user.id, rawId]); if (!row) throw AppError.notFound("Fichier introuvable."); const allowed = @@ -528,7 +530,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { const rawId = context.req.param("id"); if (!uuid.safeParse(rawId).success) throw AppError.notFound("Image introuvable."); - const row = await db.one(`${SELECT_FILE} WHERE id = ? AND deleted_at IS NULL`, [rawId]); + const row = await db.one(`${SELECT_FILE} WHERE f.id = ? AND f.deleted_at IS NULL`, [user.id, rawId]); if (!row) throw AppError.notFound("Image introuvable."); const allowed = @@ -563,7 +565,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { const rawId = context.req.param("id"); if (!uuid.safeParse(rawId).success) throw AppError.notFound("Fichier introuvable."); - const row = await db.one(`${SELECT_FILE} WHERE id = ? AND deleted_at IS NULL`, [rawId]); + const row = await db.one(`${SELECT_FILE} WHERE f.id = ? AND f.deleted_at IS NULL`, [user.id, rawId]); if (!row) throw AppError.notFound("Fichier introuvable."); const owner = row.owner_id !== null && row.owner_id === user.id; @@ -587,7 +589,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { throw error; } - const updated = await db.required(`${SELECT_FILE} WHERE id = ?`, [row.id]); + const updated = await db.required(`${SELECT_FILE} WHERE f.id = ?`, [user.id, row.id]); await audit(db, context, user, "file.rename", "file", row.id, { name }, config.TRUST_PROXY); return context.json({ file: publicFile(updated) }); }); @@ -598,7 +600,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { const rawId = context.req.param("id"); if (!uuid.safeParse(rawId).success) throw AppError.notFound("Fichier introuvable."); - const row = await db.one(`${SELECT_FILE} WHERE id = ? AND deleted_at IS NULL`, [rawId]); + const row = await db.one(`${SELECT_FILE} WHERE f.id = ? AND f.deleted_at IS NULL`, [user.id, rawId]); if (!row) throw AppError.notFound("Fichier introuvable."); const owner = row.owner_id !== null && row.owner_id === user.id; @@ -623,7 +625,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { throw error; } - const updated = await db.required(`${SELECT_FILE} WHERE id = ?`, [row.id]); + const updated = await db.required(`${SELECT_FILE} WHERE f.id = ?`, [user.id, row.id]); await audit(db, context, user, "file.move", "file", row.id, { folderId: destination }, config.TRUST_PROXY); return context.json({ file: publicFile(updated) }); }); @@ -634,7 +636,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { const rawId = context.req.param("id"); if (!uuid.safeParse(rawId).success) throw AppError.notFound("Fichier introuvable."); - const row = await db.one(`${SELECT_FILE} WHERE id = ? AND deleted_at IS NULL`, [rawId]); + const row = await db.one(`${SELECT_FILE} WHERE f.id = ? AND f.deleted_at IS NULL`, [user.id, rawId]); if (!row) throw AppError.notFound("Fichier introuvable."); const allowed = @@ -642,8 +644,22 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { (row.folder_id !== null && (await canRead(db, user, row.folder_id))); if (!allowed) throw AppError.notFound("Fichier introuvable."); - await db.exec("UPDATE files SET is_starred = 1 - is_starred WHERE id = ?", [row.id]); - const updated = await db.required(`${SELECT_FILE} WHERE id = ?`, [row.id]); + const starred = await db.one( + "SELECT 1 AS mark FROM user_file_stars WHERE user_id = ? AND file_id = ?", + [user.id, row.id], + ); + if (starred) { + await db.exec( + "DELETE FROM user_file_stars WHERE user_id = ? AND file_id = ?", + [user.id, row.id], + ); + } else { + await db.exec( + "INSERT INTO user_file_stars (user_id, file_id) VALUES (?, ?)", + [user.id, row.id], + ); + } + const updated = await db.required(`${SELECT_FILE} WHERE f.id = ?`, [user.id, row.id]); return context.json({ file: publicFile(updated) }); }); @@ -652,7 +668,7 @@ export const registerFileRoutes = (app: App, db: Db, config: Config): void => { const rawId = context.req.param("id"); if (!uuid.safeParse(rawId).success) throw AppError.notFound("Fichier introuvable."); - const row = await db.one(`${SELECT_FILE} WHERE id = ? AND deleted_at IS NULL`, [rawId]); + const row = await db.one(`${SELECT_FILE} WHERE f.id = ? AND f.deleted_at IS NULL`, [user.id, rawId]); if (!row) throw AppError.notFound("Fichier introuvable."); const owner = row.owner_id !== null && row.owner_id === user.id; diff --git a/backend/src/routes/folders.ts b/backend/src/routes/folders.ts index f2885de..c3be016 100644 --- a/backend/src/routes/folders.ts +++ b/backend/src/routes/folders.ts @@ -22,12 +22,10 @@ const uuid = z const createFolder = z.object({ name: z.string().trim().min(1).max(255), parentId: uuid.nullish(), - description: z.string().trim().max(2000).nullish(), }); const updateFolder = z.object({ - name: z.string().trim().min(1).max(255).optional(), - description: z.string().trim().max(2000).nullish(), + name: z.string().trim().min(1).max(255), }); const moveFolder = z.object({ @@ -44,7 +42,6 @@ interface FolderRow { id: string; parent_id: string | null; name: string; - description: string | null; owner_id: string | null; access_type: string; is_system: number; @@ -53,7 +50,7 @@ interface FolderRow { updated_at: string; } -const SELECT_FOLDER = `SELECT f.id, f.parent_id, f.name, f.description, f.owner_id, +const SELECT_FOLDER = `SELECT f.id, f.parent_id, f.name, f.owner_id, f.access_type, f.is_system, f.storage_location_id, f.created_at, f.updated_at FROM folders f`; @@ -61,7 +58,6 @@ const publicFolder = (row: FolderRow) => ({ id: row.id, parent_id: row.parent_id, name: row.name, - description: row.description, owner_id: row.owner_id, access_type: row.access_type, is_system: row.is_system === 1, @@ -148,7 +144,7 @@ export const registerFolderRoutes = (app: App, db: Db, config: Config): void => requireRole("admin", "user"), async (context) => { const user = currentUser(context); - const { name: rawName, parentId, description } = parse( + const { name: rawName, parentId } = parse( createFolder, await context.req.json().catch(() => null), ); @@ -190,9 +186,9 @@ export const registerFolderRoutes = (app: App, db: Db, config: Config): void => const id = randomUUID(); try { await db.exec( - `INSERT INTO folders (id, storage_location_id, parent_id, name, description, owner_id, created_by) - VALUES (?, ?, ?, ?, ?, ?, ?)`, - [id, locationId, parentId ?? null, name, description ?? null, user.id, user.id], + `INSERT INTO folders (id, storage_location_id, parent_id, name, owner_id, created_by) + VALUES (?, ?, ?, ?, ?, ?)`, + [id, locationId, parentId ?? null, name, user.id, user.id], ); } catch (error) { if ((error as { errno?: number }).errno === 1062) { @@ -219,16 +215,10 @@ export const registerFolderRoutes = (app: App, db: Db, config: Config): void => if (!(await canManageFolder(db, user, id))) throw AppError.notFound("Dossier introuvable."); const data = parse(updateFolder, await context.req.json().catch(() => null)); - if (data.name === undefined && data.description === undefined) { - throw AppError.badRequest("Rien a modifier."); - } - const name = data.name === undefined ? existing.name : assertName(data.name); + const name = assertName(data.name); try { - await db.exec( - "UPDATE folders SET name = ?, description = ? WHERE id = ?", - [name, data.description === undefined ? existing.description : data.description, id], - ); + await db.exec("UPDATE folders SET name = ? WHERE id = ?", [name, id]); } catch (error) { if ((error as { errno?: number }).errno === 1062) { throw AppError.conflict("Ce nom existe deja ici."); diff --git a/backend/src/routes/media.ts b/backend/src/routes/media.ts index c5a8ddc..271bf1c 100644 --- a/backend/src/routes/media.ts +++ b/backend/src/routes/media.ts @@ -42,10 +42,11 @@ export const registerMediaRoutes = (app: App, db: Db): void => { has_thumbnail: number; }>( `SELECT fi.id, fi.folder_id, fi.name, fi.mime_type, fi.size_bytes, fi.owner_id, - fi.is_starred, mm.kind, mm.width, mm.height, fi.created_at, + (ufs.file_id IS NOT NULL) AS is_starred, mm.kind, mm.width, mm.height, fi.created_at, EXISTS ( SELECT 1 FROM file_variants fv WHERE fv.file_id = fi.id AND fv.purpose = 'thumbnail' ) AS has_thumbnail FROM files fi + LEFT JOIN user_file_stars ufs ON ufs.file_id = fi.id AND ufs.user_id = ? LEFT JOIN media_metadata mm ON mm.file_id = fi.id WHERE fi.deleted_at IS NULL AND ( ? = '' OR mm.kind = ? ) @@ -61,7 +62,7 @@ export const registerMediaRoutes = (app: App, db: Db): void => { AND ( fp.expires_at IS NULL OR fp.expires_at > UTC_TIMESTAMP() ) ) ) ) ORDER BY fi.created_at DESC LIMIT ${limit} OFFSET ${offset}`, - [kind, kind, 0, user.id, user.id], + [user.id, kind, kind, 0, user.id, user.id], ); return context.json({ diff --git a/backend/src/routes/search.ts b/backend/src/routes/search.ts index 6227151..aa95be9 100644 --- a/backend/src/routes/search.ts +++ b/backend/src/routes/search.ts @@ -32,13 +32,12 @@ export const registerSearchRoutes = (app: App, db: Db): void => { id: string; parent_id: string | null; name: string; - description: string | null; owner_id: string | null; access_type: string; created_at: string; updated_at: string; }>( - `SELECT f.id, f.parent_id, f.name, f.description, f.owner_id, + `SELECT f.id, f.parent_id, f.name, f.owner_id, f.access_type, f.created_at, f.updated_at FROM folders f WHERE f.deleted_at IS NULL @@ -66,8 +65,9 @@ export const registerSearchRoutes = (app: App, db: Db): void => { updated_at: string; }>( `SELECT fi.id, fi.folder_id, fi.name, fi.mime_type, fi.size_bytes, fi.owner_id, - fi.is_starred, fi.created_at, fi.updated_at + (ufs.file_id IS NOT NULL) AS is_starred, fi.created_at, fi.updated_at FROM files fi + LEFT JOIN user_file_stars ufs ON ufs.file_id = fi.id AND ufs.user_id = ? WHERE fi.deleted_at IS NULL AND fi.name LIKE ? AND ( ? = 1 @@ -81,7 +81,7 @@ export const registerSearchRoutes = (app: App, db: Db): void => { AND ( fp.expires_at IS NULL OR fp.expires_at > UTC_TIMESTAMP() ) ) ) ) ORDER BY fi.name LIMIT ${LIMIT}`, - [pattern, 0, user.id, user.id], + [user.id, pattern, 0, user.id, user.id], ); return context.json({ @@ -90,7 +90,6 @@ export const registerSearchRoutes = (app: App, db: Db): void => { id: row.id, parent_id: row.parent_id, name: row.name, - description: row.description, owner_id: row.owner_id, access_type: row.access_type, created_at: row.created_at, diff --git a/backend/src/routes/trash.ts b/backend/src/routes/trash.ts index ec793c7..08ca04b 100644 --- a/backend/src/routes/trash.ts +++ b/backend/src/routes/trash.ts @@ -119,7 +119,6 @@ interface TrashFolderRow { id: string; parent_id: string | null; name: string; - description: string | null; owner_id: string | null; access_type: string; created_at: string; @@ -143,7 +142,6 @@ const publicFolder = (row: TrashFolderRow) => ({ id: row.id, parent_id: row.parent_id, name: row.name, - description: row.description, owner_id: row.owner_id, access_type: row.access_type, created_at: row.created_at, @@ -161,7 +159,7 @@ export const registerTrashRoutes = (app: App, db: Db, config: Config): void => { // Seuls les elements dont le parent n'est pas lui-meme supprime figurent en // tete de corbeille : le reste suit la suppression du parent. const folders = await db.query( - `SELECT f.id, f.parent_id, f.name, f.description, f.owner_id, f.access_type, + `SELECT f.id, f.parent_id, f.name, f.owner_id, f.access_type, f.created_at, f.updated_at, f.deleted_at FROM folders f LEFT JOIN folders p ON p.id = f.parent_id