Herzen, Reichweite und App-Anbindung (0.30.0)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 6m10s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 6m3s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m33s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 7m25s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m28s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m27s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m26s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m27s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m12s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-27 12:24:11 +00:00
parent d9fcc7a9b4
commit b3e2b525e1
161 changed files with 1249 additions and 342 deletions

View file

@ -50,12 +50,14 @@ final class EditorialNews
$published = $writable === [] ? collect() : NewsPost::query()->with(['category', 'author'])
->published()
->when($writable !== true, fn ($q) => $q->whereIn('category_id', $writable))
->withCount('likes')
->latest('published_at')->limit(15)->get()
->filter(fn (NewsPost $post) => $user->can('update', $post));
$readers = app(Reach::class)->counts($published->modelKeys());
return [
'review' => $review->map($map)->all(),
'published' => $published->map($map)->values()->all(),
'published' => $published->map(fn (NewsPost $post) => [...$map($post), 'readers' => $readers[$post->id] ?? 0, 'likes' => (int) $post->likes_count])->values()->all(),
'mine' => $mine->map($map)->all(),
'scheduled' => $scheduled->map($map)->all(),
'canCreate' => app(Policies\NewsPostPolicy::class)->create($user),

View file

@ -9,6 +9,7 @@ use Illuminate\Http\Request;
use Inhaus\Plugins\News\Http\Resources\NewsPostResource;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
use Inhaus\Plugins\News\Reach;
/**
* Neuigkeiten für Apps – dieselben Sichtbarkeitsregeln wie im Portal.
@ -22,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')
$posts = NewsPost::query()->published()->visibleTo($request->user())->with('category')->withLikes($request->user())
->when($category, fn ($q) => $q->where('category_id', $category->id))
->latest('published_at')->paginate(20);
@ -35,7 +36,9 @@ 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']);
$post->load(['category', 'author'])->loadCount('likes');
$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]);
return response()->json(['data' => [

View file

@ -13,6 +13,7 @@ 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;
use Inhaus\Plugins\News\Reach;
class NewsController extends Controller
{
@ -31,6 +32,7 @@ class NewsController extends Controller
->published()
->visibleTo($request->user())
->with('category')
->withLikes($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')))
@ -57,7 +59,11 @@ class NewsController extends Controller
{
abort_unless($post->isPublished() && $post->isVisibleTo(request()->user()), 404);
$post->load(['category', 'author']);
$user = request()->user();
$post->load(['category', 'author'])->loadCount('likes');
$post->setAttribute('liked', $post->likes()->where('user_id', $user->id)->exists());
app(Reach::class)->record($post, $user);
$canEdit = $user->can('update', $post);
return Inertia::render('news::Show', [
'post' => [
@ -72,7 +78,13 @@ class NewsController extends Controller
'avatar_url' => $post->author->avatarUrl(96),
] : null,
],
'editUrl' => request()->user()->can('update', $post) ? route('news.editor.edit', $post) : null,
'editUrl' => $canEdit ? route('news.editor.edit', $post) : null,
// Reichweite und wer ein Herz gegeben hat: nur für die Redaktion der Kategorie
'reach' => $canEdit ? [
...app(Reach::class)->stats($post),
'likers' => $post->likes()->with('user:id,first_name,last_name,name')->latest('created_at')->limit(200)->get()
->map(fn ($like) => $like->user?->name)->filter()->values()->all(),
] : null,
// 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()),

View file

@ -0,0 +1,43 @@
<?php
namespace Inhaus\Plugins\News\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inhaus\Plugins\News\Models\NewsLike;
use Inhaus\Plugins\News\Models\NewsPost;
/**
* Herzen für Beiträge – im Portal und über die App-Schnittstelle.
*/
class NewsLikeController extends Controller
{
public function store(Request $request, NewsPost $post): RedirectResponse|JsonResponse
{
return $this->set($request, $post, true);
}
public function destroy(Request $request, NewsPost $post): RedirectResponse|JsonResponse
{
return $this->set($request, $post, false);
}
private function set(Request $request, NewsPost $post, bool $liked): RedirectResponse|JsonResponse
{
abort_unless($post->isPublished() && $post->isVisibleTo($request->user()), 404);
if ($liked) {
NewsLike::query()->insertOrIgnore(['post_id' => $post->id, 'user_id' => $request->user()->id, 'created_at' => now()]);
} else {
NewsLike::query()->where('post_id', $post->id)->where('user_id', $request->user()->id)->delete();
}
if ($request->expectsJson() && ! $request->header('X-Inertia')) {
return response()->json(['data' => ['liked' => $liked, 'likes' => $post->likes()->count()]]);
}
return back();
}
}

View file

@ -29,6 +29,8 @@ class NewsPostResource extends JsonResource
'thumb_url' => $this->coverUrl('thumb'),
'published_at' => $this->published_at?->toIso8601String(),
'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),
];
}
}

View file

@ -0,0 +1,26 @@
<?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;
#[Fillable(['post_id', 'user_id', 'created_at'])]
class NewsLike extends Model
{
public const UPDATED_AT = null;
public $incrementing = false;
protected $primaryKey = null;
/**
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

View file

@ -148,6 +148,22 @@ class NewsPost extends Model
return ['blocks' => filled($this->body) ? [['type' => 'text', 'data' => ['html' => $this->body_html ?? $this->body]]] : []];
}
/** @return HasMany<NewsLike, $this> */
public function likes(): HasMany
{
return $this->hasMany(NewsLike::class, 'post_id');
}
/**
* Anzahl Herzen und ob die Person selbst eines gegeben hat (für Listen und Ansicht).
*
* @param Builder<NewsPost> $query
*/
public function scopeWithLikes(Builder $query, ?User $user): void
{
$query->withCount('likes')->withExists(['likes as liked' => fn ($q) => $q->where('user_id', $user?->id ?? 0)]);
}
/** @return HasMany<NewsComment, $this> */
public function comments(): HasMany
{

View file

@ -0,0 +1,57 @@
<?php
namespace Inhaus\Plugins\News;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Inhaus\Plugins\News\Models\NewsPost;
/**
* Reichweite von Beiträgen – anonym: je Person und Beitrag nur ein Schlüssel, aus dem
* sich die Person nicht ermitteln lässt (HMAC mit dem Anwendungsschlüssel). So zählt
* jede Person einmal, aber niemand kann nachsehen, wer was gelesen hat.
*/
class Reach
{
public function record(NewsPost $post, User $user): void
{
DB::table('news_reads')->insertOrIgnore([
'post_id' => $post->id,
'reader' => hash_hmac('sha256', 'news-read:'.$post->id.':'.$user->id, (string) config('app.key')),
'read_on' => today()->toDateString(),
]);
}
/**
* @return array{readers: int, share: int, likes: int, days: list<array{date: string, readers: int}>}
*/
public function stats(NewsPost $post): array
{
$readers = DB::table('news_reads')->where('post_id', $post->id)->count();
$active = max(1, User::query()->active()->count());
$start = ($post->published_at ?? now())->copy()->startOfDay();
$perDay = DB::table('news_reads')->where('post_id', $post->id)->where('read_on', '<', $start->copy()->addDays(14))
->selectRaw('read_on, count(*) as c')->groupBy('read_on')->pluck('c', 'read_on');
return [
'readers' => $readers,
'share' => (int) round($readers / $active * 100),
'likes' => DB::table('news_likes')->where('post_id', $post->id)->count(),
'days' => collect(range(0, 13))->map(fn ($i) => $start->copy()->addDays($i))
->filter(fn ($day) => $day->lte(today()))
->map(fn ($day) => ['date' => $day->toDateString(), 'readers' => (int) ($perDay[$day->toDateString()] ?? 0)])
->values()->all(),
];
}
/**
* Lesende je Beitrag (für Listen in der Redaktion).
*
* @param list<int> $postIds
* @return array<int, int>
*/
public function counts(array $postIds): array
{
return DB::table('news_reads')->whereIn('post_id', $postIds ?: [0])->selectRaw('post_id, count(*) as c')->groupBy('post_id')->pluck('c', 'post_id')->map(fn ($c) => (int) $c)->all();
}
}