private async Task <(UserNotification, HashSet <ICommunicationChannel>)> CreateUserNotificationAsync(UserEventMessage userEvent, User user, App app)
        {
            var notification = userNotificationFactory.Create(app, user, userEvent);

            if (notification == null)
            {
                throw new DomainException(Texts.Notification_NoSubject);
            }

            var targets = new HashSet <ICommunicationChannel>();

            foreach (var channel in channels)
            {
                if (channel.IsSystem)
                {
                    var preference = new NotificationSetting
                    {
                        Send = true
                    };

                    if (channel.CanSend(notification, preference, user, app))
                    {
                        notification.Settings[channel.Name] = preference;

                        targets.Add(channel);
                    }
                }
                else
                {
                    if (notification.Settings.TryGetValue(channel.Name, out var preference) && preference.ShouldSend)
                    {
                        if (channel.CanSend(notification, preference, user, app))
                        {
                            notification.Sending[channel.Name] = new ChannelSendInfo
                            {
                                LastUpdate = clock.GetCurrentInstant()
                            };

                            targets.Add(channel);

                            await userNotificationsStore.CollectAsync(notification, channel.Name, ProcessStatus.Attempt);
                        }
                    }
                }
            }

            return(notification, targets);
        }
Ejemplo n.º 2
0
        private async Task <UserNotification> CreateUserNotificationAsync(UserEventMessage userEvent, SendOptions options)
        {
            using (Telemetry.Activities.StartActivity("CreateUserNotification"))
            {
                var notification = userNotificationFactory.Create(options.App, options.User, userEvent);

                if (notification == null)
                {
                    throw new DomainException(Texts.Notification_NoSubject);
                }

                foreach (var channel in channels)
                {
                    if (channel.IsSystem && !string.IsNullOrWhiteSpace(channel.Name))
                    {
                        if (!notification.Channels.TryGetValue(channel.Name, out var channelInfo))
                        {
                            channelInfo = new UserNotificationChannel
                            {
                                Setting = new ChannelSetting
                                {
                                    Send = ChannelSend.Send
                                }
                            };

                            notification.Channels[channel.Name] = channelInfo;
                        }
                    }

                    if (notification.Channels.TryGetValue(channel.Name, out var channelConfig) && channelConfig.Setting.Send == ChannelSend.Send)
                    {
                        var configurations = channel.GetConfigurations(notification, channelConfig.Setting, options);

                        foreach (var configuration in configurations)
                        {
                            if (!string.IsNullOrWhiteSpace(configuration))
                            {
                                channelConfig.Status[configuration] = new ChannelSendInfo();

                                await userNotificationsStore.CollectAsync(notification, channel.Name, ProcessStatus.Attempt);
                            }
                        }
                    }
                }

                return(notification);
            }
        }