Messages : types, réponses, sauts de ligne, data volume
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
- HomeMessages : type pill (Réaction/Question/Suggestion/Retour) + sélecteur dans le formulaire (sans Réaction) - HomeMessages : white-space: pre-line sur les messages - Page /messages : type pill + white-space: pre-line (idem home) - Admin : badge type coloré + sélecteur d'édition + formulaire réponse - API : type et reply dans PUT ; readDataYaml/writeDataYaml (data/ volume Docker) - main.css : overrides light mode text-white/55, /75, /90 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
const EMPTY = { messages: [] as any[] }
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = Number(getRouterParam(event, 'id'))
|
||||
|
||||
@@ -5,7 +7,7 @@ export default defineEventHandler(async (event) => {
|
||||
throw createError({ statusCode: 400, statusMessage: 'ID invalide' })
|
||||
}
|
||||
|
||||
const data = await readYaml<{ messages: any[] }>('messages.yml')
|
||||
const data = await readDataYaml<{ messages: any[] }>('messages.yml', EMPTY)
|
||||
|
||||
const index = data.messages.findIndex(m => m.id === id)
|
||||
if (index === -1) {
|
||||
@@ -13,8 +15,7 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
data.messages.splice(index, 1)
|
||||
await writeYaml('messages.yml', data)
|
||||
gitSyncContent(`Suppression message #${id}`, ['site/messages.yml'])
|
||||
await writeDataYaml('messages.yml', data)
|
||||
|
||||
return { ok: true }
|
||||
})
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
const EMPTY = { messages: [] as any[] }
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = Number(getRouterParam(event, 'id'))
|
||||
|
||||
@@ -5,8 +7,15 @@ export default defineEventHandler(async (event) => {
|
||||
throw createError({ statusCode: 400, statusMessage: 'ID invalide' })
|
||||
}
|
||||
|
||||
const body = await readBody<{ text?: string; published?: boolean; author?: string }>(event)
|
||||
const data = await readYaml<{ messages: any[] }>('messages.yml')
|
||||
const body = await readBody<{
|
||||
text?: string
|
||||
published?: boolean
|
||||
author?: string
|
||||
type?: string
|
||||
reply?: string | null
|
||||
}>(event)
|
||||
|
||||
const data = await readDataYaml<{ messages: any[] }>('messages.yml', EMPTY)
|
||||
|
||||
const message = data.messages.find(m => m.id === id)
|
||||
if (!message) {
|
||||
@@ -16,9 +25,14 @@ export default defineEventHandler(async (event) => {
|
||||
if (body.text !== undefined) message.text = body.text
|
||||
if (body.published !== undefined) message.published = body.published
|
||||
if (body.author !== undefined) message.author = body.author
|
||||
if (body.type !== undefined) message.type = body.type
|
||||
if ('reply' in body) {
|
||||
message.reply = body.reply
|
||||
? { text: body.reply, publishedAt: new Date().toISOString() }
|
||||
: null
|
||||
}
|
||||
|
||||
await writeYaml('messages.yml', data)
|
||||
gitSyncContent(`Mise à jour message #${id}`, ['site/messages.yml'])
|
||||
await writeDataYaml('messages.yml', data)
|
||||
|
||||
return { ok: true }
|
||||
})
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
const EMPTY = { messages: [] as any[] }
|
||||
|
||||
export default defineEventHandler(async () => {
|
||||
const data = await readYaml<{ messages: any[] }>('messages.yml')
|
||||
const data = await readDataYaml<{ messages: any[] }>('messages.yml', EMPTY)
|
||||
return data.messages.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
|
||||
})
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
const EMPTY = { messages: [] as any[] }
|
||||
|
||||
export default defineEventHandler(async () => {
|
||||
const data = await readYaml<{ messages: any[] }>('messages.yml')
|
||||
const published = data.messages
|
||||
const data = await readDataYaml<{ messages: any[] }>('messages.yml', EMPTY)
|
||||
return data.messages
|
||||
.filter(m => m.published)
|
||||
.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
|
||||
return published
|
||||
})
|
||||
|
||||
@@ -1,24 +1,28 @@
|
||||
const EMPTY = { messages: [] as any[] }
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<{ author: string; email?: string; text: string }>(event)
|
||||
const body = await readBody<{ author: string; email?: string; text: string; type?: string }>(event)
|
||||
|
||||
if (!body.text?.trim()) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Message requis' })
|
||||
}
|
||||
|
||||
const data = await readYaml<{ messages: any[] }>('messages.yml')
|
||||
const data = await readDataYaml<{ messages: any[] }>('messages.yml', EMPTY)
|
||||
|
||||
const maxId = data.messages.reduce((max, m) => Math.max(max, m.id || 0), 0)
|
||||
|
||||
data.messages.push({
|
||||
id: maxId + 1,
|
||||
author: body.author.trim(),
|
||||
author: body.author?.trim() || 'Anonyme',
|
||||
email: body.email?.trim() || '',
|
||||
text: body.text.trim(),
|
||||
type: body.type || 'reaction',
|
||||
published: false,
|
||||
createdAt: new Date().toISOString(),
|
||||
reply: null,
|
||||
})
|
||||
|
||||
await writeYaml('messages.yml', data)
|
||||
await writeDataYaml('messages.yml', data)
|
||||
|
||||
return { ok: true }
|
||||
})
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { readFile, writeFile } from 'node:fs/promises'
|
||||
import { readFile, writeFile, mkdir } from 'node:fs/promises'
|
||||
import { existsSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import yaml from 'yaml'
|
||||
|
||||
const dataDir = join(process.cwd(), 'site')
|
||||
const runtimeDataDir = join(process.cwd(), 'data')
|
||||
|
||||
const cache = new Map<string, { data: unknown; mtime: number }>()
|
||||
|
||||
@@ -35,3 +37,19 @@ export function invalidateCache(relativePath?: string): void {
|
||||
cache.clear()
|
||||
}
|
||||
}
|
||||
|
||||
// Runtime data — stored in data/ (Docker volume, never overwritten by deploys)
|
||||
|
||||
export async function readDataYaml<T = unknown>(relativePath: string, defaultValue: T): Promise<T> {
|
||||
const filePath = join(runtimeDataDir, relativePath)
|
||||
if (!existsSync(filePath)) return defaultValue
|
||||
const raw = await readFile(filePath, 'utf-8')
|
||||
return yaml.parse(raw) as T
|
||||
}
|
||||
|
||||
export async function writeDataYaml(relativePath: string, data: unknown): Promise<void> {
|
||||
await mkdir(runtimeDataDir, { recursive: true })
|
||||
const filePath = join(runtimeDataDir, relativePath)
|
||||
const raw = yaml.stringify(data, { lineWidth: 120 })
|
||||
await writeFile(filePath, raw, 'utf-8')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user