< Summary

Information
Class: TeleFlow.Annotations.ChatUsernameAttribute
Assembly: TeleFlow.Annotations
File(s): /_/src/TeleFlow.Annotations/Filters/ChatUsernameAttribute.cs
Line coverage
78%
Covered lines: 11
Uncovered lines: 3
Coverable lines: 14
Total lines: 48
Line coverage: 78.5%
Branch coverage
42%
Covered branches: 3
Total branches: 7
Branch coverage: 42.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)33.33%3380%
CanonicalizeUsername(...)50%4475%

File(s)

/_/src/TeleFlow.Annotations/Filters/ChatUsernameAttribute.cs

#LineLine coverage
 1namespace TeleFlow.Annotations;
 2/// <summary>
 3/// Restricts a handler or handler class to specific Telegram chat usernames.
 4/// </summary>
 5[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
 6public sealed class ChatUsernameAttribute : TeleFlowAttribute
 7{
 8    /// <summary>
 9    /// Creates a chat username filter.
 10    /// </summary>
 11    /// <param name="usernames">Telegram chat usernames allowed to match, with or without leading <c>@</c>.</param>
 812    public ChatUsernameAttribute(params string[] usernames)
 13    {
 814        ArgumentNullException.ThrowIfNull(usernames);
 15
 816        var canonicalUsernames = usernames
 817            .Select(CanonicalizeUsername)
 818            .ToArray();
 19
 820        if (canonicalUsernames.Length == 0)
 21        {
 022            throw new ArgumentException("At least one Telegram chat username must be specified.", nameof(usernames));
 23        }
 24
 1625        if (canonicalUsernames.Any(static username => username.Length == 0))
 26        {
 027            throw new ArgumentException("Telegram chat usernames must not be empty.", nameof(usernames));
 28        }
 29
 30        Usernames = canonicalUsernames;
 831    }
 32
 33    /// <summary>
 34    /// Canonical Telegram chat usernames allowed to match, without leading <c>@</c>.
 35    /// </summary>
 36    public IReadOnlyList<string> Usernames { get; }
 37
 38    private static string CanonicalizeUsername(string? username)
 39    {
 840        if (username is null)
 41        {
 042            return string.Empty;
 43        }
 44
 845        var value = username.Trim();
 846        return value.StartsWith('@') ? value[1..] : value;
 47    }
 48}