aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Jellyfin.Api/Controllers/AlbumsController.cs133
-rw-r--r--Jellyfin.Api/Controllers/CollectionController.cs110
-rw-r--r--Jellyfin.Api/Controllers/LibraryController.cs2
-rw-r--r--Jellyfin.Api/Helpers/SimilarItemsHelper.cs182
-rw-r--r--MediaBrowser.Api/Movies/CollectionService.cs105
-rw-r--r--MediaBrowser.Api/Music/AlbumsService.cs132
6 files changed, 427 insertions, 237 deletions
diff --git a/Jellyfin.Api/Controllers/AlbumsController.cs b/Jellyfin.Api/Controllers/AlbumsController.cs
new file mode 100644
index 000000000..622123873
--- /dev/null
+++ b/Jellyfin.Api/Controllers/AlbumsController.cs
@@ -0,0 +1,133 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Jellyfin.Api.Extensions;
+using Jellyfin.Api.Helpers;
+using MediaBrowser.Controller.Dto;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.Audio;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Dto;
+using MediaBrowser.Model.Querying;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+
+namespace Jellyfin.Api.Controllers
+{
+ /// <summary>
+ /// The albums controller.
+ /// </summary>
+ public class AlbumsController : BaseJellyfinApiController
+ {
+ private readonly IUserManager _userManager;
+ private readonly ILibraryManager _libraryManager;
+ private readonly IDtoService _dtoService;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="AlbumsController"/> class.
+ /// </summary>
+ /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
+ /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
+ /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
+ public AlbumsController(
+ IUserManager userManager,
+ ILibraryManager libraryManager,
+ IDtoService dtoService)
+ {
+ _userManager = userManager;
+ _libraryManager = libraryManager;
+ _dtoService = dtoService;
+ }
+
+ /// <summary>
+ /// Finds albums similar to a given album.
+ /// </summary>
+ /// <param name="albumId">The album id.</param>
+ /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
+ /// <param name="excludeArtistIds">Optional. Ids of artists to exclude.</param>
+ /// <param name="limit">Optional. The maximum number of records to return.</param>
+ /// <response code="200">Similar albums returned.</response>
+ /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with similar albums.</returns>
+ [HttpGet("/Albums/{albumId}/Similar")]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ public ActionResult<QueryResult<BaseItemDto>> GetSimilarAlbums(
+ [FromRoute] string albumId,
+ [FromQuery] Guid userId,
+ [FromQuery] string excludeArtistIds,
+ [FromQuery] int? limit)
+ {
+ var dtoOptions = new DtoOptions().AddClientFields(Request);
+
+ return SimilarItemsHelper.GetSimilarItemsResult(
+ dtoOptions,
+ _userManager,
+ _libraryManager,
+ _dtoService,
+ userId,
+ albumId,
+ excludeArtistIds,
+ limit,
+ new[] { typeof(MusicAlbum) },
+ GetAlbumSimilarityScore);
+ }
+
+ /// <summary>
+ /// Finds artists similar to a given artist.
+ /// </summary>
+ /// <param name="artistId">The artist id.</param>
+ /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
+ /// <param name="excludeArtistIds">Optional. Ids of artists to exclude.</param>
+ /// <param name="limit">Optional. The maximum number of records to return.</param>
+ /// <response code="200">Similar artists returned.</response>
+ /// <returns>A <see cref="QueryResult{BaseItemDto}"/> with similar artists.</returns>
+ [HttpGet("/Artists/{artistId}/Similar")]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ public ActionResult<QueryResult<BaseItemDto>> GetSimilarArtists(
+ [FromRoute] string artistId,
+ [FromQuery] Guid userId,
+ [FromQuery] string excludeArtistIds,
+ [FromQuery] int? limit)
+ {
+ var dtoOptions = new DtoOptions().AddClientFields(Request);
+
+ return SimilarItemsHelper.GetSimilarItemsResult(
+ dtoOptions,
+ _userManager,
+ _libraryManager,
+ _dtoService,
+ userId,
+ artistId,
+ excludeArtistIds,
+ limit,
+ new[] { typeof(MusicArtist) },
+ SimilarItemsHelper.GetSimiliarityScore);
+ }
+
+ /// <summary>
+ /// Gets a similairty score of two albums.
+ /// </summary>
+ /// <param name="item1">The first item.</param>
+ /// <param name="item1People">The item1 people.</param>
+ /// <param name="allPeople">All people.</param>
+ /// <param name="item2">The second item.</param>
+ /// <returns>System.Int32.</returns>
+ private int GetAlbumSimilarityScore(BaseItem item1, List<PersonInfo> item1People, List<PersonInfo> allPeople, BaseItem item2)
+ {
+ var points = SimilarItemsHelper.GetSimiliarityScore(item1, item1People, allPeople, item2);
+
+ var album1 = (MusicAlbum)item1;
+ var album2 = (MusicAlbum)item2;
+
+ var artists1 = album1
+ .GetAllArtists()
+ .DistinctNames()
+ .ToList();
+
+ var artists2 = new HashSet<string>(
+ album2.GetAllArtists().DistinctNames(),
+ StringComparer.OrdinalIgnoreCase);
+
+ return points + artists1.Where(artists2.Contains).Sum(i => 5);
+ }
+ }
+}
diff --git a/Jellyfin.Api/Controllers/CollectionController.cs b/Jellyfin.Api/Controllers/CollectionController.cs
new file mode 100644
index 000000000..29db0b178
--- /dev/null
+++ b/Jellyfin.Api/Controllers/CollectionController.cs
@@ -0,0 +1,110 @@
+using System;
+using Jellyfin.Api.Constants;
+using Jellyfin.Api.Extensions;
+using Jellyfin.Api.Helpers;
+using MediaBrowser.Controller.Collections;
+using MediaBrowser.Controller.Dto;
+using MediaBrowser.Controller.Net;
+using MediaBrowser.Model.Collections;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+
+namespace Jellyfin.Api.Controllers
+{
+ /// <summary>
+ /// The collection controller.
+ /// </summary>
+ [Authorize(Policy = Policies.DefaultAuthorization)]
+ [Route("/Collections")]
+ public class CollectionController : BaseJellyfinApiController
+ {
+ private readonly ICollectionManager _collectionManager;
+ private readonly IDtoService _dtoService;
+ private readonly IAuthorizationContext _authContext;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="CollectionController"/> class.
+ /// </summary>
+ /// <param name="collectionManager">Instance of <see cref="ICollectionManager"/> interface.</param>
+ /// <param name="dtoService">Instance of <see cref="IDtoService"/> interface.</param>
+ /// <param name="authContext">Instance of <see cref="IAuthorizationContext"/> interface.</param>
+ public CollectionController(
+ ICollectionManager collectionManager,
+ IDtoService dtoService,
+ IAuthorizationContext authContext)
+ {
+ _collectionManager = collectionManager;
+ _dtoService = dtoService;
+ _authContext = authContext;
+ }
+
+ /// <summary>
+ /// Creates a new collection.
+ /// </summary>
+ /// <param name="name">The name of the collection.</param>
+ /// <param name="ids">Item Ids to add to the collection.</param>
+ /// <param name="isLocked">Whether or not to lock the new collection.</param>
+ /// <param name="parentId">Optional. Create the collection within a specific folder.</param>
+ /// <response code="200">Collection created.</response>
+ /// <returns>A <see cref="CollectionCreationOptions"/> with information about the new collection.</returns>
+ [HttpPost]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ public ActionResult<CollectionCreationResult> CreateCollection(
+ [FromQuery] string name,
+ [FromQuery] string ids,
+ [FromQuery] bool isLocked,
+ [FromQuery] Guid? parentId)
+ {
+ var userId = _authContext.GetAuthorizationInfo(Request).UserId;
+
+ var item = _collectionManager.CreateCollection(new CollectionCreationOptions
+ {
+ IsLocked = isLocked,
+ Name = name,
+ ParentId = parentId,
+ ItemIdList = RequestHelpers.Split(ids, ',', true),
+ UserIds = new[] { userId }
+ });
+
+ var dtoOptions = new DtoOptions().AddClientFields(Request);
+
+ var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
+
+ return new CollectionCreationResult
+ {
+ Id = dto.Id
+ };
+ }
+
+ /// <summary>
+ /// Adds items to a collection.
+ /// </summary>
+ /// <param name="collectionId">The collection id.</param>
+ /// <param name="itemIds">Item ids, comma delimited.</param>
+ /// <response code="204">Items added to collection.</response>
+ /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
+ [HttpPost("{collectionId}/Items")]
+ [ProducesResponseType(StatusCodes.Status204NoContent)]
+ public ActionResult AddToCollection([FromRoute] Guid collectionId, [FromQuery] string itemIds)
+ {
+ _collectionManager.AddToCollection(collectionId, RequestHelpers.Split(itemIds, ',', true));
+ return NoContent();
+ }
+
+ /// <summary>
+ /// Removes items from a collection.
+ /// </summary>
+ /// <param name="collectionId">The collection id.</param>
+ /// <param name="itemIds">Item ids, comma delimited.</param>
+ /// <response code="204">Items removed from collection.</response>
+ /// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
+ [HttpDelete("{collectionId}/Items")]
+ [ProducesResponseType(StatusCodes.Status204NoContent)]
+ public ActionResult RemoveFromCollection([FromRoute] Guid collectionId, [FromQuery] string itemIds)
+ {
+ _collectionManager.RemoveFromCollection(collectionId, RequestHelpers.Split(itemIds, ',', true));
+ return NoContent();
+ }
+ }
+}
diff --git a/Jellyfin.Api/Controllers/LibraryController.cs b/Jellyfin.Api/Controllers/LibraryController.cs
index 8c7688815..1ecf2ac73 100644
--- a/Jellyfin.Api/Controllers/LibraryController.cs
+++ b/Jellyfin.Api/Controllers/LibraryController.cs
@@ -272,6 +272,8 @@ namespace Jellyfin.Api.Controllers
/// <response code="200">Theme songs and videos returned.</response>
/// <response code="404">Item not found.</response>
/// <returns>The item theme videos.</returns>
+ [HttpGet("/Items/{itemId}/ThemeMedia")]
+ [Authorize(Policy = Policies.DefaultAuthorization)]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<AllThemeMediaResult> GetThemeMedia(
[FromRoute] Guid itemId,
diff --git a/Jellyfin.Api/Helpers/SimilarItemsHelper.cs b/Jellyfin.Api/Helpers/SimilarItemsHelper.cs
new file mode 100644
index 000000000..751e3c481
--- /dev/null
+++ b/Jellyfin.Api/Helpers/SimilarItemsHelper.cs
@@ -0,0 +1,182 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using MediaBrowser.Controller.Dto;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Persistence;
+using MediaBrowser.Model.Dto;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Querying;
+
+namespace Jellyfin.Api.Helpers
+{
+ /// <summary>
+ /// The similar items helper class.
+ /// </summary>
+ public static class SimilarItemsHelper
+ {
+ internal static QueryResult<BaseItemDto> GetSimilarItemsResult(
+ DtoOptions dtoOptions,
+ IUserManager userManager,
+ ILibraryManager libraryManager,
+ IDtoService dtoService,
+ Guid userId,
+ string id,
+ string excludeArtistIds,
+ int? limit,
+ Type[] includeTypes,
+ Func<BaseItem, List<PersonInfo>, List<PersonInfo>, BaseItem, int> getSimilarityScore)
+ {
+ var user = !userId.Equals(Guid.Empty) ? userManager.GetUserById(userId) : null;
+
+ var item = string.IsNullOrEmpty(id) ?
+ (!userId.Equals(Guid.Empty) ? libraryManager.GetUserRootFolder() :
+ libraryManager.RootFolder) : libraryManager.GetItemById(id);
+
+ var query = new InternalItemsQuery(user)
+ {
+ IncludeItemTypes = includeTypes.Select(i => i.Name).ToArray(),
+ Recursive = true,
+ DtoOptions = dtoOptions
+ };
+
+ query.ExcludeArtistIds = RequestHelpers.GetGuids(excludeArtistIds);
+
+ var inputItems = libraryManager.GetItemList(query);
+
+ var items = GetSimilaritems(item, libraryManager, inputItems, getSimilarityScore)
+ .ToList();
+
+ var returnItems = items;
+
+ if (limit.HasValue)
+ {
+ returnItems = returnItems.Take(limit.Value).ToList();
+ }
+
+ var dtos = dtoService.GetBaseItemDtos(returnItems, dtoOptions, user);
+
+ return new QueryResult<BaseItemDto>
+ {
+ Items = dtos,
+ TotalRecordCount = items.Count
+ };
+ }
+
+ /// <summary>
+ /// Gets the similaritems.
+ /// </summary>
+ /// <param name="item">The item.</param>
+ /// <param name="libraryManager">The library manager.</param>
+ /// <param name="inputItems">The input items.</param>
+ /// <param name="getSimilarityScore">The get similarity score.</param>
+ /// <returns>IEnumerable{BaseItem}.</returns>
+ private static IEnumerable<BaseItem> GetSimilaritems(
+ BaseItem item,
+ ILibraryManager libraryManager,
+ IEnumerable<BaseItem> inputItems,
+ Func<BaseItem, List<PersonInfo>, List<PersonInfo>, BaseItem, int> getSimilarityScore)
+ {
+ var itemId = item.Id;
+ inputItems = inputItems.Where(i => i.Id != itemId);
+ var itemPeople = libraryManager.GetPeople(item);
+ var allPeople = libraryManager.GetPeople(new InternalPeopleQuery
+ {
+ AppearsInItemId = item.Id
+ });
+
+ return inputItems.Select(i => new Tuple<BaseItem, int>(i, getSimilarityScore(item, itemPeople, allPeople, i)))
+ .Where(i => i.Item2 > 2)
+ .OrderByDescending(i => i.Item2)
+ .Select(i => i.Item1);
+ }
+
+ private static IEnumerable<string> GetTags(BaseItem item)
+ {
+ return item.Tags;
+ }
+
+ /// <summary>
+ /// Gets the similiarity score.
+ /// </summary>
+ /// <param name="item1">The item1.</param>
+ /// <param name="item1People">The item1 people.</param>
+ /// <param name="allPeople">All people.</param>
+ /// <param name="item2">The item2.</param>
+ /// <returns>System.Int32.</returns>
+ internal static int GetSimiliarityScore(BaseItem item1, List<PersonInfo> item1People, List<PersonInfo> allPeople, BaseItem item2)
+ {
+ var points = 0;
+
+ if (!string.IsNullOrEmpty(item1.OfficialRating) && string.Equals(item1.OfficialRating, item2.OfficialRating, StringComparison.OrdinalIgnoreCase))
+ {
+ points += 10;
+ }
+
+ // Find common genres
+ points += item1.Genres.Where(i => item2.Genres.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10);
+
+ // Find common tags
+ points += GetTags(item1).Where(i => GetTags(item2).Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 10);
+
+ // Find common studios
+ points += item1.Studios.Where(i => item2.Studios.Contains(i, StringComparer.OrdinalIgnoreCase)).Sum(i => 3);
+
+ var item2PeopleNames = allPeople.Where(i => i.ItemId == item2.Id)
+ .Select(i => i.Name)
+ .Where(i => !string.IsNullOrWhiteSpace(i))
+ .DistinctNames()
+ .ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
+
+ points += item1People.Where(i => item2PeopleNames.ContainsKey(i.Name)).Sum(i =>
+ {
+ if (string.Equals(i.Type, PersonType.Director, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Director, StringComparison.OrdinalIgnoreCase))
+ {
+ return 5;
+ }
+
+ if (string.Equals(i.Type, PersonType.Actor, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Actor, StringComparison.OrdinalIgnoreCase))
+ {
+ return 3;
+ }
+
+ if (string.Equals(i.Type, PersonType.Composer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Composer, StringComparison.OrdinalIgnoreCase))
+ {
+ return 3;
+ }
+
+ if (string.Equals(i.Type, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.GuestStar, StringComparison.OrdinalIgnoreCase))
+ {
+ return 3;
+ }
+
+ if (string.Equals(i.Type, PersonType.Writer, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Role, PersonType.Writer, StringComparison.OrdinalIgnoreCase))
+ {
+ return 2;
+ }
+
+ return 1;
+ });
+
+ if (item1.ProductionYear.HasValue && item2.ProductionYear.HasValue)
+ {
+ var diff = Math.Abs(item1.ProductionYear.Value - item2.ProductionYear.Value);
+
+ // Add if they came out within the same decade
+ if (diff < 10)
+ {
+ points += 2;
+ }
+
+ // And more if within five years
+ if (diff < 5)
+ {
+ points += 2;
+ }
+ }
+
+ return points;
+ }
+ }
+}
diff --git a/MediaBrowser.Api/Movies/CollectionService.cs b/MediaBrowser.Api/Movies/CollectionService.cs
deleted file mode 100644
index 95a37dfc5..000000000
--- a/MediaBrowser.Api/Movies/CollectionService.cs
+++ /dev/null
@@ -1,105 +0,0 @@
-using System;
-using MediaBrowser.Controller.Collections;
-using MediaBrowser.Controller.Configuration;
-using MediaBrowser.Controller.Dto;
-using MediaBrowser.Controller.Net;
-using MediaBrowser.Model.Collections;
-using MediaBrowser.Model.Services;
-using Microsoft.Extensions.Logging;
-
-namespace MediaBrowser.Api.Movies
-{
- [Route("/Collections", "POST", Summary = "Creates a new collection")]
- public class CreateCollection : IReturn<CollectionCreationResult>
- {
- [ApiMember(Name = "IsLocked", Description = "Whether or not to lock the new collection.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
- public bool IsLocked { get; set; }
-
- [ApiMember(Name = "Name", Description = "The name of the new collection.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
- public string Name { get; set; }
-
- [ApiMember(Name = "ParentId", Description = "Optional - create the collection within a specific folder", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
- public string ParentId { get; set; }
-
- [ApiMember(Name = "Ids", Description = "Item Ids to add to the collection", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
- public string Ids { get; set; }
- }
-
- [Route("/Collections/{Id}/Items", "POST", Summary = "Adds items to a collection")]
- public class AddToCollection : IReturnVoid
- {
- [ApiMember(Name = "Ids", Description = "Item id, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
- public string Ids { get; set; }
-
- [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
- public string Id { get; set; }
- }
-
- [Route("/Collections/{Id}/Items", "DELETE", Summary = "Removes items from a collection")]
- public class RemoveFromCollection : IReturnVoid
- {
- [ApiMember(Name = "Ids", Description = "Item id, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
- public string Ids { get; set; }
-
- [ApiMember(Name = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
- public string Id { get; set; }
- }
-
- [Authenticated]
- public class CollectionService : BaseApiService
- {
- private readonly ICollectionManager _collectionManager;
- private readonly IDtoService _dtoService;
- private readonly IAuthorizationContext _authContext;
-
- public CollectionService(
- ILogger<CollectionService> logger,
- IServerConfigurationManager serverConfigurationManager,
- IHttpResultFactory httpResultFactory,
- ICollectionManager collectionManager,
- IDtoService dtoService,
- IAuthorizationContext authContext)
- : base(logger, serverConfigurationManager, httpResultFactory)
- {
- _collectionManager = collectionManager;
- _dtoService = dtoService;
- _authContext = authContext;
- }
-
- public object Post(CreateCollection request)
- {
- var userId = _authContext.GetAuthorizationInfo(Request).UserId;
-
- var parentId = string.IsNullOrWhiteSpace(request.ParentId) ? (Guid?)null : new Guid(request.ParentId);
-
- var item = _collectionManager.CreateCollection(new CollectionCreationOptions
- {
- IsLocked = request.IsLocked,
- Name = request.Name,
- ParentId = parentId,
- ItemIdList = SplitValue(request.Ids, ','),
- UserIds = new[] { userId }
-
- });
-
- var dtoOptions = GetDtoOptions(_authContext, request);
-
- var dto = _dtoService.GetBaseItemDto(item, dtoOptions);
-
- return new CollectionCreationResult
- {
- Id = dto.Id
- };
- }
-
- public void Post(AddToCollection request)
- {
- _collectionManager.AddToCollection(new Guid(request.Id), SplitValue(request.Ids, ','));
- }
-
- public void Delete(RemoveFromCollection request)
- {
- _collectionManager.RemoveFromCollection(new Guid(request.Id), SplitValue(request.Ids, ','));
- }
- }
-}
diff --git a/MediaBrowser.Api/Music/AlbumsService.cs b/MediaBrowser.Api/Music/AlbumsService.cs
deleted file mode 100644
index f257d1014..000000000
--- a/MediaBrowser.Api/Music/AlbumsService.cs
+++ /dev/null
@@ -1,132 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using MediaBrowser.Controller.Configuration;
-using MediaBrowser.Controller.Dto;
-using MediaBrowser.Controller.Entities;
-using MediaBrowser.Controller.Entities.Audio;
-using MediaBrowser.Controller.Library;
-using MediaBrowser.Controller.Net;
-using MediaBrowser.Controller.Persistence;
-using MediaBrowser.Model.Services;
-using Microsoft.Extensions.Logging;
-
-namespace MediaBrowser.Api.Music
-{
- [Route("/Albums/{Id}/Similar", "GET", Summary = "Finds albums similar to a given album.")]
- public class GetSimilarAlbums : BaseGetSimilarItemsFromItem
- {
- }
-
- [Route("/Artists/{Id}/Similar", "GET", Summary = "Finds albums similar to a given album.")]
- public class GetSimilarArtists : BaseGetSimilarItemsFromItem
- {
- }
-
- [Authenticated]
- public class AlbumsService : BaseApiService
- {
- /// <summary>
- /// The _user manager
- /// </summary>
- private readonly IUserManager _userManager;
-
- /// <summary>
- /// The _user data repository
- /// </summary>
- private readonly IUserDataManager _userDataRepository;
- /// <summary>
- /// The _library manager
- /// </summary>
- private readonly ILibraryManager _libraryManager;
- private readonly IItemRepository _itemRepo;
- private readonly IDtoService _dtoService;
- private readonly IAuthorizationContext _authContext;
-
- public AlbumsService(
- ILogger<AlbumsService> logger,
- IServerConfigurationManager serverConfigurationManager,
- IHttpResultFactory httpResultFactory,
- IUserManager userManager,
- IUserDataManager userDataRepository,
- ILibraryManager libraryManager,
- IItemRepository itemRepo,
- IDtoService dtoService,
- IAuthorizationContext authContext)
- : base(logger, serverConfigurationManager, httpResultFactory)
- {
- _userManager = userManager;
- _userDataRepository = userDataRepository;
- _libraryManager = libraryManager;
- _itemRepo = itemRepo;
- _dtoService = dtoService;
- _authContext = authContext;
- }
-
- public object Get(GetSimilarArtists request)
- {
- var dtoOptions = GetDtoOptions(_authContext, request);
-
- var result = SimilarItemsHelper.GetSimilarItemsResult(
- dtoOptions,
- _userManager,
- _itemRepo,
- _libraryManager,
- _userDataRepository,
- _dtoService,
- request, new[] { typeof(MusicArtist) },
- SimilarItemsHelper.GetSimiliarityScore);
-
- return ToOptimizedResult(result);
- }
-
- /// <summary>
- /// Gets the specified request.
- /// </summary>
- /// <param name="request">The request.</param>
- /// <returns>System.Object.</returns>
- public object Get(GetSimilarAlbums request)
- {
- var dtoOptions = GetDtoOptions(_authContext, request);
-
- var result = SimilarItemsHelper.GetSimilarItemsResult(
- dtoOptions,
- _userManager,
- _itemRepo,
- _libraryManager,
- _userDataRepository,
- _dtoService,
- request, new[] { typeof(MusicAlbum) },
- GetAlbumSimilarityScore);
-
- return ToOptimizedResult(result);
- }
-
- /// <summary>
- /// Gets the album similarity score.
- /// </summary>
- /// <param name="item1">The item1.</param>
- /// <param name="item1People">The item1 people.</param>
- /// <param name="allPeople">All people.</param>
- /// <param name="item2">The item2.</param>
- /// <returns>System.Int32.</returns>
- private int GetAlbumSimilarityScore(BaseItem item1, List<PersonInfo> item1People, List<PersonInfo> allPeople, BaseItem item2)
- {
- var points = SimilarItemsHelper.GetSimiliarityScore(item1, item1People, allPeople, item2);
-
- var album1 = (MusicAlbum)item1;
- var album2 = (MusicAlbum)item2;
-
- var artists1 = album1
- .GetAllArtists()
- .DistinctNames()
- .ToList();
-
- var artists2 = new HashSet<string>(
- album2.GetAllArtists().DistinctNames(),
- StringComparer.OrdinalIgnoreCase);
-
- return points + artists1.Where(artists2.Contains).Sum(i => 5);
- }
- }
-}