public async Task <UserLinkResult> InviteUser(ConnectAuthorizationRequest request) { await _operationLock.WaitAsync().ConfigureAwait(false); try { return(await InviteUserInternal(request).ConfigureAwait(false)); } finally { _operationLock.Release(); } }
public async Task <UserLinkResult> InviteUser(ConnectAuthorizationRequest request) { if (string.IsNullOrWhiteSpace(ConnectServerId)) { await UpdateConnectInfo().ConfigureAwait(false); } await _operationLock.WaitAsync().ConfigureAwait(false); try { return(await InviteUserInternal(request).ConfigureAwait(false)); } finally { _operationLock.Release(); } }
private async Task <UserLinkResult> InviteUserInternal(ConnectAuthorizationRequest request) { var connectUsername = request.ConnectUserName; var sendingUserId = request.SendingUserId; if (string.IsNullOrWhiteSpace(connectUsername)) { throw new ArgumentNullException("connectUsername"); } if (string.IsNullOrWhiteSpace(ConnectServerId)) { throw new ArgumentNullException("ConnectServerId"); } var sendingUser = GetUser(sendingUserId); var requesterUserName = sendingUser.ConnectUserName; if (string.IsNullOrWhiteSpace(requesterUserName)) { throw new ArgumentException("A Connect account is required in order to send invitations."); } string connectUserId = null; var result = new UserLinkResult(); try { var connectUser = await GetConnectUser(new ConnectUserQuery { NameOrEmail = connectUsername }, CancellationToken.None).ConfigureAwait(false); if (!connectUser.IsActive) { throw new ArgumentException("The Media Browser account has been disabled."); } connectUserId = connectUser.Id; result.GuestDisplayName = connectUser.Name; } catch (HttpException ex) { if (!ex.StatusCode.HasValue || ex.StatusCode.Value != HttpStatusCode.NotFound || !Validator.EmailIsValid(connectUsername)) { throw; } } if (string.IsNullOrWhiteSpace(connectUserId)) { return(await SendNewUserInvitation(requesterUserName, connectUsername).ConfigureAwait(false)); } var url = GetConnectUrl("ServerAuthorizations"); var options = new HttpRequestOptions { Url = url, CancellationToken = CancellationToken.None }; var accessToken = Guid.NewGuid().ToString("N"); var postData = new Dictionary <string, string> { { "serverId", ConnectServerId }, { "userId", connectUserId }, { "userType", "Guest" }, { "accessToken", accessToken }, { "requesterUserName", requesterUserName } }; options.SetPostData(postData); SetServerAccessToken(options); SetApplicationHeader(options); // No need to examine the response using (var stream = (await _httpClient.Post(options).ConfigureAwait(false)).Content) { var response = _json.DeserializeFromStream <ServerUserAuthorizationResponse>(stream); result.IsPending = string.Equals(response.AcceptStatus, "waiting", StringComparison.OrdinalIgnoreCase); _data.PendingAuthorizations.Add(new ConnectAuthorizationInternal { ConnectUserId = response.UserId, Id = response.Id, ImageUrl = response.UserImageUrl, UserName = response.UserName, ExcludedLibraries = request.ExcludedLibraries, ExcludedChannels = request.ExcludedChannels, EnableLiveTv = request.EnableLiveTv, AccessToken = accessToken }); CacheData(); } await RefreshAuthorizationsInternal(false, CancellationToken.None).ConfigureAwait(false); return(result); }