예제 #1
0
        private async Task <Validation <BaseError, ConnectionParameters> > MediaSourceMustHaveToken(
            ConnectionParameters connectionParameters)
        {
            Option <PlexServerAuthToken> maybeToken = await
                                                      _plexSecretStore.GetServerAuthToken(connectionParameters.PlexMediaSource.ClientIdentifier);

            return(maybeToken.Map(token => connectionParameters with {
                PlexServerAuthToken = token
            })
    private async Task FindConnectionToActivate(PlexMediaSource server)
    {
        var prioritized = server.Connections
                          .OrderByDescending(pc => pc.Uri == LocalhostUri)
                          .ThenByDescending(pc => pc.IsActive)
                          .ToList();

        foreach (PlexConnection connection in server.Connections)
        {
            connection.IsActive = false;
        }

        Option <PlexServerAuthToken> maybeToken = await _plexSecretStore.GetServerAuthToken(server.ClientIdentifier);

        foreach (PlexServerAuthToken token in maybeToken)
        {
            foreach (PlexConnection connection in prioritized)
            {
                try
                {
                    _logger.LogDebug("Attempting to locate to Plex at {Uri}", connection.Uri);
                    if (await _plexServerApiClient.Ping(connection, token))
                    {
                        _logger.LogInformation("Located Plex at {Uri}", connection.Uri);
                        connection.IsActive = true;
                        break;
                    }
                }
                catch
                {
                    // do nothing
                }
            }
        }

        if (maybeToken.IsNone)
        {
            _logger.LogError(
                "Unable to activate Plex connection for server {Server} without auth token",
                server.ServerName);
        }

        if (server.Connections.All(c => !c.IsActive))
        {
            _logger.LogError("Unable to locate Plex");
            server.Connections.Head().IsActive = true;
        }

        await _mediaSourceRepository.Update(server, new List <PlexConnection>(), new List <PlexConnection>());
    }