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,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) => [
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue