Rückmeldungen, Hintergrundaufgaben, Systemzustand und Web-Installer
- Rückmeldungen an den Hersteller (docs/specs/feedback.md): Fehlerbericht und
Funktionswunsch, nur freigeschaltete Wege, Idempotency-Key je Meldung,
Warteschlange mit höchstens fünf Versuchen, 422 am Feld, 429 mit Wartezeit,
404 blendet den Weg aus, Referenznummer und „Meine Rückmeldungen“,
sichtbare technische Angaben und Datenschutzhinweis über dem Absendeknopf
- Hintergrundaufgaben ohne Dauerprozess: Cron, Web-Cron (/cron/{token}) oder
Pseudo-Cron nach dem Seitenaufruf; Queue in kurzen Häppchen
- Seite „Systemzustand“ mit Voraussetzungen, Grenzen, Betrieb, Sicherheit und
Anleitung für den Cronjob
- Web-Installer (Systemprüfung → Datenbank → Administrator), legt .env und
Anwendungsschlüssel selbst an, arbeitet vor der Einrichtung ohne Datenbank
- Datenbank-Benachrichtigungen im Admin-Bereich
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
43244d751a
commit
029dbd243d
38 changed files with 2274 additions and 12 deletions
58
app/Support/Feedback/FeedbackChannels.php
Normal file
58
app/Support/Feedback/FeedbackChannels.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\Feedback;
|
||||
|
||||
use App\Models\VendorFeedback;
|
||||
use App\Support\Settings;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Welche Meldewege für dieses Programm freigeschaltet sind – einmal täglich über
|
||||
* GET /v1/software ermittelt. Ein 404 beim Senden blendet den Weg dauerhaft aus.
|
||||
*/
|
||||
class FeedbackChannels
|
||||
{
|
||||
public function __construct(private readonly Settings $settings) {}
|
||||
|
||||
/**
|
||||
* @return array<string, string> Schlüssel => Bezeichnung
|
||||
*/
|
||||
public function available(): array
|
||||
{
|
||||
$enabled = Cache::remember('feedback.endpoints', now()->addDay(), fn () => $this->fetch());
|
||||
$disabled = (array) $this->settings->get('feedback.disabled', []);
|
||||
|
||||
return collect(VendorFeedback::TYPES)
|
||||
->only($enabled ?? array_keys(VendorFeedback::TYPES))
|
||||
->except($disabled)
|
||||
->all();
|
||||
}
|
||||
|
||||
public function disable(string $type): void
|
||||
{
|
||||
$disabled = (array) $this->settings->get('feedback.disabled', []);
|
||||
$this->settings->set('feedback.disabled', array_values(array_unique([...$disabled, $type])));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>|null null = unbekannt (Server nicht erreichbar) – dann alle anbieten
|
||||
*/
|
||||
private function fetch(): ?array
|
||||
{
|
||||
try {
|
||||
$software = Http::timeout(5)->acceptJson()->get(rtrim(config('updater.api_url'), '/').'/software')->json('software');
|
||||
|
||||
foreach ((array) $software as $entry) {
|
||||
if (($entry['slug'] ?? null) === config('updater.software')) {
|
||||
return array_values(array_filter((array) ($entry['endpoints'] ?? []), 'is_string'));
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
80
app/Support/Feedback/FeedbackClient.php
Normal file
80
app/Support/Feedback/FeedbackClient.php
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\Feedback;
|
||||
|
||||
use App\Support\License\ServerError;
|
||||
use App\Support\License\VendorHttp;
|
||||
|
||||
/**
|
||||
* Sendet Rückmeldungen an api.loheide.cloud (docs/specs/feedback.md).
|
||||
* Lizenz-Kopfzeilen nur, wenn eine Lizenz hinterlegt ist; eine ungültige oder
|
||||
* abgelaufene Lizenz verhindert das Senden nicht.
|
||||
*/
|
||||
class FeedbackClient
|
||||
{
|
||||
public function __construct(private readonly VendorHttp $http) {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
public function send(string $type, array $payload, string $idempotencyKey, int $timeout = 10): FeedbackResult
|
||||
{
|
||||
try {
|
||||
$response = VendorHttp::guard(fn () => $this->http->request($timeout, identifyDevice: false)
|
||||
->withHeaders(['Idempotency-Key' => $idempotencyKey])
|
||||
->post($this->url($type), $payload));
|
||||
} catch (ServerError $e) {
|
||||
return FeedbackResult::retry('Der Server ist nicht erreichbar.');
|
||||
}
|
||||
|
||||
$data = $response->json() ?? [];
|
||||
|
||||
return match (true) {
|
||||
$response->status() === 201 || ($response->successful() && isset($data['reference'])) => FeedbackResult::sent((string) ($data['reference'] ?? '')),
|
||||
$response->status() === 404 => FeedbackResult::disabled(),
|
||||
$response->status() === 422 => FeedbackResult::invalid($this->fieldErrors($data), $data['error'] ?? $data['message'] ?? null),
|
||||
$response->status() === 429 => FeedbackResult::rateLimited($this->retryAfter($response->header('Retry-After'), $data)),
|
||||
// 409: dieselbe Meldung wird gerade verarbeitet – kurz warten, dann erneut (kein Fehler).
|
||||
$response->status() === 409 => FeedbackResult::retry('Wird bereits verarbeitet.', 30),
|
||||
default => FeedbackResult::retry('Server antwortet mit HTTP '.$response->status().'.', $this->retryAfter($response->header('Retry-After'), $data)),
|
||||
};
|
||||
}
|
||||
|
||||
private function url(string $type): string
|
||||
{
|
||||
return rtrim(config('updater.api_url'), '/').'/software/'.config('updater.software').'/'.$type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unterstützt {"errors": {"feld": ["Grund"]}} und {"field": "feld", "error": "Grund"}.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function fieldErrors(array $data): array
|
||||
{
|
||||
$errors = [];
|
||||
|
||||
if (is_array($data['errors'] ?? null)) {
|
||||
foreach ($data['errors'] as $field => $messages) {
|
||||
$errors[(string) $field] = is_array($messages) ? (string) reset($messages) : (string) $messages;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_string($data['field'] ?? null)) {
|
||||
$errors[$data['field']] = (string) ($data['error'] ?? $data['message'] ?? 'Ungültige Eingabe.');
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
private function retryAfter(?string $header, array $data): ?int
|
||||
{
|
||||
$value = $data['retry_after'] ?? $header;
|
||||
|
||||
return is_numeric($value) ? (int) $value : null;
|
||||
}
|
||||
}
|
||||
137
app/Support/Feedback/FeedbackOutbox.php
Normal file
137
app/Support/Feedback/FeedbackOutbox.php
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\Feedback;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\VendorFeedback;
|
||||
use Filament\Notifications\Notification;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Warteschlange für Rückmeldungen: Die Meldung wird sofort gespeichert (samt
|
||||
* Idempotency-Key) und dann zugestellt. Klappt das nicht, versucht es der
|
||||
* Hintergrundlauf erneut – höchstens fünfmal, danach wird verworfen und die
|
||||
* Person informiert.
|
||||
*/
|
||||
class FeedbackOutbox
|
||||
{
|
||||
public const MAX_ATTEMPTS = 5;
|
||||
|
||||
public function __construct(
|
||||
private readonly FeedbackClient $client,
|
||||
private readonly FeedbackChannels $channels,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Neue Meldung speichern und direkt einen ersten Zustellversuch machen.
|
||||
*
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
public function submit(?User $user, string $type, array $payload): FeedbackResult
|
||||
{
|
||||
$feedback = VendorFeedback::query()->create([
|
||||
'user_id' => $user?->id,
|
||||
'type' => $type,
|
||||
'payload' => $payload,
|
||||
// Je Meldung EINMAL erzeugt und bei jedem Wiederholungsversuch mitgeschickt.
|
||||
'idempotency_key' => bin2hex(random_bytes(16)),
|
||||
'status' => VendorFeedback::PENDING,
|
||||
'next_attempt_at' => now(),
|
||||
]);
|
||||
|
||||
$result = $this->attempt($feedback, timeout: 8);
|
||||
|
||||
// Eingabe- und Mengenfehler klärt die Person im Formular – nicht die Warteschlange.
|
||||
// Die Eingabe bleibt im Formular erhalten; eine neue Meldung bekommt einen neuen Schlüssel.
|
||||
if (in_array($result->status, [FeedbackResult::INVALID, FeedbackResult::RATE_LIMITED, FeedbackResult::DISABLED], true)) {
|
||||
$feedback->delete();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fällige Meldungen erneut senden (Hintergrundlauf). Nacheinander, damit das
|
||||
* Gerätetoken zwischen den Anfragen nachgeführt wird.
|
||||
*/
|
||||
public function processDue(): int
|
||||
{
|
||||
$processed = 0;
|
||||
|
||||
VendorFeedback::query()
|
||||
->where('status', VendorFeedback::PENDING)
|
||||
->where('next_attempt_at', '<=', now())
|
||||
->orderBy('id')
|
||||
->limit(20)
|
||||
->get()
|
||||
->each(function (VendorFeedback $feedback) use (&$processed) {
|
||||
$this->attempt($feedback);
|
||||
$processed++;
|
||||
});
|
||||
|
||||
return $processed;
|
||||
}
|
||||
|
||||
private function attempt(VendorFeedback $feedback, int $timeout = 15): FeedbackResult
|
||||
{
|
||||
$result = $this->client->send($feedback->type, $feedback->payload, $feedback->idempotency_key, $timeout);
|
||||
$attempts = $feedback->attempts + 1;
|
||||
|
||||
switch ($result->status) {
|
||||
case FeedbackResult::SENT:
|
||||
$feedback->update(['status' => VendorFeedback::SENT, 'attempts' => $attempts, 'reference' => $result->reference, 'last_error' => null]);
|
||||
Log::info("Rückmeldung zugestellt: {$result->reference}");
|
||||
break;
|
||||
|
||||
case FeedbackResult::DISABLED:
|
||||
$this->channels->disable($feedback->type);
|
||||
$this->discard($feedback, $attempts, 'Dieser Meldeweg ist für das Programm nicht freigeschaltet.');
|
||||
break;
|
||||
|
||||
case FeedbackResult::INVALID:
|
||||
case FeedbackResult::RATE_LIMITED:
|
||||
if ($feedback->attempts > 0) {
|
||||
// Nur im Hintergrund möglich (z. B. 429 bei Wiederholung): später erneut.
|
||||
$this->reschedule($feedback, $attempts, $result->message ?? 'Später erneut versuchen.', $result->retryAfter);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->reschedule($feedback, $attempts, $result->message ?? 'Nicht zugestellt.', $result->retryAfter);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function reschedule(VendorFeedback $feedback, int $attempts, string $error, ?int $retryAfter): void
|
||||
{
|
||||
if ($attempts >= self::MAX_ATTEMPTS) {
|
||||
$this->discard($feedback, $attempts, $error);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// 1, 5, 15, 60 Minuten – oder so lange, wie der Server es verlangt.
|
||||
$delay = $retryAfter ?? [60, 300, 900, 3600][min($attempts - 1, 3)];
|
||||
|
||||
$feedback->update([
|
||||
'attempts' => $attempts,
|
||||
'next_attempt_at' => now()->addSeconds(max(30, $delay)),
|
||||
'last_error' => mb_substr($error, 0, 500),
|
||||
]);
|
||||
}
|
||||
|
||||
private function discard(VendorFeedback $feedback, int $attempts, string $error): void
|
||||
{
|
||||
$feedback->update(['status' => VendorFeedback::DISCARDED, 'attempts' => $attempts, 'last_error' => mb_substr($error, 0, 500)]);
|
||||
Log::warning("Rückmeldung verworfen nach {$attempts} Versuchen: {$error}");
|
||||
|
||||
if ($feedback->user) {
|
||||
Notification::make()
|
||||
->danger()
|
||||
->title('Rückmeldung konnte nicht zugestellt werden')
|
||||
->body('„'.($feedback->payload['title'] ?? '').'“ wurde nach '.$attempts.' Versuchen verworfen. Bitte senden Sie die Meldung später erneut.')
|
||||
->sendToDatabase($feedback->user);
|
||||
}
|
||||
}
|
||||
}
|
||||
58
app/Support/Feedback/FeedbackResult.php
Normal file
58
app/Support/Feedback/FeedbackResult.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\Feedback;
|
||||
|
||||
/**
|
||||
* Ergebnis eines Zustellversuchs.
|
||||
*/
|
||||
final class FeedbackResult
|
||||
{
|
||||
public const SENT = 'sent';
|
||||
|
||||
public const INVALID = 'invalid';
|
||||
|
||||
public const RATE_LIMITED = 'rate_limited';
|
||||
|
||||
public const DISABLED = 'disabled';
|
||||
|
||||
public const RETRY = 'retry';
|
||||
|
||||
/**
|
||||
* @param array<string, string> $errors Feld => Meldung (bei 422)
|
||||
*/
|
||||
private function __construct(
|
||||
public readonly string $status,
|
||||
public readonly ?string $reference = null,
|
||||
public readonly array $errors = [],
|
||||
public readonly ?int $retryAfter = null,
|
||||
public readonly ?string $message = null,
|
||||
) {}
|
||||
|
||||
public static function sent(string $reference): self
|
||||
{
|
||||
return new self(self::SENT, reference: $reference);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $errors
|
||||
*/
|
||||
public static function invalid(array $errors, ?string $message = null): self
|
||||
{
|
||||
return new self(self::INVALID, errors: $errors, message: $message);
|
||||
}
|
||||
|
||||
public static function rateLimited(?int $retryAfter): self
|
||||
{
|
||||
return new self(self::RATE_LIMITED, retryAfter: $retryAfter);
|
||||
}
|
||||
|
||||
public static function disabled(): self
|
||||
{
|
||||
return new self(self::DISABLED);
|
||||
}
|
||||
|
||||
public static function retry(string $message, ?int $retryAfter = null): self
|
||||
{
|
||||
return new self(self::RETRY, retryAfter: $retryAfter, message: $message);
|
||||
}
|
||||
}
|
||||
64
app/Support/Feedback/TechnicalInfo.php
Normal file
64
app/Support/Feedback/TechnicalInfo.php
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\Feedback;
|
||||
|
||||
use App\Support\License\VendorHttp;
|
||||
use App\Support\Update\Channels;
|
||||
use App\Support\Update\InstalledVersion;
|
||||
use App\Version;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Technische Angaben, die jede Meldung automatisch mitschickt – und die vor dem
|
||||
* Absenden sichtbar sind. Keine personenbezogenen Daten, keine Dateipfade,
|
||||
* keine Inhalte.
|
||||
*/
|
||||
class TechnicalInfo
|
||||
{
|
||||
public function __construct(
|
||||
private readonly InstalledVersion $version,
|
||||
private readonly Channels $channels,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{app_version: string, current_sha: string|null, os: string, metadata: array<string, string|int>}
|
||||
*/
|
||||
public function collect(): array
|
||||
{
|
||||
return [
|
||||
'app_version' => Version::VERSION,
|
||||
'current_sha' => $this->version->current(),
|
||||
'os' => VendorHttp::operatingSystem(),
|
||||
'metadata' => array_filter([
|
||||
'php' => PHP_VERSION,
|
||||
'datenbank' => $this->database(),
|
||||
'webserver' => $this->webserver(),
|
||||
'update_kanal' => $this->channels->active(),
|
||||
'cache' => (string) config('cache.default'),
|
||||
'queue' => (string) config('queue.default'),
|
||||
'sprache' => app()->getLocale(),
|
||||
'opcache' => function_exists('opcache_get_status') && @opcache_get_status(false) ? 'an' : 'aus',
|
||||
], fn ($value) => $value !== null && $value !== ''),
|
||||
];
|
||||
}
|
||||
|
||||
private function database(): ?string
|
||||
{
|
||||
try {
|
||||
$version = (string) DB::selectOne('select version() as v')->v;
|
||||
|
||||
return (str_contains(strtolower($version), 'mariadb') ? 'MariaDB ' : 'MySQL ').preg_replace('/-.*$/', '', $version);
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function webserver(): ?string
|
||||
{
|
||||
$software = (string) ($_SERVER['SERVER_SOFTWARE'] ?? '');
|
||||
|
||||
// Nur Produkt und Version, keine Modul-Liste.
|
||||
return $software !== '' ? strtok($software, ' ') : PHP_SAPI;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue