diff --git a/app/Http/Controllers/BrowseController.php b/app/Http/Controllers/BrowseController.php index 46bd765f4..54953c7b2 100644 --- a/app/Http/Controllers/BrowseController.php +++ b/app/Http/Controllers/BrowseController.php @@ -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 (in_array('all', $excludedTags)) { + return true; + } + foreach ($excludedTags as $tag) { + if (in_array($tag, $image->content_warnings)) { + return true; + } + } - if ($request->get('included_tags')) { - $filteredImageQuery = $filteredImageQuery->filter(function ($image) use ($includedTags) { - if (!$image->content_warnings) { 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; } - } + foreach ($includedTags as $tag) { + if (in_array($tag, $image->content_warnings)) { + return true; + } + } - return false; - }); - } + 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')) {