Abrufe öffentlicher Meldungen durch bekannte Such- und KI-Dienste werden je Meldung mit Dienst, gekürztem User-Agent und Zeitpunkt protokolliert — keine IP. Dienste und Muster stehen in config/crawlers.php. Aufzeichnung wie der Aufruf-Zähler per defer() und fehlertolerant; Bereinigung hängt an press-releases:purge-events. VisibilityStats liefert die Abrufe je Dienst (letzter Abruf, Anzahl) und verliert den site:-Suchlink (Stufe 1 verspricht nichts, was von Dritten abhängt). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
140 lines
5.3 KiB
PHP
140 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Web;
|
|
|
|
use App\Models\Contact;
|
|
use App\Models\PressRelease;
|
|
use App\Scopes\PortalScope;
|
|
use App\Support\CrawlerIdentifier;
|
|
use App\Support\WebPortalContext;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use function Illuminate\Support\defer;
|
|
|
|
/**
|
|
* Datenaufbereitung der Detailseite einer Pressemitteilung je Ausgabe inkl.
|
|
* Hit-Zähler und verwandten Meldungen (Firma, Kategorie). Unbekannter oder nicht
|
|
* öffentlich sichtbarer Slug → 404.
|
|
*/
|
|
class ReleaseDetailPageData
|
|
{
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function build(WebPortalContext $context, string $slug): array
|
|
{
|
|
$portalValues = $context->portalValues;
|
|
$language = $context->language;
|
|
|
|
$publishedScope = static fn (Builder $query): Builder => $query
|
|
->forPortalValues($portalValues)
|
|
->forLanguage($language)
|
|
->visibleInArchive();
|
|
|
|
$release = PressRelease::query()
|
|
->withoutGlobalScope(PortalScope::class)
|
|
->tap($publishedScope)
|
|
->where('slug', $slug)
|
|
->with([
|
|
'company',
|
|
'user:id,agency_name',
|
|
'category.translations' => fn ($query) => $query->where('locale', $language),
|
|
'category.parent.translations' => fn ($query) => $query->where('locale', $language),
|
|
'images' => fn ($query) => $query->orderByDesc('is_preview')->orderBy('sort_order'),
|
|
'contacts',
|
|
])
|
|
->first();
|
|
|
|
if (! $release) {
|
|
// Tombstone statt 404: die URL gab es, der Inhalt wurde bewusst
|
|
// entfernt. Zwei Anlässe mit eigenem Seitentext —
|
|
// `depublished_at` = kostenpflichtige Depublikation auf Wunsch des
|
|
// Absenders (T12), `gone_at` = redaktionelle/rechtliche Entfernung
|
|
// aus dem Admin. Vollständig gelöschte PMs bleiben bei 404.
|
|
$removed = PressRelease::query()
|
|
->withoutGlobalScope(PortalScope::class)
|
|
->forPortalValues($portalValues)
|
|
->forLanguage($language)
|
|
->where('slug', $slug)
|
|
->where(fn (Builder $query) => $query
|
|
->whereNotNull('depublished_at')
|
|
->orWhereNotNull('gone_at'))
|
|
->first(['depublished_at', 'gone_at']);
|
|
|
|
if ($removed) {
|
|
return ['tombstone' => $removed->depublished_at !== null ? 'depublished' : 'removed'];
|
|
}
|
|
|
|
abort(404);
|
|
}
|
|
|
|
// Legacy-Importe kennen fast keine PM↔Kontakt-Verknüpfung — im
|
|
// Altsystem hingen Pressekontakte an der FIRMA. Ohne eigenen
|
|
// PM-Kontakt zeigt die Seite deshalb den Firmen-Kontakt (wie das
|
|
// Altsystem), statt den Block ganz wegzulassen.
|
|
if ($release->contacts->isEmpty() && $release->company_id) {
|
|
$companyContact = Contact::query()
|
|
->withoutGlobalScopes()
|
|
->where('company_id', $release->company_id)
|
|
->orderBy('id')
|
|
->first();
|
|
|
|
if ($companyContact) {
|
|
$release->setRelation('contacts', $release->contacts->push($companyContact));
|
|
}
|
|
}
|
|
|
|
$releaseId = $release->getKey();
|
|
defer(static fn () => PressRelease::recordDeferredView($releaseId));
|
|
|
|
// Crawler-Abruf-Protokoll (Roadmap 1c.0): nur bekannte Such- und
|
|
// KI-Dienste, erkannt am User-Agent, ohne IP.
|
|
$userAgent = (string) request()->userAgent();
|
|
$crawler = CrawlerIdentifier::identify($userAgent);
|
|
|
|
if ($crawler !== null) {
|
|
defer(static fn () => PressRelease::recordDeferredCrawlerHit($releaseId, $crawler, $userAgent));
|
|
}
|
|
|
|
// Die Antwort zeigt den aktuellen Aufruf bereits an; die atomaren
|
|
// Schreibzugriffe erfolgen nach dem Senden der Response.
|
|
$release->hits = ($release->hits ?? 0) + 1;
|
|
|
|
$with = [
|
|
'company',
|
|
'category.translations' => fn ($query) => $query->where('locale', $language),
|
|
'images' => fn ($query) => $query->orderByDesc('is_preview')->orderBy('sort_order')->limit(1),
|
|
];
|
|
|
|
$companyReleases = $release->company_id
|
|
? PressRelease::query()
|
|
->withoutGlobalScope(PortalScope::class)
|
|
->tap($publishedScope)
|
|
->where('company_id', $release->company_id)
|
|
->whereKeyNot($release->getKey())
|
|
->with($with)
|
|
->orderByDesc('published_at')
|
|
->limit(3)
|
|
->get()
|
|
: collect();
|
|
|
|
$relatedReleases = $release->category_id
|
|
? PressRelease::query()
|
|
->withoutGlobalScope(PortalScope::class)
|
|
->tap($publishedScope)
|
|
->where('category_id', $release->category_id)
|
|
->whereKeyNot($release->getKey())
|
|
->when($companyReleases->isNotEmpty(), fn (Builder $query) => $query->whereNotIn('id', $companyReleases->modelKeys()))
|
|
->with($with)
|
|
->orderByDesc('published_at')
|
|
->limit(3)
|
|
->get()
|
|
: collect();
|
|
|
|
return [
|
|
'release' => $release,
|
|
'companyReleases' => $companyReleases,
|
|
'relatedReleases' => $relatedReleases,
|
|
];
|
|
}
|
|
}
|