Files
huginn/app/Livewire/Discover/Search.php
T
m105 f5c9129774 Add authentication, a public landing page, and guest data preview
Hand-rolled login/register/logout on Laravel's built-in Auth facade
rather than Breeze/Jetstream, which scaffold Tailwind v3 config and
would fight our existing Tailwind v4 CSS-first setup. Login/Register
are Livewire components; logout is a plain POST route (auth-gated,
CSRF-protected).

Routing: "/" is now a public landing page describing the project;
the sites list moved from "/" to "/sites" to make room for it.

Data gating: App\Support\GuestPreview::paginate() forces page 1 and a
5-row page size for unauthenticated visitors on Sites, Discover, and
a site's Pages/Backlinks tables, while total() still reflects the
real match count — authenticated users get the existing full
pagination unchanged. A shared guest-preview-notice component shows
"N of M results, sign in to see all" and only renders when something
is actually being hidden.

Testing: pointed the test suite at a dedicated MySQL database
(huginn_testing) instead of the default in-memory SQLite, since the
existing fulltext index migration isn't SQLite-compatible — this app
is MySQL-specific by now regardless. Added AuthFlowTest (register,
login, wrong-password rejection, logout) and GuestPreviewTest (guest
capped to a preview, authenticated sees everything), all passing.

Also fixed a real bug the tests caught: request()->session() isn't
reliably attached during Livewire's in-process test action calls;
switched Login/Register to the session() helper, which resolves the
session store directly and works in both contexts.
2026-07-25 15:22:42 +03:00

108 lines
3.9 KiB
PHP

<?php
namespace App\Livewire\Discover;
use App\Models\Site;
use App\Support\GuestPreview;
use Illuminate\Contracts\View\View;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;
/**
* The competitor-discovery search: "who sells honey with a .es domain?" — keyword
* matches a site's pages via MySQL fulltext (natural language mode, so free-text input
* never trips a boolean-mode syntax error), TLD filters the site's domain suffix,
* platform filters its detected server/powered-by/generator signature, language
* filters by the CDX-reported ISO 639 code of a page's content (e.g. "spa", "eng").
*/
#[Layout('components.layouts.app')]
class Search extends Component
{
use WithPagination;
#[Url(as: 'q', history: true)]
public string $keyword = '';
#[Url(as: 'tld', history: true)]
public string $tld = '';
#[Url(as: 'platform', history: true)]
public string $platform = '';
#[Url(as: 'lang', history: true)]
public string $language = '';
public function updatingKeyword(): void
{
$this->resetPage();
}
public function updatingTld(): void
{
$this->resetPage();
}
public function updatingPlatform(): void
{
$this->resetPage();
}
public function updatingLanguage(): void
{
$this->resetPage();
}
public function render(): View
{
$keyword = trim($this->keyword);
$tld = strtolower(trim(ltrim($this->tld, '.')));
$platform = trim($this->platform);
$language = strtolower(trim($this->language));
$hasQuery = $keyword !== '' || $tld !== '' || $platform !== '' || $language !== '';
// Without any filter this would just be the full site list — that's what the
// Sites page is for. Discover only returns results once you've asked it something.
if (! $hasQuery) {
$sites = new LengthAwarePaginator([], 0, 25, 1, ['path' => request()->url()]);
} else {
$sites = GuestPreview::paginate(Site::query()
->when(
$keyword !== '',
fn ($query) => $query->whereHas(
'pages',
fn ($pageQuery) => $pageQuery->whereFullText(['title', 'description', 'keywords', 'content'], $keyword)
)
)
->when($tld !== '', fn ($query) => $query->where('domain', 'like', '%.'.$tld))
->when($platform !== '', function ($query) use ($platform) {
$query->where(function ($query) use ($platform) {
$query->where('server', 'like', '%'.$platform.'%')
->orWhere('powered_by', 'like', '%'.$platform.'%')
->orWhere('generator', 'like', '%'.$platform.'%');
});
})
->when(
$language !== '',
// languages is a comma-separated list of ISO 639 codes (e.g. "spa,eng") —
// FIND_IN_SET matches an exact code, not a LIKE substring match.
fn ($query) => $query->whereHas(
'pages',
fn ($pageQuery) => $pageQuery->whereRaw('FIND_IN_SET(?, languages)', [$language])
)
)
->withCount(['pages as matching_pages_count' => function ($pageQuery) use ($keyword) {
if ($keyword !== '') {
$pageQuery->whereFullText(['title', 'description', 'keywords', 'content'], $keyword);
}
}])
->orderByDesc('matching_pages_count')
->orderBy('domain'));
}
return view('livewire.discover.search', ['sites' => $sites, 'hasQuery' => $hasQuery]);
}
}