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} */ 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 $postIds * @return array */ 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(); } }