public async ValueTask <Subscription?> ExecuteAsync(Subscription subscription, IServiceProvider serviceProvider, CancellationToken ct) { var userStore = serviceProvider.GetRequiredService <IUserStore>(); await CheckWhitelistAsync(userStore, subscription, ct); var newSettings = TopicSettings; if (MergeSettings || newSettings == null) { newSettings = ChannelSettings.Merged(subscription.TopicSettings, TopicSettings); } var newSubscription = subscription with { TopicSettings = newSettings }; return(newSubscription); }
public async Task PublishAsync(EventMessage @event, CancellationToken ct) { using (var activity = Telemetry.Activities.StartActivity("HandleUserEvent")) { log.LogInformation("Received event for app {appId} with ID {id} to topic {topic}.", @event.AppId, @event.Id, @event.Topic); if (string.IsNullOrWhiteSpace(@event.AppId)) { log.LogInformation("Received invalid event with ID {id} to topic {topic}: No app id found.", @event.Id, @event.Topic); return; } if (string.IsNullOrWhiteSpace(@event.Topic)) { await logStore.LogAsync(@event.AppId, Texts.Events_NoTopic); return; } if (string.IsNullOrWhiteSpace(@event.TemplateCode) && @event.Formatting?.HasSubject() != true) { await logStore.LogAsync(@event.AppId, Texts.Events_NoSubjectOrTemplateCode); return; } var count = 0; await foreach (var subscription in GetSubscriptions(@event, ct)) { ct.ThrowIfCancellationRequested(); if (count == 0) { var templateCode = (string?)null; if (@event.TemplateVariants?.Count > 0) { var random = randomizer.NextDouble(); var propability = 0d; foreach (var(key, value) in @event.TemplateVariants) { propability += value; if (random <= propability) { templateCode = key; break; } } } if (string.IsNullOrWhiteSpace(templateCode)) { templateCode = @event.TemplateCode; } if (!string.IsNullOrWhiteSpace(templateCode)) { var template = await templateStore.GetAsync(@event.AppId, templateCode, ct); if (template?.IsAutoCreated == false) { if (@event.Formatting != null) { @event.Formatting = template.Formatting.MergedWith(@event.Formatting); } else { @event.Formatting = template.Formatting; } @event.Settings = ChannelSettings.Merged(template.Settings, @event.Settings); } } if (@event.Formatting?.HasSubject() != true) { await logStore.LogAsync(@event.AppId, string.Format(CultureInfo.InvariantCulture, Texts.Template_NoSubject, templateCode)); return; } if (@event.Properties != null) { @event.Formatting = @event.Formatting.Format(@event.Properties); } try { await eventStore.InsertAsync(@event, ct); } catch (UniqueConstraintException) { await logStore.LogAsync(@event.AppId, Texts.Events_AlreadyProcessed); break; } } var userEventMessage = CreateUserEventMessage(@event, subscription); if (activity != null) { userEventMessage.UserEventActivity = activity.Context; } await ProduceAsync(subscription, userEventMessage); count++; } if (count > 0) { var counterMap = CounterMap.ForNotification(ProcessStatus.Attempt, count); var counterKey = CounterKey.ForEvent(@event); await counters.CollectAsync(counterKey, counterMap, ct); } else { await logStore.LogAsync(@event.AppId, Texts.Events_NoSubscriber); } log.LogInformation("Processed event for app {appId} with ID {id} to topic {topic}.", @event.AppId, @event.Id, @event.Topic); } }
public async ValueTask <User?> ExecuteAsync(User user, IServiceProvider serviceProvider, CancellationToken ct) { Validate <Validator> .It(this); var newUser = user; if (Is.Changed(FullName, user.FullName)) { newUser = newUser with { FullName = FullName.Trim() }; } if (Is.Changed(EmailAddress, user.EmailAddress)) { newUser = newUser with { EmailAddress = EmailAddress.Trim().ToLowerInvariant() }; } if (Is.Changed(PhoneNumber, user.PhoneNumber)) { newUser = newUser with { PhoneNumber = PhoneNumber.Trim().ToLowerInvariant() }; } if (Is.Changed(Properties, user.Properties)) { newUser = newUser with { Properties = Properties }; } if (Is.Changed(PreferredLanguage, user.PreferredLanguage)) { newUser = newUser with { PreferredLanguage = PreferredLanguage.Trim() }; } if (Is.Changed(PreferredTimezone, user.PreferredTimezone)) { newUser = newUser with { PreferredTimezone = PreferredTimezone.Trim() }; } if (Is.Changed(RequiresWhitelistedTopics, user.RequiresWhitelistedTopics)) { newUser = newUser with { RequiresWhitelistedTopics = RequiresWhitelistedTopics.Value }; } if (Settings != null) { var newSettings = Settings; if (MergeSettings) { newSettings = ChannelSettings.Merged(user.Settings, Settings); } newUser = newUser with { Settings = newSettings }; } if (string.IsNullOrWhiteSpace(user.ApiKey)) { var tokenGenerator = serviceProvider.GetRequiredService <IApiKeyGenerator>(); newUser = newUser with { ApiKey = await tokenGenerator.GenerateUserTokenAsync(user.AppId, user.Id) }; } return(newUser); } } }