[search] get_images function to get specific images in specific order with search visibility (ratings etc) taken into account
This commit is contained in:
parent
f877ad0c92
commit
7673b394f4
2 changed files with 35 additions and 0 deletions
|
@ -104,6 +104,26 @@ class Search
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific set of images, in the order that the set specifies,
|
||||
* with all the search stuff (rating filters etc) taken into account
|
||||
*
|
||||
* @param int[] $ids
|
||||
* @return Image[]
|
||||
*/
|
||||
public static function get_images(array $ids): array
|
||||
{
|
||||
$visible_images = [];
|
||||
foreach(Search::find_images(tags: ["id=" . implode(",", $ids)]) as $image) {
|
||||
$visible_images[$image->id] = $image;
|
||||
}
|
||||
$visible_ids = array_keys($visible_images);
|
||||
|
||||
$visible_popular_ids = array_filter($ids, fn ($id) => in_array($id, $visible_ids));
|
||||
$images = array_map(fn ($id) => $visible_images[$id], $visible_popular_ids);
|
||||
return $images;
|
||||
}
|
||||
|
||||
/*
|
||||
* Image-related utility functions
|
||||
*/
|
||||
|
|
|
@ -487,4 +487,19 @@ class SearchTest extends ShimmiePHPUnitTestCase
|
|||
path: ["general", "some_positives"],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* get_images
|
||||
*/
|
||||
#[Depends('testUpload')]
|
||||
public function test_get_images()
|
||||
{
|
||||
$image_ids = $this->testUpload();
|
||||
|
||||
$res = Search::get_images($image_ids);
|
||||
$this->assertGreaterThan($res[0]->id, $res[1]->id);
|
||||
|
||||
$res = Search::get_images(array_reverse($image_ids));
|
||||
$this->assertLessThan($res[0]->id, $res[1]->id);
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue