Loheide-eu-mitarbeiterplatt.../app/Feed/Feed.php
Friederich Loheide b2599f7bc3
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Failing after 13m31s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 13m53s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Failing after 13m53s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Failing after 13m44s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 12m3s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 12m20s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 10m35s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 1m1s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 12m13s
CI / Build-Artefakte aktuell (pull_request) Successful in 4m56s
Gefällt mir im Feed, Suche links in der App-Kopfzeile (0.58.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-01 14:55:48 +00:00

138 lines
5.4 KiB
PHP

<?php
namespace App\Feed;
use App\Models\User;
use App\Plugins\PluginAccess;
use Carbon\CarbonImmutable;
use Closure;
use Illuminate\Support\Facades\Log;
use Throwable;
/**
* Der Feed der Mitarbeiter-App: was im Haus passiert, als Beiträge in zeitlicher Folge.
* Kern und Plugins steuern Einträge bei:
*
* app(Feed::class)->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<string, Closure> */
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<array<string, mixed>>, 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<string, mixed>|mixed $item
* @return array<string, mixed>|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,
];
}
}