add('news', function (User $user, ?CarbonImmutable $before, int $limit) { * return NewsPost::query()->published()->visibleTo($user) * ->when($before, fn ($q) => $q->where('published_at', '<', $before)) * ->latest('published_at')->limit($limit)->get() * ->map(fn ($post) => [ * 'id' => 'news-'.$post->id, 'time' => $post->published_at, * 'actor' => Feed::person($post->author), 'action' => 'hat eine Neuigkeit veröffentlicht', * 'badge' => $post->category?->name, 'title' => $post->title, 'text' => $post->teaser, * 'image_url' => …, 'url' => …, 'stats' => ['likes' => 3, 'comments' => 1], * 'like' => ['liked' => false, 'url' => '/neuigkeiten/…/herz'], // optional * ]); * }); * * Jeder Anbieter liefert höchstens $limit Einträge, die älter als $before sind (neueste zuerst); * der Feed mischt sie nach Zeit. Nur, was die Person sehen darf. Gesperrte Plugins liefern nichts. */ class Feed { /** @var array */ private array $providers = []; public function add(string $key, Closure $items): void { $this->providers[$key] = $items; } public function remove(string $key): void { unset($this->providers[$key]); } public function has(): bool { return $this->providers !== []; } /** * @return array{items: list>, next: string|null} */ public function page(User $user, ?CarbonImmutable $before = null, int $limit = 10): array { $access = app(PluginAccess::class); $items = []; foreach ($this->providers as $key => $provider) { if ($key !== 'people' && ! $access->allows($key, $user)) { continue; } try { foreach ($provider($user, $before, $limit) as $item) { $normalized = $this->normalize($key, $item); if ($normalized && (! $before || CarbonImmutable::parse($normalized['time'])->lt($before))) { $items[] = $normalized; } } } catch (Throwable $e) { Log::error("Feed {$key}: ".$e->getMessage(), ['exception' => $e]); } } usort($items, fn (array $a, array $b) => strcmp($b['time'], $a['time'])); $page = array_slice($items, 0, $limit); return [ 'items' => $page, 'next' => count($items) > $limit ? end($page)['time'] : null, ]; } /** * Person für die Kopfzeile eines Beitrags. * * @return array{id: int, name: string, initials: string, avatar_url: string|null, subtitle: string}|null */ public static function person(?User $user): ?array { return $user ? [ 'id' => $user->id, 'name' => $user->name, 'initials' => $user->initials, 'avatar_url' => $user->avatarUrl(96), 'subtitle' => implode(' · ', array_filter([$user->job_title, $user->department])), ] : null; } /** * @param array|mixed $item * @return array|null */ private function normalize(string $key, mixed $item): ?array { if (! is_array($item) || blank($item['id'] ?? null) || blank($item['time'] ?? null) || blank($item['url'] ?? null)) { return null; } return [ 'key' => $key, 'id' => (string) $item['id'], 'kind' => (string) ($item['kind'] ?? $key), 'time' => CarbonImmutable::parse($item['time'])->toIso8601String(), 'actor' => is_array($item['actor'] ?? null) ? $item['actor'] : null, 'action' => (string) ($item['action'] ?? ''), 'context' => is_array($item['context'] ?? null) && filled($item['context']['label'] ?? null) ? $item['context'] : null, 'badge' => filled($item['badge'] ?? null) ? (string) $item['badge'] : null, 'title' => (string) ($item['title'] ?? ''), 'text' => (string) ($item['text'] ?? ''), 'image_url' => filled($item['image_url'] ?? null) ? (string) $item['image_url'] : null, 'images' => array_values(array_filter((array) ($item['images'] ?? []), 'is_string')), 'url' => (string) $item['url'], 'stats' => is_array($item['stats'] ?? null) ? $item['stats'] : null, 'cta' => is_array($item['cta'] ?? null) && filled($item['cta']['label'] ?? null) ? $item['cta'] : null, // Gefällt mir direkt im Feed: toggle = POST setzt, DELETE nimmt zurück (Antwort JSON); // reaction = POST {emoji} bzw. {emoji: null} 'like' => is_array($item['like'] ?? null) && filled($item['like']['url'] ?? null) ? [ 'liked' => (bool) ($item['like']['liked'] ?? false), 'url' => (string) $item['like']['url'], 'mode' => ($item['like']['mode'] ?? 'toggle') === 'reaction' ? 'reaction' : 'toggle', ] : null, ]; } }