Skip to content

Commit

Permalink
10.10
Browse files Browse the repository at this point in the history
  • Loading branch information
crobibero committed Oct 14, 2024
1 parent 74b894c commit a673d90
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
24 changes: 21 additions & 3 deletions Trakt/Api/TraktApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand All @@ -56,15 +57,18 @@ public class TraktApi
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
/// <param name="appHost">The <see cref="IServerApplicationHost"/>.</param>
/// <param name="userDataManager">The <see cref="IUserDataManager"/>.</param>
/// <param name="userManager">The <see cref="IUserManager"/>.</param>
public TraktApi(
ILogger<TraktApi> logger,
IHttpClientFactory httpClientFactory,
IServerApplicationHost appHost,
IUserDataManager userDataManager)
IUserDataManager userDataManager,
IUserManager userManager)
{
_httpClientFactory = httpClientFactory;
_appHost = appHost;
_userDataManager = userDataManager;
_userManager = userManager;
_logger = logger;
}

Expand Down Expand Up @@ -707,10 +711,17 @@ public async Task<List<TraktSyncResponse>> SendMoviePlaystateUpdates(
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
Expand Down Expand Up @@ -784,6 +795,13 @@ private async Task<TraktSyncResponse> SendEpisodePlaystateUpdatesInternalAsync(
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<TraktEpisodeWatched>(),
Expand All @@ -793,7 +811,7 @@ private async Task<TraktSyncResponse> SendEpisodePlaystateUpdatesInternalAsync(
foreach (var episode in episodeChunk)
{
var lastPlayedDate = seen
? _userDataManager.GetUserData(traktUser.LinkedMbUserId, episode)
? _userDataManager.GetUserData(user, episode)
.LastPlayedDate
: null;

Expand Down
6 changes: 4 additions & 2 deletions Trakt/Api/TraktController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ public class TraktController : ControllerBase
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
/// <param name="httpClientFactory">Instance of the <see cref="IHttpClientFactory"/> interface.</param>
/// <param name="appHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
public TraktController(
IUserDataManager userDataManager,
ILoggerFactory loggerFactory,
IHttpClientFactory httpClientFactory,
IServerApplicationHost appHost,
ILibraryManager libraryManager)
ILibraryManager libraryManager,
IUserManager userManager)
{
_logger = loggerFactory.CreateLogger<TraktController>();
_traktApi = new TraktApi(loggerFactory.CreateLogger<TraktApi>(), httpClientFactory, appHost, userDataManager);
_traktApi = new TraktApi(loggerFactory.CreateLogger<TraktApi>(), httpClientFactory, appHost, userDataManager, userManager);
_libraryManager = libraryManager;
}

Expand Down
10 changes: 5 additions & 5 deletions Trakt/ScheduledTasks/SyncFromTraktTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public SyncFromTraktTask(
_userDataManager = userDataManager;
_libraryManager = libraryManager;
_logger = loggerFactory.CreateLogger<SyncFromTraktTask>();
_traktApi = new TraktApi(loggerFactory.CreateLogger<TraktApi>(), httpClientFactory, appHost, userDataManager);
_traktApi = new TraktApi(loggerFactory.CreateLogger<TraktApi>(), httpClientFactory, appHost, userDataManager, userManager);
}

/// <inheritdoc />
Expand Down Expand Up @@ -206,7 +206,7 @@ private async Task SyncTraktDataForUser(User user, double currentProgress, IProg
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)
Expand Down Expand Up @@ -304,7 +304,7 @@ private async Task SyncTraktDataForUser(User user, double currentProgress, IProg
if (changed)
{
_userDataManager.SaveUserData(
user.Id,
user,
movie,
userData,
UserDataSaveReason.Import,
Expand All @@ -321,7 +321,7 @@ private async Task SyncTraktDataForUser(User user, double currentProgress, IProg
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;

Expand Down Expand Up @@ -455,7 +455,7 @@ private async Task SyncTraktDataForUser(User user, double currentProgress, IProg
if (changed)
{
_userDataManager.SaveUserData(
user.Id,
user,
episode,
userData,
UserDataSaveReason.Import,
Expand Down
6 changes: 3 additions & 3 deletions Trakt/ScheduledTasks/SyncLibraryTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public SyncLibraryTask(
_userDataManager = userDataManager;
_libraryManager = libraryManager;
_logger = loggerFactory.CreateLogger<SyncLibraryTask>();
_traktApi = new TraktApi(loggerFactory.CreateLogger<TraktApi>(), httpClientFactory, appHost, userDataManager);
_traktApi = new TraktApi(loggerFactory.CreateLogger<TraktApi>(), httpClientFactory, appHost, userDataManager, userManager);
}

/// <inheritdoc />
Expand Down Expand Up @@ -199,7 +199,7 @@ private async Task SyncMovies(
foreach (var libraryMovie in movieItems)
{
cancellationToken.ThrowIfCancellationRequested();
var userData = _userDataManager.GetUserData(user.Id, libraryMovie);
var userData = _userDataManager.GetUserData(user, libraryMovie);

if (traktUser.SynchronizeCollections)
{
Expand Down Expand Up @@ -438,7 +438,7 @@ private async Task SyncShows(
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);

Expand Down
6 changes: 4 additions & 2 deletions Trakt/ServerMediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ public class ServerMediator : IHostedService, IDisposable
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
/// <param name="appHost">The <see cref="IServerApplicationHost"/>.</param>
/// <param name="userManager">The <see cref="IUserManager"/>.</param>
public ServerMediator(
ISessionManager sessionManager,
IUserDataManager userDataManager,
ILibraryManager libraryManager,
ILoggerFactory loggerFactory,
IHttpClientFactory httpClientFactory,
IServerApplicationHost appHost)
IServerApplicationHost appHost,
IUserManager userManager)
{
_sessionManager = sessionManager;
_libraryManager = libraryManager;
Expand All @@ -59,7 +61,7 @@ public ServerMediator(
_logger = loggerFactory.CreateLogger<ServerMediator>();
_playbackState = new Dictionary<Guid, PlaybackState>();

_traktApi = new TraktApi(loggerFactory.CreateLogger<TraktApi>(), httpClientFactory, appHost, userDataManager);
_traktApi = new TraktApi(loggerFactory.CreateLogger<TraktApi>(), httpClientFactory, appHost, userDataManager, userManager);
_libraryManagerEventsHelper = new LibraryManagerEventsHelper(loggerFactory.CreateLogger<LibraryManagerEventsHelper>(), _traktApi);
_userDataManagerEventsHelper = new UserDataManagerEventsHelper(loggerFactory.CreateLogger<UserDataManagerEventsHelper>(), _traktApi);
}
Expand Down

0 comments on commit a673d90

Please sign in to comment.