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 Emby account is not active. Please ensure the account has been activated by following the instructions within the email confirmation."); } connectUserId = connectUser.Id; result.GuestDisplayName = connectUser.Name; } catch (HttpException ex) { if (!ex.StatusCode.HasValue) { throw; } // If they entered a username, then whatever the error is just throw it, for example, user not found if (!Validator.EmailIsValid(connectUsername)) { if (ex.StatusCode.Value == HttpStatusCode.NotFound) { throw new ResourceNotFoundException(); } throw; } if (ex.StatusCode.Value != HttpStatusCode.NotFound) { 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, EnabledLibraries = request.EnabledLibraries, EnabledChannels = request.EnabledChannels, EnableLiveTv = request.EnableLiveTv, AccessToken = accessToken }); CacheData(); } await RefreshAuthorizationsInternal(false, CancellationToken.None).ConfigureAwait(false); return result; }
public async Task<UserLinkResult> InviteUser(ConnectAuthorizationRequest request) { await _operationLock.WaitAsync().ConfigureAwait(false); try { return await InviteUserInternal(request).ConfigureAwait(false); } finally { _operationLock.Release(); } }