< Summary

Information
Class: TeleFlow.Telegram.Internal.TelegramBotIdentity
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/Internal/TelegramBotIdentity.cs
Line coverage
68%
Covered lines: 20
Uncovered lines: 9
Coverable lines: 29
Total lines: 81
Line coverage: 68.9%
Branch coverage
71%
Covered branches: 10
Total branches: 14
Branch coverage: 71.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%2280%
MatchesMention(...)100%22100%
EnsureResolvedAsync(...)83.33%6687.5%
ResolveAsync()50%6446.15%

File(s)

/_/src/TeleFlow.Framework/Internal/TelegramBotIdentity.cs

#LineLine coverage
 1using TeleFlow.Telegram.Internal;
 2
 3namespace 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>
 10internal sealed class TelegramBotIdentity
 11{
 21912    private readonly object _sync = new();
 13    private string? _username;
 14    private Task? _resolutionTask;
 15
 16    public TelegramBotIdentity(TelegramBotOptions options)
 17    {
 21918        ArgumentNullException.ThrowIfNull(options);
 19
 21920        if (!TelegramBotUsernameNormalizer.TryNormalize(options.BotUsername, out _username, out var error))
 21        {
 022            throw new InvalidOperationException(error);
 23        }
 21924    }
 25
 26    public bool MatchesMention(ReadOnlySpan<char> mention)
 27    {
 2928        var username = Volatile.Read(ref _username);
 29
 2930        return username is not null &&
 2931               username.AsSpan().Equals(mention, StringComparison.OrdinalIgnoreCase);
 32    }
 33
 34    public Task EnsureResolvedAsync(
 35        ITelegramClient bot,
 36        CancellationToken cancellationToken = default)
 37    {
 1638        ArgumentNullException.ThrowIfNull(bot);
 39
 1640        if (Volatile.Read(ref _username) is not null)
 41        {
 1542            return Task.CompletedTask;
 43        }
 44
 145        lock (_sync)
 46        {
 147            if (_username is not null)
 48            {
 049                return Task.CompletedTask;
 50            }
 51
 152            return _resolutionTask ??= ResolveAsync(bot, cancellationToken);
 53        }
 154    }
 55
 56    private async Task ResolveAsync(ITelegramClient bot, CancellationToken cancellationToken)
 57    {
 58        try
 59        {
 160            var user = await bot.GetMeAsync(cancellationToken).ConfigureAwait(false);
 61
 162            if (!TelegramBotUsernameNormalizer.TryNormalize(user.Username, out var username, out var error) ||
 163                username is null)
 64            {
 065                throw new InvalidOperationException(
 066                    $"Telegram bot identity could not be resolved because getMe returned an invalid username. {error}");
 67            }
 68
 169            Volatile.Write(ref _username, username);
 170        }
 071        catch
 72        {
 073            lock (_sync)
 74            {
 075                _resolutionTask = null;
 076            }
 77
 078            throw;
 79        }
 180    }
 81}