| | | 1 | | namespace TeleFlow.Telegram; |
| | | 2 | | |
| | | 3 | | public sealed class TelegramAllowedUpdates |
| | | 4 | | { |
| | | 5 | | private TelegramAllowedUpdates(TelegramAllowedUpdatesMode mode, IReadOnlyList<TelegramUpdateType> updateTypes) |
| | | 6 | | { |
| | | 7 | | Mode = mode; |
| | | 8 | | UpdateTypes = updateTypes; |
| | 3 | 9 | | } |
| | | 10 | | |
| | | 11 | | internal TelegramAllowedUpdatesMode Mode { get; } |
| | | 12 | | |
| | | 13 | | internal IReadOnlyList<TelegramUpdateType> UpdateTypes { get; } |
| | | 14 | | |
| | 1 | 15 | | public static TelegramAllowedUpdates Auto { get; } = new(TelegramAllowedUpdatesMode.Auto, []); |
| | | 16 | | |
| | 1 | 17 | | public static TelegramAllowedUpdates All { get; } = new(TelegramAllowedUpdatesMode.All, []); |
| | | 18 | | |
| | | 19 | | public static TelegramAllowedUpdates Only(params TelegramUpdateType[] updateTypes) |
| | | 20 | | { |
| | 3 | 21 | | return Only((IEnumerable<TelegramUpdateType>)updateTypes); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public static TelegramAllowedUpdates Only(IEnumerable<TelegramUpdateType> updateTypes) |
| | | 25 | | { |
| | 3 | 26 | | ArgumentNullException.ThrowIfNull(updateTypes); |
| | | 27 | | |
| | 3 | 28 | | var normalized = updateTypes.ToArray(); |
| | 3 | 29 | | if (normalized.Length == 0) |
| | | 30 | | { |
| | 1 | 31 | | throw new ArgumentException("At least one Telegram update type must be specified.", nameof(updateTypes)); |
| | | 32 | | } |
| | | 33 | | |
| | 12 | 34 | | foreach (var updateType in normalized) |
| | | 35 | | { |
| | 4 | 36 | | if (string.IsNullOrWhiteSpace(updateType.Value)) |
| | | 37 | | { |
| | 0 | 38 | | throw new ArgumentException("Telegram update type values must not be empty.", nameof(updateTypes)); |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | 2 | 42 | | var duplicate = normalized |
| | 4 | 43 | | .GroupBy(static updateType => updateType.Value, StringComparer.Ordinal) |
| | 5 | 44 | | .FirstOrDefault(static group => group.Count() > 1); |
| | | 45 | | |
| | 2 | 46 | | if (duplicate is not null) |
| | | 47 | | { |
| | 1 | 48 | | throw new ArgumentException( |
| | 1 | 49 | | $"Duplicate Telegram update type '{duplicate.Key}' was specified.", |
| | 1 | 50 | | nameof(updateTypes)); |
| | | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | return new TelegramAllowedUpdates(TelegramAllowedUpdatesMode.Only, normalized); |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | internal enum TelegramAllowedUpdatesMode |
| | | 58 | | { |
| | | 59 | | Auto, |
| | | 60 | | All, |
| | | 61 | | Only |
| | | 62 | | } |