Skip to content

Commit

Permalink
feat(optimisation): chunk image query for content warning tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Sep 25, 2024
1 parent f23e09d commit 1998e15
Showing 1 changed file with 42 additions and 43 deletions.
85 changes: 42 additions & 43 deletions app/Http/Controllers/BrowseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,57 +397,56 @@ private function handleMasterlistSearch(Request $request, $query, $imageQuery, $
});
}
if ($request->get('excluded_tags') || $request->get('included_tags')) {
$excludedTags = $request->get('excluded_tags');
$includedTags = $request->get('included_tags');
$images = $imageQuery->get();

$filteredImageQuery = $images;

// first exclude, then include
if ($excludedTags) {
$filteredImageQuery = $images->filter(function ($image) use ($excludedTags) {
if (!$image->content_warnings) {
return true;
}

if (in_array('all', $excludedTags) && $image->content_warnings) {
return false;
}

foreach ($excludedTags as $tag) {
if ($image->content_warnings && in_array($tag, $image->content_warnings)) {
$filteredImageIds = collect();
$excludedTags = $request->get('excluded_tags', []);
$includedTags = $request->get('included_tags', []);

$imageQuery->chunk(100, function ($images) use (&$filteredImageIds, $excludedTags, $includedTags) {
// excluded tags
if (!empty($excludedTags)) {
$images = $images->reject(function ($image) use ($excludedTags) {
if (!$image->content_warnings) {
return false;
}
}

return true;
});
}

if ($request->get('included_tags')) {
$filteredImageQuery = $filteredImageQuery->filter(function ($image) use ($includedTags) {
if (!$image->content_warnings) {
if (in_array('all', $excludedTags)) {
return true;
}
foreach ($excludedTags as $tag) {
if (in_array($tag, $image->content_warnings)) {
return true;
}
}

return false;
}

if (in_array('all', $includedTags) && $image->content_warnings) {
return true;
}

foreach ($includedTags as $tag) {
if ($image->content_warnings && in_array($tag, $image->content_warnings)) {
});
}

// included tags
if (!empty($includedTags)) {
$images = $images->filter(function ($image) use ($includedTags) {
if (!$image->content_warnings) {
return false;
}
if (in_array('all', $includedTags)) {
return true;
}
}

return false;
});
}
foreach ($includedTags as $tag) {
if (in_array($tag, $image->content_warnings)) {
return true;
}
}

return false;
});
}

$filteredImageIds = $filteredImageIds->merge($images->pluck('character_id'));
});
} else {
$filteredImageQuery = $imageQuery;
$filteredImageIds = $imageQuery->pluck('character_id');
}

$query->whereIn('id', $filteredImageQuery->pluck('character_id')->toArray());
$query->whereIn('id', $filteredImageIds->toArray());

if (!$isMyo) {
if ($request->get('is_gift_art_allowed')) {
Expand Down

0 comments on commit 1998e15

Please sign in to comment.