diff --git a/Trakt/Api/TraktApi.cs b/Trakt/Api/TraktApi.cs index 9edf478..8b60e4b 100644 --- a/Trakt/Api/TraktApi.cs +++ b/Trakt/Api/TraktApi.cs @@ -47,6 +47,7 @@ public class TraktApi private readonly IHttpClientFactory _httpClientFactory; private readonly IServerApplicationHost _appHost; private readonly IUserDataManager _userDataManager; + private readonly IUserManager _userManager; private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; /// @@ -56,15 +57,18 @@ public class TraktApi /// The . /// The . /// The . + /// The . public TraktApi( ILogger logger, IHttpClientFactory httpClientFactory, IServerApplicationHost appHost, - IUserDataManager userDataManager) + IUserDataManager userDataManager, + IUserManager userManager) { _httpClientFactory = httpClientFactory; _appHost = appHost; _userDataManager = userDataManager; + _userManager = userManager; _logger = logger; } @@ -707,10 +711,17 @@ public class TraktApi ArgumentOutOfRangeException.ThrowIfZero(movies.Count); ArgumentNullException.ThrowIfNull(traktUser); + var user = _userManager.GetUserById(traktUser.LinkedMbUserId); + if (user is null) + { + _logger.LogWarning("User id ({UserId}) linked to Trakt does not exist", traktUser.LinkedMbUserId); + return null; + } + var moviesPayload = movies.Select(m => { var lastPlayedDate = seen - ? _userDataManager.GetUserData(traktUser.LinkedMbUserId, m).LastPlayedDate + ? _userDataManager.GetUserData(user, m).LastPlayedDate : null; return new TraktMovieWatched @@ -784,6 +795,13 @@ public class TraktApi CancellationToken cancellationToken, bool useProviderIds = true) { + var user = _userManager.GetUserById(traktUser.LinkedMbUserId); + if (user is null) + { + _logger.LogWarning("User id ({UserId}) linked to Trakt does not exist", traktUser.LinkedMbUserId); + return null; + } + var data = new TraktSyncWatched { Episodes = new List(), @@ -793,7 +811,7 @@ public class TraktApi foreach (var episode in episodeChunk) { var lastPlayedDate = seen - ? _userDataManager.GetUserData(traktUser.LinkedMbUserId, episode) + ? _userDataManager.GetUserData(user, episode) .LastPlayedDate : null; diff --git a/Trakt/Api/TraktController.cs b/Trakt/Api/TraktController.cs index 9140732..767e0f5 100644 --- a/Trakt/Api/TraktController.cs +++ b/Trakt/Api/TraktController.cs @@ -37,15 +37,17 @@ public class TraktController : ControllerBase /// Instance of the interface. /// Instance of the interface. /// Instance of the interface. + /// Instance of the interface. public TraktController( IUserDataManager userDataManager, ILoggerFactory loggerFactory, IHttpClientFactory httpClientFactory, IServerApplicationHost appHost, - ILibraryManager libraryManager) + ILibraryManager libraryManager, + IUserManager userManager) { _logger = loggerFactory.CreateLogger(); - _traktApi = new TraktApi(loggerFactory.CreateLogger(), httpClientFactory, appHost, userDataManager); + _traktApi = new TraktApi(loggerFactory.CreateLogger(), httpClientFactory, appHost, userDataManager, userManager); _libraryManager = libraryManager; } diff --git a/Trakt/ScheduledTasks/SyncFromTraktTask.cs b/Trakt/ScheduledTasks/SyncFromTraktTask.cs index 519290a..ba73feb 100644 --- a/Trakt/ScheduledTasks/SyncFromTraktTask.cs +++ b/Trakt/ScheduledTasks/SyncFromTraktTask.cs @@ -58,7 +58,7 @@ public class SyncFromTraktTask : IScheduledTask _userDataManager = userDataManager; _libraryManager = libraryManager; _logger = loggerFactory.CreateLogger(); - _traktApi = new TraktApi(loggerFactory.CreateLogger(), httpClientFactory, appHost, userDataManager); + _traktApi = new TraktApi(loggerFactory.CreateLogger(), httpClientFactory, appHost, userDataManager, userManager); } /// @@ -206,7 +206,7 @@ public class SyncFromTraktTask : IScheduledTask cancellationToken.ThrowIfCancellationRequested(); var matchedWatchedMovie = Extensions.FindMatch(movie, traktWatchedMovies); var matchedPausedMovie = Extensions.FindMatch(movie, traktPausedMovies); - var userData = _userDataManager.GetUserData(user.Id, movie); + var userData = _userDataManager.GetUserData(user, movie); bool changed = false; if (matchedWatchedMovie != null) @@ -304,7 +304,7 @@ public class SyncFromTraktTask : IScheduledTask if (changed) { _userDataManager.SaveUserData( - user.Id, + user, movie, userData, UserDataSaveReason.Import, @@ -321,7 +321,7 @@ public class SyncFromTraktTask : IScheduledTask cancellationToken.ThrowIfCancellationRequested(); var matchedWatchedShow = Extensions.FindMatch(episode.Series, traktWatchedShows); var matchedPausedEpisode = Extensions.FindMatch(episode, traktPausedEpisodes); - var userData = _userDataManager.GetUserData(user.Id, episode); + var userData = _userDataManager.GetUserData(user, episode); bool changed = false; bool episodeWatched = false; @@ -455,7 +455,7 @@ public class SyncFromTraktTask : IScheduledTask if (changed) { _userDataManager.SaveUserData( - user.Id, + user, episode, userData, UserDataSaveReason.Import, diff --git a/Trakt/ScheduledTasks/SyncLibraryTask.cs b/Trakt/ScheduledTasks/SyncLibraryTask.cs index 073d8d4..5b8ce1a 100644 --- a/Trakt/ScheduledTasks/SyncLibraryTask.cs +++ b/Trakt/ScheduledTasks/SyncLibraryTask.cs @@ -60,7 +60,7 @@ public class SyncLibraryTask : IScheduledTask _userDataManager = userDataManager; _libraryManager = libraryManager; _logger = loggerFactory.CreateLogger(); - _traktApi = new TraktApi(loggerFactory.CreateLogger(), httpClientFactory, appHost, userDataManager); + _traktApi = new TraktApi(loggerFactory.CreateLogger(), httpClientFactory, appHost, userDataManager, userManager); } /// @@ -199,7 +199,7 @@ public class SyncLibraryTask : IScheduledTask foreach (var libraryMovie in movieItems) { cancellationToken.ThrowIfCancellationRequested(); - var userData = _userDataManager.GetUserData(user.Id, libraryMovie); + var userData = _userDataManager.GetUserData(user, libraryMovie); if (traktUser.SynchronizeCollections) { @@ -438,7 +438,7 @@ public class SyncLibraryTask : IScheduledTask foreach (var episode in episodeItems) { cancellationToken.ThrowIfCancellationRequested(); - var userData = _userDataManager.GetUserData(user.Id, episode); + var userData = _userDataManager.GetUserData(user, episode); var isPlayedTraktTv = false; var traktWatchedShow = Extensions.FindMatch(episode.Series, traktWatchedShows); diff --git a/Trakt/ServerMediator.cs b/Trakt/ServerMediator.cs index f84cdb5..52591f1 100644 --- a/Trakt/ServerMediator.cs +++ b/Trakt/ServerMediator.cs @@ -44,13 +44,15 @@ public class ServerMediator : IHostedService, IDisposable /// The . /// The . /// The . + /// The . public ServerMediator( ISessionManager sessionManager, IUserDataManager userDataManager, ILibraryManager libraryManager, ILoggerFactory loggerFactory, IHttpClientFactory httpClientFactory, - IServerApplicationHost appHost) + IServerApplicationHost appHost, + IUserManager userManager) { _sessionManager = sessionManager; _libraryManager = libraryManager; @@ -59,7 +61,7 @@ public class ServerMediator : IHostedService, IDisposable _logger = loggerFactory.CreateLogger(); _playbackState = new Dictionary(); - _traktApi = new TraktApi(loggerFactory.CreateLogger(), httpClientFactory, appHost, userDataManager); + _traktApi = new TraktApi(loggerFactory.CreateLogger(), httpClientFactory, appHost, userDataManager, userManager); _libraryManagerEventsHelper = new LibraryManagerEventsHelper(loggerFactory.CreateLogger(), _traktApi); _userDataManagerEventsHelper = new UserDataManagerEventsHelper(loggerFactory.CreateLogger(), _traktApi); }