Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(optimisation): chunk image query for content warning tags #1073

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 39 additions & 40 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) {
ScuffedNewt marked this conversation as resolved.
Show resolved Hide resolved
// 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')) {
Expand Down