| | | 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 | | { |
| | 21 | 36 | | _dbProvider = dbProvider; |
| | 21 | 37 | | _itemTypeLookup = itemTypeLookup; |
| | 21 | 38 | | _queryHelpers = queryHelpers; |
| | 21 | 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 | | { |
| | 0 | 79 | | ArgumentNullException.ThrowIfNull(filter); |
| | 0 | 80 | | ArgumentNullException.ThrowIfNull(filter.User); |
| | | 81 | | |
| | 0 | 82 | | if (seriesKeys.Count == 0) |
| | | 83 | | { |
| | 0 | 84 | | return new Dictionary<string, NextUpEpisodeBatchResult>(); |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | _queryHelpers.PrepareFilterQuery(filter); |
| | 0 | 88 | | using var context = _dbProvider.CreateDbContext(); |
| | | 89 | | |
| | 0 | 90 | | var userId = filter.User.Id; |
| | 0 | 91 | | var episodeTypeName = _itemTypeLookup.BaseItemKindNames[BaseItemKind.Episode]; |
| | | 92 | | |
| | 0 | 93 | | var lastWatchedBase = context.BaseItems |
| | 0 | 94 | | .AsNoTracking() |
| | 0 | 95 | | .Where(e => e.Type == episodeTypeName) |
| | 0 | 96 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 0 | 97 | | .Where(e => e.ParentIndexNumber != 0) |
| | 0 | 98 | | .Where(e => e.UserData!.Any(ud => ud.UserId == userId && ud.Played)); |
| | 0 | 99 | | lastWatchedBase = _queryHelpers.ApplyAccessFiltering(context, lastWatchedBase, filter); |
| | | 100 | | |
| | | 101 | | // Use lightweight projection + client-side grouping to avoid correlated scalar subquery |
| | | 102 | | // per group that EF generates for GroupBy+OrderByDescending+FirstOrDefault. |
| | 0 | 103 | | var allPlayedLite = lastWatchedBase |
| | 0 | 104 | | .Select(e => new |
| | 0 | 105 | | { |
| | 0 | 106 | | e.Id, |
| | 0 | 107 | | e.SeriesPresentationUniqueKey, |
| | 0 | 108 | | e.ParentIndexNumber, |
| | 0 | 109 | | e.IndexNumber |
| | 0 | 110 | | }) |
| | 0 | 111 | | .ToList(); |
| | | 112 | | |
| | 0 | 113 | | var lastWatchedInfo = new Dictionary<string, Guid>(); |
| | 0 | 114 | | foreach (var group in allPlayedLite.GroupBy(e => e.SeriesPresentationUniqueKey)) |
| | | 115 | | { |
| | 0 | 116 | | var lastWatched = group |
| | 0 | 117 | | .OrderByDescending(e => e.ParentIndexNumber) |
| | 0 | 118 | | .ThenByDescending(e => e.IndexNumber) |
| | 0 | 119 | | .First(); |
| | 0 | 120 | | lastWatchedInfo[group.Key!] = lastWatched.Id; |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | Dictionary<string, Guid> lastWatchedByDateInfo = new(); |
| | 0 | 124 | | if (includeWatchedForRewatching) |
| | | 125 | | { |
| | 0 | 126 | | var lastWatchedByDateBase = context.BaseItems |
| | 0 | 127 | | .AsNoTracking() |
| | 0 | 128 | | .Where(e => e.Type == episodeTypeName) |
| | 0 | 129 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 0 | 130 | | .Where(e => e.ParentIndexNumber != 0); |
| | 0 | 131 | | lastWatchedByDateBase = _queryHelpers.ApplyAccessFiltering(context, lastWatchedByDateBase, filter); |
| | | 132 | | |
| | | 133 | | // Use an explicit Join (INNER JOIN) instead of SelectMany on a collection navigation. |
| | | 134 | | // SelectMany on UserData with a correlated Where would translate to APPLY, |
| | | 135 | | // which SQLite does not support. |
| | 0 | 136 | | var playedWithDates = lastWatchedByDateBase |
| | 0 | 137 | | .Join( |
| | 0 | 138 | | context.UserData |
| | 0 | 139 | | .AsNoTracking() |
| | 0 | 140 | | .Where(ud => ud.ItemId != EF.Constant(BaseItemRepository.PlaceholderId)) |
| | 0 | 141 | | .Where(ud => ud.Played), |
| | 0 | 142 | | e => new { UserId = userId, ItemId = e.Id }, |
| | 0 | 143 | | ud => new { ud.UserId, ud.ItemId }, |
| | 0 | 144 | | (e, ud) => new { EpisodeId = e.Id, e.SeriesPresentationUniqueKey, ud.LastPlayedDate }) |
| | 0 | 145 | | .ToList(); |
| | | 146 | | |
| | 0 | 147 | | foreach (var group in playedWithDates.GroupBy(x => x.SeriesPresentationUniqueKey)) |
| | | 148 | | { |
| | 0 | 149 | | var mostRecent = group.OrderByDescending(x => x.LastPlayedDate).First(); |
| | 0 | 150 | | lastWatchedByDateInfo[group.Key!] = mostRecent.EpisodeId; |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | var allLastWatchedIds = lastWatchedInfo.Values |
| | 0 | 155 | | .Concat(lastWatchedByDateInfo.Values) |
| | 0 | 156 | | .Where(id => id != Guid.Empty) |
| | 0 | 157 | | .Distinct() |
| | 0 | 158 | | .ToList(); |
| | 0 | 159 | | var lwQuery = context.BaseItems.AsNoTracking().Where(e => allLastWatchedIds.Contains(e.Id)); |
| | 0 | 160 | | lwQuery = _queryHelpers.ApplyNavigations(lwQuery, filter); |
| | 0 | 161 | | var lastWatchedEpisodes = lwQuery.ToDictionary(e => e.Id); |
| | | 162 | | |
| | 0 | 163 | | Dictionary<string, List<BaseItemEntity>> specialsBySeriesKey = new(); |
| | 0 | 164 | | if (includeSpecials) |
| | | 165 | | { |
| | 0 | 166 | | var specialsQuery = context.BaseItems |
| | 0 | 167 | | .AsNoTracking() |
| | 0 | 168 | | .Where(e => e.Type == episodeTypeName) |
| | 0 | 169 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 0 | 170 | | .Where(e => e.ParentIndexNumber == 0) |
| | 0 | 171 | | .Where(e => !e.IsVirtualItem); |
| | 0 | 172 | | specialsQuery = _queryHelpers.ApplyAccessFiltering(context, specialsQuery, filter); |
| | 0 | 173 | | specialsQuery = _queryHelpers.ApplyNavigations(specialsQuery, filter).AsSingleQuery(); |
| | | 174 | | |
| | 0 | 175 | | foreach (var special in specialsQuery) |
| | | 176 | | { |
| | 0 | 177 | | var key = special.SeriesPresentationUniqueKey!; |
| | 0 | 178 | | if (!specialsBySeriesKey.TryGetValue(key, out var list)) |
| | | 179 | | { |
| | 0 | 180 | | list = new List<BaseItemEntity>(); |
| | 0 | 181 | | specialsBySeriesKey[key] = list; |
| | | 182 | | } |
| | | 183 | | |
| | 0 | 184 | | list.Add(special); |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | var positionLookup = new Dictionary<string, (int Season, int Episode)>(); |
| | 0 | 189 | | foreach (var kvp in lastWatchedInfo) |
| | | 190 | | { |
| | 0 | 191 | | if (kvp.Value != Guid.Empty |
| | 0 | 192 | | && lastWatchedEpisodes.TryGetValue(kvp.Value, out var lw) |
| | 0 | 193 | | && lw.ParentIndexNumber.HasValue |
| | 0 | 194 | | && lw.IndexNumber.HasValue) |
| | | 195 | | { |
| | 0 | 196 | | positionLookup[kvp.Key] = (lw.ParentIndexNumber.Value, lw.IndexNumber.Value); |
| | | 197 | | } |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | var allUnplayedBase = context.BaseItems |
| | 0 | 201 | | .AsNoTracking() |
| | 0 | 202 | | .Where(e => e.Type == episodeTypeName) |
| | 0 | 203 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 0 | 204 | | .Where(e => e.ParentIndexNumber != 0) |
| | 0 | 205 | | .Where(e => !e.IsVirtualItem) |
| | 0 | 206 | | .Where(e => !e.UserData!.Any(ud => ud.UserId == userId && ud.Played)); |
| | 0 | 207 | | allUnplayedBase = _queryHelpers.ApplyAccessFiltering(context, allUnplayedBase, filter); |
| | 0 | 208 | | var allUnplayedCandidates = allUnplayedBase |
| | 0 | 209 | | .Select(e => new |
| | 0 | 210 | | { |
| | 0 | 211 | | e.Id, |
| | 0 | 212 | | e.SeriesPresentationUniqueKey, |
| | 0 | 213 | | e.ParentIndexNumber, |
| | 0 | 214 | | EpisodeNumber = e.IndexNumber |
| | 0 | 215 | | }) |
| | 0 | 216 | | .ToList(); |
| | | 217 | | |
| | 0 | 218 | | var nextEpisodeIds = new HashSet<Guid>(); |
| | 0 | 219 | | var seriesNextIdMap = new Dictionary<string, Guid>(); |
| | | 220 | | |
| | 0 | 221 | | foreach (var seriesKey in seriesKeys) |
| | | 222 | | { |
| | 0 | 223 | | var candidates = allUnplayedCandidates |
| | 0 | 224 | | .Where(c => c.SeriesPresentationUniqueKey == seriesKey); |
| | | 225 | | |
| | 0 | 226 | | if (positionLookup.TryGetValue(seriesKey, out var pos)) |
| | | 227 | | { |
| | 0 | 228 | | candidates = candidates.Where(c => |
| | 0 | 229 | | c.ParentIndexNumber > pos.Season |
| | 0 | 230 | | || (c.ParentIndexNumber == pos.Season && c.EpisodeNumber > pos.Episode)); |
| | | 231 | | } |
| | | 232 | | |
| | 0 | 233 | | var nextCandidate = candidates |
| | 0 | 234 | | .OrderBy(c => c.ParentIndexNumber) |
| | 0 | 235 | | .ThenBy(c => c.EpisodeNumber) |
| | 0 | 236 | | .FirstOrDefault(); |
| | | 237 | | |
| | 0 | 238 | | if (nextCandidate is not null && nextCandidate.Id != Guid.Empty) |
| | | 239 | | { |
| | 0 | 240 | | nextEpisodeIds.Add(nextCandidate.Id); |
| | 0 | 241 | | seriesNextIdMap[seriesKey] = nextCandidate.Id; |
| | | 242 | | } |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | var seriesNextPlayedIdMap = new Dictionary<string, Guid>(); |
| | 0 | 246 | | if (includeWatchedForRewatching) |
| | | 247 | | { |
| | 0 | 248 | | var allPlayedBase = context.BaseItems |
| | 0 | 249 | | .AsNoTracking() |
| | 0 | 250 | | .Where(e => e.Type == episodeTypeName) |
| | 0 | 251 | | .Where(e => e.SeriesPresentationUniqueKey != null && seriesKeys.Contains(e.SeriesPresentationUniqueKey)) |
| | 0 | 252 | | .Where(e => e.ParentIndexNumber != 0) |
| | 0 | 253 | | .Where(e => !e.IsVirtualItem) |
| | 0 | 254 | | .Where(e => e.UserData!.Any(ud => ud.UserId == userId && ud.Played)); |
| | 0 | 255 | | allPlayedBase = _queryHelpers.ApplyAccessFiltering(context, allPlayedBase, filter); |
| | 0 | 256 | | var allPlayedCandidates = allPlayedBase |
| | 0 | 257 | | .Select(e => new |
| | 0 | 258 | | { |
| | 0 | 259 | | e.Id, |
| | 0 | 260 | | e.SeriesPresentationUniqueKey, |
| | 0 | 261 | | e.ParentIndexNumber, |
| | 0 | 262 | | EpisodeNumber = e.IndexNumber |
| | 0 | 263 | | }) |
| | 0 | 264 | | .ToList(); |
| | | 265 | | |
| | 0 | 266 | | foreach (var seriesKey in seriesKeys) |
| | | 267 | | { |
| | 0 | 268 | | if (!lastWatchedByDateInfo.TryGetValue(seriesKey, out var lastByDateId)) |
| | | 269 | | { |
| | | 270 | | continue; |
| | | 271 | | } |
| | | 272 | | |
| | 0 | 273 | | var lastByDateEntity = lastWatchedEpisodes.GetValueOrDefault(lastByDateId); |
| | 0 | 274 | | if (lastByDateEntity is null) |
| | | 275 | | { |
| | | 276 | | continue; |
| | | 277 | | } |
| | | 278 | | |
| | 0 | 279 | | var playedCandidates = allPlayedCandidates |
| | 0 | 280 | | .Where(c => c.SeriesPresentationUniqueKey == seriesKey); |
| | | 281 | | |
| | 0 | 282 | | if (lastByDateEntity.ParentIndexNumber.HasValue && lastByDateEntity.IndexNumber.HasValue) |
| | | 283 | | { |
| | 0 | 284 | | var lastSeason = lastByDateEntity.ParentIndexNumber.Value; |
| | 0 | 285 | | var lastEp = lastByDateEntity.IndexNumber.Value; |
| | 0 | 286 | | playedCandidates = playedCandidates.Where(c => |
| | 0 | 287 | | c.ParentIndexNumber > lastSeason |
| | 0 | 288 | | || (c.ParentIndexNumber == lastSeason && c.EpisodeNumber > lastEp)); |
| | | 289 | | } |
| | | 290 | | |
| | 0 | 291 | | var nextPlayedCandidate = playedCandidates |
| | 0 | 292 | | .OrderBy(c => c.ParentIndexNumber) |
| | 0 | 293 | | .ThenBy(c => c.EpisodeNumber) |
| | 0 | 294 | | .FirstOrDefault(); |
| | | 295 | | |
| | 0 | 296 | | if (nextPlayedCandidate is not null && nextPlayedCandidate.Id != Guid.Empty) |
| | | 297 | | { |
| | 0 | 298 | | nextEpisodeIds.Add(nextPlayedCandidate.Id); |
| | 0 | 299 | | seriesNextPlayedIdMap[seriesKey] = nextPlayedCandidate.Id; |
| | | 300 | | } |
| | | 301 | | } |
| | | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | var nextEpisodes = new Dictionary<Guid, BaseItemEntity>(); |
| | 0 | 305 | | if (nextEpisodeIds.Count > 0) |
| | | 306 | | { |
| | 0 | 307 | | var nextQuery = context.BaseItems.AsNoTracking().Where(e => nextEpisodeIds.Contains(e.Id)); |
| | 0 | 308 | | nextQuery = _queryHelpers.ApplyNavigations(nextQuery, filter).AsSingleQuery(); |
| | 0 | 309 | | nextEpisodes = nextQuery.ToDictionary(e => e.Id); |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | var result = new Dictionary<string, NextUpEpisodeBatchResult>(); |
| | 0 | 313 | | foreach (var seriesKey in seriesKeys) |
| | | 314 | | { |
| | 0 | 315 | | var batchResult = new NextUpEpisodeBatchResult(); |
| | | 316 | | |
| | 0 | 317 | | if (lastWatchedInfo.TryGetValue(seriesKey, out var lwId) && lwId != Guid.Empty) |
| | | 318 | | { |
| | 0 | 319 | | if (lastWatchedEpisodes.TryGetValue(lwId, out var entity)) |
| | | 320 | | { |
| | 0 | 321 | | batchResult.LastWatched = _queryHelpers.DeserializeBaseItem(entity, filter.SkipDeserialization); |
| | | 322 | | } |
| | | 323 | | } |
| | | 324 | | |
| | 0 | 325 | | if (seriesNextIdMap.TryGetValue(seriesKey, out var nextId) && nextEpisodes.TryGetValue(nextId, out var nextE |
| | | 326 | | { |
| | 0 | 327 | | batchResult.NextUp = _queryHelpers.DeserializeBaseItem(nextEntity, filter.SkipDeserialization); |
| | | 328 | | } |
| | | 329 | | |
| | 0 | 330 | | if (includeSpecials && specialsBySeriesKey.TryGetValue(seriesKey, out var specials)) |
| | | 331 | | { |
| | 0 | 332 | | batchResult.Specials = specials.Select(s => _queryHelpers.DeserializeBaseItem(s, filter.SkipDeserializat |
| | | 333 | | } |
| | | 334 | | else |
| | | 335 | | { |
| | 0 | 336 | | batchResult.Specials = Array.Empty<BaseItemDto>(); |
| | | 337 | | } |
| | | 338 | | |
| | 0 | 339 | | if (includeWatchedForRewatching) |
| | | 340 | | { |
| | 0 | 341 | | if (lastWatchedByDateInfo.TryGetValue(seriesKey, out var lastByDateId) && |
| | 0 | 342 | | lastWatchedEpisodes.TryGetValue(lastByDateId, out var lastByDateEntity)) |
| | | 343 | | { |
| | 0 | 344 | | batchResult.LastWatchedForRewatching = _queryHelpers.DeserializeBaseItem(lastByDateEntity, filter.Sk |
| | | 345 | | } |
| | | 346 | | |
| | 0 | 347 | | if (seriesNextPlayedIdMap.TryGetValue(seriesKey, out var nextPlayedId) && |
| | 0 | 348 | | nextEpisodes.TryGetValue(nextPlayedId, out var nextPlayedEntity)) |
| | | 349 | | { |
| | 0 | 350 | | batchResult.NextPlayedForRewatching = _queryHelpers.DeserializeBaseItem(nextPlayedEntity, filter.Ski |
| | | 351 | | } |
| | | 352 | | } |
| | | 353 | | |
| | 0 | 354 | | result[seriesKey] = batchResult; |
| | | 355 | | } |
| | | 356 | | |
| | 0 | 357 | | return result; |
| | 0 | 358 | | } |
| | | 359 | | } |