/// <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); } }
/// <inheritdoc /> protected override async Task Handle(BotActionRequest <ChannelsAction> request, CancellationToken cancellationToken) { var chatId = request.Message.Chat.Id; var channels = await _channelStore.GetAllAsync(cancellationToken); if (channels.Count < 1) { var notSubscribedText = Localizer.GetString(BotResources.ChannelsNotFound); var replyRequest = new SendTelegramReply(chatId, notSubscribedText); await Reply.SendAsync(replyRequest, cancellationToken); return; } var subscribedText = Localizer.GetString(BotResources.Channels); var messageBuilder = new StringBuilder(subscribedText) .AppendLines(count: 2); foreach (var(index, channel) in channels.Index()) { var userSubscribed = await _subscriptionsStore.ExistsAsync(channel.Name, chatId, cancellationToken); var descriptionSuffix = string.IsNullOrWhiteSpace(channel.Description) ? string.Empty : $" — {channel.Description}"; var subscribeSuffix = userSubscribed ? string.Empty : $" (/subscribe_{channel.Name.Replace('-', '_')})"; messageBuilder.AppendLine($"{index + 1}. {channel.Name}{descriptionSuffix}{subscribeSuffix}"); } await Reply.SendAsync(new SendTelegramReply(chatId, messageBuilder.ToString()), cancellationToken); }