Feed in der Mitarbeiter-App (0.54.0)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Failing after 15m6s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 15m58s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Failing after 15m42s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Failing after 15m21s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 1h1m41s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 1h2m13s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 23m20s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 23m33s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 1m17s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m57s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Failing after 15m6s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Failing after 15m58s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Failing after 15m42s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Failing after 15m21s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 1h1m41s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 1h2m13s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 23m20s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 23m33s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 1m17s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m57s
Feed-Registry mit Anbietern (Neu im Team, Neuigkeiten, Termine, Umfragen), Schnellaktionen je nach Rechten, Nachladen über /feed. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
ab76b65aff
commit
8acc0cfd8b
152 changed files with 950 additions and 361 deletions
130
app/Feed/Feed.php
Normal file
130
app/Feed/Feed.php
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
<?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],
|
||||
* ]);
|
||||
* });
|
||||
*
|
||||
* 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,
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue