| | | 1 | | using TeleFlow.Telegram.Internal; |
| | | 2 | | |
| | | 3 | | namespace TeleFlow.Telegram.Internal; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Holds the normalized identity of the current Telegram bot for framework features that must distinguish |
| | | 7 | | /// commands addressed to this bot from commands addressed to another bot. Identity is configured locally or |
| | | 8 | | /// resolved once during transport startup; handler selection only reads the cached value. |
| | | 9 | | /// </summary> |
| | | 10 | | internal sealed class TelegramBotIdentity |
| | | 11 | | { |
| | 219 | 12 | | private readonly object _sync = new(); |
| | | 13 | | private string? _username; |
| | | 14 | | private Task? _resolutionTask; |
| | | 15 | | |
| | | 16 | | public TelegramBotIdentity(TelegramBotOptions options) |
| | | 17 | | { |
| | 219 | 18 | | ArgumentNullException.ThrowIfNull(options); |
| | | 19 | | |
| | 219 | 20 | | if (!TelegramBotUsernameNormalizer.TryNormalize(options.BotUsername, out _username, out var error)) |
| | | 21 | | { |
| | 0 | 22 | | throw new InvalidOperationException(error); |
| | | 23 | | } |
| | 219 | 24 | | } |
| | | 25 | | |
| | | 26 | | public bool MatchesMention(ReadOnlySpan<char> mention) |
| | | 27 | | { |
| | 29 | 28 | | var username = Volatile.Read(ref _username); |
| | | 29 | | |
| | 29 | 30 | | return username is not null && |
| | 29 | 31 | | username.AsSpan().Equals(mention, StringComparison.OrdinalIgnoreCase); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public Task EnsureResolvedAsync( |
| | | 35 | | ITelegramClient bot, |
| | | 36 | | CancellationToken cancellationToken = default) |
| | | 37 | | { |
| | 16 | 38 | | ArgumentNullException.ThrowIfNull(bot); |
| | | 39 | | |
| | 16 | 40 | | if (Volatile.Read(ref _username) is not null) |
| | | 41 | | { |
| | 15 | 42 | | return Task.CompletedTask; |
| | | 43 | | } |
| | | 44 | | |
| | 1 | 45 | | lock (_sync) |
| | | 46 | | { |
| | 1 | 47 | | if (_username is not null) |
| | | 48 | | { |
| | 0 | 49 | | return Task.CompletedTask; |
| | | 50 | | } |
| | | 51 | | |
| | 1 | 52 | | return _resolutionTask ??= ResolveAsync(bot, cancellationToken); |
| | | 53 | | } |
| | 1 | 54 | | } |
| | | 55 | | |
| | | 56 | | private async Task ResolveAsync(ITelegramClient bot, CancellationToken cancellationToken) |
| | | 57 | | { |
| | | 58 | | try |
| | | 59 | | { |
| | 1 | 60 | | var user = await bot.GetMeAsync(cancellationToken).ConfigureAwait(false); |
| | | 61 | | |
| | 1 | 62 | | if (!TelegramBotUsernameNormalizer.TryNormalize(user.Username, out var username, out var error) || |
| | 1 | 63 | | username is null) |
| | | 64 | | { |
| | 0 | 65 | | throw new InvalidOperationException( |
| | 0 | 66 | | $"Telegram bot identity could not be resolved because getMe returned an invalid username. {error}"); |
| | | 67 | | } |
| | | 68 | | |
| | 1 | 69 | | Volatile.Write(ref _username, username); |
| | 1 | 70 | | } |
| | 0 | 71 | | catch |
| | | 72 | | { |
| | 0 | 73 | | lock (_sync) |
| | | 74 | | { |
| | 0 | 75 | | _resolutionTask = null; |
| | 0 | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | throw; |
| | | 79 | | } |
| | 1 | 80 | | } |
| | | 81 | | } |