| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using Jellyfin.Data.Enums; |
| | | 7 | | using Jellyfin.Database.Implementations; |
| | | 8 | | using Jellyfin.Database.Implementations.Entities; |
| | | 9 | | using MediaBrowser.Controller.Entities; |
| | | 10 | | using MediaBrowser.Controller.Persistence; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem; |
| | | 13 | | |
| | | 14 | | namespace Jellyfin.Server.Implementations.Item; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides next-up episode query operations. |
| | | 18 | | /// </summary> |
| | | 19 | | public class NextUpService : INextUpService |
| | | 20 | | { |
| | | 21 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 22 | | private readonly IItemTypeLookup _itemTypeLookup; |
| | | 23 | | private readonly IItemQueryHelpers _queryHelpers; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="NextUpService"/> class. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 29 | | /// <param name="itemTypeLookup">The item type lookup.</param> |
| | | 30 | | /// <param name="queryHelpers">The shared query helpers.</param> |
| | | 31 | | public NextUpService( |
| | | 32 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 33 | | IItemTypeLookup itemTypeLookup, |
| | | 34 | | IItemQueryHelpers queryHelpers) |
| | | 35 | | { |
| | 28 | 36 | | _dbProvider = dbProvider; |
| | 28 | 37 | | _itemTypeLookup = itemTypeLookup; |
| | 28 | 38 | | _queryHelpers = queryHelpers; |
| | 28 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public IReadOnlyList<string> GetNextUpSeriesKeys(InternalItemsQuery filter, DateTime dateCutoff) |
| | | 43 | | { |
| | 0 | 44 | | ArgumentNullException.ThrowIfNull(filter); |
| | 0 | 45 | | ArgumentNullException.ThrowIfNull(filter.User); |
| | | 46 | | |
| | 0 | 47 | | using var context = _dbProvider.CreateDbContext(); |
| | | 48 | | |
| | 0 | 49 | | var query = context.BaseItems |
| | 0 | 50 | | .AsNoTracking() |
| | 0 | 51 | | .Where(i => filter.TopParentIds.Contains(i.TopParentId!.Value)) |
| | 0 | 52 | | .Where(i => i.Type == _itemTypeLookup.BaseItemKindNames[BaseItemKind.Episode]) |
| | 0 | 53 | | .Join( |
| | 0 | 54 | | context.UserData.AsNoTracking().Where(e => e.ItemId != EF.Constant(BaseItemRepository.PlaceholderId)), |
| | 0 | 55 | | i => new { UserId = filter.User.Id, ItemId = i.Id }, |
| | 0 | 56 | | u => new { u.UserId, u.ItemId }, |
| | 0 | 57 | | (entity, data) => new { Item = entity, UserData = data }) |
| | 0 | 58 | | .GroupBy(g => g.Item.SeriesPresentationUniqueKey) |
| | 0 | 59 | | .Select(g => new { g.Key, LastPlayedDate = g.Max(u => u.UserData.LastPlayedDate) }) |
| | 0 | 60 | | .Where(g => g.Key != null && g.LastPlayedDate != null && g.LastPlayedDate >= dateCutoff) |
| | 0 | 61 | | .OrderByDescending(g => g.LastPlayedDate) |
| | 0 | 62 | | .Select(g => g.Key!); |
| | | 63 | | |
| | 0 | 64 | | if (filter.Limit.HasValue) |
| | | 65 | | { |
| | 0 | 66 | | query = query.Take(filter.Limit.Value); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | return query.ToArray(); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <inheritdoc /> |
| | | 73 | | public IReadOnlyDictionary<string, NextUpEpisodeBatchResult> GetNextUpEpisodesBatch( |
| | | 74 | | InternalItemsQuery filter, |
| | | 75 | | IReadOnlyList<string> seriesKeys, |
| | | 76 | | bool includeSpecials, |
| | | 77 | | bool includeWatchedForRewatching) |
| | | 78 | | { |
| | 1 | 79 | | ArgumentNullException.ThrowIfNull(filter); |
| | 1 | 80 | | ArgumentNullException.ThrowIfNull(filter.User); |
| | | 81 | | |
| | 1 | 82 | | if (seriesKeys.Count == 0) |
| | | 83 | | { |
| | 0 | 84 | | return new Dictionary<string, NextUpEpisodeBatchResult>(); |
| | | 85 | | } |
| | | 86 | | |
| | 1 | 87 | | _queryHelpers.PrepareFilterQuery(filter); |
| | 1 | 88 | | using var context = _dbProvider.CreateDbContext(); |
| | | 89 | | |
| | 1 | 90 | | var userId = filter.User.Id; |
| | 1 | 91 | | var episodeTypeName = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Episode]; |
| | | 92 | | |
| | 1 | 93 | | var lastWatchedBase = context.BaseItems |
| | 1 | 94 | | .AsNoTracking() |
| | 1 | 95 | | .Where(e => e.Type == episodeTypeName) |
| | 1 | 96 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 1 | 97 | | .Where(e => e.ParentIndexNumber != 0) |
| | 1 | 98 | | .Where(DescendantQueryHelper.IsPlayedBy(userId)); |
| | 1 | 99 | | lastWatchedBase = _queryHelpers.ApplyAccessFiltering(context, lastWatchedBase, filter); |
| | | 100 | | |
| | | 101 | | // Use lightweight projection + client-side dedup to avoid the correlated scalar subquery |
| | | 102 | | // per group that EF generates for GroupBy+OrderByDescending+FirstOrDefault. |
| | 1 | 103 | | var allPlayedLite = lastWatchedBase |
| | 1 | 104 | | .Select(e => new |
| | 1 | 105 | | { |
| | 1 | 106 | | e.Id, |
| | 1 | 107 | | e.SeriesPresentationUniqueKey, |
| | 1 | 108 | | e.ParentIndexNumber, |
| | 1 | 109 | | e.IndexNumber |
| | 1 | 110 | | }) |
| | 1 | 111 | | .ToList(); |
| | | 112 | | |
| | 1 | 113 | | var lastWatchedInfo = allPlayedLite |
| | 1 | 114 | | .OrderByDescending(e => e.ParentIndexNumber) |
| | 1 | 115 | | .ThenByDescending(e => e.IndexNumber) |
| | 1 | 116 | | .DistinctBy(e => e.SeriesPresentationUniqueKey) |
| | 1 | 117 | | .ToDictionary(e => e.SeriesPresentationUniqueKey!, e => e.Id); |
| | | 118 | | |
| | 1 | 119 | | Dictionary<string, Guid> lastWatchedByDateInfo = new(); |
| | 1 | 120 | | if (includeWatchedForRewatching) |
| | | 121 | | { |
| | 0 | 122 | | var lastWatchedByDateBase = context.BaseItems |
| | 0 | 123 | | .AsNoTracking() |
| | 0 | 124 | | .Where(e => e.Type == episodeTypeName) |
| | 0 | 125 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 0 | 126 | | .Where(e => e.ParentIndexNumber != 0); |
| | 0 | 127 | | lastWatchedByDateBase = _queryHelpers.ApplyAccessFiltering(context, lastWatchedByDateBase, filter); |
| | | 128 | | |
| | | 129 | | // Use an explicit Join (INNER JOIN) instead of SelectMany on a collection navigation. |
| | | 130 | | // SelectMany on UserData with a correlated Where would translate to APPLY, |
| | | 131 | | // which SQLite does not support. |
| | | 132 | | // Access filtering leaves only primaries in the base query, but a play can be recorded |
| | | 133 | | // against any version, so each row is attributed to its group's primary before the join. |
| | 0 | 134 | | var playedByGroupPrimary = context.UserData |
| | 0 | 135 | | .AsNoTracking() |
| | 0 | 136 | | .Where(ud => ud.ItemId != EF.Constant(BaseItemRepository.PlaceholderId)) |
| | 0 | 137 | | .Where(ud => ud.Played) |
| | 0 | 138 | | .Join( |
| | 0 | 139 | | context.BaseItems.AsNoTracking(), |
| | 0 | 140 | | ud => ud.ItemId, |
| | 0 | 141 | | bi => bi.Id, |
| | 0 | 142 | | (ud, bi) => new { ud.UserId, ItemId = bi.PrimaryVersionId ?? bi.Id, ud.LastPlayedDate }); |
| | | 143 | | |
| | 0 | 144 | | var playedWithDates = lastWatchedByDateBase |
| | 0 | 145 | | .Join( |
| | 0 | 146 | | playedByGroupPrimary, |
| | 0 | 147 | | e => new { UserId = userId, ItemId = e.Id }, |
| | 0 | 148 | | ud => new { ud.UserId, ud.ItemId }, |
| | 0 | 149 | | (e, ud) => new { EpisodeId = e.Id, e.SeriesPresentationUniqueKey, ud.LastPlayedDate }) |
| | 0 | 150 | | .ToList(); |
| | | 151 | | |
| | 0 | 152 | | lastWatchedByDateInfo = playedWithDates |
| | 0 | 153 | | .OrderByDescending(x => x.LastPlayedDate) |
| | 0 | 154 | | .DistinctBy(x => x.SeriesPresentationUniqueKey) |
| | 0 | 155 | | .ToDictionary(x => x.SeriesPresentationUniqueKey!, x => x.EpisodeId); |
| | | 156 | | } |
| | | 157 | | |
| | 1 | 158 | | var allLastWatchedIds = lastWatchedInfo.Values |
| | 1 | 159 | | .Concat(lastWatchedByDateInfo.Values) |
| | 1 | 160 | | .Where(id => id != Guid.Empty) |
| | 1 | 161 | | .Distinct() |
| | 1 | 162 | | .ToList(); |
| | 1 | 163 | | var lwQuery = context.BaseItems.AsNoTracking().Where(e => allLastWatchedIds.Contains(e.Id)); |
| | 1 | 164 | | lwQuery = _queryHelpers.ApplyNavigations(lwQuery, filter); |
| | 1 | 165 | | var lastWatchedEpisodes = lwQuery.ToDictionary(e => e.Id); |
| | | 166 | | |
| | 1 | 167 | | Dictionary<string, List<BaseItemEntity>> specialsBySeriesKey = new(); |
| | 1 | 168 | | if (includeSpecials) |
| | | 169 | | { |
| | 0 | 170 | | var specialsQuery = context.BaseItems |
| | 0 | 171 | | .AsNoTracking() |
| | 0 | 172 | | .Where(e => e.Type == episodeTypeName) |
| | 0 | 173 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 0 | 174 | | .Where(e => e.ParentIndexNumber == 0) |
| | 0 | 175 | | .Where(e => !e.IsVirtualItem); |
| | 0 | 176 | | specialsQuery = _queryHelpers.ApplyAccessFiltering(context, specialsQuery, filter); |
| | 0 | 177 | | specialsQuery = _queryHelpers.ApplyNavigations(specialsQuery, filter).AsSingleQuery(); |
| | | 178 | | |
| | 0 | 179 | | foreach (var special in specialsQuery) |
| | | 180 | | { |
| | 0 | 181 | | var key = special.SeriesPresentationUniqueKey!; |
| | 0 | 182 | | if (!specialsBySeriesKey.TryGetValue(key, out var list)) |
| | | 183 | | { |
| | 0 | 184 | | list = new List<BaseItemEntity>(); |
| | 0 | 185 | | specialsBySeriesKey[key] = list; |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | list.Add(special); |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | 1 | 192 | | var positionLookup = new Dictionary<string, (int Season, int Episode)>(); |
| | 4 | 193 | | foreach (var kvp in lastWatchedInfo) |
| | | 194 | | { |
| | 1 | 195 | | if (kvp.Value != Guid.Empty |
| | 1 | 196 | | && lastWatchedEpisodes.TryGetValue(kvp.Value, out var lw) |
| | 1 | 197 | | && lw.ParentIndexNumber.HasValue |
| | 1 | 198 | | && lw.IndexNumber.HasValue) |
| | | 199 | | { |
| | 1 | 200 | | positionLookup[kvp.Key] = (lw.ParentIndexNumber.Value, lw.IndexNumber.Value); |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | |
| | 1 | 204 | | var allUnplayedBase = context.BaseItems |
| | 1 | 205 | | .AsNoTracking() |
| | 1 | 206 | | .Where(e => e.Type == episodeTypeName) |
| | 1 | 207 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 1 | 208 | | .Where(e => e.ParentIndexNumber != 0) |
| | 1 | 209 | | .Where(e => !e.IsVirtualItem) |
| | 1 | 210 | | .Where(DescendantQueryHelper.IsUnplayedBy(userId)); |
| | 1 | 211 | | allUnplayedBase = _queryHelpers.ApplyAccessFiltering(context, allUnplayedBase, filter); |
| | 1 | 212 | | var allUnplayedCandidates = allUnplayedBase |
| | 1 | 213 | | .Select(e => new |
| | 1 | 214 | | { |
| | 1 | 215 | | e.Id, |
| | 1 | 216 | | e.SeriesPresentationUniqueKey, |
| | 1 | 217 | | e.ParentIndexNumber, |
| | 1 | 218 | | EpisodeNumber = e.IndexNumber |
| | 1 | 219 | | }) |
| | 1 | 220 | | .ToList(); |
| | | 221 | | |
| | 1 | 222 | | var nextEpisodeIds = new HashSet<Guid>(); |
| | 1 | 223 | | var seriesNextIdMap = new Dictionary<string, Guid>(); |
| | | 224 | | |
| | 4 | 225 | | foreach (var seriesKey in seriesKeys) |
| | | 226 | | { |
| | 1 | 227 | | var candidates = allUnplayedCandidates |
| | 1 | 228 | | .Where(c => c.SeriesPresentationUniqueKey == seriesKey); |
| | | 229 | | |
| | 1 | 230 | | if (positionLookup.TryGetValue(seriesKey, out var pos)) |
| | | 231 | | { |
| | 1 | 232 | | candidates = candidates.Where(c => |
| | 1 | 233 | | c.ParentIndexNumber > pos.Season |
| | 1 | 234 | | || (c.ParentIndexNumber == pos.Season && c.EpisodeNumber > pos.Episode)); |
| | | 235 | | } |
| | | 236 | | |
| | 1 | 237 | | var nextCandidate = candidates |
| | 1 | 238 | | .OrderBy(c => c.ParentIndexNumber) |
| | 1 | 239 | | .ThenBy(c => c.EpisodeNumber) |
| | 1 | 240 | | .FirstOrDefault(); |
| | | 241 | | |
| | 1 | 242 | | if (nextCandidate is not null && nextCandidate.Id != Guid.Empty) |
| | | 243 | | { |
| | 1 | 244 | | nextEpisodeIds.Add(nextCandidate.Id); |
| | 1 | 245 | | seriesNextIdMap[seriesKey] = nextCandidate.Id; |
| | | 246 | | } |
| | | 247 | | } |
| | | 248 | | |
| | 1 | 249 | | var seriesNextPlayedIdMap = new Dictionary<string, Guid>(); |
| | 1 | 250 | | if (includeWatchedForRewatching) |
| | | 251 | | { |
| | 0 | 252 | | var allPlayedBase = context.BaseItems |
| | 0 | 253 | | .AsNoTracking() |
| | 0 | 254 | | .Where(e => e.Type == episodeTypeName) |
| | 0 | 255 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 0 | 256 | | .Where(e => e.ParentIndexNumber != 0) |
| | 0 | 257 | | .Where(e => !e.IsVirtualItem) |
| | 0 | 258 | | .Where(DescendantQueryHelper.IsPlayedBy(userId)); |
| | 0 | 259 | | allPlayedBase = _queryHelpers.ApplyAccessFiltering(context, allPlayedBase, filter); |
| | 0 | 260 | | var allPlayedCandidates = allPlayedBase |
| | 0 | 261 | | .Select(e => new |
| | 0 | 262 | | { |
| | 0 | 263 | | e.Id, |
| | 0 | 264 | | e.SeriesPresentationUniqueKey, |
| | 0 | 265 | | e.ParentIndexNumber, |
| | 0 | 266 | | EpisodeNumber = e.IndexNumber |
| | 0 | 267 | | }) |
| | 0 | 268 | | .ToList(); |
| | | 269 | | |
| | 0 | 270 | | foreach (var seriesKey in seriesKeys) |
| | | 271 | | { |
| | 0 | 272 | | if (!lastWatchedByDateInfo.TryGetValue(seriesKey, out var lastByDateId)) |
| | | 273 | | { |
| | | 274 | | continue; |
| | | 275 | | } |
| | | 276 | | |
| | 0 | 277 | | var lastByDateEntity = lastWatchedEpisodes.GetValueOrDefault(lastByDateId); |
| | 0 | 278 | | if (lastByDateEntity is null) |
| | | 279 | | { |
| | | 280 | | continue; |
| | | 281 | | } |
| | | 282 | | |
| | 0 | 283 | | var playedCandidates = allPlayedCandidates |
| | 0 | 284 | | .Where(c => c.SeriesPresentationUniqueKey == seriesKey); |
| | | 285 | | |
| | 0 | 286 | | if (lastByDateEntity.ParentIndexNumber.HasValue && lastByDateEntity.IndexNumber.HasValue) |
| | | 287 | | { |
| | 0 | 288 | | var lastSeason = lastByDateEntity.ParentIndexNumber.Value; |
| | 0 | 289 | | var lastEp = lastByDateEntity.IndexNumber.Value; |
| | 0 | 290 | | playedCandidates = playedCandidates.Where(c => |
| | 0 | 291 | | c.ParentIndexNumber > lastSeason |
| | 0 | 292 | | || (c.ParentIndexNumber == lastSeason && c.EpisodeNumber > lastEp)); |
| | | 293 | | } |
| | | 294 | | |
| | 0 | 295 | | var nextPlayedCandidate = playedCandidates |
| | 0 | 296 | | .OrderBy(c => c.ParentIndexNumber) |
| | 0 | 297 | | .ThenBy(c => c.EpisodeNumber) |
| | 0 | 298 | | .FirstOrDefault(); |
| | | 299 | | |
| | 0 | 300 | | if (nextPlayedCandidate is not null && nextPlayedCandidate.Id != Guid.Empty) |
| | | 301 | | { |
| | 0 | 302 | | nextEpisodeIds.Add(nextPlayedCandidate.Id); |
| | 0 | 303 | | seriesNextPlayedIdMap[seriesKey] = nextPlayedCandidate.Id; |
| | | 304 | | } |
| | | 305 | | } |
| | | 306 | | } |
| | | 307 | | |
| | 1 | 308 | | var nextEpisodes = new Dictionary<Guid, BaseItemEntity>(); |
| | 1 | 309 | | if (nextEpisodeIds.Count > 0) |
| | | 310 | | { |
| | 1 | 311 | | var nextQuery = context.BaseItems.AsNoTracking().Where(e => nextEpisodeIds.Contains(e.Id)); |
| | 1 | 312 | | nextQuery = _queryHelpers.ApplyNavigations(nextQuery, filter).AsSingleQuery(); |
| | 1 | 313 | | nextEpisodes = nextQuery.ToDictionary(e => e.Id); |
| | | 314 | | } |
| | | 315 | | |
| | 1 | 316 | | var result = new Dictionary<string, NextUpEpisodeBatchResult>(); |
| | 4 | 317 | | foreach (var seriesKey in seriesKeys) |
| | | 318 | | { |
| | 1 | 319 | | var batchResult = new NextUpEpisodeBatchResult(); |
| | | 320 | | |
| | 1 | 321 | | if (lastWatchedInfo.TryGetValue(seriesKey, out var lwId) && lwId != Guid.Empty) |
| | | 322 | | { |
| | 1 | 323 | | if (lastWatchedEpisodes.TryGetValue(lwId, out var entity)) |
| | | 324 | | { |
| | 1 | 325 | | batchResult.LastWatched = _queryHelpers.DeserializeBaseItem(entity, filter.SkipDeserialization); |
| | | 326 | | } |
| | | 327 | | } |
| | | 328 | | |
| | 1 | 329 | | if (seriesNextIdMap.TryGetValue(seriesKey, out var nextId) && nextEpisodes.TryGetValue(nextId, out var nextE |
| | | 330 | | { |
| | 1 | 331 | | batchResult.NextUp = _queryHelpers.DeserializeBaseItem(nextEntity, filter.SkipDeserialization); |
| | | 332 | | } |
| | | 333 | | |
| | 1 | 334 | | if (includeSpecials && specialsBySeriesKey.TryGetValue(seriesKey, out var specials)) |
| | | 335 | | { |
| | 0 | 336 | | batchResult.Specials = specials.Select(s => _queryHelpers.DeserializeBaseItem(s, filter.SkipDeserializat |
| | | 337 | | } |
| | | 338 | | else |
| | | 339 | | { |
| | 1 | 340 | | batchResult.Specials = Array.Empty<BaseItemDto>(); |
| | | 341 | | } |
| | | 342 | | |
| | 1 | 343 | | if (includeWatchedForRewatching) |
| | | 344 | | { |
| | 0 | 345 | | if (lastWatchedByDateInfo.TryGetValue(seriesKey, out var lastByDateId) && |
| | 0 | 346 | | lastWatchedEpisodes.TryGetValue(lastByDateId, out var lastByDateEntity)) |
| | | 347 | | { |
| | 0 | 348 | | batchResult.LastWatchedForRewatching = _queryHelpers.DeserializeBaseItem(lastByDateEntity, filter.Sk |
| | | 349 | | } |
| | | 350 | | |
| | 0 | 351 | | if (seriesNextPlayedIdMap.TryGetValue(seriesKey, out var nextPlayedId) && |
| | 0 | 352 | | nextEpisodes.TryGetValue(nextPlayedId, out var nextPlayedEntity)) |
| | | 353 | | { |
| | 0 | 354 | | batchResult.NextPlayedForRewatching = _queryHelpers.DeserializeBaseItem(nextPlayedEntity, filter.Ski |
| | | 355 | | } |
| | | 356 | | } |
| | | 357 | | |
| | 1 | 358 | | result[seriesKey] = batchResult; |
| | | 359 | | } |
| | | 360 | | |
| | 1 | 361 | | return result; |
| | 1 | 362 | | } |
| | | 363 | | } |