| | | 1 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 2 | | |
| | | 3 | | internal sealed class TelegramRouteDescriptor |
| | | 4 | | { |
| | | 5 | | public TelegramRouteDescriptor( |
| | | 6 | | TelegramHandlerKind kind, |
| | | 7 | | string? command, |
| | | 8 | | IReadOnlyList<TelegramTextFilter> textFilters, |
| | | 9 | | Type? callbackPayloadType) |
| | 0 | 10 | | : this( |
| | 0 | 11 | | MapLegacyRouteKind(kind, command), |
| | 0 | 12 | | command, |
| | 0 | 13 | | TelegramCommandPolicy.Default, |
| | 0 | 14 | | textFilters, |
| | 0 | 15 | | callbackPayloadType, |
| | 0 | 16 | | routeValues: [], |
| | 0 | 17 | | filters: [], |
| | 0 | 18 | | chatMemberTransitions: [], |
| | 0 | 19 | | roleRequirements: []) |
| | | 20 | | { |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public TelegramRouteDescriptor( |
| | | 24 | | TelegramRouteKind routeKind, |
| | | 25 | | string? pattern, |
| | | 26 | | TelegramCommandPolicy? commandPolicy, |
| | | 27 | | IReadOnlyList<TelegramTextFilter> textFilters, |
| | | 28 | | Type? callbackPayloadType, |
| | | 29 | | IReadOnlyList<TelegramRouteValueDescriptor> routeValues) |
| | 0 | 30 | | : this( |
| | 0 | 31 | | routeKind, |
| | 0 | 32 | | pattern, |
| | 0 | 33 | | commandPolicy, |
| | 0 | 34 | | textFilters, |
| | 0 | 35 | | callbackPayloadType, |
| | 0 | 36 | | routeValues, |
| | 0 | 37 | | filters: [], |
| | 0 | 38 | | chatMemberTransitions: [], |
| | 0 | 39 | | roleRequirements: []) |
| | | 40 | | { |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public TelegramRouteDescriptor( |
| | | 44 | | TelegramRouteKind routeKind, |
| | | 45 | | string? pattern, |
| | | 46 | | TelegramCommandPolicy? commandPolicy, |
| | | 47 | | IReadOnlyList<TelegramTextFilter> textFilters, |
| | | 48 | | Type? callbackPayloadType, |
| | | 49 | | IReadOnlyList<TelegramRouteValueDescriptor> routeValues, |
| | | 50 | | IReadOnlyList<TelegramFilterDescriptor> filters) |
| | 0 | 51 | | : this( |
| | 0 | 52 | | routeKind, |
| | 0 | 53 | | pattern, |
| | 0 | 54 | | commandPolicy, |
| | 0 | 55 | | textFilters, |
| | 0 | 56 | | callbackPayloadType, |
| | 0 | 57 | | routeValues, |
| | 0 | 58 | | filters, |
| | 0 | 59 | | chatMemberTransitions: [], |
| | 0 | 60 | | roleRequirements: []) |
| | | 61 | | { |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public TelegramRouteDescriptor( |
| | | 65 | | TelegramRouteKind routeKind, |
| | | 66 | | string? pattern, |
| | | 67 | | TelegramCommandPolicy? commandPolicy, |
| | | 68 | | IReadOnlyList<TelegramTextFilter> textFilters, |
| | | 69 | | Type? callbackPayloadType, |
| | | 70 | | IReadOnlyList<TelegramRouteValueDescriptor> routeValues, |
| | | 71 | | IReadOnlyList<TelegramFilterDescriptor> filters, |
| | | 72 | | IReadOnlyList<TelegramChatMemberTransitionDescriptor> chatMemberTransitions) |
| | 0 | 73 | | : this( |
| | 0 | 74 | | routeKind, |
| | 0 | 75 | | pattern, |
| | 0 | 76 | | commandPolicy, |
| | 0 | 77 | | textFilters, |
| | 0 | 78 | | callbackPayloadType, |
| | 0 | 79 | | routeValues, |
| | 0 | 80 | | filters, |
| | 0 | 81 | | chatMemberTransitions, |
| | 0 | 82 | | roleRequirements: []) |
| | | 83 | | { |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | public TelegramRouteDescriptor( |
| | | 87 | | TelegramRouteKind routeKind, |
| | | 88 | | string? pattern, |
| | | 89 | | TelegramCommandPolicy? commandPolicy, |
| | | 90 | | IReadOnlyList<TelegramTextFilter> textFilters, |
| | | 91 | | Type? callbackPayloadType, |
| | | 92 | | IReadOnlyList<TelegramRouteValueDescriptor> routeValues, |
| | | 93 | | IReadOnlyList<TelegramFilterDescriptor> filters, |
| | | 94 | | IReadOnlyList<TelegramChatMemberTransitionDescriptor> chatMemberTransitions, |
| | | 95 | | IReadOnlyList<TelegramRoleRequirementDescriptor> roleRequirements) |
| | | 96 | | { |
| | 1152 | 97 | | ArgumentNullException.ThrowIfNull(textFilters); |
| | 1152 | 98 | | ArgumentNullException.ThrowIfNull(routeValues); |
| | 1152 | 99 | | ArgumentNullException.ThrowIfNull(filters); |
| | 1152 | 100 | | ArgumentNullException.ThrowIfNull(chatMemberTransitions); |
| | 1152 | 101 | | ArgumentNullException.ThrowIfNull(roleRequirements); |
| | | 102 | | |
| | 1152 | 103 | | var normalizedPattern = NormalizeCommandPattern(routeKind, pattern); |
| | | 104 | | |
| | | 105 | | RouteKind = routeKind; |
| | 1152 | 106 | | Kind = GetHandlerKind(routeKind); |
| | | 107 | | Pattern = normalizedPattern; |
| | 1152 | 108 | | CommandPolicy = commandPolicy ?? TelegramCommandPolicy.Default; |
| | | 109 | | TextFilters = textFilters; |
| | | 110 | | CallbackPayloadType = callbackPayloadType; |
| | | 111 | | RouteValues = routeValues; |
| | | 112 | | Filters = filters; |
| | | 113 | | ChatMemberTransitions = chatMemberTransitions; |
| | | 114 | | RoleRequirements = roleRequirements; |
| | 1152 | 115 | | Matcher = TelegramRouteMatcher.Create(routeKind, normalizedPattern, CommandPolicy.IgnoreCase); |
| | 1152 | 116 | | Specificity = GetSpecificity(routeKind, normalizedPattern); |
| | 1152 | 117 | | } |
| | | 118 | | |
| | | 119 | | private static TelegramRouteKind MapLegacyRouteKind( |
| | | 120 | | TelegramHandlerKind kind, |
| | | 121 | | string? command) |
| | | 122 | | { |
| | 0 | 123 | | return kind switch |
| | 0 | 124 | | { |
| | 0 | 125 | | TelegramHandlerKind.Command => TelegramRouteKind.CommandExact, |
| | 0 | 126 | | TelegramHandlerKind.Callback => TelegramRouteKind.Callback, |
| | 0 | 127 | | TelegramHandlerKind.Message when command is null => TelegramRouteKind.MessageAny, |
| | 0 | 128 | | _ => throw new InvalidOperationException($"Unsupported Telegram handler kind '{kind}'.") |
| | 0 | 129 | | }; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | private static TelegramHandlerKind GetHandlerKind(TelegramRouteKind routeKind) |
| | | 133 | | { |
| | 1152 | 134 | | return routeKind switch |
| | 1152 | 135 | | { |
| | 1152 | 136 | | TelegramRouteKind.CommandExact or |
| | 1152 | 137 | | TelegramRouteKind.CommandTemplate or |
| | 383 | 138 | | TelegramRouteKind.CommandRegex => TelegramHandlerKind.Command, |
| | 268 | 139 | | TelegramRouteKind.Callback => TelegramHandlerKind.Callback, |
| | 1152 | 140 | | TelegramRouteKind.ChatMemberUpdated or |
| | 42 | 141 | | TelegramRouteKind.MyChatMemberUpdated => TelegramHandlerKind.ChatMember, |
| | 459 | 142 | | _ => TelegramHandlerKind.Message |
| | 1152 | 143 | | }; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public TelegramHandlerKind Kind { get; } |
| | | 147 | | |
| | | 148 | | public TelegramRouteKind RouteKind { get; } |
| | | 149 | | |
| | | 150 | | public string? Pattern { get; } |
| | | 151 | | |
| | 1116 | 152 | | public string? Command => Kind == TelegramHandlerKind.Command ? Pattern : null; |
| | | 153 | | |
| | | 154 | | public TelegramCommandPolicy CommandPolicy { get; } |
| | | 155 | | |
| | | 156 | | public IReadOnlyList<TelegramTextFilter> TextFilters { get; } |
| | | 157 | | |
| | | 158 | | public Type? CallbackPayloadType { get; } |
| | | 159 | | |
| | | 160 | | public IReadOnlyList<TelegramRouteValueDescriptor> RouteValues { get; } |
| | | 161 | | |
| | | 162 | | public IReadOnlyList<TelegramFilterDescriptor> Filters { get; } |
| | | 163 | | |
| | | 164 | | public IReadOnlyList<TelegramChatMemberTransitionDescriptor> ChatMemberTransitions { get; } |
| | | 165 | | |
| | | 166 | | public IReadOnlyList<TelegramRoleRequirementDescriptor> RoleRequirements { get; } |
| | | 167 | | |
| | | 168 | | public TelegramRouteMatcher Matcher { get; } |
| | | 169 | | |
| | | 170 | | public int Specificity { get; } |
| | | 171 | | |
| | | 172 | | private static string? NormalizeCommandPattern( |
| | | 173 | | TelegramRouteKind routeKind, |
| | | 174 | | string? pattern) |
| | | 175 | | { |
| | 1152 | 176 | | if (pattern is null || |
| | 1152 | 177 | | routeKind is not (TelegramRouteKind.CommandExact or TelegramRouteKind.CommandTemplate)) |
| | | 178 | | { |
| | 772 | 179 | | return pattern; |
| | | 180 | | } |
| | | 181 | | |
| | 380 | 182 | | return TelegramCommandTextNormalizer.Normalize(pattern); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | private static int GetSpecificity( |
| | | 186 | | TelegramRouteKind routeKind, |
| | | 187 | | string? pattern) |
| | | 188 | | { |
| | 1152 | 189 | | return routeKind is TelegramRouteKind.TextTemplate or TelegramRouteKind.CommandTemplate && |
| | 1152 | 190 | | !string.IsNullOrWhiteSpace(pattern) |
| | 1152 | 191 | | ? TelegramTemplateRouteParser.GetSpecificity(pattern) |
| | 1152 | 192 | | : 0; |
| | | 193 | | } |
| | | 194 | | } |