Loheide-eu-mitarbeiterplatt.../plugins-core/news/src/Reach.php
Friederich Loheide b3e2b525e1
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
Herzen, Reichweite und App-Anbindung (0.30.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 12:24:11 +00:00

57 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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();
}
}