Пример #1
0
        /// <inheritdoc />
        public async Task ExecuteAsync(CancellationToken cancellation = default)
        {
            var subscriptions = await _subscriptionsStore.GetAllAsync(cancellation);

            var channels = await _channelStore.GetAllAsync(cancellation);

            var orphanSubscriptions = new List <AlertsSubscription>();

            foreach (var subscription in subscriptions)
            {
                var hasMatchedChannel = channels.Any(c => string
                                                     .Equals(c.Name, subscription.Channel, StringComparison.InvariantCultureIgnoreCase));

                if (!hasMatchedChannel)
                {
                    orphanSubscriptions.Add(subscription);
                }
            }

            _logger.LogInformation($"Found '{orphanSubscriptions.Count}' orphan subscriptions");

            foreach (var orphanSubscription in orphanSubscriptions)
            {
                _logger.LogInformation("Removing orphan subscription {Chat} {Channel}",
                                       orphanSubscription.ChatName, orphanSubscription.Channel);

                await _subscriptionsStore.RemoveAsync(orphanSubscription, cancellation);
            }
        }
Пример #2
0
        /// <inheritdoc />
        protected override async Task Handle(BotActionRequest <UnsubscribeAction> request, CancellationToken cancellationToken)
        {
            var chat = request.Update.Message.Chat;

            var channel = await _channelStore.GetAsync(request.Action.Channel, cancellationToken);

            if (channel == null)
            {
                var message = Localizer.GetString(BotResources.UnsubscribeFailedChannelNotFound);
                await Reply.SendAsync(new SendTelegramReply(chat.Id, message), cancellationToken);

                return;
            }

            var existingSubscription = await _subscriptionsStore.GetAsync(chat.Id, request.Action.Channel, cancellationToken);

            if (existingSubscription == null)
            {
                var message = Localizer.GetString(BotResources.UnsubscribeFailedNotSubscribed);
                await Reply.SendAsync(new SendTelegramReply(chat.Id, message), cancellationToken);

                return;
            }

            await _subscriptionsStore.RemoveAsync(existingSubscription, cancellationToken);

            var replyMessage = Localizer.GetString(BotResources.UnsubscribeSucceed);
            await Reply.SendAsync(new SendTelegramReply(chat.Id, replyMessage), cancellationToken);
        }