Publication IA: - 4 templates (magazine, brief, essay, simple) avec CSS riche - Rewrite IA (article/exercises/tutorial/reference/mixed) - Modération avec timeout 12s + fallback safe - Quotas publish_enhance par tier (basic=2, pro=15, business=100) - Détection contenu stale (hash) - Migration DB publishedContent/publishedTemplate/publishedSourceHash Fixes: - cheerio v1.2: Element -> AnyNode (domhandler), decodeEntities cast - _isShared ajouté au type Note (champ virtuel serveur) - callout colors PDF export: extraction fonction pure testable - admin/published: guard note.userId null - Cmd+S fonctionne en mode dialog (pas seulement fullPage) i18n: - 23 clés publish* traduites dans les 15 locales - Extension Web Clipper: 13 locales mise à jour Tests: - callout-colors.test.ts (6 tests) - note-visible-in-view.test.ts (5 tests) - entitlements.test.ts + byok-entitlements.test.ts: mock usageLog + unstubAllEnvs - 199/199 tests passent Tracker: user-stories.md sync avec sprint-status.yaml
85 lines
2.5 KiB
TypeScript
85 lines
2.5 KiB
TypeScript
'use server'
|
|
|
|
import { auth } from '@/auth'
|
|
import prisma from '@/lib/prisma'
|
|
import { revalidatePath } from 'next/cache'
|
|
|
|
function generateSlug(title: string): string {
|
|
const base = title
|
|
.toLowerCase()
|
|
.normalize('NFD')
|
|
.replace(/[\u0300-\u036f]/g, '')
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-+|-+$/g, '')
|
|
.slice(0, 60) || 'note'
|
|
|
|
return `${base}-${Math.random().toString(36).slice(2, 8)}`
|
|
}
|
|
|
|
export async function publishNote(noteId: string): Promise<{ success: boolean; slug?: string; url?: string; error?: string }> {
|
|
const session = await auth()
|
|
if (!session?.user?.id) return { success: false, error: 'Unauthorized' }
|
|
|
|
const note = await prisma.note.findFirst({
|
|
where: { id: noteId, userId: session.user.id },
|
|
select: { id: true, title: true, isPublic: true, publicSlug: true },
|
|
})
|
|
if (!note) return { success: false, error: 'Note not found' }
|
|
|
|
let slug = note.publicSlug
|
|
if (!slug) {
|
|
slug = generateSlug(note.title || 'note')
|
|
// Ensure uniqueness
|
|
const existing = await prisma.note.findUnique({ where: { publicSlug: slug } })
|
|
if (existing && existing.id !== noteId) {
|
|
slug = `${slug}-${Date.now().toString(36)}`
|
|
}
|
|
}
|
|
|
|
await prisma.note.update({
|
|
where: { id: noteId },
|
|
data: { isPublic: true, publicSlug: slug, publishedAt: new Date() },
|
|
})
|
|
|
|
revalidatePath(`/p/${slug}`)
|
|
return { success: true, slug, url: `/p/${slug}` }
|
|
}
|
|
|
|
export async function unpublishNote(noteId: string): Promise<{ success: boolean; error?: string }> {
|
|
const session = await auth()
|
|
if (!session?.user?.id) return { success: false, error: 'Unauthorized' }
|
|
|
|
const note = await prisma.note.findFirst({
|
|
where: { id: noteId, userId: session.user.id },
|
|
select: { id: true, publicSlug: true },
|
|
})
|
|
if (!note) return { success: false, error: 'Note not found' }
|
|
|
|
await prisma.note.update({
|
|
where: { id: noteId },
|
|
data: { isPublic: false, publicSlug: null, publishedAt: null },
|
|
})
|
|
|
|
if (note.publicSlug) revalidatePath(`/p/${note.publicSlug}`)
|
|
return { success: true }
|
|
}
|
|
|
|
export async function getPublishedNote(slug: string) {
|
|
const note = await prisma.note.findFirst({
|
|
where: { publicSlug: slug, isPublic: true, trashedAt: null },
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
content: true,
|
|
publishedContent: true,
|
|
publishedTemplate: true,
|
|
publishedSourceHash: true,
|
|
publishedAt: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
user: { select: { name: true, image: true } },
|
|
},
|
|
})
|
|
return note
|
|
}
|