Skip to content

Commit

Permalink
10.9
Browse files Browse the repository at this point in the history
  • Loading branch information
crobibero committed Mar 30, 2024
1 parent 215a4d8 commit e1cfd9b
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 151 deletions.
59 changes: 14 additions & 45 deletions Trakt/Api/TraktApi.cs
Expand Up @@ -253,15 +253,9 @@ public async Task<List<TraktScrobbleResponse>> SendEpisodeStatusUpdateAsync(Epis
EventType eventType,
CancellationToken cancellationToken)
{
if (movies is null || movies.Count == 0)
{
throw new ArgumentNullException(nameof(movies));
}

if (traktUser == null)
{
throw new ArgumentNullException(nameof(traktUser));
}
ArgumentNullException.ThrowIfNull(movies);
ArgumentOutOfRangeException.ThrowIfZero(movies.Count);
ArgumentNullException.ThrowIfNull(traktUser);

var moviesPayload = movies.Select(m =>
{
Expand Down Expand Up @@ -319,15 +313,9 @@ public async Task<List<TraktScrobbleResponse>> SendEpisodeStatusUpdateAsync(Epis
EventType eventType,
CancellationToken cancellationToken)
{
if (episodes is null || episodes.Count == 0)
{
throw new ArgumentNullException(nameof(episodes));
}

if (traktUser == null)
{
throw new ArgumentNullException(nameof(traktUser));
}
ArgumentNullException.ThrowIfNull(episodes);
ArgumentOutOfRangeException.ThrowIfZero(episodes.Count);
ArgumentNullException.ThrowIfNull(traktUser);

var responses = new List<TraktSyncResponse>();
var chunks = episodes.Chunk(100);
Expand Down Expand Up @@ -468,15 +456,8 @@ public async Task<List<TraktScrobbleResponse>> SendEpisodeStatusUpdateAsync(Epis
EventType eventType,
CancellationToken cancellationToken)
{
if (show == null)
{
throw new ArgumentNullException(nameof(show));
}

if (traktUser == null)
{
throw new ArgumentNullException(nameof(traktUser));
}
ArgumentNullException.ThrowIfNull(show);
ArgumentNullException.ThrowIfNull(traktUser);

var showPayload = new List<TraktShowCollected>
{
Expand Down Expand Up @@ -722,15 +703,9 @@ public async Task<List<DataContracts.Users.Collection.TraktShowCollected>> SendG
bool seen,
CancellationToken cancellationToken)
{
if (movies == null)
{
throw new ArgumentNullException(nameof(movies));
}

if (traktUser == null)
{
throw new ArgumentNullException(nameof(traktUser));
}
ArgumentNullException.ThrowIfNull(movies);
ArgumentOutOfRangeException.ThrowIfZero(movies.Count);
ArgumentNullException.ThrowIfNull(traktUser);

var moviesPayload = movies.Select(m =>
{
Expand Down Expand Up @@ -782,15 +757,9 @@ public async Task<List<DataContracts.Users.Collection.TraktShowCollected>> SendG
bool seen,
CancellationToken cancellationToken)
{
if (episodes == null)
{
throw new ArgumentNullException(nameof(episodes));
}

if (traktUser == null)
{
throw new ArgumentNullException(nameof(traktUser));
}
ArgumentNullException.ThrowIfNull(episodes);
ArgumentOutOfRangeException.ThrowIfZero(episodes.Count);
ArgumentNullException.ThrowIfNull(traktUser);

var chunks = episodes.Chunk(100);
var traktResponses = new List<TraktSyncResponse>();
Expand Down
9 changes: 5 additions & 4 deletions Trakt/Api/TraktController.cs
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http;
using System.Net.Mime;
using System.Threading.Tasks;
using MediaBrowser.Common.Api;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -19,7 +20,7 @@ namespace Trakt.Api;
/// The trakt.tv controller class.
/// </summary>
[ApiController]
[Authorize(Policy = "DefaultAuthorization")]
[Authorize]
[Route("[controller]")]
[Produces(MediaTypeNames.Application.Json)]
public class TraktController : ControllerBase
Expand Down Expand Up @@ -55,7 +56,7 @@ public class TraktController : ControllerBase
/// <response code="200">Authorization code requested successfully.</response>
/// <returns>The trakt.tv authorization code.</returns>
[HttpPost("Users/{userGuid}/Authorize")]
[Authorize(Policy = "RequiresElevation")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<object>> TraktDeviceAuthorization([FromRoute] Guid userGuid)
{
Expand Down Expand Up @@ -86,7 +87,7 @@ public async Task<ActionResult<object>> TraktDeviceAuthorization([FromRoute] Gui
/// <response code="200">Deauthorization successful.</response>
/// <returns>Empty string.</returns>
[HttpPost("Users/{userGuid}/Deauthorize")]
[Authorize(Policy = "RequiresElevation")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status200OK)]
public string TraktDeviceDeAuthorization([FromRoute] Guid userGuid)
{
Expand Down Expand Up @@ -115,7 +116,7 @@ public string TraktDeviceDeAuthorization([FromRoute] Guid userGuid)
/// <response code="200">Polling successful.</response>
/// <returns>A value indicating whether the authorization code was connected to a trakt.tv account.</returns>
[HttpGet("Users/{userGuid}/PollAuthorizationStatus")]
[Authorize(Policy = "RequiresElevation")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<object> TraktPollAuthorizationStatus([FromRoute] Guid userGuid)
{
Expand Down
21 changes: 9 additions & 12 deletions Trakt/Extensions.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
Expand Down Expand Up @@ -250,17 +251,13 @@ public static bool MetadataIsDifferent(this TraktEpisodeCollected collectedEpiso
}

var rageType = videoStream.VideoRangeType;
switch (rageType)
return rageType switch
{
case "DOVI":
return TraktHdr.dolby_vision;
case "HDR10":
return TraktHdr.hdr10;
case "HLG":
return TraktHdr.hlg;
default:
return null;
}
VideoRangeType.DOVI => TraktHdr.dolby_vision,
VideoRangeType.HDR10 => TraktHdr.hdr10,
VideoRangeType.HLG => TraktHdr.hlg,
_ => null
};
}

/// <summary>
Expand Down Expand Up @@ -430,9 +427,9 @@ public static TraktEpisodeWatchedHistory FindMatch(Episode item, IEnumerable<Tra
/// <param name="item">The <see cref="BaseItem"/>.</param>
/// <param name="results">>The <see cref="IEnumerable{TraktEpisodeWatchedHistory}"/>.</param>
/// <returns>IEnumerable{TraktEpisodeWatchedHistory}.</returns>
public static IEnumerable<TraktEpisodeWatchedHistory> FindAllMatches(Episode item, IEnumerable<TraktEpisodeWatchedHistory> results)
public static IReadOnlyList<TraktEpisodeWatchedHistory> FindAllMatches(Episode item, IEnumerable<TraktEpisodeWatchedHistory> results)
{
return results.Where(i => IsMatch(item, i)).AsEnumerable();
return results.Where(i => IsMatch(item, i)).ToList();
}

/// <summary>
Expand Down
25 changes: 6 additions & 19 deletions Trakt/Helpers/LibraryManagerEventsHelper.cs
Expand Up @@ -14,7 +14,7 @@

namespace Trakt.Helpers;

internal class LibraryManagerEventsHelper : IDisposable
internal sealed class LibraryManagerEventsHelper : IDisposable
{
private readonly List<LibraryEvent> _queuedEvents;
private readonly ILogger<LibraryManagerEventsHelper> _logger;
Expand All @@ -40,13 +40,9 @@ public LibraryManagerEventsHelper(ILogger<LibraryManagerEventsHelper> logger, Tr
/// <param name="eventType">The <see cref="EventType"/>.</param>
public void QueueItem(BaseItem item, EventType eventType)
{
ArgumentNullException.ThrowIfNull(item);
lock (_queuedEvents)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}

if (_queueTimer == null)
{
_queueTimer = new Timer(
Expand Down Expand Up @@ -106,7 +102,7 @@ private async Task OnQueueTimerCallbackInternal()

lock (_queuedEvents)
{
if (!_queuedEvents.Any())
if (_queuedEvents.Count == 0)
{
_logger.LogInformation("No events... stopping queue timer");
return;
Expand Down Expand Up @@ -287,7 +283,7 @@ private async Task ProcessQueuedEpisodeEvents(IReadOnlyCollection<LibraryEvent>
.ToList();

// Can't progress further without episodes
if (!episodes.Any())
if (episodes.Count == 0)
{
_logger.LogDebug("Episodes count is 0");

Expand All @@ -311,7 +307,7 @@ private async Task ProcessQueuedEpisodeEvents(IReadOnlyCollection<LibraryEvent>
payload.Add(ep);
}

if (payload.Any())
if (payload.Count != 0)
{
await _traktApi.SendLibraryUpdateAsync(payload, traktUser, eventType, CancellationToken.None).ConfigureAwait(false);
}
Expand All @@ -324,15 +320,6 @@ private async Task ProcessQueuedEpisodeEvents(IReadOnlyCollection<LibraryEvent>

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_queueTimer?.Dispose();
}
_queueTimer?.Dispose();
}
}
27 changes: 9 additions & 18 deletions Trakt/Helpers/UserDataManagerEventsHelper.cs
Expand Up @@ -15,7 +15,7 @@ namespace Trakt.Helpers;
/// Helper class used to update the watched status of movies/episodes.
/// Attempts to organise requests to lower API calls.
/// </summary>
internal class UserDataManagerEventsHelper : IDisposable
internal sealed class UserDataManagerEventsHelper : IDisposable
{
private readonly ILogger<UserDataManagerEventsHelper> _logger;
private readonly TraktApi _traktApi;
Expand Down Expand Up @@ -103,7 +103,7 @@ public void ProcessUserDataSaveEventArgs(UserDataSaveEventArgs userDataSaveEvent
{
if (!userPackage.CurrentSeriesId.Equals(episode.Series.Id))
{
if (userPackage.SeenEpisodes.Any())
if (userPackage.SeenEpisodes.Count != 0)
{
_traktApi.SendEpisodePlaystateUpdates(
userPackage.SeenEpisodes.ToList(),
Expand All @@ -113,7 +113,7 @@ public void ProcessUserDataSaveEventArgs(UserDataSaveEventArgs userDataSaveEvent
userPackage.SeenEpisodes.Clear();
}

if (userPackage.UnSeenEpisodes.Any())
if (userPackage.UnSeenEpisodes.Count != 0)
{
_traktApi.SendEpisodePlaystateUpdates(
userPackage.UnSeenEpisodes.ToList(),
Expand Down Expand Up @@ -170,7 +170,7 @@ private void OnTimerCallback(object state)
Dictionary<Guid, UserDataPackage> userDataQueue;
lock (_userDataPackages)
{
if (!_userDataPackages.Any())
if (_userDataPackages.Count == 0)
{
_logger.LogInformation("No events... stopping queue timer");
return;
Expand All @@ -182,7 +182,7 @@ private void OnTimerCallback(object state)

foreach (var package in userDataQueue)
{
if (package.Value.UnSeenMovies.Any())
if (package.Value.UnSeenMovies.Count != 0)
{
_traktApi.SendMoviePlaystateUpdates(
package.Value.UnSeenMovies.ToList(),
Expand All @@ -192,7 +192,7 @@ private void OnTimerCallback(object state)
package.Value.UnSeenMovies.Clear();
}

if (package.Value.SeenMovies.Any())
if (package.Value.SeenMovies.Count != 0)
{
_traktApi.SendMoviePlaystateUpdates(
package.Value.SeenMovies.ToList(),
Expand All @@ -202,7 +202,7 @@ private void OnTimerCallback(object state)
package.Value.SeenMovies.Clear();
}

if (package.Value.UnSeenEpisodes.Any())
if (package.Value.UnSeenEpisodes.Count != 0)
{
_traktApi.SendEpisodePlaystateUpdates(
package.Value.UnSeenEpisodes.ToList(),
Expand All @@ -212,7 +212,7 @@ private void OnTimerCallback(object state)
package.Value.UnSeenEpisodes.Clear();
}

if (package.Value.SeenEpisodes.Any())
if (package.Value.SeenEpisodes.Count != 0)
{
_traktApi.SendEpisodePlaystateUpdates(
package.Value.SeenEpisodes.ToList(),
Expand All @@ -226,15 +226,6 @@ private void OnTimerCallback(object state)

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_queueTimer?.Dispose();
}
_queueTimer?.Dispose();
}
}
2 changes: 1 addition & 1 deletion Trakt/Model/LibraryEvent.cs
Expand Up @@ -3,7 +3,7 @@

namespace Trakt.Model;

internal class LibraryEvent
internal sealed class LibraryEvent
{
public BaseItem Item { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion Trakt/Model/PlaybackState.cs
Expand Up @@ -2,7 +2,7 @@

namespace Trakt.Model;

internal class PlaybackState
internal sealed class PlaybackState
{
public bool IsPaused { get; set; } = false;

Expand Down
2 changes: 1 addition & 1 deletion Trakt/Model/UserDataPackage.cs
Expand Up @@ -8,7 +8,7 @@ namespace Trakt.Helpers;
/// <summary>
/// Class that contains all the items to be reported to trakt.tv and supporting properties.
/// </summary>
internal class UserDataPackage
internal sealed class UserDataPackage
{
public UserDataPackage()
{
Expand Down
17 changes: 17 additions & 0 deletions Trakt/PluginServiceRegistrator.cs
@@ -0,0 +1,17 @@
using MediaBrowser.Controller;
using MediaBrowser.Controller.Plugins;
using Microsoft.Extensions.DependencyInjection;

namespace Trakt;

/// <summary>
/// Register plugin service.
/// </summary>
public class PluginServiceRegistrator : IPluginServiceRegistrator
{
/// <inheritdoc />
public void RegisterServices(IServiceCollection serviceCollection, IServerApplicationHost applicationHost)
{
serviceCollection.AddHostedService<ServerMediator>();
}
}

0 comments on commit e1cfd9b

Please sign in to comment.