| | | 1 | | using TeleFlow.Annotations; |
| | | 2 | | |
| | | 3 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 4 | | |
| | | 5 | | internal sealed class TelegramCommandPolicy |
| | | 6 | | { |
| | 1 | 7 | | public static TelegramCommandPolicy Default { get; } = new(["/"], allowSpaceAfterPrefix: false, ignoreCase: true); |
| | | 8 | | |
| | | 9 | | public TelegramCommandPolicy( |
| | | 10 | | IReadOnlyList<string> prefixes, |
| | | 11 | | bool allowSpaceAfterPrefix, |
| | | 12 | | bool ignoreCase, |
| | | 13 | | CommandPrefixMode prefixMode = CommandPrefixMode.Required) |
| | | 14 | | { |
| | 934 | 15 | | ArgumentNullException.ThrowIfNull(prefixes); |
| | | 16 | | |
| | 934 | 17 | | if (!Enum.IsDefined(prefixMode)) |
| | | 18 | | { |
| | 0 | 19 | | throw new InvalidOperationException($"Telegram command prefix mode '{prefixMode}' is not supported."); |
| | | 20 | | } |
| | | 21 | | |
| | 934 | 22 | | if (prefixes.Count == 0 || prefixes.Any(string.IsNullOrWhiteSpace)) |
| | | 23 | | { |
| | 1 | 24 | | throw new InvalidOperationException("Telegram command route prefixes must contain at least one non-empty pre |
| | | 25 | | } |
| | | 26 | | |
| | 933 | 27 | | Prefixes = prefixes |
| | 961 | 28 | | .Select(static prefix => prefix.Trim()) |
| | 933 | 29 | | .Distinct(StringComparer.Ordinal) |
| | 56 | 30 | | .OrderByDescending(static prefix => prefix.Length) |
| | 56 | 31 | | .ThenBy(static prefix => prefix, StringComparer.Ordinal) |
| | 933 | 32 | | .ToArray(); |
| | | 33 | | AllowSpaceAfterPrefix = allowSpaceAfterPrefix; |
| | | 34 | | IgnoreCase = ignoreCase; |
| | | 35 | | PrefixMode = prefixMode; |
| | 933 | 36 | | } |
| | | 37 | | |
| | | 38 | | public IReadOnlyList<string> Prefixes { get; } |
| | | 39 | | |
| | | 40 | | public bool AllowSpaceAfterPrefix { get; } |
| | | 41 | | |
| | | 42 | | public bool IgnoreCase { get; } |
| | | 43 | | |
| | | 44 | | public CommandPrefixMode PrefixMode { get; } |
| | | 45 | | } |