Herzen und Kommentare schon in der Vorschau (0.44.0)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m6s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m4s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m25s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 8m3s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m26s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 9m49s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m1s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m41s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 45s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m6s

- NewsPost::withStats: Herzen (Anzahl, eigenes) und Kommentare (Anzahl) für
  alle Vorschau-Abfragen: Übersicht, Widgets, Slider, Block, Profil, „Weitere Beiträge“, API
- NewsPostResource: „comments“ (null bei ausgeschalteten Kommentaren)
- NewsStats.vue in Karte, Listeneintrag, Block-Layouts, Slider und Seitenleiste

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-27 20:44:37 +00:00
parent 60dd200300
commit 21307fc953
145 changed files with 471 additions and 358 deletions

View file

@ -23,7 +23,7 @@ class NewsApiController extends Controller
? NewsCategory::query()->visibleTo($request->user())->where('slug', $request->query('category'))->first()
: null;
$posts = NewsPost::query()->published()->visibleTo($request->user())->with('category')->withLikes($request->user())
$posts = NewsPost::query()->published()->visibleTo($request->user())->with('category')->withStats($request->user())
->when($category, fn ($q) => $q->where('category_id', $category->id))
->latest('published_at')->paginate(20);
@ -36,7 +36,7 @@ class NewsApiController extends Controller
public function show(Request $request, NewsPost $post, Content $content): JsonResponse
{
abort_unless($post->isPublished() && $post->isVisibleTo($request->user()), 404);
$post->load(['category', 'author'])->loadCount('likes');
$post->load(['category', 'author'])->loadCount(['likes', 'comments']);
$post->setAttribute('liked', $post->likes()->where('user_id', $request->user()->id)->exists());
app(Reach::class)->record($post, $request->user());
$rendered = $content->render($post->blocks(), $request->user(), ['news_id' => $post->id]);

View file

@ -33,7 +33,7 @@ class NewsController extends Controller
->published()
->visibleTo($request->user())
->with('category')
->withLikes($request->user())
->withStats($request->user())
->when($category, fn ($query) => $query->where('category_id', $category->id))
->when($author, fn ($query) => $query->where('author_id', $author->id))
->when($only->isNotEmpty(), fn ($query) => $query->whereIn('category_id', $only->pluck('id')))
@ -96,6 +96,7 @@ class NewsController extends Controller
->published()
->visibleTo(request()->user())
->with('category')
->withStats(request()->user())
->whereKeyNot($post->id)
->orderByRaw('category_id = ? DESC', [$post->category_id ?? 0])
->latest('published_at')

View file

@ -31,6 +31,8 @@ class NewsPostResource extends JsonResource
'reading_minutes' => $this->reading_minutes,
'likes' => $this->when(isset($this->likes_count), fn () => (int) $this->likes_count),
'liked' => $this->when(isset($this->liked), fn () => (bool) $this->liked),
// null = Kommentare für diesen Beitrag ausgeschaltet
'comments' => $this->when(isset($this->comments_count), fn () => $this->comments_enabled ? (int) $this->comments_count : null),
];
}
}

View file

@ -164,6 +164,16 @@ class NewsPost extends Model
$query->withCount('likes')->withExists(['likes as liked' => fn ($q) => $q->where('user_id', $user?->id ?? 0)]);
}
/**
* Herzen (Anzahl, eigenes) und Kommentare (Anzahl) – für Vorschauen in Listen, Kacheln und Slider.
*
* @param Builder<NewsPost> $query
*/
public function scopeWithStats(Builder $query, ?User $user): void
{
$query->withLikes($user)->withCount('comments');
}
/** @return HasMany<NewsComment, $this> */
public function comments(): HasMany
{

View file

@ -82,7 +82,7 @@ class NewsPlugin extends Plugin
return $total === 0 ? null : [
'total' => $total,
'posts' => NewsPostResource::collection($query->with('category')->latest('published_at')->limit(4)->get())->resolve(),
'posts' => NewsPostResource::collection($query->with('category')->withStats($viewer)->latest('published_at')->limit(4)->get())->resolve(),
'all_url' => '/neuigkeiten?autor='.$person->id,
];
});
@ -140,7 +140,7 @@ class NewsPlugin extends Plugin
])
->resolveUsing(function (array $data, ?User $user) {
$slugs = $data['filter'] === 'all' ? [] : $data['categories'];
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')->withStats($user)
->when($slugs !== [] && $data['filter'] === 'include', fn ($q) => $q->whereHas('category', fn ($c) => $c->whereIn('slug', $slugs)))
->when($slugs !== [] && $data['filter'] === 'exclude', fn ($q) => $q->where(fn ($q) => $q->whereNull('category_id')->orWhereHas('category', fn ($c) => $c->whereNotIn('slug', $slugs))))
->latest('published_at')->limit($data['count'])->get();
@ -204,7 +204,7 @@ class NewsPlugin extends Plugin
}, label: 'Slider mit hervorgehobenen Beiträgen', mobileSort: 10);
$widgets->add('news.latest', 'news::Latest', 'feed', sort: 10, props: function (User $user, Request $request) {
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')->withStats($user)
->whereKeyNot($this->featured($user)->modelKeys())
->latest('published_at')->limit(4)->get();
@ -215,7 +215,7 @@ class NewsPlugin extends Plugin
}, label: 'Neuigkeiten', mobileSort: 20);
$widgets->add('news.areas', 'news::Areas', 'main', sort: 20, props: function (User $user) {
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')->withStats($user)
->whereKeyNot($this->featured($user)->modelKeys())
->latest('published_at')->skip(4)->limit(8)->get();
@ -231,11 +231,11 @@ class NewsPlugin extends Plugin
static $cache = [];
return $cache[$user->id] ??= (function () use ($user) {
$featured = NewsPost::query()->published()->visibleTo($user)->with('category')
$featured = NewsPost::query()->published()->visibleTo($user)->with('category')->withStats($user)
->where('is_featured', true)->whereNotNull('cover_path')
->latest('published_at')->limit(5)->get();
return $featured->isNotEmpty() ? $featured : NewsPost::query()->published()->visibleTo($user)->with('category')
return $featured->isNotEmpty() ? $featured : NewsPost::query()->published()->visibleTo($user)->with('category')->withStats($user)
->whereNotNull('cover_path')->latest('published_at')->limit(3)->get();
})();
}