| | | 1 | | using System.Reflection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using TeleFlow.Annotations; |
| | | 4 | | |
| | | 5 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 6 | | |
| | | 7 | | internal static class TelegramHandlerRegistrationValidator |
| | | 8 | | { |
| | | 9 | | public static void EnsureAssemblyCanRegister( |
| | | 10 | | IServiceCollection services, |
| | | 11 | | Assembly assembly, |
| | | 12 | | TelegramHandlerRegistrationMode requestedMode) |
| | | 13 | | { |
| | 54 | 14 | | ArgumentNullException.ThrowIfNull(services); |
| | 54 | 15 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 16 | | |
| | 54 | 17 | | var existing = services |
| | 1426 | 18 | | .Where(static descriptor => descriptor.ServiceType == typeof(TelegramHandlerAssemblyRegistrationMarker)) |
| | 2 | 19 | | .Select(static descriptor => descriptor.ImplementationInstance) |
| | 54 | 20 | | .OfType<TelegramHandlerAssemblyRegistrationMarker>() |
| | 56 | 21 | | .FirstOrDefault(marker => marker.Assembly == assembly); |
| | | 22 | | |
| | 54 | 23 | | if (existing is null) |
| | | 24 | | { |
| | 52 | 25 | | return; |
| | | 26 | | } |
| | | 27 | | |
| | 2 | 28 | | throw new InvalidOperationException( |
| | 2 | 29 | | $"Telegram handler assembly '{assembly.FullName}' is already registered through {FormatMode(existing.Mode)} |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public static void EnsureHandlerTypesCanRegister( |
| | | 33 | | IServiceCollection services, |
| | | 34 | | IReadOnlyList<Type> handlerTypes, |
| | | 35 | | TelegramHandlerRegistrationMode requestedMode) |
| | | 36 | | { |
| | 381 | 37 | | ArgumentNullException.ThrowIfNull(services); |
| | 381 | 38 | | ArgumentNullException.ThrowIfNull(handlerTypes); |
| | | 39 | | |
| | 381 | 40 | | var duplicateInRequest = handlerTypes |
| | 1290 | 41 | | .GroupBy(static type => type) |
| | 1671 | 42 | | .FirstOrDefault(static group => group.Count() > 1); |
| | | 43 | | |
| | 381 | 44 | | if (duplicateInRequest is not null) |
| | | 45 | | { |
| | 0 | 46 | | throw new InvalidOperationException( |
| | 0 | 47 | | $"Telegram handler type '{duplicateInRequest.Key.FullName}' is listed more than once for {FormatMode(req |
| | | 48 | | } |
| | | 49 | | |
| | 381 | 50 | | var existingMarkers = services |
| | 11119 | 51 | | .Where(static descriptor => descriptor.ServiceType == typeof(TelegramHandlerTypeRegistrationMarker)) |
| | 231 | 52 | | .Select(static descriptor => descriptor.ImplementationInstance) |
| | 381 | 53 | | .OfType<TelegramHandlerTypeRegistrationMarker>() |
| | 381 | 54 | | .ToArray(); |
| | | 55 | | |
| | 3340 | 56 | | foreach (var handlerType in handlerTypes) |
| | | 57 | | { |
| | 1521 | 58 | | var existing = existingMarkers.FirstOrDefault(marker => marker.HandlerType == handlerType); |
| | | 59 | | |
| | 1290 | 60 | | if (existing is null) |
| | | 61 | | { |
| | | 62 | | continue; |
| | | 63 | | } |
| | | 64 | | |
| | 2 | 65 | | throw new InvalidOperationException( |
| | 2 | 66 | | $"Telegram handler type '{handlerType.FullName}' is already registered through {FormatMode(existing.Mode |
| | | 67 | | } |
| | 379 | 68 | | } |
| | | 69 | | |
| | | 70 | | public static void EnsureNoDuplicateCommands( |
| | | 71 | | IServiceCollection services, |
| | | 72 | | IReadOnlyList<TelegramHandlerDescriptor> newDescriptors) |
| | | 73 | | { |
| | 354 | 74 | | ArgumentNullException.ThrowIfNull(services); |
| | 354 | 75 | | ArgumentNullException.ThrowIfNull(newDescriptors); |
| | | 76 | | |
| | 354 | 77 | | var existingCommandHandlers = services |
| | 10434 | 78 | | .Where(static descriptor => descriptor.ServiceType == typeof(TelegramHandlerDescriptor)) |
| | 226 | 79 | | .Select(static descriptor => descriptor.ImplementationInstance) |
| | 354 | 80 | | .OfType<TelegramHandlerDescriptor>() |
| | 226 | 81 | | .Where(static descriptor => descriptor.Route.RouteKind == TelegramRouteKind.CommandExact) |
| | 354 | 82 | | .ToArray(); |
| | | 83 | | |
| | 354 | 84 | | var allCommandHandlers = existingCommandHandlers |
| | 1151 | 85 | | .Concat(newDescriptors.Where(static descriptor => descriptor.Route.RouteKind == TelegramRouteKind.CommandExa |
| | 354 | 86 | | .SelectMany(CreateDuplicateKeys) |
| | 354 | 87 | | .ToArray(); |
| | | 88 | | |
| | 354 | 89 | | var duplicate = allCommandHandlers |
| | 391 | 90 | | .GroupBy(static item => item.Key, StringComparer.Ordinal) |
| | 743 | 91 | | .FirstOrDefault(static group => group.Count() > 1); |
| | | 92 | | |
| | 354 | 93 | | if (duplicate is not null) |
| | | 94 | | { |
| | 2 | 95 | | var display = duplicate.First().Display; |
| | | 96 | | |
| | 2 | 97 | | throw new InvalidOperationException( |
| | 2 | 98 | | $"Duplicate Telegram command handler registration for command '{display}'."); |
| | | 99 | | } |
| | 352 | 100 | | } |
| | | 101 | | |
| | | 102 | | private static IEnumerable<DuplicateCommandCandidate> CreateDuplicateKeys(TelegramHandlerDescriptor descriptor) |
| | | 103 | | { |
| | 334 | 104 | | if (string.IsNullOrWhiteSpace(descriptor.Command)) |
| | | 105 | | { |
| | 0 | 106 | | yield break; |
| | | 107 | | } |
| | | 108 | | |
| | 334 | 109 | | if (descriptor.Route.CommandPolicy.PrefixMode is CommandPrefixMode.Required or CommandPrefixMode.Optional) |
| | | 110 | | { |
| | 1386 | 111 | | foreach (var prefix in descriptor.Route.CommandPolicy.Prefixes) |
| | | 112 | | { |
| | 361 | 113 | | yield return new DuplicateCommandCandidate( |
| | 361 | 114 | | $"{prefix.ToUpperInvariant()}\u001F{descriptor.Route.CommandPolicy.AllowSpaceAfterPrefix}\u001FPREFI |
| | 361 | 115 | | $"{prefix}{descriptor.Command}"); |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | 334 | 119 | | if (descriptor.Route.CommandPolicy.PrefixMode is CommandPrefixMode.Optional or CommandPrefixMode.NoPrefix) |
| | | 120 | | { |
| | 30 | 121 | | yield return new DuplicateCommandCandidate( |
| | 30 | 122 | | $"NO_PREFIX\u001F{descriptor.Command.ToUpperInvariant()}", |
| | 30 | 123 | | descriptor.Command); |
| | | 124 | | } |
| | 334 | 125 | | } |
| | | 126 | | |
| | | 127 | | private static string FormatMode(TelegramHandlerRegistrationMode mode) |
| | | 128 | | { |
| | 8 | 129 | | return mode switch |
| | 8 | 130 | | { |
| | 2 | 131 | | TelegramHandlerRegistrationMode.Direct => "direct", |
| | 2 | 132 | | TelegramHandlerRegistrationMode.Generated => "generated", |
| | 4 | 133 | | TelegramHandlerRegistrationMode.Reflection => "reflection", |
| | 0 | 134 | | _ => mode.ToString() |
| | 8 | 135 | | }; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | private sealed record DuplicateCommandCandidate(string Key, string Display); |
| | | 139 | | } |