Nachrichten 1.1.0: Schnittstelle für die App, Verbindung prüfen

- /api/v1/chat: config, Gruppenchats, verlassen, bearbeiten, löschen, Reaktionen,
  „Gibt es Neues?“, „schreibt …“, GIFs, Echtzeit-Anmeldung und Anhänge mit Token
- Anhang-Adressen zeigen bei API-Aufrufen auf die API (Token statt Sitzung)
- Backend: „Echtzeit prüfen“ und „GIPHY prüfen“ melden sofort, ob die Zugangsdaten stimmen
- README: Schritt-für-Schritt-Einrichtung für pusher.com und GIPHY

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-27 20:10:35 +00:00
parent 2844452002
commit 5f71c3e250
9 changed files with 207 additions and 26 deletions

View file

@ -18,6 +18,8 @@ use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Validation\ValidationException;
use Inhaus\Plugins\Chat\Gifs;
use Inhaus\Plugins\Chat\Realtime;
use UnitEnum;
/**
@ -138,9 +140,23 @@ class ChatSettings extends Page
Notification::make()->success()->title('Gespeichert')->send();
}
/**
* @param array{ok: bool, message: string} $result
*/
private function report(array $result): void
{
Notification::make()->{$result['ok'] ? 'success' : 'danger'}()->title($result['ok'] ? 'Verbindung steht' : 'Das klappt noch nicht')->body($result['message'])->send();
}
protected function getHeaderActions(): array
{
return [
Action::make('checkRealtime')->label('Echtzeit prüfen')->color('gray')->icon(Heroicon::OutlinedSignal)
->visible(fn () => app(Realtime::class)->enabled())
->action(fn () => $this->report(app(Realtime::class)->check())),
Action::make('checkGiphy')->label('GIPHY prüfen')->color('gray')->icon(Heroicon::OutlinedGif)
->visible(fn () => app(Gifs::class)->giphyEnabled())
->action(fn () => $this->report(app(Gifs::class)->checkGiphy())),
Action::make('forgetGiphy')->label('GIPHY ausschalten')->color('gray')
->visible(fn () => filled(app(Settings::class)->get('chat.giphy_key')))
->requiresConfirmation()

View file

@ -56,6 +56,34 @@ class Gifs
return $path === '' ? Storage::disk(self::DISK)->url(self::FOLDER) : Storage::disk(self::DISK)->url($path);
}
/**
* Schlüssel prüfen (Backend) – ein Abruf der Trends.
*
* @return array{ok: bool, message: string}
*/
public function checkGiphy(): array
{
if (! $this->giphyEnabled()) {
return ['ok' => false, 'message' => 'Es ist kein GIPHY-Schlüssel gespeichert.'];
}
try {
$response = Http::timeout(5)->acceptJson()->get('https://api.giphy.com/v1/gifs/trending', [
'api_key' => Crypt::decryptString((string) $this->settings->get('chat.giphy_key')),
'limit' => 1,
]);
} catch (Throwable $e) {
return ['ok' => false, 'message' => 'GIPHY ist nicht erreichbar: '.mb_substr($e->getMessage(), 0, 160)];
}
return match (true) {
$response->successful() => ['ok' => true, 'message' => 'Der Schlüssel funktioniert – die GIF-Suche im Chat findet jetzt auch GIPHY.'],
in_array($response->status(), [401, 403], true) => ['ok' => false, 'message' => 'GIPHY lehnt den Schlüssel ab – bitte prüfen.'],
$response->status() === 429 => ['ok' => false, 'message' => 'Der Schlüssel funktioniert, das Kontingent ist aber gerade aufgebraucht (Beta-Schlüssel: 100 Abrufe pro Stunde).'],
default => ['ok' => false, 'message' => 'Unerwartete Antwort von GIPHY (HTTP '.$response->status().').'],
};
}
/**
* @return list<array<string, mixed>>
*/

View file

@ -44,6 +44,19 @@ class ConversationController extends Controller
]);
}
/** Einstellungen für die App (entspricht den Seitendaten im Portal) */
public function config(Request $request, Gifs $gifs, Realtime $realtime): JsonResponse
{
return response()->json([
'me' => Presenter::person($request->user()),
'can_create_groups' => $request->user()->can('chat.groups.create'),
'giphy' => $gifs->giphyEnabled(),
'realtime' => $realtime->clientConfig(),
'reactions' => Reaction::EMOJIS,
'max_upload_mb' => (int) app(Settings::class)->get('chat.max_upload_mb', 25),
]);
}
public function index(Request $request): JsonResponse
{
return response()->json(['conversations' => $this->list($request->user())]);

View file

@ -14,6 +14,12 @@ class Presenter
/**
* @return array<string, mixed>
*/
/** Die App lädt Anhänge mit ihrem Token über die API, der Browser über die Sitzung */
public static function attachmentUrl(Attachment $attachment): string
{
return request()->is('api/*') ? route('chat.app.attachment', $attachment) : route('chat.attachment', $attachment);
}
public static function person(?User $user): ?array
{
return $user ? ['id' => $user->id, 'name' => $user->name, 'first_name' => $user->first_name, 'initials' => $user->initials, 'avatar_url' => $user->avatarUrl(96), 'job_title' => $user->job_title] : null;
@ -100,7 +106,7 @@ class Presenter
'image' => $a->isImage(),
'width' => $a->width,
'height' => $a->height,
'url' => route('chat.attachment', $a),
'url' => self::attachmentUrl($a),
])->all(),
'reactions' => $m->reactions->groupBy('emoji')->map(fn ($group, $emoji) => [
'emoji' => $emoji,

View file

@ -77,6 +77,32 @@ class Realtime
return ['auth' => $this->settings->get('chat.realtime.key').':'.hash_hmac('sha256', $socketId.':'.$channel, $this->secret())];
}
/**
* Verbindung prüfen (Backend): fragt beim Dienst die offenen Kanäle ab – klappt das,
* stimmen Server, App-ID, Key und Secret.
*
* @return array{ok: bool, message: string}
*/
public function check(): array
{
if (! $this->enabled()) {
return ['ok' => false, 'message' => 'Echtzeit ist nicht eingeschaltet oder unvollständig eingerichtet.'];
}
try {
$response = Http::timeout(5)->acceptJson()->get($this->signedUrl('GET', '/apps/'.$this->settings->get('chat.realtime.app_id').'/channels'));
} catch (Throwable $e) {
return ['ok' => false, 'message' => 'Der Dienst ist nicht erreichbar: '.mb_substr($e->getMessage(), 0, 160)];
}
return match (true) {
$response->successful() => ['ok' => true, 'message' => 'Verbindung steht – Nachrichten kommen jetzt sofort an.'],
in_array($response->status(), [401, 403], true) => ['ok' => false, 'message' => 'Der Dienst lehnt die Anmeldung ab – bitte Key und Secret prüfen.'],
$response->status() === 404 => ['ok' => false, 'message' => 'Diese App-ID kennt der Dienst nicht.'],
default => ['ok' => false, 'message' => 'Unerwartete Antwort des Dienstes (HTTP '.$response->status().').'],
};
}
/**
* @param list<string> $channels
* @param array<string, mixed> $data
@ -84,24 +110,30 @@ class Realtime
private function trigger(array $channels, string $event, array $data): void
{
$body = json_encode(['name' => $event, 'channels' => $channels, 'data' => json_encode($data)]);
$path = '/apps/'.$this->settings->get('chat.realtime.app_id').'/events';
$query = [
'auth_key' => (string) $this->settings->get('chat.realtime.key'),
'auth_timestamp' => (string) time(),
'auth_version' => '1.0',
'body_md5' => md5($body),
];
ksort($query);
$query['auth_signature'] = hash_hmac('sha256', "POST\n{$path}\n".http_build_query($query), $this->secret());
try {
Http::timeout(3)->withBody($body, 'application/json')->post($this->baseUrl().$path.'?'.http_build_query($query));
Http::timeout(3)->withBody($body, 'application/json')->post($this->signedUrl('POST', '/apps/'.$this->settings->get('chat.realtime.app_id').'/events', $body));
} catch (Throwable $e) {
// Echtzeit ist nur eine Beschleunigung – die Oberfläche fragt ohnehin regelmäßig nach
report($e);
}
}
/** Adresse mit Signatur nach dem Pusher-HTTP-Protokoll */
private function signedUrl(string $method, string $path, ?string $body = null): string
{
$query = [
'auth_key' => (string) $this->settings->get('chat.realtime.key'),
'auth_timestamp' => (string) time(),
'auth_version' => '1.0',
...($body === null ? [] : ['body_md5' => md5($body)]),
];
ksort($query);
$query['auth_signature'] = hash_hmac('sha256', "{$method}\n{$path}\n".http_build_query($query), $this->secret());
return $this->baseUrl().$path.'?'.http_build_query($query);
}
private function baseUrl(): string
{
$scheme = $this->settings->get('chat.realtime.scheme') ?: 'https';