|
| 1 | +import type { HttpContext } from '@adonisjs/core/http' |
| 2 | +import { DateTime } from 'luxon' |
| 3 | +import { errors as lucidErrors } from '@adonisjs/lucid' |
| 4 | +import Article from '#models/article' |
| 5 | +import Like from '#models/like' |
| 6 | +import type User from '#models/user' |
| 7 | + |
| 8 | +/** |
| 9 | + * Load an article the user may manage (owner or moderator). Drafts of |
| 10 | + * other people stay invisible (404); published articles get a flash error. |
| 11 | + */ |
| 12 | +async function managedArticle(slug: string, user: User) { |
| 13 | + const article = await Article.findByOrFail('slug', slug) |
| 14 | + if (user.canManage(article.userId)) return { article, allowed: true as const } |
| 15 | + if (!article.isPublished) throw new lucidErrors.E_ROW_NOT_FOUND() |
| 16 | + return { article, allowed: false as const } |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * One-click actions on an article: like, publish / unpublish, feature. |
| 21 | + */ |
| 22 | +export default class ArticleActionsController { |
| 23 | + /** |
| 24 | + * POST /articles/:slug/like — toggle the viewer's like. |
| 25 | + */ |
| 26 | + async like({ params, auth, response, session }: HttpContext) { |
| 27 | + const user = auth.user! |
| 28 | + const article = await Article.query() |
| 29 | + .where('slug', params.slug) |
| 30 | + .withScopes((scopes) => scopes.published()) |
| 31 | + .firstOrFail() |
| 32 | + |
| 33 | + if (article.userId === user.id) { |
| 34 | + session.flash('error', 'Vous ne pouvez pas aimer votre propre article.') |
| 35 | + return response.redirect().back() |
| 36 | + } |
| 37 | + |
| 38 | + const existing = await Like.query() |
| 39 | + .where('user_id', user.id) |
| 40 | + .where('article_id', article.id) |
| 41 | + .first() |
| 42 | + if (existing) { |
| 43 | + await existing.delete() |
| 44 | + } else { |
| 45 | + try { |
| 46 | + await Like.create({ userId: user.id, articleId: article.id }) |
| 47 | + } catch (error) { |
| 48 | + // Double click: the unique (user_id, article_id) index already holds it. |
| 49 | + if ((error as { code?: string }).code !== '23505') throw error |
| 50 | + } |
| 51 | + } |
| 52 | + return response.redirect().back() |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * POST /articles/:slug/publish — publish a draft right away. |
| 57 | + */ |
| 58 | + async publish({ params, auth, response, session }: HttpContext) { |
| 59 | + const { article, allowed } = await managedArticle(params.slug, auth.user!) |
| 60 | + if (!allowed) { |
| 61 | + session.flash('error', 'Seul l’auteur de cet article peut le publier.') |
| 62 | + return response.redirect().back() |
| 63 | + } |
| 64 | + |
| 65 | + if (!article.isPublished) { |
| 66 | + article.publishedAt = DateTime.now() |
| 67 | + await article.save() |
| 68 | + session.flash('success', 'Article publié. Merci pour ce partage !') |
| 69 | + } |
| 70 | + return response.redirect().toPath(`/articles/${article.slug}`) |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * POST /articles/:slug/unpublish — back to draft (and off the front page). |
| 75 | + */ |
| 76 | + async unpublish({ params, auth, response, session }: HttpContext) { |
| 77 | + const { article, allowed } = await managedArticle(params.slug, auth.user!) |
| 78 | + if (!allowed) { |
| 79 | + session.flash('error', 'Seul l’auteur de cet article peut le dépublier.') |
| 80 | + return response.redirect().back() |
| 81 | + } |
| 82 | + |
| 83 | + if (article.publishedAt) { |
| 84 | + article.publishedAt = null |
| 85 | + article.featuredAt = null |
| 86 | + await article.save() |
| 87 | + } |
| 88 | + session.flash('success', 'Article repassé en brouillon : il n’est plus visible publiquement.') |
| 89 | + return response.redirect().back() |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * POST /articles/:slug/feature — moderators toggle "à la une". |
| 94 | + */ |
| 95 | + async feature({ params, auth, response, session }: HttpContext) { |
| 96 | + const user = auth.user! |
| 97 | + if (!user.isModerator) { |
| 98 | + session.flash('error', 'Action réservée à l’équipe de modération.') |
| 99 | + return response.redirect().back() |
| 100 | + } |
| 101 | + |
| 102 | + const article = await Article.findByOrFail('slug', params.slug) |
| 103 | + if (!article.isPublished) { |
| 104 | + session.flash('error', 'Publiez l’article avant de le mettre à la une.') |
| 105 | + return response.redirect().back() |
| 106 | + } |
| 107 | + |
| 108 | + // Query update: featuring is editorial, not a content change (keeps updated_at). |
| 109 | + const featuring = article.featuredAt === null |
| 110 | + await Article.query() |
| 111 | + .where('id', article.id) |
| 112 | + .update({ featured_at: featuring ? DateTime.now().toSQL() : null }) |
| 113 | + session.flash('success', featuring ? 'Article mis à la une.' : 'Article retiré de la une.') |
| 114 | + return response.redirect().back() |
| 115 | + } |
| 116 | +} |
0 commit comments