subscribe

This commit is contained in:
kislovdm
2026-03-10 16:24:21 +03:00
parent 4c60efd689
commit e768ee6604
9 changed files with 618 additions and 40 deletions
@@ -8,6 +8,7 @@ public class DealService
{
private readonly IPepperClient _pepperClient;
private readonly IDealRepository _repository;
private readonly ISubscriptionRepository _subscriptionRepository;
private readonly ITelegramNotifier _notifier;
private readonly ILogger<DealService> _logger;
private readonly IHealthMonitor _healthMonitor;
@@ -15,12 +16,14 @@ public class DealService
public DealService(
IPepperClient pepperClient,
IDealRepository repository,
ISubscriptionRepository subscriptionRepository,
ITelegramNotifier notifier,
ILogger<DealService> logger,
IHealthMonitor healthMonitor)
{
_pepperClient = pepperClient;
_repository = repository;
_subscriptionRepository = subscriptionRepository;
_notifier = notifier;
_logger = logger;
_healthMonitor = healthMonitor;
@@ -36,6 +39,9 @@ public class DealService
_healthMonitor.ReportSuccess(DateTimeOffset.UtcNow);
}
// Загружаем активные подписки один раз на проход
var subscriptions = await _subscriptionRepository.GetAllActiveAsync(cancellationToken);
foreach (var deal in deals)
{
if (await _repository.ExistsAsync(deal.Id, cancellationToken))
@@ -46,8 +52,63 @@ public class DealService
_logger.LogInformation("Новая скидка: {Title} ({Id})", deal.Title, deal.Id);
await _repository.AddAsync(deal, cancellationToken);
// Общая рассылка в основной чат (как было раньше)
await _notifier.NotifyNewDealAsync(deal, cancellationToken);
if (subscriptions.Count == 0)
{
continue;
}
var searchableText = $"{deal.Title} {deal.StoreName}";
foreach (var subscription in subscriptions)
{
if (!subscription.IsActive)
{
continue;
}
var keywords = ParseKeywords(subscription.Keywords);
if (keywords.Count == 0)
{
continue;
}
if (IsMatch(searchableText, keywords))
{
await _notifier.NotifyDealToChatAsync(deal, subscription.ChatId, cancellationToken);
}
}
}
}
private static List<string> ParseKeywords(string raw)
{
return raw
.Split(new[] { '-',',', ';', '\n', '\r', '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Select(k => k.Trim())
.Where(k => k.Length > 0)
.ToList();
}
private static bool IsMatch(string text, List<string> keywords)
{
if (keywords.Count == 0)
{
return false;
}
foreach (var keyword in keywords)
{
if (text.Contains(keyword, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
}