Kommentare mit Antworten unter Neuigkeiten
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m48s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 8m0s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 8m6s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m10s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m19s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m21s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m48s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 8m0s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 8m6s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m10s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m19s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m21s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f3f2d23961
commit
9320d602a8
117 changed files with 1015 additions and 423 deletions
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* Kommentare zu Beiträgen mit Antworten (eine Ebene: Antworten hängen am Kommentar).
|
||||
*/
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('news_comments', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('post_id')->constrained('news_posts')->cascadeOnDelete();
|
||||
$table->foreignId('parent_id')->nullable()->constrained('news_comments')->cascadeOnDelete();
|
||||
$table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
|
||||
$table->foreignId('reply_to_user_id')->nullable()->constrained('users')->nullOnDelete();
|
||||
$table->text('body');
|
||||
$table->timestamp('edited_at')->nullable();
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
$table->index(['post_id', 'parent_id', 'created_at']);
|
||||
});
|
||||
|
||||
if (! Schema::hasColumn('news_posts', 'comments_enabled')) {
|
||||
Schema::table('news_posts', fn (Blueprint $table) => $table->boolean('comments_enabled')->default(true)->after('form_id'));
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('news_comments');
|
||||
|
||||
if (Schema::hasColumn('news_posts', 'comments_enabled')) {
|
||||
Schema::table('news_posts', fn (Blueprint $table) => $table->dropColumn('comments_enabled'));
|
||||
}
|
||||
}
|
||||
};
|
||||
215
plugins-core/news/resources/js/components/NewsComments.vue
Normal file
215
plugins-core/news/resources/js/components/NewsComments.vue
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
<script setup>
|
||||
import { nextTick, onMounted, ref } from 'vue';
|
||||
import { router, usePage } from '@inertiajs/vue3';
|
||||
import { CornerDownRight, LoaderCircle, MessageCircle, Pencil, Reply, Send, Trash2 } from '@lucide/vue';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import PersonAvatar from '@/components/PersonAvatar.vue';
|
||||
|
||||
/**
|
||||
* Kommentare unter einem Beitrag – mit Antworten (eine Ebene, „Antwort an …“).
|
||||
*/
|
||||
const props = defineProps({
|
||||
postSlug: { type: String, required: true },
|
||||
comments: { type: Object, required: true },
|
||||
});
|
||||
|
||||
const page = usePage();
|
||||
const me = page.props.auth?.user;
|
||||
|
||||
const body = ref('');
|
||||
const sending = ref(false);
|
||||
const errors = ref({});
|
||||
const replyTo = ref(null); // Kommentar, auf den geantwortet wird
|
||||
const replyBody = ref('');
|
||||
const editing = ref(null);
|
||||
const editBody = ref('');
|
||||
|
||||
const relative = new Intl.RelativeTimeFormat('de-DE', { numeric: 'auto' });
|
||||
const dateFormat = new Intl.DateTimeFormat('de-DE', { dateStyle: 'medium', timeStyle: 'short' });
|
||||
|
||||
function ago(iso) {
|
||||
const seconds = (new Date(iso) - new Date()) / 1000;
|
||||
const steps = [[60, 'second'], [60, 'minute'], [24, 'hour'], [7, 'day']];
|
||||
let value = seconds;
|
||||
|
||||
for (const [size, unit] of steps) {
|
||||
if (Math.abs(value) < size) return relative.format(Math.round(value), unit);
|
||||
value /= size;
|
||||
}
|
||||
|
||||
return dateFormat.format(new Date(iso));
|
||||
}
|
||||
|
||||
function send(data, done) {
|
||||
sending.value = true;
|
||||
router.post(`/neuigkeiten/${props.postSlug}/kommentare`, data, {
|
||||
preserveScroll: true,
|
||||
only: ['comments', 'flash'],
|
||||
onError: (e) => (errors.value = e),
|
||||
onSuccess: () => {
|
||||
errors.value = {};
|
||||
done();
|
||||
const id = page.props.flash?.comment;
|
||||
if (id) nextTick(() => highlight(id));
|
||||
},
|
||||
onFinish: () => (sending.value = false),
|
||||
});
|
||||
}
|
||||
|
||||
function submit() {
|
||||
if (!body.value.trim()) return;
|
||||
send({ body: body.value }, () => (body.value = ''));
|
||||
}
|
||||
|
||||
function startReply(comment) {
|
||||
editing.value = null;
|
||||
replyTo.value = comment;
|
||||
replyBody.value = '';
|
||||
nextTick(() => document.getElementById(`antwort-${comment.id}`)?.focus());
|
||||
}
|
||||
|
||||
function submitReply() {
|
||||
if (!replyBody.value.trim()) return;
|
||||
send({ body: replyBody.value, parent_id: replyTo.value.id }, () => {
|
||||
replyTo.value = null;
|
||||
replyBody.value = '';
|
||||
});
|
||||
}
|
||||
|
||||
function startEdit(comment) {
|
||||
replyTo.value = null;
|
||||
editing.value = comment.id;
|
||||
editBody.value = comment.body;
|
||||
}
|
||||
|
||||
function saveEdit(comment) {
|
||||
router.put(`/kommentare/${comment.id}`, { body: editBody.value }, { preserveScroll: true, only: ['comments', 'flash'], onSuccess: () => (editing.value = null) });
|
||||
}
|
||||
|
||||
function remove(comment) {
|
||||
if (window.confirm('Kommentar löschen?')) {
|
||||
router.delete(`/kommentare/${comment.id}`, { preserveScroll: true, only: ['comments', 'flash'] });
|
||||
}
|
||||
}
|
||||
|
||||
function highlight(id) {
|
||||
const el = document.getElementById(`kommentar-${id}`);
|
||||
|
||||
if (!el) return;
|
||||
|
||||
el.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||||
el.classList.add('ring-2', 'ring-brand/40');
|
||||
setTimeout(() => el.classList.remove('ring-2', 'ring-brand/40'), 2000);
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
const match = window.location.hash.match(/^#kommentar-(\d+)$/);
|
||||
if (match) setTimeout(() => highlight(match[1]), 300);
|
||||
});
|
||||
|
||||
const grow = (event) => {
|
||||
event.target.style.height = 'auto';
|
||||
event.target.style.height = `${event.target.scrollHeight}px`;
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section id="kommentare" class="rounded-xl border bg-card p-6 shadow-xs sm:p-8" aria-labelledby="kommentare-titel">
|
||||
<h2 id="kommentare-titel" class="flex items-center gap-2 text-lg font-semibold">
|
||||
<MessageCircle class="size-5 text-brand-text" /> Kommentare
|
||||
<span v-if="comments.count" class="rounded-full bg-muted px-2 py-0.5 text-xs font-medium text-muted-foreground tabular-nums">{{ comments.count }}</span>
|
||||
</h2>
|
||||
|
||||
<!-- Neuer Kommentar -->
|
||||
<form v-if="comments.enabled" class="mt-5 flex gap-3" @submit.prevent="submit">
|
||||
<PersonAvatar v-if="me" :person="me" class="size-9 shrink-0" />
|
||||
<div class="min-w-0 flex-1">
|
||||
<textarea
|
||||
v-model="body"
|
||||
rows="2"
|
||||
:maxlength="comments.max"
|
||||
placeholder="Was meinen Sie dazu?"
|
||||
aria-label="Kommentar schreiben"
|
||||
class="w-full resize-none rounded-lg border border-input bg-background px-3 py-2 text-sm leading-relaxed focus-visible:border-brand-400 focus-visible:ring-3 focus-visible:ring-ring/30 focus-visible:outline-none"
|
||||
@input="grow"
|
||||
@keydown.ctrl.enter="submit"
|
||||
@keydown.meta.enter="submit"
|
||||
/>
|
||||
<p v-if="errors.body && !replyTo" class="mt-1 text-[13px] text-destructive">{{ errors.body }}</p>
|
||||
<div v-if="body.trim()" class="mt-2 flex items-center justify-end gap-3">
|
||||
<span class="text-xs text-muted-foreground">Strg + Enter zum Senden</span>
|
||||
<Button type="submit" size="sm" :disabled="sending"><LoaderCircle v-if="sending" class="animate-spin" /><Send v-else /> Kommentieren</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<p v-else class="mt-4 rounded-md bg-muted px-3 py-2 text-[13px] text-muted-foreground">Die Kommentare zu diesem Beitrag sind geschlossen.</p>
|
||||
|
||||
<p v-if="!comments.threads.length && comments.enabled" class="mt-6 text-sm text-muted-foreground">Noch keine Kommentare – schreiben Sie den ersten.</p>
|
||||
|
||||
<!-- Stränge -->
|
||||
<ol class="mt-6 grid gap-6">
|
||||
<li v-for="thread in comments.threads" :key="thread.id">
|
||||
<template v-for="(comment, index) in [thread, ...thread.replies]" :key="comment.id">
|
||||
<article :id="`kommentar-${comment.id}`" class="flex gap-3 rounded-lg transition-shadow" :class="index > 0 ? 'mt-4 ml-6 sm:ml-12' : ''">
|
||||
<PersonAvatar v-if="comment.author" :person="comment.author" :class="index > 0 ? 'size-8' : 'size-9'" class="shrink-0" />
|
||||
<span v-else class="grid shrink-0 place-items-center rounded-full bg-muted text-muted-foreground" :class="index > 0 ? 'size-8' : 'size-9'"><MessageCircle class="size-4" /></span>
|
||||
|
||||
<div class="min-w-0 flex-1">
|
||||
<p v-if="comment.deleted" class="py-1 text-sm text-muted-foreground italic">Dieser Kommentar wurde gelöscht.</p>
|
||||
<template v-else>
|
||||
<div class="rounded-lg bg-muted/60 px-3.5 py-2.5">
|
||||
<p class="flex flex-wrap items-baseline gap-x-2 text-[13px]">
|
||||
<span class="font-semibold">{{ comment.author?.name ?? 'Ehemalige Person' }}</span>
|
||||
<span v-if="comment.author?.department" class="text-muted-foreground">{{ comment.author.department }}</span>
|
||||
</p>
|
||||
<p v-if="comment.reply_to" class="mt-0.5 flex items-center gap-1 text-xs text-muted-foreground"><CornerDownRight class="size-3" /> Antwort an {{ comment.reply_to }}</p>
|
||||
|
||||
<form v-if="editing === comment.id" class="mt-2" @submit.prevent="saveEdit(comment)">
|
||||
<textarea v-model="editBody" rows="3" :maxlength="comments.max" class="w-full rounded-md border border-input bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-3 focus-visible:ring-ring/30" aria-label="Kommentar bearbeiten" />
|
||||
<div class="mt-2 flex justify-end gap-2">
|
||||
<Button type="button" size="sm" variant="ghost" @click="editing = null">Abbrechen</Button>
|
||||
<Button type="submit" size="sm" :disabled="!editBody.trim()">Speichern</Button>
|
||||
</div>
|
||||
</form>
|
||||
<p v-else class="mt-1 text-sm leading-relaxed break-words whitespace-pre-line">{{ comment.body }}</p>
|
||||
</div>
|
||||
<div class="mt-1 flex flex-wrap items-center gap-x-3 px-1 text-xs text-muted-foreground">
|
||||
<time :datetime="comment.created_at" :title="dateFormat.format(new Date(comment.created_at))">{{ ago(comment.created_at) }}</time>
|
||||
<span v-if="comment.edited">bearbeitet</span>
|
||||
<button v-if="comments.enabled" type="button" class="inline-flex items-center gap-1 font-medium hover:text-foreground" @click="startReply(comment)"><Reply class="size-3.5" /> Antworten</button>
|
||||
<button v-if="comment.can.edit" type="button" class="inline-flex items-center gap-1 hover:text-foreground" @click="startEdit(comment)"><Pencil class="size-3" /> Bearbeiten</button>
|
||||
<button v-if="comment.can.delete" type="button" class="inline-flex items-center gap-1 hover:text-destructive" @click="remove(comment)"><Trash2 class="size-3" /> Löschen</button>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</article>
|
||||
</template>
|
||||
|
||||
<!-- Antwort schreiben (unter dem Strang) -->
|
||||
<form v-if="replyTo && [thread.id, ...thread.replies.map((r) => r.id)].includes(replyTo.id)" class="mt-3 ml-6 flex gap-3 sm:ml-12" @submit.prevent="submitReply">
|
||||
<PersonAvatar v-if="me" :person="me" class="size-8 shrink-0" />
|
||||
<div class="min-w-0 flex-1">
|
||||
<p class="mb-1 text-xs text-muted-foreground">Antwort an {{ replyTo.author?.name }}</p>
|
||||
<textarea
|
||||
:id="`antwort-${replyTo.id}`"
|
||||
v-model="replyBody"
|
||||
rows="2"
|
||||
:maxlength="comments.max"
|
||||
class="w-full resize-none rounded-lg border border-input bg-background px-3 py-2 text-sm focus-visible:border-brand-400 focus-visible:ring-3 focus-visible:ring-ring/30 focus-visible:outline-none"
|
||||
aria-label="Antwort schreiben"
|
||||
@input="grow"
|
||||
@keydown.ctrl.enter="submitReply"
|
||||
@keydown.meta.enter="submitReply"
|
||||
@keydown.esc="replyTo = null"
|
||||
/>
|
||||
<p v-if="errors.body" class="mt-1 text-[13px] text-destructive">{{ errors.body }}</p>
|
||||
<div class="mt-2 flex justify-end gap-2">
|
||||
<Button type="button" size="sm" variant="ghost" @click="replyTo = null">Abbrechen</Button>
|
||||
<Button type="submit" size="sm" :disabled="sending || !replyBody.trim()"><Send /> Antworten</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</li>
|
||||
</ol>
|
||||
</section>
|
||||
</template>
|
||||
|
|
@ -74,6 +74,7 @@ const form = useForm({
|
|||
is_featured: props.post?.is_featured ?? false,
|
||||
published_at: localValue(props.post?.published_at),
|
||||
form_id: props.post?.form_id ?? null,
|
||||
comments_enabled: props.post?.comments_enabled ?? true,
|
||||
action: 'draft',
|
||||
});
|
||||
|
||||
|
|
@ -607,6 +608,11 @@ function autoGrow(event) {
|
|||
In dieser Kategorie veröffentlicht die Redaktion. Reichen Sie den Beitrag zur Freigabe ein, wenn er fertig ist.
|
||||
</p>
|
||||
|
||||
<label class="flex items-center justify-between gap-3 text-[13px] font-medium">
|
||||
Kommentare erlauben
|
||||
<Switch v-model="form.comments_enabled" />
|
||||
</label>
|
||||
|
||||
<FormField v-if="forms.length" id="form_id" label="Formular unter dem Beitrag" :error="form.errors.form_id" hint="Z. B. Anmeldung oder Rückmeldung zum Beitrag.">
|
||||
<NativeSelect id="form_id" v-model="form.form_id" :options="forms" placeholder="– kein Formular –" />
|
||||
</FormField>
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@
|
|||
import { ref, watch } from 'vue';
|
||||
import { Link } from '@inertiajs/vue3';
|
||||
import { useWindowScroll } from '@vueuse/core';
|
||||
import { ArrowLeft, CalendarDays, Check, Clock, Link2, Mail, PenLine } from '@lucide/vue';
|
||||
import { ArrowLeft, CalendarDays, Check, Clock, Link2, Mail, MessageCircle, PenLine } from '@lucide/vue';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import PersonAvatar from '@/components/PersonAvatar.vue';
|
||||
import NewsComments from '../components/NewsComments.vue';
|
||||
import AppLayout from '@/layouts/AppLayout.vue';
|
||||
import PageContent from '@/pagebuilder/PageContent.vue';
|
||||
import { formatDate, formatShortDate } from '@/lib/format';
|
||||
|
|
@ -15,6 +16,7 @@ const props = defineProps({
|
|||
editUrl: { type: String, default: null },
|
||||
form: { type: Object, default: null },
|
||||
content: { type: Object, default: null },
|
||||
comments: { type: Object, default: null },
|
||||
});
|
||||
|
||||
// Lesefortschritt: Anteil des Artikels, der schon oben aus dem Bild gescrollt ist
|
||||
|
|
@ -78,13 +80,15 @@ const mailLink = () => `mailto:?subject=${encodeURIComponent(props.post.title)}&
|
|||
<div class="mt-2 flex flex-wrap items-center gap-x-5 gap-y-2 text-sm text-white/80">
|
||||
<span class="inline-flex items-center gap-1.5"><CalendarDays class="size-4" /> {{ formatDate(post.published_at) }}</span>
|
||||
<span class="inline-flex items-center gap-1.5"><Clock class="size-4" /> {{ post.reading_minutes }} Min. Lesezeit</span>
|
||||
<a v-if="comments?.count" href="#kommentare" class="inline-flex items-center gap-1.5 hover:text-white"><MessageCircle class="size-4" /> {{ comments.count }} {{ comments.count === 1 ? 'Kommentar' : 'Kommentare' }}</a>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="mt-6 grid gap-6 lg:grid-cols-12">
|
||||
<!-- Inhalt in einer weißen Karte -->
|
||||
<article ref="article" class="min-w-0 rounded-xl border bg-card p-6 shadow-xs sm:p-10 lg:col-span-8">
|
||||
<div class="grid min-w-0 grid-cols-1 content-start gap-6 lg:col-span-8">
|
||||
<article ref="article" class="min-w-0 rounded-xl border bg-card p-6 shadow-xs sm:p-10">
|
||||
<!-- Am Handy steht die Seitenleiste unten – dort den Autor gleich oben zeigen -->
|
||||
<div v-if="post.author" class="mb-8 flex items-center gap-3 border-b pb-6 lg:hidden">
|
||||
<PersonAvatar :person="post.author" class="size-11" />
|
||||
|
|
@ -114,6 +118,9 @@ const mailLink = () => `mailto:?subject=${encodeURIComponent(props.post.title)}&
|
|||
</footer>
|
||||
</article>
|
||||
|
||||
<NewsComments v-if="comments" :post-slug="post.slug" :comments="comments" />
|
||||
</div>
|
||||
|
||||
<aside class="grid content-start gap-6 lg:col-span-4">
|
||||
<section v-if="post.author" class="rounded-xl border bg-card p-5 shadow-xs">
|
||||
<h2 class="mb-3 text-xs font-semibold tracking-wider text-muted-foreground uppercase">Geschrieben von</h2>
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Inhaus\Plugins\News\Http\Controllers\NewsCommentController;
|
||||
use Inhaus\Plugins\News\Http\Controllers\NewsController;
|
||||
use Inhaus\Plugins\News\Http\Controllers\NewsEditorController;
|
||||
|
||||
Route::middleware('auth')->group(function () {
|
||||
Route::get('/neuigkeiten', [NewsController::class, 'index'])->name('news.index');
|
||||
Route::get('/neuigkeiten/{post:slug}', [NewsController::class, 'show'])->name('news.show');
|
||||
Route::post('/neuigkeiten/{post:slug}/kommentare', [NewsCommentController::class, 'store'])->name('news.comments.store');
|
||||
Route::put('/kommentare/{comment}', [NewsCommentController::class, 'update'])->name('news.comments.update');
|
||||
Route::delete('/kommentare/{comment}', [NewsCommentController::class, 'destroy'])->name('news.comments.destroy');
|
||||
|
||||
Route::prefix('redaktion/neuigkeiten')->name('news.editor.')->controller(NewsEditorController::class)->group(function () {
|
||||
Route::get('/neu', 'create')->name('create');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\News\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Notifications\PortalNotification;
|
||||
use App\Support\Audit;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Inhaus\Plugins\News\Models\NewsComment;
|
||||
use Inhaus\Plugins\News\Models\NewsPost;
|
||||
|
||||
/**
|
||||
* Kommentare und Antworten unter Beiträgen.
|
||||
*/
|
||||
class NewsCommentController extends Controller
|
||||
{
|
||||
public function store(Request $request, NewsPost $post): RedirectResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
abort_unless($post->isPublished() && $post->isVisibleTo($user), 404);
|
||||
abort_unless($post->comments_enabled, 403, 'Kommentare sind für diesen Beitrag geschlossen.');
|
||||
|
||||
$data = $request->validate([
|
||||
'body' => ['required', 'string', 'max:'.NewsComment::MAX_LENGTH],
|
||||
'parent_id' => ['nullable', 'integer'],
|
||||
], [], ['body' => 'Kommentar']);
|
||||
|
||||
$parent = isset($data['parent_id']) ? $post->comments()->withTrashed()->find($data['parent_id']) : null;
|
||||
abort_if(isset($data['parent_id']) && ! $parent, 422);
|
||||
|
||||
// Antworten hängen immer am obersten Kommentar des Strangs.
|
||||
$thread = $parent?->parent_id ? $parent->parent()->withTrashed()->first() : $parent;
|
||||
|
||||
$comment = $post->comments()->create([
|
||||
'parent_id' => $thread?->id,
|
||||
'user_id' => $user->id,
|
||||
'reply_to_user_id' => $parent && $parent->user_id !== $user->id ? $parent->user_id : null,
|
||||
'body' => trim($data['body']),
|
||||
]);
|
||||
|
||||
$this->notify($post, $comment, $parent, $user);
|
||||
|
||||
return back()->with('status', $parent ? 'Antwort veröffentlicht.' : 'Kommentar veröffentlicht.')->with('comment', $comment->id);
|
||||
}
|
||||
|
||||
public function update(Request $request, NewsComment $comment): RedirectResponse
|
||||
{
|
||||
abort_unless($comment->user_id === $request->user()->id, 403);
|
||||
|
||||
$data = $request->validate(['body' => ['required', 'string', 'max:'.NewsComment::MAX_LENGTH]], [], ['body' => 'Kommentar']);
|
||||
$comment->forceFill(['body' => trim($data['body']), 'edited_at' => now()])->save();
|
||||
|
||||
return back()->with('status', 'Kommentar geändert.');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, NewsComment $comment): RedirectResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
$own = $comment->user_id === $user->id;
|
||||
abort_unless($own || $user->can('news.comments.moderate'), 403);
|
||||
|
||||
$comment->delete();
|
||||
|
||||
if (! $own) {
|
||||
Audit::log('news.comment.deleted', ['comment' => $comment->id, 'post' => $comment->post_id]);
|
||||
}
|
||||
|
||||
return back()->with('status', 'Kommentar gelöscht.');
|
||||
}
|
||||
|
||||
private function notify(NewsPost $post, NewsComment $comment, ?NewsComment $parent, User $user): void
|
||||
{
|
||||
$url = route('news.show', $post).'#kommentar-'.$comment->id;
|
||||
$excerpt = Str::limit($comment->body, 120);
|
||||
$notified = [$user->id];
|
||||
|
||||
if ($parent?->author && ! in_array($parent->user_id, $notified, true)) {
|
||||
$parent->author->notify(new PortalNotification("{$user->name} hat Ihnen geantwortet", "„{$post->title}“: {$excerpt}", $url, 'message-circle'));
|
||||
$notified[] = $parent->user_id;
|
||||
}
|
||||
|
||||
if ($post->author && ! in_array($post->author_id, $notified, true)) {
|
||||
$post->author->notify(new PortalNotification("Neuer Kommentar von {$user->name}", "„{$post->title}“: {$excerpt}", $url, 'message-circle'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@
|
|||
namespace Inhaus\Plugins\News\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Pagebuilder\Content;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
|
|
@ -10,6 +11,7 @@ use Inertia\Response;
|
|||
use Inhaus\Plugins\News\FormEmbed;
|
||||
use Inhaus\Plugins\News\Http\Resources\NewsPostResource;
|
||||
use Inhaus\Plugins\News\Models\NewsCategory;
|
||||
use Inhaus\Plugins\News\Models\NewsComment;
|
||||
use Inhaus\Plugins\News\Models\NewsPost;
|
||||
|
||||
class NewsController extends Controller
|
||||
|
|
@ -59,6 +61,7 @@ class NewsController extends Controller
|
|||
// Mit dem Baukasten geschriebene Beiträge: Blöcke wie auf Seiten
|
||||
'content' => $post->content !== null ? app(Content::class)->render($post->content, request()->user(), ['news_id' => $post->id]) : null,
|
||||
'form' => FormEmbed::render($post->form_id, request()->user()),
|
||||
'comments' => $this->comments($post, request()->user()),
|
||||
'related' => NewsPostResource::collection(
|
||||
NewsPost::query()
|
||||
->published()
|
||||
|
|
@ -72,4 +75,60 @@ class NewsController extends Controller
|
|||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Kommentare als Stränge: oberster Kommentar mit seinen Antworten. Gelöschte
|
||||
* Kommentare mit Antworten bleiben als Platzhalter stehen.
|
||||
*
|
||||
* @return array{enabled: bool, count: int, threads: list<array<string, mixed>>, max: int}
|
||||
*/
|
||||
private function comments(NewsPost $post, User $user): array
|
||||
{
|
||||
$moderate = $user->can('news.comments.moderate');
|
||||
$all = $post->comments()->withTrashed()->with(['author', 'replyTo'])->orderBy('created_at')->get();
|
||||
$replies = $all->whereNotNull('parent_id')->groupBy('parent_id');
|
||||
|
||||
$present = function (NewsComment $comment) use ($user, $moderate) {
|
||||
$deleted = $comment->trashed();
|
||||
|
||||
return [
|
||||
'id' => $comment->id,
|
||||
'body' => $deleted ? null : $comment->body,
|
||||
'deleted' => $deleted,
|
||||
'created_at' => $comment->created_at?->toIso8601String(),
|
||||
'edited' => $comment->edited_at !== null,
|
||||
'reply_to' => $comment->replyTo?->name,
|
||||
'author' => $deleted || ! $comment->author ? null : [
|
||||
'id' => $comment->author->id,
|
||||
'name' => $comment->author->name,
|
||||
'initials' => $comment->author->initials,
|
||||
'avatar_url' => $comment->author->avatarUrl(96),
|
||||
'department' => $comment->author->department,
|
||||
],
|
||||
'can' => [
|
||||
'edit' => ! $deleted && $comment->user_id === $user->id,
|
||||
'delete' => ! $deleted && ($comment->user_id === $user->id || $moderate),
|
||||
],
|
||||
];
|
||||
};
|
||||
|
||||
$threads = [];
|
||||
|
||||
foreach ($all->whereNull('parent_id') as $comment) {
|
||||
$children = ($replies[$comment->id] ?? collect())->reject->trashed()->values();
|
||||
|
||||
if ($comment->trashed() && $children->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$threads[] = [...$present($comment), 'replies' => $children->map($present)->all()];
|
||||
}
|
||||
|
||||
return [
|
||||
'enabled' => (bool) $post->comments_enabled,
|
||||
'count' => $all->reject->trashed()->count(),
|
||||
'threads' => $threads,
|
||||
'max' => NewsComment::MAX_LENGTH,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -150,6 +150,7 @@ class NewsEditorController extends Controller
|
|||
'cover' => ['nullable', File::image()->types(['jpg', 'jpeg', 'png', 'webp'])->max(10 * 1024)],
|
||||
'remove_cover' => ['boolean'],
|
||||
'is_featured' => ['boolean'],
|
||||
'comments_enabled' => ['boolean'],
|
||||
'published_at' => ['nullable', 'date'],
|
||||
'form_id' => ['nullable', 'integer', Rule::in(array_map('intval', array_keys(FormEmbed::options())))],
|
||||
'action' => ['required', Rule::in(['draft', 'submit', 'publish'])],
|
||||
|
|
@ -172,6 +173,7 @@ class NewsEditorController extends Controller
|
|||
'body' => $text !== '' ? '<p>'.e($text).'</p>' : null,
|
||||
'category_id' => $data['category_id'] ?? null,
|
||||
'form_id' => FormEmbed::available() ? ($data['form_id'] ?? null) : $post->form_id,
|
||||
'comments_enabled' => $request->boolean('comments_enabled', $post->comments_enabled ?? true),
|
||||
]);
|
||||
|
||||
if ($canPublish) {
|
||||
|
|
@ -244,6 +246,7 @@ class NewsEditorController extends Controller
|
|||
'url' => $post->isPublished() ? route('news.show', $post) : null,
|
||||
'review_note' => $post->review_note,
|
||||
'form_id' => $post->form_id,
|
||||
'comments_enabled' => $post->comments_enabled,
|
||||
'author' => $post->author?->name,
|
||||
] : null,
|
||||
'categories' => $categories->map(fn (NewsCategory $c) => [
|
||||
|
|
|
|||
57
plugins-core/news/src/Models/NewsComment.php
Normal file
57
plugins-core/news/src/Models/NewsComment.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\News\Models;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* Kommentar zu einem Beitrag. Antworten (parent_id) hängen immer am obersten
|
||||
* Kommentar eines Strangs; reply_to_user_id merkt sich, wem geantwortet wurde.
|
||||
*/
|
||||
#[Fillable(['post_id', 'parent_id', 'user_id', 'reply_to_user_id', 'body'])]
|
||||
class NewsComment extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
public const MAX_LENGTH = 3000;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['edited_at' => 'datetime'];
|
||||
}
|
||||
|
||||
/** @return BelongsTo<NewsPost, $this> */
|
||||
public function post(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(NewsPost::class, 'post_id');
|
||||
}
|
||||
|
||||
/** @return BelongsTo<User, $this> */
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
/** @return BelongsTo<User, $this> */
|
||||
public function replyTo(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'reply_to_user_id');
|
||||
}
|
||||
|
||||
/** @return BelongsTo<NewsComment, $this> */
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(self::class, 'parent_id');
|
||||
}
|
||||
|
||||
/** @return HasMany<NewsComment, $this> */
|
||||
public function replies(): HasMany
|
||||
{
|
||||
return $this->hasMany(self::class, 'parent_id')->withTrashed()->orderBy('created_at');
|
||||
}
|
||||
}
|
||||
|
|
@ -11,12 +11,13 @@ use Illuminate\Database\Eloquent\Builder;
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Str;
|
||||
use Inhaus\Plugins\News\Database\Factories\NewsPostFactory;
|
||||
|
||||
#[Fillable([
|
||||
'category_id', 'author_id', 'title', 'slug', 'teaser', 'body',
|
||||
'cover_path', 'is_featured', 'published_at', 'form_id', 'content',
|
||||
'cover_path', 'is_featured', 'published_at', 'form_id', 'content', 'comments_enabled',
|
||||
])]
|
||||
class NewsPost extends Model
|
||||
{
|
||||
|
|
@ -79,6 +80,7 @@ class NewsPost extends Model
|
|||
return [
|
||||
'is_featured' => 'boolean',
|
||||
'content' => 'array',
|
||||
'comments_enabled' => 'boolean',
|
||||
'published_at' => 'datetime',
|
||||
'submitted_at' => 'datetime',
|
||||
'form_id' => 'integer',
|
||||
|
|
@ -153,6 +155,12 @@ class NewsPost extends Model
|
|||
return ['blocks' => filled($this->body) ? [['type' => 'text', 'data' => ['html' => $this->body_html ?? $this->body]]] : []];
|
||||
}
|
||||
|
||||
/** @return HasMany<NewsComment, $this> */
|
||||
public function comments(): HasMany
|
||||
{
|
||||
return $this->hasMany(NewsComment::class, 'post_id');
|
||||
}
|
||||
|
||||
public function coverUrl(string $variant = 'large'): ?string
|
||||
{
|
||||
return app(Images::class)->url($this->cover_path, $variant);
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ class NewsPlugin extends Plugin
|
|||
$abilities->define('news.manage', 'Neuigkeiten: alle Kategorien verwalten', 'Neuigkeiten');
|
||||
$abilities->define('news.post.create', 'Beiträge schreiben', 'Neuigkeiten', resource: 'news_category');
|
||||
$abilities->define('news.post.publish', 'Beiträge veröffentlichen', 'Neuigkeiten', resource: 'news_category');
|
||||
$abilities->define('news.comments.moderate', 'Kommentare anderer löschen', 'Neuigkeiten');
|
||||
$abilities->resourceType('news_category', 'Kategorie', fn () => NewsCategory::query()->orderBy('sort')->pluck('name', 'id')->all());
|
||||
|
||||
Gate::policy(NewsCategory::class, NewsCategoryPolicy::class);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue