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
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:
parent
d9fcc7a9b4
commit
b3e2b525e1
161 changed files with 1249 additions and 342 deletions
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
// Herzen: wer einen Beitrag mag (sichtbar als Anzahl, Namen nur für die Redaktion)
|
||||
Schema::create('news_likes', function (Blueprint $table) {
|
||||
$table->foreignId('post_id')->constrained('news_posts')->cascadeOnDelete();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->timestamp('created_at')->nullable();
|
||||
|
||||
$table->primary(['post_id', 'user_id']);
|
||||
});
|
||||
|
||||
// Reichweite: anonym – nur ein nicht umkehrbarer Schlüssel je Person und Beitrag
|
||||
Schema::create('news_reads', function (Blueprint $table) {
|
||||
$table->foreignId('post_id')->constrained('news_posts')->cascadeOnDelete();
|
||||
$table->char('reader', 64);
|
||||
$table->date('read_on');
|
||||
|
||||
$table->primary(['post_id', 'reader']);
|
||||
$table->index(['post_id', 'read_on']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('news_reads');
|
||||
Schema::dropIfExists('news_likes');
|
||||
}
|
||||
};
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
<script setup>
|
||||
import { Link } from '@inertiajs/vue3';
|
||||
import { Heart } from '@lucide/vue';
|
||||
import CategoryLabel from './CategoryLabel.vue';
|
||||
import NewsImage from './NewsImage.vue';
|
||||
import { formatShortDate } from '@/lib/format';
|
||||
|
|
@ -16,7 +17,10 @@ defineProps({
|
|||
<CategoryLabel :category="post.category" />
|
||||
<h3 class="mt-1 line-clamp-2 text-[15px] leading-snug font-semibold group-hover:underline">{{ post.title }}</h3>
|
||||
<p v-if="post.teaser" class="mt-1.5 line-clamp-2 text-[13px] text-muted-foreground">{{ post.teaser }}</p>
|
||||
<p class="mt-auto pt-3 text-xs text-muted-foreground">{{ formatShortDate(post.published_at) }} · {{ post.reading_minutes }} Min.</p>
|
||||
<p class="mt-auto flex items-center gap-1 pt-3 text-xs text-muted-foreground">
|
||||
{{ formatShortDate(post.published_at) }} · {{ post.reading_minutes }} Min.
|
||||
<span v-if="post.likes" class="ml-auto inline-flex items-center gap-1 tabular-nums" :title="`${post.likes} ${post.likes === 1 ? 'Herz' : 'Herzen'}`"><Heart class="size-3.5" :class="post.liked ? 'fill-current text-brand-text' : ''" /> {{ post.likes }}</span>
|
||||
</p>
|
||||
</div>
|
||||
</Link>
|
||||
</template>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<script setup>
|
||||
import { Link } from '@inertiajs/vue3';
|
||||
import { MessageSquareWarning, Plus } from '@lucide/vue';
|
||||
import { Eye, Heart, MessageSquareWarning, Plus } from '@lucide/vue';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import Panel from '@/components/Panel.vue';
|
||||
import { formatRelative } from '@/lib/format';
|
||||
|
|
@ -46,6 +46,10 @@ const badge = (post) => ({
|
|||
<template v-if="post.date"> · {{ formatRelative(post.date) }}</template>
|
||||
</p>
|
||||
</div>
|
||||
<span v-if="post.readers !== undefined" class="flex shrink-0 items-center gap-2.5 text-xs text-muted-foreground tabular-nums">
|
||||
<span class="inline-flex items-center gap-1" title="Gelesen (anonym gezählt)"><Eye class="size-3.5" /> {{ post.readers }}</span>
|
||||
<span class="inline-flex items-center gap-1" title="Herzen"><Heart class="size-3.5" /> {{ post.likes }}</span>
|
||||
</span>
|
||||
<MessageSquareWarning v-if="post.review_note" class="size-4 shrink-0 text-amber-600" title="Mit Anmerkung zurückgegeben" />
|
||||
<span class="shrink-0 rounded px-1.5 py-0.5 text-[11px] font-medium" :class="badge(post)">{{ post.status_label }}</span>
|
||||
</li>
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
<script setup>
|
||||
import { ref, watch } from 'vue';
|
||||
import { Link } from '@inertiajs/vue3';
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import { Link, router } from '@inertiajs/vue3';
|
||||
import { useWindowScroll } from '@vueuse/core';
|
||||
import { ArrowLeft, CalendarDays, Check, Clock, Link2, Mail, MessageCircle, PenLine } from '@lucide/vue';
|
||||
import { ArrowLeft, CalendarDays, ChartNoAxesColumn, Check, Clock, Heart, 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';
|
||||
|
|
@ -17,8 +17,34 @@ const props = defineProps({
|
|||
form: { type: Object, default: null },
|
||||
content: { type: Object, default: null },
|
||||
comments: { type: Object, default: null },
|
||||
reach: { type: Object, default: null },
|
||||
});
|
||||
|
||||
// Herz: sofort umschalten, der Server bestätigt im Hintergrund
|
||||
const liked = ref(!!props.post.liked);
|
||||
const likes = ref(props.post.likes ?? 0);
|
||||
watch(() => [props.post.liked, props.post.likes], ([l, n]) => {
|
||||
liked.value = !!l;
|
||||
likes.value = n ?? 0;
|
||||
});
|
||||
function toggleLike() {
|
||||
const next = !liked.value;
|
||||
liked.value = next;
|
||||
likes.value += next ? 1 : -1;
|
||||
router.visit(`/neuigkeiten/${props.post.slug}/herz`, {
|
||||
method: next ? 'post' : 'delete',
|
||||
preserveScroll: true,
|
||||
preserveState: true,
|
||||
onError: () => {
|
||||
liked.value = !next;
|
||||
likes.value += next ? -1 : 1;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const maxDay = computed(() => Math.max(1, ...(props.reach?.days ?? []).map((d) => d.readers)));
|
||||
const showLikers = ref(false);
|
||||
|
||||
// Lesefortschritt: Anteil des Artikels, der schon oben aus dem Bild gescrollt ist
|
||||
const article = ref(null);
|
||||
const progress = ref(0);
|
||||
|
|
@ -80,6 +106,7 @@ 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>
|
||||
<span v-if="likes" class="inline-flex items-center gap-1.5"><Heart class="size-4" /> {{ likes }}</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>
|
||||
|
|
@ -110,7 +137,17 @@ const mailLink = () => `mailto:?subject=${encodeURIComponent(props.post.title)}&
|
|||
<PageContent v-if="form" :content="form" class="mt-10" />
|
||||
|
||||
<footer class="mt-10 flex flex-wrap items-center gap-2 border-t pt-6">
|
||||
<span class="mr-2 text-sm text-muted-foreground">Beitrag teilen:</span>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
:aria-pressed="liked"
|
||||
:class="liked ? 'border-brand/40 bg-brand-soft text-brand-text hover:bg-brand-soft hover:text-brand-text' : ''"
|
||||
@click="toggleLike"
|
||||
>
|
||||
<Heart :class="liked ? 'fill-current' : ''" />
|
||||
{{ liked ? 'Gefällt mir' : 'Herz geben' }}<span v-if="likes" class="tabular-nums"> · {{ likes }}</span>
|
||||
</Button>
|
||||
<span class="mr-2 ml-auto text-sm text-muted-foreground max-sm:ml-0">Beitrag teilen:</span>
|
||||
<Button variant="outline" size="sm" @click="copyLink">
|
||||
<component :is="copied ? Check : Link2" /> {{ copied ? 'Link kopiert' : 'Link kopieren' }}
|
||||
</Button>
|
||||
|
|
@ -133,6 +170,35 @@ const mailLink = () => `mailto:?subject=${encodeURIComponent(props.post.title)}&
|
|||
</Link>
|
||||
</section>
|
||||
|
||||
<!-- Reichweite: nur für die Redaktion der Kategorie -->
|
||||
<section v-if="reach" class="rounded-xl border bg-card p-5 shadow-xs">
|
||||
<h2 class="mb-3 flex items-center gap-1.5 text-xs font-semibold tracking-wider text-muted-foreground uppercase"><ChartNoAxesColumn class="size-3.5" /> Reichweite</h2>
|
||||
<dl class="grid grid-cols-3 gap-3 text-center">
|
||||
<div><dt class="text-[11px] text-muted-foreground">Gelesen</dt><dd class="text-xl font-semibold tabular-nums">{{ reach.readers }}</dd></div>
|
||||
<div><dt class="text-[11px] text-muted-foreground">der Belegschaft</dt><dd class="text-xl font-semibold tabular-nums">{{ reach.share }} %</dd></div>
|
||||
<div><dt class="text-[11px] text-muted-foreground">Herzen</dt><dd class="text-xl font-semibold tabular-nums">{{ reach.likes }}</dd></div>
|
||||
</dl>
|
||||
<div v-if="reach.days.length > 1" class="mt-4">
|
||||
<div class="flex h-16 items-end gap-0.5" role="img" :aria-label="`Lesende je Tag in den ersten ${reach.days.length} Tagen`">
|
||||
<div
|
||||
v-for="day in reach.days"
|
||||
:key="day.date"
|
||||
class="flex-1 rounded-t-sm bg-brand/70"
|
||||
:style="{ height: `${Math.max(4, (day.readers / maxDay) * 100)}%` }"
|
||||
:title="`${formatShortDate(day.date)}: ${day.readers}`"
|
||||
/>
|
||||
</div>
|
||||
<p class="mt-1 text-[11px] text-muted-foreground">Lesende je Tag seit Veröffentlichung</p>
|
||||
</div>
|
||||
<div v-if="reach.likers.length" class="mt-4 border-t pt-3">
|
||||
<button type="button" class="text-[13px] font-medium text-brand-text hover:underline" @click="showLikers = !showLikers">
|
||||
{{ showLikers ? 'Ausblenden' : 'Wer hat ein Herz gegeben?' }}
|
||||
</button>
|
||||
<p v-if="showLikers" class="mt-2 text-[13px] leading-relaxed text-muted-foreground">{{ reach.likers.join(', ') }}</p>
|
||||
</div>
|
||||
<p class="mt-3 text-[11px] leading-snug text-muted-foreground">Gezählt wird anonym: jede Person einmal, ohne festzuhalten, wer gelesen hat.</p>
|
||||
</section>
|
||||
|
||||
<section v-if="related.data.length" class="rounded-xl border bg-card p-5 shadow-xs lg:sticky lg:top-20">
|
||||
<h2 class="mb-3 text-xs font-semibold tracking-wider text-muted-foreground uppercase">Weitere Beiträge</h2>
|
||||
<ul class="grid gap-1">
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@
|
|||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Inhaus\Plugins\News\Http\Controllers\NewsApiController;
|
||||
use Inhaus\Plugins\News\Http\Controllers\NewsLikeController;
|
||||
|
||||
// /api/v1/news … (Token nötig, siehe docs/API.md)
|
||||
Route::get('/news', [NewsApiController::class, 'index']);
|
||||
Route::get('/news/{post:slug}', [NewsApiController::class, 'show']);
|
||||
Route::post('/news/{post:slug}/like', [NewsLikeController::class, 'store'])->middleware('throttle:60,1');
|
||||
Route::delete('/news/{post:slug}/like', [NewsLikeController::class, 'destroy'])->middleware('throttle:60,1');
|
||||
|
|
|
|||
|
|
@ -4,11 +4,14 @@ 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;
|
||||
use Inhaus\Plugins\News\Http\Controllers\NewsLikeController;
|
||||
|
||||
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'])->middleware('throttle:20,1')->name('news.comments.store');
|
||||
Route::post('/neuigkeiten/{post:slug}/herz', [NewsLikeController::class, 'store'])->middleware('throttle:60,1')->name('news.like');
|
||||
Route::delete('/neuigkeiten/{post:slug}/herz', [NewsLikeController::class, 'destroy'])->middleware('throttle:60,1')->name('news.unlike');
|
||||
Route::put('/kommentare/{comment}', [NewsCommentController::class, 'update'])->name('news.comments.update');
|
||||
Route::delete('/kommentare/{comment}', [NewsCommentController::class, 'destroy'])->name('news.comments.destroy');
|
||||
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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' => [
|
||||
|
|
|
|||
|
|
@ -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()),
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
plugins-core/news/src/Models/NewsLike.php
Normal file
26
plugins-core/news/src/Models/NewsLike.php
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
57
plugins-core/news/src/Reach.php
Normal file
57
plugins-core/news/src/Reach.php
Normal 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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue