| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 2 | | #pragma warning disable CA1304 // Specify CultureInfo |
| | | 3 | | #pragma warning disable CA1311 // Specify a culture or use an invariant version |
| | | 4 | | #pragma warning disable CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string compari |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Linq.Expressions; |
| | | 9 | | using Jellyfin.Data.Enums; |
| | | 10 | | using Jellyfin.Database.Implementations; |
| | | 11 | | using Jellyfin.Database.Implementations.Entities; |
| | | 12 | | using Jellyfin.Extensions; |
| | | 13 | | using MediaBrowser.Controller.Entities; |
| | | 14 | | using Microsoft.EntityFrameworkCore; |
| | | 15 | | |
| | | 16 | | namespace Jellyfin.Server.Implementations.Item; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Static class for methods which maps types of ordering to their respecting ordering functions. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class OrderMapper |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates Func to be executed later with a given BaseItemEntity input for sorting items on query. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="sortBy">Item property to sort by.</param> |
| | | 27 | | /// <param name="query">Context Query.</param> |
| | | 28 | | /// <param name="jellyfinDbContext">Context.</param> |
| | | 29 | | /// <returns>Func to be executed later for sorting query.</returns> |
| | | 30 | | public static Expression<Func<BaseItemEntity, object?>> MapOrderByField(ItemSortBy sortBy, InternalItemsQuery query, |
| | | 31 | | { |
| | 154 | 32 | | return (sortBy, query.User) switch |
| | 154 | 33 | | { |
| | 0 | 34 | | (ItemSortBy.AirTime, _) => e => e.SortName, |
| | 0 | 35 | | (ItemSortBy.Runtime, _) => e => e.RunTimeTicks, |
| | 62 | 36 | | (ItemSortBy.Random, _) => e => EF.Functions.Random(), |
| | 1 | 37 | | (ItemSortBy.DatePlayed, _) => e => e.UserData!.Where(f => f.UserId.Equals(query.User!.Id)).OrderBy(f => f.Cu |
| | 0 | 38 | | (ItemSortBy.PlayCount, _) => e => e.UserData!.Where(f => f.UserId.Equals(query.User!.Id)).OrderBy(f => f.Cus |
| | 0 | 39 | | (ItemSortBy.IsFavoriteOrLiked, _) => e => e.UserData!.Where(f => f.UserId.Equals(query.User!.Id)).OrderBy(f |
| | 42 | 40 | | (ItemSortBy.IsFolder, _) => e => e.IsFolder, |
| | 0 | 41 | | (ItemSortBy.IsPlayed, _) => e => e.UserData!.Where(f => f.UserId.Equals(query.User!.Id)).OrderBy(f => f.Cust |
| | 0 | 42 | | (ItemSortBy.IsUnplayed, _) => e => !e.UserData!.Where(f => f.UserId.Equals(query.User!.Id)).OrderBy(f => f.C |
| | 0 | 43 | | (ItemSortBy.DateLastContentAdded, _) => e => e.DateLastMediaAdded, |
| | 0 | 44 | | (ItemSortBy.Artist, _) => e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.Artist).OrderBy(f |
| | 0 | 45 | | (ItemSortBy.AlbumArtist, _) => e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.AlbumArtist). |
| | 0 | 46 | | (ItemSortBy.Studio, _) => e => e.ItemValues!.Where(f => f.ItemValue.Type == ItemValueType.Studios).OrderBy(f |
| | 0 | 47 | | (ItemSortBy.OfficialRating, _) => e => e.InheritedParentalRatingValue, |
| | 0 | 48 | | (ItemSortBy.SeriesSortName, _) => e => e.SeriesName, |
| | 0 | 49 | | (ItemSortBy.Album, _) => e => e.Album, |
| | 0 | 50 | | (ItemSortBy.DateCreated, _) => e => e.DateCreated, |
| | 1 | 51 | | (ItemSortBy.PremiereDate, _) => e => (e.PremiereDate ?? (e.ProductionYear.HasValue ? DateTime.MinValue.AddYe |
| | 0 | 52 | | (ItemSortBy.StartDate, _) => e => e.StartDate, |
| | 0 | 53 | | (ItemSortBy.Name, _) => e => e.CleanName, |
| | 0 | 54 | | (ItemSortBy.CommunityRating, _) => e => e.CommunityRating, |
| | 0 | 55 | | (ItemSortBy.ProductionYear, _) => e => e.ProductionYear, |
| | 0 | 56 | | (ItemSortBy.CriticRating, _) => e => e.CriticRating, |
| | 0 | 57 | | (ItemSortBy.VideoBitRate, _) => e => e.TotalBitrate, |
| | 0 | 58 | | (ItemSortBy.ParentIndexNumber, _) => e => e.ParentIndexNumber, |
| | 0 | 59 | | (ItemSortBy.IndexNumber, _) => e => e.IndexNumber, |
| | 154 | 60 | | // SeriesDatePlayed is normally handled via pre-aggregated join in ApplySeriesDatePlayedOrder. |
| | 154 | 61 | | // This correlated subquery fallback is only reached when combined with search. |
| | 0 | 62 | | (ItemSortBy.SeriesDatePlayed, not null) => e => |
| | 0 | 63 | | jellyfinDbContext.UserData |
| | 0 | 64 | | .Where(w => w.UserId == query.User.Id && w.Played && w.Item!.SeriesPresentationUniqueKey == e.Presen |
| | 0 | 65 | | .Max(f => f.LastPlayedDate), |
| | 0 | 66 | | (ItemSortBy.SeriesDatePlayed, null) => e => |
| | 0 | 67 | | jellyfinDbContext.UserData |
| | 0 | 68 | | .Where(w => w.Played && w.Item!.SeriesPresentationUniqueKey == e.PresentationUniqueKey) |
| | 0 | 69 | | .Max(f => f.LastPlayedDate), |
| | 48 | 70 | | _ => e => e.SortName |
| | 154 | 71 | | }; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Creates an expression to order search results by match quality. |
| | | 76 | | /// Prioritizes: exact match (0) > prefix match with word boundary (1) > prefix match (2) > contains (3). |
| | | 77 | | /// Considers both CleanName and OriginalTitle for matching. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="searchTerm">The search term to match against.</param> |
| | | 80 | | /// <returns>An expression that returns an integer representing match quality (lower is better).</returns> |
| | | 81 | | public static Expression<Func<BaseItemEntity, int>> MapSearchRelevanceOrder(string searchTerm) |
| | | 82 | | { |
| | 0 | 83 | | var cleanSearchTerm = GetCleanValue(searchTerm); |
| | 0 | 84 | | var searchPrefix = cleanSearchTerm + " "; |
| | 0 | 85 | | var originalSearchLower = searchTerm.ToLowerInvariant(); |
| | 0 | 86 | | var originalSearchPrefix = originalSearchLower + " "; |
| | 0 | 87 | | return e => |
| | 0 | 88 | | // Exact match on CleanName or OriginalTitle |
| | 0 | 89 | | (e.CleanName == cleanSearchTerm || (e.OriginalTitle != null && e.OriginalTitle.ToLower() == originalSearchLo |
| | 0 | 90 | | // Prefix match with word boundary |
| | 0 | 91 | | (e.CleanName!.StartsWith(searchPrefix) || (e.OriginalTitle != null && e.OriginalTitle.ToLower().StartsWith(o |
| | 0 | 92 | | // Prefix match |
| | 0 | 93 | | (e.CleanName!.StartsWith(cleanSearchTerm) || (e.OriginalTitle != null && e.OriginalTitle.ToLower().StartsWit |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | private static string GetCleanValue(string value) |
| | | 97 | | { |
| | 0 | 98 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 99 | | { |
| | 0 | 100 | | return value; |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | return value.RemoveDiacritics().ToLowerInvariant(); |
| | | 104 | | } |
| | | 105 | | } |