From 9c84acb6faa9a3b9e5d1b3c5a873960738c7ee78 Mon Sep 17 00:00:00 2001 From: sephrat <34862846+sephrat@users.noreply.github.com> Date: Thu, 9 Mar 2023 19:28:13 +0100 Subject: [PATCH] First step towards played sync --- src/Ombi.Api.Emby/EmbyApi.cs | 31 + src/Ombi.Api.Emby/IBaseEmbyApi.cs | 2 + src/Ombi.DependencyInjection/IocExtensions.cs | 2 + .../Jobs/Emby/EmbyContentSync.cs | 125 +--- .../Jobs/Emby/EmbyLibrarySync.cs | 146 +++++ src/Ombi.Schedule/Jobs/Emby/EmbyPlayedSync.cs | 110 ++++ .../Jobs/Emby/IEmbyPlayedSync.cs | 6 + src/Ombi.Schedule/OmbiScheduler.cs | 2 + src/Ombi.Store/Context/ExternalContext.cs | 1 + src/Ombi.Store/Entities/MovieUserPlayed.cs | 10 + ...20230309182556_MovieUserPlayed.Designer.cs | 564 ++++++++++++++++++ .../20230309182556_MovieUserPlayed.cs | 32 + .../ExternalSqliteContextModelSnapshot.cs | 19 +- .../Repository/IUserPlayedMovieRepository.cs | 13 + .../Repository/UserPlayedMovieRepository.cs | 27 + .../ClientApp/src/app/services/job.service.ts | 8 + .../src/app/settings/emby/emby.component.html | 9 + .../src/app/settings/emby/emby.component.ts | 16 + src/Ombi/Controllers/V1/JobController.cs | 13 + 19 files changed, 1018 insertions(+), 118 deletions(-) create mode 100644 src/Ombi.Schedule/Jobs/Emby/EmbyLibrarySync.cs create mode 100644 src/Ombi.Schedule/Jobs/Emby/EmbyPlayedSync.cs create mode 100644 src/Ombi.Schedule/Jobs/Emby/IEmbyPlayedSync.cs create mode 100644 src/Ombi.Store/Entities/MovieUserPlayed.cs create mode 100644 src/Ombi.Store/Migrations/ExternalSqlite/20230309182556_MovieUserPlayed.Designer.cs create mode 100644 src/Ombi.Store/Migrations/ExternalSqlite/20230309182556_MovieUserPlayed.cs create mode 100644 src/Ombi.Store/Repository/IUserPlayedMovieRepository.cs create mode 100644 src/Ombi.Store/Repository/UserPlayedMovieRepository.cs diff --git a/src/Ombi.Api.Emby/EmbyApi.cs b/src/Ombi.Api.Emby/EmbyApi.cs index e9e5f0fca..1d951f242 100644 --- a/src/Ombi.Api.Emby/EmbyApi.cs +++ b/src/Ombi.Api.Emby/EmbyApi.cs @@ -248,5 +248,36 @@ namespace Ombi.Api.Emby req.AddContentHeader("Content-Type", "application/json"); req.AddHeader("Device", "Ombi"); } + + public async Task> GetMoviesPlayed(string apiKey, string parentIdFilder, int startIndex, int count, string userId, string baseUri) + { + return await GetPlayed("Movie", apiKey, userId, baseUri, startIndex, count, parentIdFilder); + } + + private async Task> GetPlayed(string type, string apiKey, string userId, string baseUri, int startIndex, int count, string parentIdFilder = default) + { + var request = new Request($"emby/items", baseUri, HttpMethod.Get); + + request.AddQueryString("Recursive", true.ToString()); + request.AddQueryString("IncludeItemTypes", type); + request.AddQueryString("Fields", "ProviderIds"); + request.AddQueryString("UserId", userId); + + // paginate and display recently played items first + request.AddQueryString("sortBy", "DatePlayed"); + request.AddQueryString("SortOrder", "Descending"); + request.AddQueryString("startIndex", startIndex.ToString()); + request.AddQueryString("limit", count.ToString()); + + if (!string.IsNullOrEmpty(parentIdFilder)) + { + request.AddQueryString("ParentId", parentIdFilder); + } + + AddHeaders(request, apiKey); + + var obj = await Api.Request>(request); + return obj; + } } } diff --git a/src/Ombi.Api.Emby/IBaseEmbyApi.cs b/src/Ombi.Api.Emby/IBaseEmbyApi.cs index 248c0a88f..582eac0c9 100644 --- a/src/Ombi.Api.Emby/IBaseEmbyApi.cs +++ b/src/Ombi.Api.Emby/IBaseEmbyApi.cs @@ -32,5 +32,7 @@ namespace Ombi.Api.Emby Task> RecentlyAddedMovies(string apiKey, string parentIdFilder, int startIndex, int count, string userId, string baseUri); Task> RecentlyAddedEpisodes(string apiKey, string parentIdFilder, int startIndex, int count, string userId, string baseUri); Task> RecentlyAddedShows(string apiKey, string parentIdFilder, int startIndex, int count, string userId, string baseUri); + + Task> GetMoviesPlayed(string apiKey, string parentIdFilder, int startIndex, int count, string userId, string baseUri); } } \ No newline at end of file diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index 09d99d4b7..c9bcc13d3 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -197,6 +197,7 @@ namespace Ombi.DependencyInjection services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); @@ -244,6 +245,7 @@ namespace Ombi.DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs index 35cc66be4..89a09a164 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs @@ -19,108 +19,29 @@ using MediaType = Ombi.Store.Entities.MediaType; namespace Ombi.Schedule.Jobs.Emby { - public class EmbyContentSync : IEmbyContentSync + public class EmbyContentSync : EmbyLibrarySync, IEmbyContentSync { public EmbyContentSync(ISettingsService settings, IEmbyApiFactory api, ILogger logger, - IEmbyContentRepository repo, INotificationHubService notification) + IEmbyContentRepository repo, INotificationHubService notification): + base(settings, api, logger, notification) { - _logger = logger; - _settings = settings; - _apiFactory = api; _repo = repo; - _notification = notification; } - private readonly ILogger _logger; - private readonly ISettingsService _settings; - private readonly IEmbyApiFactory _apiFactory; private readonly IEmbyContentRepository _repo; - private readonly INotificationHubService _notification; - private const int AmountToTake = 100; - private IEmbyApi Api { get; set; } - - public async Task Execute(IJobExecutionContext context) + public async override Task Execute(IJobExecutionContext context) { - JobDataMap dataMap = context.JobDetail.JobDataMap; - var recentlyAddedSearch = false; - if (dataMap.TryGetValue(JobDataKeys.EmbyRecentlyAddedSearch, out var recentlyAddedObj)) - { - recentlyAddedSearch = Convert.ToBoolean(recentlyAddedObj); - } - var embySettings = await _settings.GetSettingsAsync(); - if (!embySettings.Enable) - return; + await base.Execute(context); - Api = _apiFactory.CreateClient(embySettings); - - await _notification.SendNotificationToAdmins(recentlyAddedSearch ? "Emby Recently Added Started" : "Emby Content Sync Started"); - - foreach (var server in embySettings.Servers) - { - try - { - await StartServerCache(server, recentlyAddedSearch); - } - catch (Exception e) - { - await _notification.SendNotificationToAdmins("Emby Content Sync Failed"); - _logger.LogError(e, "Exception when caching Emby for server {0}", server.Name); - } - } - - await _notification.SendNotificationToAdmins("Emby Content Sync Finished"); // Episodes - - - await OmbiQuartz.Scheduler.TriggerJob(new JobKey(nameof(IEmbyEpisodeSync), "Emby"), new JobDataMap(new Dictionary { { JobDataKeys.EmbyRecentlyAddedSearch, recentlyAddedSearch.ToString() } })); + await OmbiQuartz.Scheduler.TriggerJob(new JobKey(nameof(IEmbyEpisodeSync), "Emby"), new JobDataMap(new Dictionary { { JobDataKeys.EmbyRecentlyAddedSearch, recentlyAdded.ToString() } })); } - private async Task StartServerCache(EmbyServers server, bool recentlyAdded) - { - if (!ValidateSettings(server)) - { - return; - } - - - if (server.EmbySelectedLibraries.Any() && server.EmbySelectedLibraries.Any(x => x.Enabled)) - { - var movieLibsToFilter = server.EmbySelectedLibraries.Where(x => x.Enabled && x.CollectionType == "movies"); - - foreach (var movieParentIdFilder in movieLibsToFilter) - { - _logger.LogInformation($"Scanning Lib '{movieParentIdFilder.Title}'"); - await ProcessMovies(server, recentlyAdded, movieParentIdFilder.Key); - } - - var tvLibsToFilter = server.EmbySelectedLibraries.Where(x => x.Enabled && x.CollectionType == "tvshows"); - foreach (var tvParentIdFilter in tvLibsToFilter) - { - _logger.LogInformation($"Scanning Lib '{tvParentIdFilter.Title}'"); - await ProcessTv(server, recentlyAdded, tvParentIdFilter.Key); - } - - - var mixedLibs = server.EmbySelectedLibraries.Where(x => x.Enabled && x.CollectionType == "mixed"); - foreach (var m in mixedLibs) - { - _logger.LogInformation($"Scanning Lib '{m.Title}'"); - await ProcessTv(server, recentlyAdded, m.Key); - await ProcessMovies(server, recentlyAdded, m.Key); - } - } - else - { - await ProcessMovies(server, recentlyAdded); - await ProcessTv(server, recentlyAdded); - } - } - - private async Task ProcessTv(EmbyServers server, bool recentlyAdded, string parentId = default) + protected async override Task ProcessTv(EmbyServers server, string parentId = default) { // TV Time var mediaToAdd = new HashSet(); @@ -196,7 +117,7 @@ namespace Ombi.Schedule.Jobs.Emby await _repo.AddRange(mediaToAdd); } - private async Task ProcessMovies(EmbyServers server, bool recentlyAdded, string parentId = default) + protected override async Task ProcessMovies(EmbyServers server, string parentId = default) { EmbyItemContainer movies; if (recentlyAdded) @@ -319,36 +240,6 @@ namespace Ombi.Schedule.Jobs.Emby content.Quality = has4K ? null : quality; content.Has4K = has4K; } - - private bool ValidateSettings(EmbyServers server) - { - if (server?.Ip == null || string.IsNullOrEmpty(server?.ApiKey)) - { - _logger.LogInformation(LoggingEvents.EmbyContentCacher, $"Server {server?.Name} is not configured correctly"); - return false; - } - - return true; - } - - private bool _disposed; - protected virtual void Dispose(bool disposing) - { - if (_disposed) - return; - - if (disposing) - { - //_settings?.Dispose(); - } - _disposed = true; - } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } } } diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyLibrarySync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyLibrarySync.cs new file mode 100644 index 000000000..9695530de --- /dev/null +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyLibrarySync.cs @@ -0,0 +1,146 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Ombi.Api.Emby; +using Ombi.Core.Settings; +using Ombi.Core.Settings.Models.External; +using Ombi.Helpers; +using Ombi.Hubs; +using Quartz; + +namespace Ombi.Schedule.Jobs.Emby +{ + public abstract class EmbyLibrarySync + { + public EmbyLibrarySync(ISettingsService settings, IEmbyApiFactory api, ILogger logger, + INotificationHubService notification) + { + _logger = logger; + _settings = settings; + _apiFactory = api; + _notification = notification; + } + + protected readonly ILogger _logger; + protected readonly ISettingsService _settings; + protected readonly IEmbyApiFactory _apiFactory; + protected bool recentlyAdded; + protected readonly INotificationHubService _notification; + + protected const int AmountToTake = 100; + + protected IEmbyApi Api { get; set; } + + public virtual async Task Execute(IJobExecutionContext context) + { + + JobDataMap dataMap = context.JobDetail.JobDataMap; + if (dataMap.TryGetValue(JobDataKeys.EmbyRecentlyAddedSearch, out var recentlyAddedObj)) + { + recentlyAdded = Convert.ToBoolean(recentlyAddedObj); + } + + await _notification.SendNotificationToAdmins(recentlyAdded ? "Emby Recently Added Started" : "Emby Content Sync Started"); + + + var embySettings = await _settings.GetSettingsAsync(); + if (!embySettings.Enable) + return; + + Api = _apiFactory.CreateClient(embySettings); + + foreach (var server in embySettings.Servers) + { + try + { + await StartServerCache(server); + } + catch (Exception e) + { + await _notification.SendNotificationToAdmins("Emby Content Sync Failed"); + _logger.LogError(e, "Exception when caching Emby for server {0}", server.Name); + } + } + + await _notification.SendNotificationToAdmins("Emby Content Sync Finished"); + } + + + private async Task StartServerCache(EmbyServers server) + { + if (!ValidateSettings(server)) + { + return; + } + + + if (server.EmbySelectedLibraries.Any() && server.EmbySelectedLibraries.Any(x => x.Enabled)) + { + var movieLibsToFilter = server.EmbySelectedLibraries.Where(x => x.Enabled && x.CollectionType == "movies"); + + foreach (var movieParentIdFilder in movieLibsToFilter) + { + _logger.LogInformation($"Scanning Lib '{movieParentIdFilder.Title}'"); + await ProcessMovies(server, movieParentIdFilder.Key); + } + + var tvLibsToFilter = server.EmbySelectedLibraries.Where(x => x.Enabled && x.CollectionType == "tvshows"); + foreach (var tvParentIdFilter in tvLibsToFilter) + { + _logger.LogInformation($"Scanning Lib '{tvParentIdFilter.Title}'"); + await ProcessTv(server, tvParentIdFilter.Key); + } + + + var mixedLibs = server.EmbySelectedLibraries.Where(x => x.Enabled && x.CollectionType == "mixed"); + foreach (var m in mixedLibs) + { + _logger.LogInformation($"Scanning Lib '{m.Title}'"); + await ProcessTv(server, m.Key); + await ProcessMovies(server, m.Key); + } + } + else + { + await ProcessMovies(server); + await ProcessTv(server); + } + } + + protected abstract Task ProcessTv(EmbyServers server, string parentId = default); + + protected abstract Task ProcessMovies(EmbyServers server, string parentId = default); + + private bool ValidateSettings(EmbyServers server) + { + if (server?.Ip == null || string.IsNullOrEmpty(server?.ApiKey)) + { + _logger.LogInformation(LoggingEvents.EmbyContentCacher, $"Server {server?.Name} is not configured correctly"); + return false; + } + + return true; + } + + private bool _disposed; + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + //_settings?.Dispose(); + } + _disposed = true; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + } + +} diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyPlayedSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyPlayedSync.cs new file mode 100644 index 000000000..05f0666d0 --- /dev/null +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyPlayedSync.cs @@ -0,0 +1,110 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Ombi.Api.Emby; +using Ombi.Api.Emby.Models; +using Ombi.Api.Emby.Models.Movie; +using Ombi.Core.Authentication; +using Ombi.Core.Settings; +using Ombi.Core.Settings.Models.External; +using Ombi.Helpers; +using Ombi.Hubs; +using Ombi.Store.Entities; +using Ombi.Store.Repository; + +namespace Ombi.Schedule.Jobs.Emby +{ + public class EmbyPlayedSync : EmbyLibrarySync, IEmbyPlayedSync + { + public EmbyPlayedSync(ISettingsService settings, IEmbyApiFactory api, ILogger logger, + IUserPlayedMovieRepository repo, INotificationHubService notification, OmbiUserManager user) : base(settings, api, logger, notification) + { + _userManager = user; + _repo = repo; + } + private OmbiUserManager _userManager { get; } + + private readonly IUserPlayedMovieRepository _repo; + + protected override Task ProcessTv(EmbyServers server, string parentId = default) + { + // TODO + return Task.CompletedTask; + } + + protected async override Task ProcessMovies(EmbyServers server, string parentId = default) + { + + var allUsers = await _userManager.Users.Where(x => x.UserType == UserType.EmbyUser || x.UserType == UserType.EmbyConnectUser).ToListAsync(); + foreach (var user in allUsers) + { + await ProcessMoviesUser(server, user, parentId); + } + } + + + private async Task ProcessMoviesUser(EmbyServers server, OmbiUser user, string parentId = default) + { + EmbyItemContainer movies; + if (recentlyAdded) + { + var recentlyAddedAmountToTake = 5; // to be adjusted? + movies = await Api.GetMoviesPlayed(server.ApiKey, parentId, 0, recentlyAddedAmountToTake, user.ProviderUserId, server.FullUri); + // Setting this so we don't attempt to grab more than we need + if (movies.TotalRecordCount > recentlyAddedAmountToTake) + { + movies.TotalRecordCount = recentlyAddedAmountToTake; + } + } + else + { + movies = await Api.GetMoviesPlayed(server.ApiKey, parentId, 0, AmountToTake, user.ProviderUserId, server.FullUri); + } + var totalCount = movies.TotalRecordCount; + var processed = 0; + var mediaToAdd = new HashSet(); + _logger.LogCritical($"Adding {totalCount.ToString()} for {user.UserName}"); + while (processed < totalCount) + { + foreach (var movie in movies.Items) + { + await ProcessMovie(movie, user, mediaToAdd, server); + processed++; + } + + // Get the next batch + // Recently Added should never be checked as the TotalRecords should equal the amount to take + if (!recentlyAdded) + { + movies = await Api.GetMoviesPlayed(server.ApiKey, parentId, processed, AmountToTake, user.ProviderUserId, server.FullUri); + } + await _repo.AddRange(mediaToAdd); + mediaToAdd.Clear(); + } + } + + private async Task ProcessMovie(EmbyMovie movieInfo, OmbiUser user, ICollection content, EmbyServers server) + { + if (movieInfo.ProviderIds.Tmdb.IsNullOrEmpty()) + { + _logger.LogWarning($"Movie {movieInfo.Name} has no relevant metadata. Skipping."); + return; + } + var userPlayedMovie = new UserPlayedMovie() + { + TheMovieDbId = movieInfo.ProviderIds.Tmdb, + UserId = user.Id + }; + // Check if it exists + var existingMovie = await _repo.Get(userPlayedMovie.TheMovieDbId, userPlayedMovie.UserId); + var alreadyGoingToAdd = content.Any(x => x.TheMovieDbId == userPlayedMovie.TheMovieDbId && x.UserId == userPlayedMovie.UserId); + if (existingMovie == null && !alreadyGoingToAdd) + { + content.Add(userPlayedMovie); + } + } + } + +} diff --git a/src/Ombi.Schedule/Jobs/Emby/IEmbyPlayedSync.cs b/src/Ombi.Schedule/Jobs/Emby/IEmbyPlayedSync.cs new file mode 100644 index 000000000..80434bddb --- /dev/null +++ b/src/Ombi.Schedule/Jobs/Emby/IEmbyPlayedSync.cs @@ -0,0 +1,6 @@ +namespace Ombi.Schedule.Jobs.Emby +{ + public interface IEmbyPlayedSync : IBaseJob + { + } +} \ No newline at end of file diff --git a/src/Ombi.Schedule/OmbiScheduler.cs b/src/Ombi.Schedule/OmbiScheduler.cs index 41602f641..1b1c10d2a 100644 --- a/src/Ombi.Schedule/OmbiScheduler.cs +++ b/src/Ombi.Schedule/OmbiScheduler.cs @@ -98,6 +98,8 @@ namespace Ombi.Schedule { await OmbiQuartz.Instance.AddJob(nameof(IEmbyContentSync), "Emby", JobSettingsHelper.EmbyContent(s)); await OmbiQuartz.Instance.AddJob(nameof(IEmbyContentSync) + "RecentlyAdded", "Emby", JobSettingsHelper.EmbyRecentlyAddedSync(s), new Dictionary { { JobDataKeys.EmbyRecentlyAddedSearch, "true" } }); + await OmbiQuartz.Instance.AddJob(nameof(IEmbyPlayedSync), "Emby", JobSettingsHelper.EmbyContent(s)); + await OmbiQuartz.Instance.AddJob(nameof(IEmbyPlayedSync) + "RecentlyAdded", "Emby", JobSettingsHelper.EmbyRecentlyAddedSync(s), new Dictionary { { JobDataKeys.EmbyRecentlyAddedSearch, "true" } }); await OmbiQuartz.Instance.AddJob(nameof(IEmbyEpisodeSync), "Emby", null); await OmbiQuartz.Instance.AddJob(nameof(IEmbyAvaliabilityChecker), "Emby", null); await OmbiQuartz.Instance.AddJob(nameof(IEmbyUserImporter), "Emby", JobSettingsHelper.UserImporter(s)); diff --git a/src/Ombi.Store/Context/ExternalContext.cs b/src/Ombi.Store/Context/ExternalContext.cs index f13c1e74f..c54c39beb 100644 --- a/src/Ombi.Store/Context/ExternalContext.cs +++ b/src/Ombi.Store/Context/ExternalContext.cs @@ -41,6 +41,7 @@ namespace Ombi.Store.Context public DbSet SonarrEpisodeCache { get; set; } public DbSet SickRageCache { get; set; } public DbSet SickRageEpisodeCache { get; set; } + public DbSet UserPlayedMovie { get; set; } protected override void OnModelCreating(ModelBuilder builder) { diff --git a/src/Ombi.Store/Entities/MovieUserPlayed.cs b/src/Ombi.Store/Entities/MovieUserPlayed.cs new file mode 100644 index 000000000..85f6ba6d3 --- /dev/null +++ b/src/Ombi.Store/Entities/MovieUserPlayed.cs @@ -0,0 +1,10 @@ +using System.ComponentModel.DataAnnotations.Schema; + +namespace Ombi.Store.Entities +{ + public class UserPlayedMovie : Entity + { + public string TheMovieDbId { get; set; } + public string UserId { get; set; } + } +} \ No newline at end of file diff --git a/src/Ombi.Store/Migrations/ExternalSqlite/20230309182556_MovieUserPlayed.Designer.cs b/src/Ombi.Store/Migrations/ExternalSqlite/20230309182556_MovieUserPlayed.Designer.cs new file mode 100644 index 000000000..d01d63968 --- /dev/null +++ b/src/Ombi.Store/Migrations/ExternalSqlite/20230309182556_MovieUserPlayed.Designer.cs @@ -0,0 +1,564 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Ombi.Store.Context.Sqlite; + +#nullable disable + +namespace Ombi.Store.Migrations.ExternalSqlite +{ + [DbContext(typeof(ExternalSqliteContext))] + [Migration("20230309182556_MovieUserPlayed")] + partial class MovieUserPlayed + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "6.0.9"); + + modelBuilder.Entity("Ombi.Store.Entities.CouchPotatoCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("TheMovieDbId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("CouchPotatoCache"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.EmbyContent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AddedAt") + .HasColumnType("TEXT"); + + b.Property("EmbyId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Has4K") + .HasColumnType("INTEGER"); + + b.Property("ImdbId") + .HasColumnType("TEXT"); + + b.Property("ProviderId") + .HasColumnType("TEXT"); + + b.Property("Quality") + .HasColumnType("TEXT"); + + b.Property("TheMovieDbId") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TvDbId") + .HasColumnType("TEXT"); + + b.Property("Type") + .HasColumnType("INTEGER"); + + b.Property("Url") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("EmbyContent"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.EmbyEpisode", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AddedAt") + .HasColumnType("TEXT"); + + b.Property("EmbyId") + .HasColumnType("TEXT"); + + b.Property("EpisodeNumber") + .HasColumnType("INTEGER"); + + b.Property("ImdbId") + .HasColumnType("TEXT"); + + b.Property("ParentId") + .HasColumnType("TEXT"); + + b.Property("ProviderId") + .HasColumnType("TEXT"); + + b.Property("SeasonNumber") + .HasColumnType("INTEGER"); + + b.Property("TheMovieDbId") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TvDbId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ParentId"); + + b.ToTable("EmbyEpisode"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.JellyfinContent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AddedAt") + .HasColumnType("TEXT"); + + b.Property("Has4K") + .HasColumnType("INTEGER"); + + b.Property("ImdbId") + .HasColumnType("TEXT"); + + b.Property("JellyfinId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ProviderId") + .HasColumnType("TEXT"); + + b.Property("Quality") + .HasColumnType("TEXT"); + + b.Property("TheMovieDbId") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TvDbId") + .HasColumnType("TEXT"); + + b.Property("Type") + .HasColumnType("INTEGER"); + + b.Property("Url") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("JellyfinContent"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.JellyfinEpisode", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AddedAt") + .HasColumnType("TEXT"); + + b.Property("EpisodeNumber") + .HasColumnType("INTEGER"); + + b.Property("ImdbId") + .HasColumnType("TEXT"); + + b.Property("JellyfinId") + .HasColumnType("TEXT"); + + b.Property("ParentId") + .HasColumnType("TEXT"); + + b.Property("ProviderId") + .HasColumnType("TEXT"); + + b.Property("SeasonNumber") + .HasColumnType("INTEGER"); + + b.Property("TheMovieDbId") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TvDbId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("ParentId"); + + b.ToTable("JellyfinEpisode"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.LidarrAlbumCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AddedAt") + .HasColumnType("TEXT"); + + b.Property("ArtistId") + .HasColumnType("INTEGER"); + + b.Property("ForeignAlbumId") + .HasColumnType("TEXT"); + + b.Property("Monitored") + .HasColumnType("INTEGER"); + + b.Property("PercentOfTracks") + .HasColumnType("TEXT"); + + b.Property("ReleaseDate") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TrackCount") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("LidarrAlbumCache"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.LidarrArtistCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ArtistId") + .HasColumnType("INTEGER"); + + b.Property("ArtistName") + .HasColumnType("TEXT"); + + b.Property("ForeignArtistId") + .HasColumnType("TEXT"); + + b.Property("Monitored") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("LidarrArtistCache"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.PlexEpisode", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("EpisodeNumber") + .HasColumnType("INTEGER"); + + b.Property("GrandparentKey") + .HasColumnType("TEXT"); + + b.Property("Key") + .HasColumnType("TEXT"); + + b.Property("ParentKey") + .HasColumnType("TEXT"); + + b.Property("SeasonNumber") + .HasColumnType("INTEGER"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("GrandparentKey"); + + b.ToTable("PlexEpisode"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.PlexSeasonsContent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("ParentKey") + .HasColumnType("TEXT"); + + b.Property("PlexContentId") + .HasColumnType("TEXT"); + + b.Property("PlexServerContentId") + .HasColumnType("INTEGER"); + + b.Property("SeasonKey") + .HasColumnType("TEXT"); + + b.Property("SeasonNumber") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("PlexServerContentId"); + + b.ToTable("PlexSeasonsContent"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.PlexServerContent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AddedAt") + .HasColumnType("TEXT"); + + b.Property("Has4K") + .HasColumnType("INTEGER"); + + b.Property("ImdbId") + .HasColumnType("TEXT"); + + b.Property("Key") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Quality") + .HasColumnType("TEXT"); + + b.Property("ReleaseYear") + .HasColumnType("TEXT"); + + b.Property("RequestId") + .HasColumnType("INTEGER"); + + b.Property("TheMovieDbId") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TvDbId") + .HasColumnType("TEXT"); + + b.Property("Type") + .HasColumnType("INTEGER"); + + b.Property("Url") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("PlexServerContent"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.PlexWatchlistHistory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("TmdbId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("PlexWatchlistHistory"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.RadarrCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Has4K") + .HasColumnType("INTEGER"); + + b.Property("HasFile") + .HasColumnType("INTEGER"); + + b.Property("HasRegular") + .HasColumnType("INTEGER"); + + b.Property("TheMovieDbId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("RadarrCache"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.SickRageCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("TvDbId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("SickRageCache"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.SickRageEpisodeCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("EpisodeNumber") + .HasColumnType("INTEGER"); + + b.Property("SeasonNumber") + .HasColumnType("INTEGER"); + + b.Property("TvDbId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("SickRageEpisodeCache"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.SonarrCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("TheMovieDbId") + .HasColumnType("INTEGER"); + + b.Property("TvDbId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("SonarrCache"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.SonarrEpisodeCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("EpisodeNumber") + .HasColumnType("INTEGER"); + + b.Property("HasFile") + .HasColumnType("INTEGER"); + + b.Property("MovieDbId") + .HasColumnType("INTEGER"); + + b.Property("SeasonNumber") + .HasColumnType("INTEGER"); + + b.Property("TvDbId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.ToTable("SonarrEpisodeCache"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.UserPlayedMovie", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("TheMovieDbId") + .HasColumnType("TEXT"); + + b.Property("UserId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("UserPlayedMovie"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.EmbyEpisode", b => + { + b.HasOne("Ombi.Store.Entities.EmbyContent", "Series") + .WithMany("Episodes") + .HasForeignKey("ParentId") + .HasPrincipalKey("EmbyId"); + + b.Navigation("Series"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.JellyfinEpisode", b => + { + b.HasOne("Ombi.Store.Entities.JellyfinContent", "Series") + .WithMany("Episodes") + .HasForeignKey("ParentId") + .HasPrincipalKey("JellyfinId"); + + b.Navigation("Series"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.PlexEpisode", b => + { + b.HasOne("Ombi.Store.Entities.PlexServerContent", "Series") + .WithMany("Episodes") + .HasForeignKey("GrandparentKey") + .HasPrincipalKey("Key"); + + b.Navigation("Series"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.PlexSeasonsContent", b => + { + b.HasOne("Ombi.Store.Entities.PlexServerContent", null) + .WithMany("Seasons") + .HasForeignKey("PlexServerContentId"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.EmbyContent", b => + { + b.Navigation("Episodes"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.JellyfinContent", b => + { + b.Navigation("Episodes"); + }); + + modelBuilder.Entity("Ombi.Store.Entities.PlexServerContent", b => + { + b.Navigation("Episodes"); + + b.Navigation("Seasons"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Ombi.Store/Migrations/ExternalSqlite/20230309182556_MovieUserPlayed.cs b/src/Ombi.Store/Migrations/ExternalSqlite/20230309182556_MovieUserPlayed.cs new file mode 100644 index 000000000..f24fbf40c --- /dev/null +++ b/src/Ombi.Store/Migrations/ExternalSqlite/20230309182556_MovieUserPlayed.cs @@ -0,0 +1,32 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Ombi.Store.Migrations.ExternalSqlite +{ + public partial class MovieUserPlayed : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "UserPlayedMovie", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + TheMovieDbId = table.Column(type: "TEXT", nullable: true), + UserId = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_UserPlayedMovie", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "UserPlayedMovie"); + } + } +} diff --git a/src/Ombi.Store/Migrations/ExternalSqlite/ExternalSqliteContextModelSnapshot.cs b/src/Ombi.Store/Migrations/ExternalSqlite/ExternalSqliteContextModelSnapshot.cs index 2f5de3382..092ec9c33 100644 --- a/src/Ombi.Store/Migrations/ExternalSqlite/ExternalSqliteContextModelSnapshot.cs +++ b/src/Ombi.Store/Migrations/ExternalSqlite/ExternalSqliteContextModelSnapshot.cs @@ -15,7 +15,7 @@ namespace Ombi.Store.Migrations.ExternalSqlite protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "6.0.0"); + modelBuilder.HasAnnotation("ProductVersion", "6.0.9"); modelBuilder.Entity("Ombi.Store.Entities.CouchPotatoCache", b => { @@ -486,6 +486,23 @@ namespace Ombi.Store.Migrations.ExternalSqlite b.ToTable("SonarrEpisodeCache"); }); + modelBuilder.Entity("Ombi.Store.Entities.UserPlayedMovie", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("TheMovieDbId") + .HasColumnType("TEXT"); + + b.Property("UserId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("UserPlayedMovie"); + }); + modelBuilder.Entity("Ombi.Store.Entities.EmbyEpisode", b => { b.HasOne("Ombi.Store.Entities.EmbyContent", "Series") diff --git a/src/Ombi.Store/Repository/IUserPlayedMovieRepository.cs b/src/Ombi.Store/Repository/IUserPlayedMovieRepository.cs new file mode 100644 index 000000000..cad79fd96 --- /dev/null +++ b/src/Ombi.Store/Repository/IUserPlayedMovieRepository.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Ombi.Store.Entities; + +namespace Ombi.Store.Repository +{ + public interface IUserPlayedMovieRepository : IExternalRepository + { + Task Get(string theMovieDbId, string userId); + } +} \ No newline at end of file diff --git a/src/Ombi.Store/Repository/UserPlayedMovieRepository.cs b/src/Ombi.Store/Repository/UserPlayedMovieRepository.cs new file mode 100644 index 000000000..77cb08bb2 --- /dev/null +++ b/src/Ombi.Store/Repository/UserPlayedMovieRepository.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query; +using Ombi.Store.Context; +using Ombi.Store.Entities; + +namespace Ombi.Store.Repository +{ + public class UserPlayedMovieRepository : ExternalRepository, IUserPlayedMovieRepository + { + protected ExternalContext Db { get; } + public UserPlayedMovieRepository(ExternalContext db) : base(db) + { + Db = db; + } + + public async Task Get(string theMovieDbId, string userId) + { + return await Db.UserPlayedMovie.FirstOrDefaultAsync(x => x.TheMovieDbId == theMovieDbId && x.UserId == userId); + + } + } +} \ No newline at end of file diff --git a/src/Ombi/ClientApp/src/app/services/job.service.ts b/src/Ombi/ClientApp/src/app/services/job.service.ts index 6cece68d9..d10ff6a30 100644 --- a/src/Ombi/ClientApp/src/app/services/job.service.ts +++ b/src/Ombi/ClientApp/src/app/services/job.service.ts @@ -51,6 +51,10 @@ export class JobService extends ServiceHelpers { return this.http.post(`${this.url}embyrecentlyadded/`, {headers: this.headers}); } + public runEmbyRecentlyPlayedCacher(): Observable { + return this.http.post(`${this.url}embyrecentlyplayed/`, {headers: this.headers}); + } + public clearMediaserverData(): Observable { return this.http.post(`${this.url}clearmediaserverdata/`, {headers: this.headers}); } @@ -59,6 +63,10 @@ export class JobService extends ServiceHelpers { return this.http.post(`${this.url}embycontentcacher/`, {headers: this.headers}); } + public runEmbyPlayedCacher(): Observable { + return this.http.post(`${this.url}embyplayedcacher/`, {headers: this.headers}); + } + public runJellyfinCacher(): Observable { return this.http.post(`${this.url}jellyfincontentcacher/`, {headers: this.headers}); } diff --git a/src/Ombi/ClientApp/src/app/settings/emby/emby.component.html b/src/Ombi/ClientApp/src/app/settings/emby/emby.component.html index 11d19c5ca..fd0bd1cef 100644 --- a/src/Ombi/ClientApp/src/app/settings/emby/emby.component.html +++ b/src/Ombi/ClientApp/src/app/settings/emby/emby.component.html @@ -115,6 +115,15 @@ class="mat-focus-indicator mat-stroked-button mat-button-base">Manually Run Recently Added Sync +
+
+ +
+
+
+ +