public async Task DeleteUserInfoRecordAsync(string account, string channelName, string channelId, string requestId, CancellationToken cancellationToken) { var storeAgent = await storeManager.GetStoreAgent(); var userInfo = await storeAgent.UserInfoStore.GetUserInfoAsync(account, channelName, channelId); Validator.IsTrue <ResourceNotFoundException>(userInfo != null, nameof(userInfo), string.Format($"The user information does not exist for account: {account}, channelName: {channelName}, channelId: {channelId}")); try { await storeAgent.UserInfoStore.DeleteUserInfoAsync(account, channelName, channelId); // Update history in Social storage await this.telemetryManager.CreateSocialLoginHistoryRecordAsync(storeAgent, account, channelName, channelId.ToString(), null, ActionType.Delete.ToString(), DateTime.UtcNow); await QuotaCheckClient.ReleaseQuotaAsync(account, Constants.SocialLoginTotal, 1); SocialProviderEventSource.Current.Info(requestId, this, nameof(this.DeleteUserInfoRecordAsync), OperationStates.Succeeded, $"account: {account}, channel: {channelName}, channelId: {channelId}"); } catch (Exception ex) { SocialProviderEventSource.Current.ErrorException(requestId, this, nameof(this.DeleteUserInfoRecordAsync), OperationStates.Failed, $"Failed to delete user information for account: {account}, channel: {channelName}, channelId: {channelId}", ex); throw new Exception(string.Format($"Failed to delete user information for account: {account}, channel: {channelName}, channelId: {channelId}")); } }
public async Task <ServiceProviderResponse> RemoveSignatureQuotaAsync( [FromHeader(Constant.OperationTrackingIdHeader)] string requestId, string account, string value) { var currentAccount = await ValidateAccount(account); var signature = await this.store.GetSignatureAsync(account, value); Validator.IsTrue <ResourceNotFoundException>(signature != null, nameof(signature), "The signature '{0}' does not exist.", value); var quotaName = string.Format(Constants.SmsSignatureMAUNamingTemplate, value); await QuotaCheckClient.RemoveQuotaAsync(account, quotaName); return(ServiceProviderResponse.CreateResponse(HttpStatusCode.OK)); }
public async Task <ServiceProviderResponse> SendMessageAsync( [FromHeader(Constant.OperationTrackingIdHeader)] string requestId, [FromHeader] string account, [FromBody] MessageSendRequest request) { Account currentAccount = null; try { SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Received, $"Request is received"); currentAccount = await this.ValidateAccount(account); Validator.ArgumentNotNull(request, nameof(request)); Validator.ArgumentNotNull(request.MessageBody, nameof(request.MessageBody)); Validator.ArgumentNotNullOrEmpty(request.MessageBody.TemplateName, request.MessageBody.TemplateName); request.Targets = this.ValidatePhoneNumbers(request.Targets); var pack = await this.BuildInputMessageAsync(currentAccount, request, requestId); var message = pack.InputMessage; SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Starting, $"Message is ready. messageId={message.MessageInfo.MessageId}"); var signatureQuotaName = string.Format(Constant.SmsSignatureMAUNamingTemplate, pack.Signature.Value); SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Empty, $"If quota is enabled, request {request.Targets.Count} for account {account}. Otherwise, ignore"); await QuotaCheckClient.AcquireQuotaAsync(account, SmsConstant.SmsQuotaName, request.Targets.Count); await QuotaCheckClient.AcquireQuotaAsync(account, signatureQuotaName, request.Targets.Count); try { await this.reportManager.OnMessageSentAsync(account, message, pack.Extension); SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Starting, $"Report is updated. messageId={message.MessageInfo.MessageId}"); await this.DispatchMessageAsync(message, pack.Extension, requestId); SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Starting, $"Message is dispatched. messageId={message.MessageInfo.MessageId}"); this.metricManager.LogSendSuccess(1, account, currentAccount.SubscriptionId ?? string.Empty, pack.Extension.MessageCategory); this.metricManager.LogSendTotal(BillingHelper.GetTotalSegments(message.MessageInfo.MessageBody), account, currentAccount.SubscriptionId ?? string.Empty, pack.Extension.MessageCategory); var response = new MessageSendResponse { MessageId = message.MessageInfo.MessageId, SendTime = message.MessageInfo.SendTime }; return(ServiceProviderResponse.CreateJsonResponse(HttpStatusCode.OK, response)); } catch { SmsProviderEventSource.Current.Info(requestId, this, nameof(this.SendMessageAsync), OperationStates.Empty, $"If quota is enabled, release {request.Targets.Count} for account {account}. Otherwise, ignore"); await QuotaCheckClient.ReleaseQuotaAsync(account, SmsConstant.SmsQuotaName, request.Targets.Count); await QuotaCheckClient.ReleaseQuotaAsync(account, signatureQuotaName, request.Targets.Count); this.metricManager.LogSendFailed(1, account, currentAccount.SubscriptionId ?? string.Empty, pack.Extension.MessageCategory); throw; } } catch { this.metricManager.LogSendFailed(1, account, currentAccount?.SubscriptionId ?? string.Empty, null); throw; } }
public async Task <UserInfoRecord> CreateOrUpdateUserInfoRecordAsync(string account, string channel, string accessToken, string channelId, string socialPlatform, string requestId, CancellationToken cancellationToken) { UserInfoRecordDescription profileDescription = new UserInfoRecordDescription(channel); UserInfoRecordDescription description = new UserInfoRecordDescription(channel); description.Properties.Add(ChannelIdKey, channelId); description.Properties.Add(AccessTokenKey, accessToken); description.Properties.Add(PlatformKey, socialPlatform); PropertyCollection <object> profile = new PropertyCollection <object>(); var subscriptionId = await RequestHelper.GetSubscriptionId(account); UserInfo userInfo = new UserInfo(); bool updatedQuotaTotal = false; bool updatedQuotaMAU = false; try { // get appid for QQ and add into description if (channel == SocialChannelHelper.QQ) { var appId = await this.GetCredentialAsync(account, channel, socialPlatform); Validator.IsTrue <ArgumentException>(appId != null, nameof(appId), "No app_id for getting user information from {0}", description.Channel); description.Properties.Add(AppIdKey, appId); } // GET Profile from Social service with connector var connector = this.CreateConnector(channel); profile.Add(ChannelIdKey, channelId); var socialprofile = await connector.GetSocialProfileAsync(description.Properties); profile.Add(SocialProfileKey, socialprofile); profileDescription.Properties = profile; var storeAgent = await storeManager.GetStoreAgent(); userInfo = await storeAgent.UserInfoStore.GetUserInfoAsync(account, profileDescription.Channel, channelId.ToString()); // Create or update profile in Social store var userInfoResult = await storeAgent.UserInfoStore.CreateorUpdateUserInfoAsync(account, channelId.ToString(), profileDescription); // Update history in Social storage await this.telemetryManager.CreateSocialLoginHistoryRecordAsync(storeAgent, account, channel, channelId, socialPlatform, userInfoResult.Action.ToString(), userInfoResult.UserInfo.ModifiedTime); // Update Social Login MAU and Total quota for New user if (userInfo == null) { await QuotaCheckClient.AcquireQuotaAsync(account, Constants.SocialLoginTotal, 1); updatedQuotaTotal = true; await QuotaCheckClient.AcquireQuotaAsync(account, Constants.SocialLoginMAU, 1); updatedQuotaMAU = true; } else { // Update Social Login MAU quota for login first time this month if (userInfo.ModifiedTime < new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1)) { await QuotaCheckClient.AcquireQuotaAsync(account, Constants.SocialLoginMAU, 1); updatedQuotaMAU = true; } } var userInfoRecord = userInfoResult.UserInfo.ToUserInfoRecord(); this.metricManager.LogSocialLoginSuccess(1, account, subscriptionId ?? string.Empty, channel, socialPlatform); SocialProviderEventSource.Current.Info(requestId, this, nameof(this.CreateOrUpdateUserInfoRecordAsync), OperationStates.Succeeded, $"account: {account}, channel: {channel}, platform: {socialPlatform}, channelId: {channelId}"); return(userInfoRecord); } catch (Exception ex) { if (updatedQuotaTotal == true) { await QuotaCheckClient.ReleaseQuotaAsync(account, Constants.SocialLoginTotal, 1); } if (updatedQuotaMAU == true) { await QuotaCheckClient.ReleaseQuotaAsync(account, Constants.SocialLoginMAU, 1); } this.metricManager.LogSocialLoginFailed(1, account, subscriptionId ?? string.Empty, channel, socialPlatform); SocialProviderEventSource.Current.ErrorException(requestId, this, nameof(this.CreateOrUpdateUserInfoRecordAsync), OperationStates.Failed, $"Failed to create or update user information for account: {account}, channel: {channel}, platform: {socialPlatform}, channelId: {channelId}", ex); if ((ex is ArgumentException) || (ex is SocialChannelExceptionBase) || (ex is QuotaExceededException)) { throw; } throw new Exception(string.Format($"Failed to create or update user information for account: {account}, channel: {channel}, platform: {socialPlatform}, channelId: {channelId}")); } }