using System; using System.Linq; using System.Threading; using Jellyfin.Database.Implementations.Entities; using Jellyfin.Plugin.MediaCleaner.Configuration; using MediaBrowser.Controller.Entities; using Microsoft.Extensions.Logging; namespace Jellyfin.Plugin.MediaCleaner.Helpers; public class MovieHelper(ILogger logger) { private readonly LoggingHelper _loggingHelper = new(logger); private static PluginConfiguration Configuration => Plugin.Instance!.Configuration; public bool IsMovieStale(BaseItem movie) { _loggingHelper.LogDebugInformation("Start of scanning for movie: {Movie}", movie); _loggingHelper.LogDebugInformation("-------------------------------------------------"); bool movieIsStale = false; bool createdOutsideCutoff = movie.DateCreated < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff); bool hasUserData = movie.UserData.Where(data => data.LastPlayedDate != null).ToList().Count > 0; if (hasUserData) { var mostRecentUserData = movie.UserData.OrderByDescending(data => data.LastPlayedDate).First(data => data.LastPlayedDate != null); _loggingHelper.LogDebugInformation("Most recent user data: {Movie}", movie); foreach (var property in typeof(UserData).GetProperties()) { _loggingHelper.LogDebugInformation("{PropertyName}: {PropertyValue}", property.Name, property.GetValue(mostRecentUserData)); } _loggingHelper.LogDebugInformation("-------------------------------------------------"); if (mostRecentUserData.LastPlayedDate < DateTime.Now.AddDays(-Configuration.StaleMediaCutoff)) { _loggingHelper.LogDebugInformation("Most recent user data has last played date that is outside of cutoff."); _loggingHelper.LogDebugInformation("Adding {Movie} to stale movies.", movie); _loggingHelper.LogDebugInformation("With Last Played Date: {LastPlayedDate}", mostRecentUserData.LastPlayedDate); movieIsStale = true; } } else if (createdOutsideCutoff) { _loggingHelper.LogDebugInformation("Movie has no user data and was created outside of cutoff: {DateCreated}.", movie.DateCreated); _loggingHelper.LogDebugInformation("Adding {Movie} to stale movies.", movie); movieIsStale = true; } _loggingHelper.LogDebugInformation("-------------------------------------------------"); _loggingHelper.LogDebugInformation("End of scanning for movie: {Movie}", movie); return movieIsStale; } }