Enhance TelegramNotifier to include channel names in notifications
ci / build-backend (push) Successful in 2m3s
ci / build-frontend (push) Successful in 42s
ci / tests (push) Successful in 1m26s
ci / sonar (push) Successful in 3m39s

Updated the TelegramNotifier to fetch channel names in a single database query, improving efficiency. Modified notification messages to include channel names for better clarity, ensuring users know where to switch for live content. Added tests to verify the inclusion of channel names in notifications and updated documentation to reflect these changes.
This commit is contained in:
Leonid Pershin
2026-08-01 09:49:32 +03:00
parent 3dbe0b7698
commit cbeee92d26
3 changed files with 71 additions and 5 deletions
@@ -37,20 +37,36 @@ public sealed class TelegramNotifier(IAppDbContext dbContext, ITelegramApi api)
? marker
: now - MaxCatchUp;
// Имена каналов — одним запросом на тик: их десятки, а оповещений за тик единицы, и ходить
// за названием по каждому значило бы столько же одинаковых запросов.
var channels = await dbContext
.Channels.AsNoTracking()
.ToDictionaryAsync(c => c.Id, c => c.Name, cancellationToken);
var sent = 0;
sent += await NotifyStartedAsync(settings, from, now, cancellationToken);
sent += await NotifyUpNextAsync(settings, from, now, cancellationToken);
sent += await NotifyStartedAsync(settings, channels, from, now, cancellationToken);
sent += await NotifyUpNextAsync(settings, channels, from, now, cancellationToken);
settings.MarkNotifiedUntil(now);
return sent;
}
/// <summary>
/// Подпись канала под заголовком программы. Каналов у зрителя несколько, а «Сейчас в эфире»
/// без названия не отвечает на вопрос «где» — по такому сообщению непонятно, куда переключаться.
/// </summary>
private static string ChannelLine(IReadOnlyDictionary<Guid, string> channels, Guid channelId) =>
channels.GetValueOrDefault(channelId) is { } name
? $" · {TelegramText.Escape(name)}"
: string.Empty;
/// <summary>
/// Начавшиеся программы. Оповещаем о смене шоу, а не о каждой записи: четыре серии подряд —
/// это одна программа в глазах зрителя, и четыре сообщения о ней читаются как спам.
/// </summary>
private async Task<int> NotifyStartedAsync(
TelegramSettings settings,
IReadOnlyDictionary<Guid, string> channels,
DateTimeOffset from,
DateTimeOffset now,
CancellationToken cancellationToken
@@ -81,7 +97,7 @@ public sealed class TelegramNotifier(IAppDbContext dbContext, ITelegramApi api)
settings,
entry.ChannelId,
TelegramNotificationKind.ShowStart,
$"▶️ <b>{TelegramText.Escape(title)}</b>\nСейчас в эфире",
$"▶️ <b>{TelegramText.Escape(title)}</b>\nСейчас в эфире{ChannelLine(channels, entry.ChannelId)}",
cancellationToken
);
}
@@ -92,6 +108,7 @@ public sealed class TelegramNotifier(IAppDbContext dbContext, ITelegramApi api)
/// <summary>Что начнётся через <see cref="UpNextLead"/> — предупреждаем один раз, в своё окно.</summary>
private async Task<int> NotifyUpNextAsync(
TelegramSettings settings,
IReadOnlyDictionary<Guid, string> channels,
DateTimeOffset from,
DateTimeOffset now,
CancellationToken cancellationToken
@@ -123,7 +140,7 @@ public sealed class TelegramNotifier(IAppDbContext dbContext, ITelegramApi api)
settings,
entry.ChannelId,
TelegramNotificationKind.UpNext,
$"🕔 Через {minutes} мин: <b>{TelegramText.Escape(title)}</b>",
$"🕔 <b>{TelegramText.Escape(title)}</b>\nЧерез {minutes} мин{ChannelLine(channels, entry.ChannelId)}",
cancellationToken
);
}