Skip to content

Commit

Permalink
feat: more query options for the APIv3 random image metadata endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Dec 26, 2024
1 parent 7c76681 commit fb60709
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions Shoko.Server/API/v3/Controllers/ImageController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Shoko.Commons.Extensions;
using Shoko.Models.Enums;
using Shoko.Plugin.Abstractions.Enums;
using Shoko.Server.API.Annotations;
using Shoko.Server.API.ModelBinders;
using Shoko.Server.API.v3.Helpers;
using Shoko.Server.API.v3.Models.Common;
using Shoko.Server.API.v3.Models.Shoko;
using Shoko.Server.Repositories;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;
Expand Down Expand Up @@ -143,12 +148,20 @@ public ActionResult GetRandomImageForType([FromRoute] Image.ImageType imageType)
/// Returns the metadata for a random image for the <paramref name="imageType"/>.
/// </summary>
/// <param name="imageType">Poster, Backdrop, Banner, Thumb</param>
/// <param name="includeRestricted">Include or exclude restricted images</param>
/// <param name="seriesType">Series types to include in the search</param>
/// <param name="maxAttempts">Maximum number of attempts to find a valid image</param>
/// <returns>200 on found, 400 if the type or source are invalid</returns>
[HttpGet("Random/{imageType}/Metadata")]
[ProducesResponseType(typeof(Image), 200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public ActionResult<Image> GetRandomImageMetadataForType([FromRoute] Image.ImageType imageType)
public ActionResult<Image> GetRandomImageMetadataForType(
[FromRoute] Image.ImageType imageType,
[FromQuery] IncludeOnlyFilter includeRestricted = IncludeOnlyFilter.False,
[FromQuery, ModelBinder(typeof(CommaDelimitedModelBinder))] HashSet<SeriesType> seriesType = null,
[FromQuery, Range(0, 100)] int maxAttempts = 5
)
{
if (imageType == Image.ImageType.Avatar)
return ValidationProblem("Unsupported image type for random image.", "imageType");
Expand All @@ -171,13 +184,23 @@ public ActionResult<Image> GetRandomImageMetadataForType([FromRoute] Image.Image

var image = new Image(metadata);
var series = ImageUtils.GetFirstSeriesForImage(metadata);
if (series == null || (series.AniDB_Anime?.IsRestricted ?? false))
if (series.AniDB_Anime is not { } anime)
continue;

if (includeRestricted != IncludeOnlyFilter.True)
{
var onlyRestricted = includeRestricted is IncludeOnlyFilter.Only;
if (onlyRestricted != anime.IsRestricted)
continue;
}

if (seriesType is not null && !seriesType.Contains(anime.GetAnimeTypeEnum().ToAniDBSeriesType()))
continue;

image.Series = new(series.AnimeSeriesID, series.PreferredTitle);

return image;
} while (tries++ < 5);
} while (tries++ < maxAttempts);

return InternalError("Unable to find a random image to send.");
}
Expand Down

0 comments on commit fb60709

Please sign in to comment.