feat(search): design consistency with other feed/content aggregation lists

This commit is contained in:
Cory Dransfeldt 2025-06-10 10:31:50 -07:00
parent f2bca309f5
commit 0caf857a7e
No known key found for this signature in database
9 changed files with 162 additions and 144 deletions

View file

@ -18,13 +18,13 @@ class SearchHandler extends BaseHandler
{
try {
$query = $this->validateAndSanitizeQuery($_GET["q"] ?? null);
$types = $this->validateAndSanitizeTypes($_GET["type"] ?? "");
$sections = $this->validateAndSanitizeSections($_GET["section"] ?? "");
$page = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
$pageSize = isset($_GET["pageSize"]) ? intval($_GET["pageSize"]) : 10;
$offset = ($page - 1) * $pageSize;
$cacheKey = $this->generateCacheKey($query, $types, $page, $pageSize);
$cacheKey = $this->generateCacheKey($query, $sections, $page, $pageSize);
$results = [];
$results = $this->getCachedResults($cacheKey) ?? $this->fetchSearchResults($query, $types, $pageSize, $offset);
$results = $this->getCachedResults($cacheKey) ?? $this->fetchSearchResults($query, $sections, $pageSize, $offset);
if (empty($results) || empty($results["data"])) {
$this->sendResponse(["results" => [], "total" => 0, "page" => $page, "pageSize" => $pageSize], 200);
@ -61,38 +61,38 @@ class SearchHandler extends BaseHandler
return $query;
}
private function validateAndSanitizeTypes(string $rawTypes): ?array
private function validateAndSanitizeSections(string $rawSections): ?array
{
$allowedTypes = ["post", "artist", "genre", "book", "movie", "show"];
$allowedSections = ["post", "artist", "genre", "book", "movie", "show"];
if (empty($rawTypes)) return null;
if (empty($rawSections)) return null;
$types = array_map(
fn($type) => strtolower(
trim(htmlspecialchars($type, ENT_QUOTES, "UTF-8"))
$sections = array_map(
fn($section) => strtolower(
trim(htmlspecialchars($section, ENT_QUOTES, "UTF-8"))
),
explode(",", $rawTypes)
explode(",", $rawSections)
);
$invalidTypes = array_diff($types, $allowedTypes);
$invalidSections = array_diff($sections, $allowedSections);
if (!empty($invalidTypes)) throw new Exception("Invalid 'type' parameter. Unsupported types: " . implode(", ", $invalidTypes));
if (!empty($invalidSections)) throw new Exception("Invalid 'section' parameter. Unsupported sections: " . implode(", ", $invalidSections));
return $types;
return $sections;
}
private function fetchSearchResults(
string $query,
?array $types,
?array $sections,
int $pageSize,
int $offset
): array {
$typesParam = $types && count($types) > 0 ? "%7B" . implode(",", $types) . "%7D" : "";
$sectionsParam = $sections && count($sections) > 0 ? "%7B" . implode(",", $sections) . "%7D" : "";
$endpoint = "rpc/search_optimized_index";
$queryString =
"search_query=" .
urlencode($query) .
"&page_size={$pageSize}&page_offset={$offset}" .
($typesParam ? "&types={$typesParam}" : "");
($sectionsParam ? "&sections={$sectionsParam}" : "");
$data = $this->makeRequest("GET", "{$endpoint}?{$queryString}");
$total = count($data) > 0 ? $data[0]["total_count"] : 0;
$results = array_map(function ($item) {
@ -105,16 +105,16 @@ class SearchHandler extends BaseHandler
private function generateCacheKey(
string $query,
?array $types,
?array $sections,
int $page,
int $pageSize
): string {
$typesKey = $types ? implode(",", $types) : "all";
$sectionsKey = $sections ? implode(",", $sections) : "all";
return sprintf(
"search:%s:types:%s:page:%d:pageSize:%d",
"search:%s:sections:%s:page:%d:pageSize:%d",
md5($query),
$typesKey,
$sectionsKey,
$page,
$pageSize
);