mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.5 KiB
45 lines
1.5 KiB
using System; |
|
using System.Threading; |
|
using System.Threading.Channels; |
|
using System.Threading.Tasks; |
|
using ErsatzTV.Core; |
|
using ErsatzTV.Core.Interfaces.Plex; |
|
using LanguageExt; |
|
using MediatR; |
|
|
|
namespace ErsatzTV.Application.MediaSources.Commands |
|
{ |
|
public class TryCompletePlexPinFlowHandler : IRequestHandler<TryCompletePlexPinFlow, Either<BaseError, bool>> |
|
{ |
|
private readonly ChannelWriter<IPlexBackgroundServiceRequest> _channel; |
|
private readonly IPlexTvApiClient _plexTvApiClient; |
|
|
|
public TryCompletePlexPinFlowHandler( |
|
IPlexTvApiClient plexTvApiClient, |
|
ChannelWriter<IPlexBackgroundServiceRequest> channel) |
|
{ |
|
_plexTvApiClient = plexTvApiClient; |
|
_channel = channel; |
|
} |
|
|
|
public async Task<Either<BaseError, bool>> |
|
Handle(TryCompletePlexPinFlow request, CancellationToken cancellationToken) |
|
{ |
|
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(2)); |
|
CancellationToken token = cts.Token; |
|
while (!token.IsCancellationRequested) |
|
{ |
|
bool result = await _plexTvApiClient.TryCompletePinFlow(request.AuthPin); |
|
if (result) |
|
{ |
|
await _channel.WriteAsync(new SynchronizePlexMediaSources(), cancellationToken); |
|
return true; |
|
} |
|
|
|
await Task.Delay(TimeSpan.FromSeconds(1), token); |
|
} |
|
|
|
return false; |
|
} |
|
} |
|
}
|
|
|