| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | using TeleFlow.Annotations; |
| | | 4 | | using TeleFlow.Framework.States; |
| | | 5 | | |
| | | 6 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 7 | | |
| | | 8 | | internal static class TelegramHandlerDescriptorBuilder |
| | | 9 | | { |
| | 1 | 10 | | private static readonly NullabilityInfoContext NullabilityInfoContext = new(); |
| | | 11 | | |
| | | 12 | | public static IReadOnlyList<TelegramHandlerDescriptor> Build( |
| | | 13 | | Type handlerType, |
| | | 14 | | int firstRegistrationOrder) |
| | | 15 | | { |
| | 356 | 16 | | ArgumentNullException.ThrowIfNull(handlerType); |
| | | 17 | | |
| | 356 | 18 | | if (handlerType.IsAbstract || handlerType.IsInterface) |
| | | 19 | | { |
| | 0 | 20 | | throw new InvalidOperationException( |
| | 0 | 21 | | $"Telegram handler type '{handlerType.FullName}' must be a concrete class."); |
| | | 22 | | } |
| | | 23 | | |
| | 356 | 24 | | var registrationOrder = firstRegistrationOrder; |
| | 356 | 25 | | var descriptors = new List<TelegramHandlerDescriptor>(); |
| | 356 | 26 | | var moduleName = GetModuleName(handlerType); |
| | 356 | 27 | | var isClassBasedHandler = IsClassBasedHandlerType(handlerType); |
| | | 28 | | |
| | 356 | 29 | | if (!isClassBasedHandler && HasRouteAttributes(handlerType)) |
| | | 30 | | { |
| | 1 | 31 | | throw new InvalidOperationException( |
| | 1 | 32 | | $"Telegram handler type '{handlerType.FullName}' declares class-level route metadata but does not derive |
| | | 33 | | } |
| | | 34 | | |
| | 1405 | 35 | | foreach (var method in GetCandidateMethods(handlerType, isClassBasedHandler)) |
| | | 36 | | { |
| | 358 | 37 | | var methodDescriptors = BuildForMethod( |
| | 358 | 38 | | handlerType, |
| | 358 | 39 | | method, |
| | 358 | 40 | | registrationOrder, |
| | 358 | 41 | | moduleName, |
| | 358 | 42 | | includeClassRouteAttributes: isClassBasedHandler); |
| | | 43 | | |
| | 338 | 44 | | descriptors.AddRange(methodDescriptors); |
| | 338 | 45 | | registrationOrder += methodDescriptors.Count; |
| | | 46 | | } |
| | | 47 | | |
| | 334 | 48 | | if (HasInheritedHandlerMethods(handlerType)) |
| | | 49 | | { |
| | 2 | 50 | | throw new InvalidOperationException( |
| | 2 | 51 | | $"Telegram handler type '{handlerType.FullName}' inherits handler methods. " + |
| | 2 | 52 | | "Inherited handler methods must be overridden in the concrete handler type for generated registration pa |
| | | 53 | | } |
| | | 54 | | |
| | 332 | 55 | | return descriptors; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private static IEnumerable<MethodInfo> GetCandidateMethods( |
| | | 59 | | Type handlerType, |
| | | 60 | | bool isClassBasedHandler) |
| | | 61 | | { |
| | 355 | 62 | | if (!isClassBasedHandler) |
| | | 63 | | { |
| | 346 | 64 | | return GetDeclaredCandidateMethods(handlerType); |
| | | 65 | | } |
| | | 66 | | |
| | 9 | 67 | | var handleMethods = GetDeclaredCandidateMethods(handlerType) |
| | 8 | 68 | | .Where(static method => string.Equals(method.Name, "HandleAsync", StringComparison.Ordinal)) |
| | 9 | 69 | | .ToArray(); |
| | | 70 | | |
| | 9 | 71 | | if (handleMethods.Length != 1) |
| | | 72 | | { |
| | 1 | 73 | | throw new InvalidOperationException( |
| | 1 | 74 | | $"Class-based Telegram handler type '{handlerType.FullName}' must declare exactly one public instance Ha |
| | | 75 | | } |
| | | 76 | | |
| | 8 | 77 | | var routeMethods = GetDeclaredCandidateMethods(handlerType) |
| | 8 | 78 | | .Where(static method => !string.Equals(method.Name, "HandleAsync", StringComparison.Ordinal)) |
| | 8 | 79 | | .Where(HasRouteAttributes) |
| | 8 | 80 | | .ToArray(); |
| | | 81 | | |
| | 8 | 82 | | if (routeMethods.Length > 0) |
| | | 83 | | { |
| | 0 | 84 | | throw new InvalidOperationException( |
| | 0 | 85 | | $"Class-based Telegram handler type '{handlerType.FullName}' must declare route metadata on the type or |
| | | 86 | | } |
| | | 87 | | |
| | 8 | 88 | | return handleMethods; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | private static IEnumerable<MethodInfo> GetDeclaredCandidateMethods(Type handlerType) |
| | | 92 | | { |
| | 363 | 93 | | return handlerType |
| | 363 | 94 | | .GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly) |
| | 366 | 95 | | .Where(static method => !method.IsSpecialName) |
| | 729 | 96 | | .OrderBy(static method => method.MetadataToken); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static bool HasInheritedHandlerMethods(Type handlerType) |
| | | 100 | | { |
| | 334 | 101 | | return handlerType |
| | 334 | 102 | | .GetMethods(BindingFlags.Instance | BindingFlags.Public) |
| | 1668 | 103 | | .Where(method => method.DeclaringType != handlerType) |
| | 334 | 104 | | .Any(HasRouteAttributes); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | private static bool HasRouteAttributes(MemberInfo member) |
| | | 108 | | { |
| | 1677 | 109 | | return member.GetCustomAttributes<CommandAttribute>(inherit: true).Any() || |
| | 1677 | 110 | | member.GetCustomAttributes<MessageAttribute>(inherit: true).Any() || |
| | 1677 | 111 | | member.GetCustomAttributes<TextAttribute>(inherit: true).Any() || |
| | 1677 | 112 | | member.GetCustomAttributes<TextTemplateAttribute>(inherit: true).Any() || |
| | 1677 | 113 | | member.GetCustomAttributes<CommandTemplateAttribute>(inherit: true).Any() || |
| | 1677 | 114 | | member.GetCustomAttributes<TextRegexAttribute>(inherit: true).Any() || |
| | 1677 | 115 | | member.GetCustomAttributes<CommandRegexAttribute>(inherit: true).Any() || |
| | 1677 | 116 | | member.GetCustomAttributes<CallbackAttribute>(inherit: true).Any() || |
| | 1677 | 117 | | HasGenericCallbackAttribute(member) || |
| | 1677 | 118 | | member.GetCustomAttributes<ChatMemberUpdatedAttribute>(inherit: true).Any() || |
| | 1677 | 119 | | member.GetCustomAttributes<MyChatMemberUpdatedAttribute>(inherit: true).Any(); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | private static List<TelegramHandlerDescriptor> BuildForMethod( |
| | | 123 | | Type handlerType, |
| | | 124 | | MethodInfo method, |
| | | 125 | | int firstRegistrationOrder, |
| | | 126 | | string? moduleName, |
| | | 127 | | bool includeClassRouteAttributes = false) |
| | | 128 | | { |
| | 358 | 129 | | var callback = GetRouteAttribute<CallbackAttribute>(handlerType, method, includeClassRouteAttributes); |
| | 358 | 130 | | var callbackPayloadType = GetGenericCallbackPayloadType(handlerType, method, includeClassRouteAttributes); |
| | 358 | 131 | | var hasCallbackRoute = callback is not null || callbackPayloadType is not null; |
| | 358 | 132 | | var chatMemberUpdated = GetRouteAttribute<ChatMemberUpdatedAttribute>(handlerType, method, includeClassRouteAttr |
| | 358 | 133 | | var myChatMemberUpdated = GetRouteAttribute<MyChatMemberUpdatedAttribute>(handlerType, method, includeClassRoute |
| | 358 | 134 | | var hasChatMemberRoute = chatMemberUpdated is not null || myChatMemberUpdated is not null; |
| | | 135 | | |
| | 358 | 136 | | var commandAttributes = GetRouteAttributes<CommandAttribute>(handlerType, method, includeClassRouteAttributes); |
| | 358 | 137 | | var message = GetRouteAttribute<MessageAttribute>(handlerType, method, includeClassRouteAttributes); |
| | 358 | 138 | | var textAttributes = GetRouteAttributes<TextAttribute>(handlerType, method, includeClassRouteAttributes); |
| | 358 | 139 | | var textTemplates = GetRouteAttributes<TextTemplateAttribute>(handlerType, method, includeClassRouteAttributes); |
| | 358 | 140 | | var commandTemplates = GetRouteAttributes<CommandTemplateAttribute>(handlerType, method, includeClassRouteAttrib |
| | 358 | 141 | | var textRegexes = GetRouteAttributes<TextRegexAttribute>(handlerType, method, includeClassRouteAttributes); |
| | 358 | 142 | | var commandRegexes = GetRouteAttributes<CommandRegexAttribute>(handlerType, method, includeClassRouteAttributes) |
| | 358 | 143 | | var filters = BuildFilters(handlerType, method); |
| | 355 | 144 | | var roleRequirements = BuildRoleRequirements(handlerType, method); |
| | 355 | 145 | | var scene = handlerType.GetCustomAttribute<SceneAttribute>(inherit: false); |
| | 355 | 146 | | var sceneStep = method.GetCustomAttribute<SceneStepAttribute>(inherit: true); |
| | | 147 | | |
| | 355 | 148 | | var hasMessageRoute = |
| | 355 | 149 | | commandAttributes.Length > 0 || |
| | 355 | 150 | | message is not null || |
| | 355 | 151 | | textAttributes.Length > 0 || |
| | 355 | 152 | | textTemplates.Length > 0 || |
| | 355 | 153 | | commandTemplates.Length > 0 || |
| | 355 | 154 | | textRegexes.Length > 0 || |
| | 355 | 155 | | commandRegexes.Length > 0; |
| | 355 | 156 | | var autoAnswerCallback = BuildAutoAnswerCallback(handlerType, method, hasCallbackRoute); |
| | | 157 | | |
| | 355 | 158 | | if (!hasCallbackRoute && !hasMessageRoute) |
| | | 159 | | { |
| | 44 | 160 | | if (!hasChatMemberRoute) |
| | | 161 | | { |
| | 26 | 162 | | if (sceneStep is not null) |
| | | 163 | | { |
| | 1 | 164 | | throw CreateSignatureException( |
| | 1 | 165 | | method, |
| | 1 | 166 | | "SceneStepAttribute requires an explicit Telegram route attribute."); |
| | | 167 | | } |
| | | 168 | | |
| | 25 | 169 | | return []; |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | 329 | 173 | | if (sceneStep is not null && scene is null) |
| | | 174 | | { |
| | 0 | 175 | | throw CreateSignatureException( |
| | 0 | 176 | | method, |
| | 0 | 177 | | "SceneStepAttribute can only be used on handler methods declared inside a SceneAttribute type."); |
| | | 178 | | } |
| | | 179 | | |
| | 329 | 180 | | if (sceneStep is not null && HasStateAttributes(handlerType, method)) |
| | | 181 | | { |
| | 1 | 182 | | throw CreateSignatureException( |
| | 1 | 183 | | method, |
| | 1 | 184 | | "SceneStepAttribute cannot be mixed with StateAttribute or StateAttribute<TStateGroup>."); |
| | | 185 | | } |
| | | 186 | | |
| | 328 | 187 | | if (callback is not null && callbackPayloadType is not null) |
| | | 188 | | { |
| | 0 | 189 | | throw CreateSignatureException( |
| | 0 | 190 | | method, |
| | 0 | 191 | | "Raw CallbackAttribute and CallbackAttribute<TPayload> cannot be used on the same handler method."); |
| | | 192 | | } |
| | | 193 | | |
| | 328 | 194 | | if (hasCallbackRoute && hasMessageRoute) |
| | | 195 | | { |
| | 0 | 196 | | throw CreateSignatureException( |
| | 0 | 197 | | method, |
| | 0 | 198 | | "Callback routes cannot be mixed with message, text, command, template, or regex routes."); |
| | | 199 | | } |
| | | 200 | | |
| | 328 | 201 | | if (hasChatMemberRoute && (hasCallbackRoute || hasMessageRoute)) |
| | | 202 | | { |
| | 0 | 203 | | throw CreateSignatureException( |
| | 0 | 204 | | method, |
| | 0 | 205 | | "Chat member update routes cannot be mixed with message, text, command, template, regex, or callback rou |
| | | 206 | | } |
| | | 207 | | |
| | 328 | 208 | | if (!hasChatMemberRoute && HasChatMemberTransitionAttributes(handlerType, method)) |
| | | 209 | | { |
| | 0 | 210 | | throw CreateSignatureException( |
| | 0 | 211 | | method, |
| | 0 | 212 | | "Chat member transition attributes can only be used on chat member update handlers."); |
| | | 213 | | } |
| | | 214 | | |
| | 328 | 215 | | if (hasCallbackRoute && |
| | 352 | 216 | | filters.Any(static filter => filter.Kind is not null && |
| | 352 | 217 | | !TelegramFilterCompatibility.Supports(filter.Kind.Value, TelegramHandlerKind.Ca |
| | | 218 | | { |
| | 4 | 219 | | var invalidFilter = filters.First(filter => filter.Kind is not null && |
| | 4 | 220 | | !TelegramFilterCompatibility.Supports(filter.Kind.Value, Telegra |
| | 2 | 221 | | throw CreateSignatureException( |
| | 2 | 222 | | method, |
| | 2 | 223 | | TelegramFilterCompatibility.GetInvalidPlacementMessage(invalidFilter.Kind!.Value, TelegramHandlerKind.Ca |
| | | 224 | | } |
| | | 225 | | |
| | 326 | 226 | | if (!hasCallbackRoute && |
| | 326 | 227 | | !hasChatMemberRoute && |
| | 378 | 228 | | filters.Any(static filter => filter.Kind is not null && |
| | 378 | 229 | | !TelegramFilterCompatibility.Supports(filter.Kind.Value, TelegramHandlerKind.Me |
| | | 230 | | { |
| | 0 | 231 | | var invalidFilter = filters.First(filter => filter.Kind is not null && |
| | 0 | 232 | | !TelegramFilterCompatibility.Supports(filter.Kind.Value, Telegra |
| | 0 | 233 | | throw CreateSignatureException( |
| | 0 | 234 | | method, |
| | 0 | 235 | | TelegramFilterCompatibility.GetInvalidPlacementMessage(invalidFilter.Kind!.Value, TelegramHandlerKind.Me |
| | | 236 | | } |
| | | 237 | | |
| | 326 | 238 | | if (hasChatMemberRoute && |
| | 328 | 239 | | filters.Any(static filter => filter.Kind is not null && |
| | 328 | 240 | | !TelegramFilterCompatibility.Supports(filter.Kind.Value, TelegramHandlerKind.Ch |
| | | 241 | | { |
| | 2 | 242 | | var invalidFilter = filters.First(filter => filter.Kind is not null && |
| | 2 | 243 | | !TelegramFilterCompatibility.Supports(filter.Kind.Value, Telegra |
| | 1 | 244 | | throw CreateSignatureException( |
| | 1 | 245 | | method, |
| | 1 | 246 | | TelegramFilterCompatibility.GetInvalidPlacementMessage(invalidFilter.Kind!.Value, TelegramHandlerKind.Ch |
| | | 247 | | } |
| | | 248 | | |
| | 325 | 249 | | ValidateReturnType(method); |
| | 324 | 250 | | ValidateCallbackPayloadType(method, callbackPayloadType); |
| | | 251 | | |
| | 324 | 252 | | var states = TelegramStateAttributeResolver.GetStates(handlerType, method).ToList(); |
| | | 253 | | |
| | 324 | 254 | | if (sceneStep is not null) |
| | | 255 | | { |
| | 11 | 256 | | states.Add(ResolveSceneStepState(handlerType, method, scene!, sceneStep)); |
| | | 257 | | } |
| | | 258 | | |
| | 322 | 259 | | List<RouteDefinition> routeDefinitions = hasChatMemberRoute |
| | 322 | 260 | | ? BuildChatMemberRouteDefinitions(handlerType, method, chatMemberUpdated, myChatMemberUpdated) |
| | 322 | 261 | | : hasCallbackRoute |
| | 322 | 262 | | ? [CreateCallbackRouteDefinition(callbackPayloadType)] |
| | 322 | 263 | | : BuildMessageRouteDefinitions(method, message, textAttributes, commandAttributes, textTemplates, comman |
| | | 264 | | |
| | 318 | 265 | | var routeValues = ResolveRouteValueTypes(method, routeDefinitions); |
| | 315 | 266 | | var kind = hasChatMemberRoute |
| | 315 | 267 | | ? TelegramHandlerKind.ChatMember |
| | 315 | 268 | | : hasCallbackRoute |
| | 315 | 269 | | ? TelegramHandlerKind.Callback |
| | 315 | 270 | | : TelegramHandlerKind.Message; |
| | | 271 | | |
| | 315 | 272 | | ValidateClassBasedHandlerCompatibility(handlerType, method, kind, callbackPayloadType); |
| | | 273 | | |
| | 315 | 274 | | ValidateCustomFilters(method, filters, kind); |
| | | 275 | | |
| | 314 | 276 | | var parameters = BuildParameterDescriptors(method, kind, callbackPayloadType, routeValues); |
| | 313 | 277 | | var descriptors = new List<TelegramHandlerDescriptor>(routeDefinitions.Count); |
| | | 278 | | |
| | 1266 | 279 | | for (var index = 0; index < routeDefinitions.Count; index++) |
| | | 280 | | { |
| | 320 | 281 | | var definition = routeDefinitions[index]; |
| | 320 | 282 | | var route = new TelegramRouteDescriptor( |
| | 320 | 283 | | definition.RouteKind, |
| | 320 | 284 | | definition.Pattern, |
| | 320 | 285 | | definition.CommandPolicy, |
| | 320 | 286 | | definition.TextFilters, |
| | 320 | 287 | | callbackPayloadType, |
| | 320 | 288 | | routeValues |
| | 29 | 289 | | .Select(static value => value.Value) |
| | 320 | 290 | | .ToArray(), |
| | 320 | 291 | | filters, |
| | 320 | 292 | | definition.ChatMemberTransitions, |
| | 320 | 293 | | roleRequirements); |
| | | 294 | | |
| | 320 | 295 | | descriptors.Add(new TelegramHandlerDescriptor( |
| | 320 | 296 | | handlerType, |
| | 320 | 297 | | method, |
| | 320 | 298 | | route, |
| | 320 | 299 | | firstRegistrationOrder + index, |
| | 320 | 300 | | moduleName, |
| | 320 | 301 | | states, |
| | 320 | 302 | | parameters, |
| | 320 | 303 | | sceneStep is null ? null : scene!.Prefix, |
| | 320 | 304 | | autoAnswerCallback)); |
| | | 305 | | } |
| | | 306 | | |
| | 313 | 307 | | return descriptors; |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | private static List<TelegramFilterDescriptor> BuildFilters( |
| | | 311 | | Type handlerType, |
| | | 312 | | MethodInfo method) |
| | | 313 | | { |
| | 358 | 314 | | var filters = new List<TelegramFilterDescriptor>(); |
| | | 315 | | |
| | 358 | 316 | | AppendFilters(filters, handlerType); |
| | 358 | 317 | | AppendFilters(filters, method); |
| | | 318 | | |
| | 355 | 319 | | return filters; |
| | | 320 | | } |
| | | 321 | | |
| | | 322 | | private static TelegramRoleRequirementDescriptor[] BuildRoleRequirements( |
| | | 323 | | Type handlerType, |
| | | 324 | | MethodInfo method) |
| | | 325 | | { |
| | 355 | 326 | | return handlerType |
| | 355 | 327 | | .GetCustomAttributes<RequireTelegramRoleAttribute>(inherit: true) |
| | 355 | 328 | | .Concat(method.GetCustomAttributes<RequireTelegramRoleAttribute>(inherit: true)) |
| | 12 | 329 | | .Select(static attribute => new TelegramRoleRequirementDescriptor(attribute.AllowedStatuses)) |
| | 355 | 330 | | .ToArray(); |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | private static TelegramAutoAnswerCallbackDescriptor? BuildAutoAnswerCallback( |
| | | 334 | | Type handlerType, |
| | | 335 | | MethodInfo method, |
| | | 336 | | bool hasCallbackRoute) |
| | | 337 | | { |
| | 355 | 338 | | var methodAttribute = method.GetCustomAttribute<AutoAnswerCallbackAttribute>(inherit: true); |
| | | 339 | | |
| | 355 | 340 | | if (methodAttribute is not null && !hasCallbackRoute) |
| | | 341 | | { |
| | 0 | 342 | | throw new InvalidOperationException( |
| | 0 | 343 | | $"{nameof(AutoAnswerCallbackAttribute)} can be used only on callback handlers."); |
| | | 344 | | } |
| | | 345 | | |
| | 355 | 346 | | if (!hasCallbackRoute) |
| | | 347 | | { |
| | 285 | 348 | | return null; |
| | | 349 | | } |
| | | 350 | | |
| | 70 | 351 | | var attribute = methodAttribute ?? |
| | 70 | 352 | | handlerType.GetCustomAttribute<AutoAnswerCallbackAttribute>(inherit: true); |
| | | 353 | | |
| | 70 | 354 | | return attribute is null |
| | 70 | 355 | | ? null |
| | 70 | 356 | | : new TelegramAutoAnswerCallbackDescriptor( |
| | 70 | 357 | | attribute.Enabled, |
| | 70 | 358 | | NormalizeAutoAnswerText(attribute.Text), |
| | 70 | 359 | | attribute.ShowAlert); |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | private static string? NormalizeAutoAnswerText(string? text) |
| | | 363 | | { |
| | 5 | 364 | | if (text is null) |
| | | 365 | | { |
| | 1 | 366 | | return null; |
| | | 367 | | } |
| | | 368 | | |
| | 4 | 369 | | if (string.IsNullOrWhiteSpace(text)) |
| | | 370 | | { |
| | 0 | 371 | | throw new InvalidOperationException("Auto callback answer text must not be empty."); |
| | | 372 | | } |
| | | 373 | | |
| | 4 | 374 | | return text; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | private static void AppendFilters( |
| | | 378 | | List<TelegramFilterDescriptor> filters, |
| | | 379 | | MemberInfo member) |
| | | 380 | | { |
| | 716 | 381 | | var chatType = member.GetCustomAttribute<ChatTypeAttribute>(inherit: true); |
| | | 382 | | |
| | 716 | 383 | | if (chatType is not null) |
| | | 384 | | { |
| | 7 | 385 | | filters.Add(new TelegramFilterDescriptor( |
| | 7 | 386 | | TelegramFilterKind.ChatType, |
| | 7 | 387 | | chatType.ChatTypes.Select(TelegramFilterFacts.MapChatType).ToArray(), |
| | 7 | 388 | | longValues: [])); |
| | | 389 | | } |
| | | 390 | | |
| | 715 | 391 | | var senderChatType = member.GetCustomAttribute<SenderChatTypeAttribute>(inherit: true); |
| | | 392 | | |
| | 715 | 393 | | if (senderChatType is not null) |
| | | 394 | | { |
| | 5 | 395 | | filters.Add(new TelegramFilterDescriptor( |
| | 5 | 396 | | TelegramFilterKind.SenderChatType, |
| | 5 | 397 | | senderChatType.ChatTypes.Select(TelegramFilterFacts.MapChatType).ToArray(), |
| | 5 | 398 | | longValues: [])); |
| | | 399 | | } |
| | | 400 | | |
| | 713 | 401 | | var chatId = member.GetCustomAttribute<ChatIdAttribute>(inherit: true); |
| | | 402 | | |
| | 713 | 403 | | if (chatId is not null) |
| | | 404 | | { |
| | 4 | 405 | | filters.Add(new TelegramFilterDescriptor( |
| | 4 | 406 | | TelegramFilterKind.ChatId, |
| | 4 | 407 | | stringValues: [], |
| | 4 | 408 | | chatId.ChatIds.ToArray())); |
| | | 409 | | } |
| | | 410 | | |
| | 713 | 411 | | var chatUsername = member.GetCustomAttribute<ChatUsernameAttribute>(inherit: true); |
| | | 412 | | |
| | 713 | 413 | | if (chatUsername is not null) |
| | | 414 | | { |
| | 2 | 415 | | filters.Add(new TelegramFilterDescriptor( |
| | 2 | 416 | | TelegramFilterKind.ChatUsername, |
| | 2 | 417 | | chatUsername.Usernames.ToArray(), |
| | 2 | 418 | | longValues: [])); |
| | | 419 | | } |
| | | 420 | | |
| | 713 | 421 | | var fromUser = member.GetCustomAttribute<FromUserAttribute>(inherit: true); |
| | | 422 | | |
| | 713 | 423 | | if (fromUser is not null) |
| | | 424 | | { |
| | 7 | 425 | | filters.Add(new TelegramFilterDescriptor( |
| | 7 | 426 | | TelegramFilterKind.FromUser, |
| | 7 | 427 | | stringValues: [], |
| | 7 | 428 | | fromUser.UserIds.ToArray())); |
| | | 429 | | } |
| | | 430 | | |
| | 713 | 431 | | AppendMarkerFilters(filters, member, TelegramMarkerFilterGroup.MessageContent); |
| | | 432 | | |
| | 713 | 433 | | var fromBot = member.GetCustomAttribute<FromBotAttribute>(inherit: true); |
| | | 434 | | |
| | 713 | 435 | | if (fromBot is not null) |
| | | 436 | | { |
| | 3 | 437 | | filters.Add(new TelegramFilterDescriptor( |
| | 3 | 438 | | TelegramFilterKind.FromBot, |
| | 3 | 439 | | stringValues: [], |
| | 3 | 440 | | fromBot.BotIds.ToArray())); |
| | | 441 | | } |
| | | 442 | | |
| | 713 | 443 | | AppendMarkerFilters(filters, member, TelegramMarkerFilterGroup.SenderUser); |
| | 713 | 444 | | AppendMarkerFilters(filters, member, TelegramMarkerFilterGroup.MessageSender); |
| | | 445 | | |
| | 713 | 446 | | var messageThreadId = member.GetCustomAttribute<MessageThreadIdAttribute>(inherit: true); |
| | | 447 | | |
| | 713 | 448 | | if (messageThreadId is not null) |
| | | 449 | | { |
| | 2 | 450 | | filters.Add(new TelegramFilterDescriptor( |
| | 2 | 451 | | TelegramFilterKind.MessageThreadId, |
| | 2 | 452 | | stringValues: [], |
| | 2 | 453 | | messageThreadId.MessageThreadIds.ToArray())); |
| | | 454 | | } |
| | | 455 | | |
| | 713 | 456 | | AppendMarkerFilters(filters, member, TelegramMarkerFilterGroup.SharedMetadata); |
| | | 457 | | |
| | 713 | 458 | | var callbackDataPrefix = member.GetCustomAttribute<CallbackDataPrefixAttribute>(inherit: true); |
| | | 459 | | |
| | 713 | 460 | | if (callbackDataPrefix is not null) |
| | | 461 | | { |
| | 8 | 462 | | filters.Add(new TelegramFilterDescriptor( |
| | 8 | 463 | | TelegramFilterKind.CallbackDataPrefix, |
| | 8 | 464 | | [callbackDataPrefix.Prefix], |
| | 8 | 465 | | longValues: [])); |
| | | 466 | | } |
| | | 467 | | |
| | 1458 | 468 | | foreach (var customFilter in GetCustomFilters(member)) |
| | | 469 | | { |
| | 16 | 470 | | filters.Add(customFilter); |
| | | 471 | | } |
| | 713 | 472 | | } |
| | | 473 | | |
| | | 474 | | private static void AppendMarkerFilters( |
| | | 475 | | List<TelegramFilterDescriptor> filters, |
| | | 476 | | MemberInfo member, |
| | | 477 | | TelegramMarkerFilterGroup group) |
| | | 478 | | { |
| | 34224 | 479 | | foreach (var spec in TelegramFilterFacts.GetMarkerFilters(group)) |
| | | 480 | | { |
| | 14260 | 481 | | if (TelegramFilterFacts.HasMarkerFilter(member, spec)) |
| | | 482 | | { |
| | 27 | 483 | | filters.Add(new TelegramFilterDescriptor(spec.Kind, stringValues: [], longValues: [])); |
| | | 484 | | } |
| | | 485 | | } |
| | 2852 | 486 | | } |
| | | 487 | | |
| | | 488 | | private static IEnumerable<TelegramFilterDescriptor> GetCustomFilters(MemberInfo member) |
| | | 489 | | { |
| | 3264 | 490 | | foreach (var attribute in member.GetCustomAttributes(inherit: true)) |
| | | 491 | | { |
| | 919 | 492 | | var attributeType = attribute.GetType(); |
| | | 493 | | |
| | 919 | 494 | | if (attributeType.IsGenericType && |
| | 919 | 495 | | attributeType.GetGenericTypeDefinition() == typeof(UseFilterAttribute<>)) |
| | | 496 | | { |
| | 15 | 497 | | yield return new TelegramFilterDescriptor(attributeType.GetGenericArguments()[0]); |
| | 15 | 498 | | continue; |
| | | 499 | | } |
| | | 500 | | |
| | 904 | 501 | | if (TryGetTelegramFilterAttributeFilterType(attributeType, out var filterType)) |
| | | 502 | | { |
| | 1 | 503 | | yield return new TelegramFilterDescriptor(filterType, (Attribute)attribute); |
| | | 504 | | } |
| | | 505 | | } |
| | 713 | 506 | | } |
| | | 507 | | |
| | | 508 | | private static bool TryGetTelegramFilterAttributeFilterType( |
| | | 509 | | Type attributeType, |
| | | 510 | | out Type filterType) |
| | | 511 | | { |
| | 8296 | 512 | | for (var current = attributeType; current is not null; current = current.BaseType) |
| | | 513 | | { |
| | 3245 | 514 | | if (current.IsGenericType && |
| | 3245 | 515 | | current.GetGenericTypeDefinition() == typeof(TelegramFilterAttribute<>)) |
| | | 516 | | { |
| | 1 | 517 | | filterType = current.GetGenericArguments()[0]; |
| | 1 | 518 | | return true; |
| | | 519 | | } |
| | | 520 | | } |
| | | 521 | | |
| | 903 | 522 | | filterType = null!; |
| | 903 | 523 | | return false; |
| | | 524 | | } |
| | | 525 | | |
| | | 526 | | private static void ValidateCustomFilters( |
| | | 527 | | MethodInfo method, |
| | | 528 | | IReadOnlyList<TelegramFilterDescriptor> filters, |
| | | 529 | | TelegramHandlerKind kind) |
| | | 530 | | { |
| | 736 | 531 | | foreach (var filter in filters.Where(static filter => filter.CustomFilterType is not null)) |
| | | 532 | | { |
| | 16 | 533 | | var filterType = filter.CustomFilterType!; |
| | | 534 | | |
| | 16 | 535 | | if (filterType.IsInterface || |
| | 16 | 536 | | filterType.IsAbstract || |
| | 16 | 537 | | filterType.ContainsGenericParameters) |
| | | 538 | | { |
| | 0 | 539 | | throw CreateSignatureException( |
| | 0 | 540 | | method, |
| | 0 | 541 | | $"Custom Telegram filter type '{filterType.FullName}' must be a concrete closed type."); |
| | | 542 | | } |
| | | 543 | | |
| | 16 | 544 | | var expectedContextType = kind switch |
| | 16 | 545 | | { |
| | 3 | 546 | | TelegramHandlerKind.Callback => typeof(CallbackQueryContext), |
| | 0 | 547 | | TelegramHandlerKind.ChatMember => typeof(ChatMemberUpdatedContext), |
| | 13 | 548 | | _ => typeof(MessageContext) |
| | 16 | 549 | | }; |
| | | 550 | | |
| | 16 | 551 | | if (filter.CustomFilterAttribute is null) |
| | | 552 | | { |
| | 15 | 553 | | var contextTypes = GetCustomFilterContextTypes(filterType, attributeType: null); |
| | | 554 | | |
| | 15 | 555 | | if (contextTypes.Length == 0) |
| | | 556 | | { |
| | 0 | 557 | | throw CreateSignatureException( |
| | 0 | 558 | | method, |
| | 0 | 559 | | $"Custom Telegram filter type '{filterType.FullName}' must implement ITelegramFilter<TContext>." |
| | | 560 | | } |
| | | 561 | | |
| | 15 | 562 | | if (!SupportsContextType(contextTypes, expectedContextType)) |
| | | 563 | | { |
| | 1 | 564 | | throw CreateSignatureException( |
| | 1 | 565 | | method, |
| | 1 | 566 | | $"Custom Telegram filter type '{filterType.FullName}' is not compatible with {expectedContextTyp |
| | | 567 | | } |
| | | 568 | | |
| | | 569 | | continue; |
| | | 570 | | } |
| | | 571 | | |
| | 1 | 572 | | var attributeType = filter.CustomFilterAttribute.GetType(); |
| | | 573 | | |
| | 1 | 574 | | if (attributeType.IsGenericType) |
| | | 575 | | { |
| | 0 | 576 | | throw CreateSignatureException( |
| | 0 | 577 | | method, |
| | 0 | 578 | | $"Custom Telegram filter attribute type '{attributeType.FullName}' must be non-generic."); |
| | | 579 | | } |
| | | 580 | | |
| | 1 | 581 | | var typedContextTypes = GetCustomFilterContextTypes(filterType, attributeType); |
| | | 582 | | |
| | 1 | 583 | | if (typedContextTypes.Length == 0) |
| | | 584 | | { |
| | 0 | 585 | | throw CreateSignatureException( |
| | 0 | 586 | | method, |
| | 0 | 587 | | "Parameterized custom Telegram filter attributes require a filter type that implements ITelegramFilt |
| | | 588 | | } |
| | | 589 | | |
| | 1 | 590 | | if (!SupportsContextType(typedContextTypes, expectedContextType)) |
| | | 591 | | { |
| | 0 | 592 | | throw CreateSignatureException( |
| | 0 | 593 | | method, |
| | 0 | 594 | | $"Custom Telegram filter type '{filterType.FullName}' is not compatible with {expectedContextType.Na |
| | | 595 | | } |
| | | 596 | | } |
| | 314 | 597 | | } |
| | | 598 | | |
| | | 599 | | private static Type[] GetCustomFilterContextTypes( |
| | | 600 | | Type filterType, |
| | | 601 | | Type? attributeType) |
| | | 602 | | { |
| | 16 | 603 | | var interfaces = filterType.GetInterfaces(); |
| | | 604 | | |
| | 16 | 605 | | if (attributeType is null) |
| | | 606 | | { |
| | 15 | 607 | | return interfaces |
| | 15 | 608 | | .Where(static type => type.IsGenericType && |
| | 15 | 609 | | type.GetGenericTypeDefinition() == typeof(ITelegramFilter<>)) |
| | 15 | 610 | | .Select(static type => type.GetGenericArguments()[0]) |
| | 15 | 611 | | .ToArray(); |
| | | 612 | | } |
| | | 613 | | |
| | 1 | 614 | | return interfaces |
| | 1 | 615 | | .Where(type => type.IsGenericType && |
| | 1 | 616 | | type.GetGenericTypeDefinition() == typeof(ITelegramFilter<,>) && |
| | 1 | 617 | | type.GetGenericArguments()[1] == attributeType) |
| | 1 | 618 | | .Select(static type => type.GetGenericArguments()[0]) |
| | 1 | 619 | | .ToArray(); |
| | | 620 | | } |
| | | 621 | | |
| | | 622 | | private static bool SupportsContextType( |
| | | 623 | | IReadOnlyCollection<Type> contextTypes, |
| | | 624 | | Type expectedContextType) |
| | | 625 | | { |
| | 16 | 626 | | return contextTypes.Contains(expectedContextType) || |
| | 16 | 627 | | contextTypes.Contains(typeof(TelegramUpdateContext)); |
| | | 628 | | } |
| | | 629 | | |
| | | 630 | | private static string? GetModuleName(Type handlerType) |
| | | 631 | | { |
| | 356 | 632 | | return handlerType.GetCustomAttribute<TelegramModuleAttribute>(inherit: false)?.Name; |
| | | 633 | | } |
| | | 634 | | |
| | | 635 | | private static bool IsClassBasedHandlerType(Type handlerType) |
| | | 636 | | { |
| | 671 | 637 | | return typeof(MessageHandler).IsAssignableFrom(handlerType) || |
| | 671 | 638 | | typeof(CallbackHandler).IsAssignableFrom(handlerType) || |
| | 671 | 639 | | typeof(ChatMemberUpdateHandler).IsAssignableFrom(handlerType); |
| | | 640 | | } |
| | | 641 | | |
| | | 642 | | private static void ValidateClassBasedHandlerCompatibility( |
| | | 643 | | Type handlerType, |
| | | 644 | | MethodInfo method, |
| | | 645 | | TelegramHandlerKind kind, |
| | | 646 | | Type? callbackPayloadType) |
| | | 647 | | { |
| | 315 | 648 | | if (!IsClassBasedHandlerType(handlerType)) |
| | | 649 | | { |
| | 307 | 650 | | return; |
| | | 651 | | } |
| | | 652 | | |
| | 8 | 653 | | if (typeof(MessageHandler).IsAssignableFrom(handlerType)) |
| | | 654 | | { |
| | 6 | 655 | | if (kind is TelegramHandlerKind.Message or TelegramHandlerKind.Command) |
| | | 656 | | { |
| | 6 | 657 | | return; |
| | | 658 | | } |
| | | 659 | | |
| | 0 | 660 | | throw CreateSignatureException( |
| | 0 | 661 | | method, |
| | 0 | 662 | | $"Class-based handler type '{handlerType.FullName}' derives from {nameof(MessageHandler)} and can only u |
| | | 663 | | } |
| | | 664 | | |
| | 2 | 665 | | if (typeof(ChatMemberUpdateHandler).IsAssignableFrom(handlerType)) |
| | | 666 | | { |
| | 1 | 667 | | if (kind == TelegramHandlerKind.ChatMember) |
| | | 668 | | { |
| | 1 | 669 | | return; |
| | | 670 | | } |
| | | 671 | | |
| | 0 | 672 | | throw CreateSignatureException( |
| | 0 | 673 | | method, |
| | 0 | 674 | | $"Class-based handler type '{handlerType.FullName}' derives from {nameof(ChatMemberUpdateHandler)} and c |
| | | 675 | | } |
| | | 676 | | |
| | 1 | 677 | | if (typeof(CallbackHandler).IsAssignableFrom(handlerType)) |
| | | 678 | | { |
| | 1 | 679 | | if (kind != TelegramHandlerKind.Callback) |
| | | 680 | | { |
| | 0 | 681 | | throw CreateSignatureException( |
| | 0 | 682 | | method, |
| | 0 | 683 | | $"Class-based handler type '{handlerType.FullName}' derives from {nameof(CallbackHandler)} and can o |
| | | 684 | | } |
| | | 685 | | |
| | 1 | 686 | | var basePayloadType = GetCallbackHandlerPayloadType(handlerType); |
| | | 687 | | |
| | 1 | 688 | | if (basePayloadType is null && callbackPayloadType is null) |
| | | 689 | | { |
| | 0 | 690 | | return; |
| | | 691 | | } |
| | | 692 | | |
| | 1 | 693 | | if (basePayloadType is not null && |
| | 1 | 694 | | callbackPayloadType is not null && |
| | 1 | 695 | | basePayloadType == callbackPayloadType) |
| | | 696 | | { |
| | 1 | 697 | | return; |
| | | 698 | | } |
| | | 699 | | |
| | 0 | 700 | | throw CreateSignatureException( |
| | 0 | 701 | | method, |
| | 0 | 702 | | $"Class-based typed callback handler '{handlerType.FullName}' must use matching CallbackAttribute<TPaylo |
| | | 703 | | } |
| | 0 | 704 | | } |
| | | 705 | | |
| | | 706 | | private static Type? GetCallbackHandlerPayloadType(Type handlerType) |
| | | 707 | | { |
| | 4 | 708 | | for (var current = handlerType; current is not null; current = current.BaseType) |
| | | 709 | | { |
| | 2 | 710 | | if (current.IsGenericType && |
| | 2 | 711 | | current.GetGenericTypeDefinition() == typeof(CallbackHandler<>)) |
| | | 712 | | { |
| | 1 | 713 | | return current.GetGenericArguments()[0]; |
| | | 714 | | } |
| | | 715 | | } |
| | | 716 | | |
| | 0 | 717 | | return null; |
| | | 718 | | } |
| | | 719 | | |
| | | 720 | | private static TAttribute? GetRouteAttribute<TAttribute>( |
| | | 721 | | Type handlerType, |
| | | 722 | | MethodInfo method, |
| | | 723 | | bool includeClassRouteAttributes) |
| | | 724 | | where TAttribute : Attribute |
| | | 725 | | { |
| | 1432 | 726 | | var attributes = GetRouteAttributes<TAttribute>(handlerType, method, includeClassRouteAttributes); |
| | 1432 | 727 | | return attributes.Length > 0 ? attributes[0] : null; |
| | | 728 | | } |
| | | 729 | | |
| | | 730 | | private static TAttribute[] GetRouteAttributes<TAttribute>( |
| | | 731 | | Type handlerType, |
| | | 732 | | MethodInfo method, |
| | | 733 | | bool includeClassRouteAttributes) |
| | | 734 | | where TAttribute : Attribute |
| | | 735 | | { |
| | 3580 | 736 | | var methodAttributes = method.GetCustomAttributes<TAttribute>(inherit: true); |
| | | 737 | | |
| | 3580 | 738 | | return includeClassRouteAttributes |
| | 3580 | 739 | | ? handlerType.GetCustomAttributes<TAttribute>(inherit: true) |
| | 3580 | 740 | | .Concat(methodAttributes) |
| | 3580 | 741 | | .ToArray() |
| | 3580 | 742 | | : methodAttributes.ToArray(); |
| | | 743 | | } |
| | | 744 | | |
| | | 745 | | private static List<RouteDefinition> BuildMessageRouteDefinitions( |
| | | 746 | | MethodInfo method, |
| | | 747 | | MessageAttribute? message, |
| | | 748 | | IReadOnlyList<TextAttribute> textAttributes, |
| | | 749 | | IReadOnlyList<CommandAttribute> commandAttributes, |
| | | 750 | | IReadOnlyList<TextTemplateAttribute> textTemplates, |
| | | 751 | | IReadOnlyList<CommandTemplateAttribute> commandTemplates, |
| | | 752 | | IReadOnlyList<TextRegexAttribute> textRegexes, |
| | | 753 | | IReadOnlyList<CommandRegexAttribute> commandRegexes) |
| | | 754 | | { |
| | 237 | 755 | | var definitions = new List<RouteDefinition>(); |
| | | 756 | | |
| | 599 | 757 | | foreach (var command in commandAttributes) |
| | | 758 | | { |
| | 63 | 759 | | var policy = CreateCommandPolicy(command.Prefixes, command.AllowSpaceAfterPrefix, command.IgnoreCase, comman |
| | 62 | 760 | | definitions.Add(new RouteDefinition( |
| | 62 | 761 | | TelegramRouteKind.CommandExact, |
| | 62 | 762 | | NormalizeCommand(method, command.Command), |
| | 62 | 763 | | policy, |
| | 62 | 764 | | TextFilters: [], |
| | 62 | 765 | | RouteValues: CreateEmptyRouteValues(), |
| | 62 | 766 | | ChatMemberTransitions: [])); |
| | | 767 | | } |
| | | 768 | | |
| | 499 | 769 | | foreach (var template in commandTemplates) |
| | | 770 | | { |
| | 14 | 771 | | var policy = CreateCommandPolicy(template.Prefixes, template.AllowSpaceAfterPrefix, template.IgnoreCase, tem |
| | 14 | 772 | | definitions.Add(new RouteDefinition( |
| | 14 | 773 | | TelegramRouteKind.CommandTemplate, |
| | 14 | 774 | | NormalizeCommandPattern(method, template.Template, policy), |
| | 14 | 775 | | policy, |
| | 14 | 776 | | TextFilters: [], |
| | 14 | 777 | | TelegramTemplateRouteParser.GetRouteValues(template.Template) |
| | 14 | 778 | | .ToDictionary( |
| | 11 | 779 | | static value => value.Name, |
| | 11 | 780 | | static value => new RouteValueDefinition(value.ValueType, value.IsOptional), |
| | 14 | 781 | | StringComparer.Ordinal), |
| | 14 | 782 | | ChatMemberTransitions: [])); |
| | | 783 | | } |
| | | 784 | | |
| | 476 | 785 | | foreach (var regex in commandRegexes) |
| | | 786 | | { |
| | 3 | 787 | | var policy = CreateCommandPolicy(regex.Prefixes, regex.AllowSpaceAfterPrefix, regex.IgnoreCase, regex.Prefix |
| | 3 | 788 | | var routeValues = GetRegexRouteValueNames(method, regex.Pattern, regex.IgnoreCase) |
| | 3 | 789 | | .ToDictionary( |
| | 2 | 790 | | static name => name, |
| | 2 | 791 | | static _ => new RouteValueDefinition(ValueType: null, IsOptional: false), |
| | 3 | 792 | | StringComparer.Ordinal); |
| | 3 | 793 | | definitions.Add(new RouteDefinition( |
| | 3 | 794 | | TelegramRouteKind.CommandRegex, |
| | 3 | 795 | | regex.Pattern, |
| | 3 | 796 | | policy, |
| | 3 | 797 | | TextFilters: [], |
| | 3 | 798 | | routeValues, |
| | 3 | 799 | | ChatMemberTransitions: [])); |
| | | 800 | | } |
| | | 801 | | |
| | 235 | 802 | | if (message is not null) |
| | | 803 | | { |
| | 135 | 804 | | definitions.Add(new RouteDefinition( |
| | 135 | 805 | | TelegramRouteKind.MessageAny, |
| | 135 | 806 | | Pattern: null, |
| | 135 | 807 | | CommandPolicy: null, |
| | 135 | 808 | | textAttributes |
| | 3 | 809 | | .Select(static attribute => new TelegramTextFilter(attribute.Value, attribute.Mode, attribute.Ignore |
| | 135 | 810 | | .ToArray(), |
| | 135 | 811 | | RouteValues: CreateEmptyRouteValues(), |
| | 135 | 812 | | ChatMemberTransitions: [])); |
| | | 813 | | } |
| | | 814 | | else |
| | | 815 | | { |
| | 216 | 816 | | foreach (var text in textAttributes) |
| | | 817 | | { |
| | 8 | 818 | | definitions.Add(new RouteDefinition( |
| | 8 | 819 | | TelegramRouteKind.TextExact, |
| | 8 | 820 | | text.Value, |
| | 8 | 821 | | CommandPolicy: null, |
| | 8 | 822 | | [new TelegramTextFilter(text.Value, text.Mode, text.IgnoreCase)], |
| | 8 | 823 | | RouteValues: CreateEmptyRouteValues(), |
| | 8 | 824 | | ChatMemberTransitions: [])); |
| | | 825 | | } |
| | | 826 | | } |
| | | 827 | | |
| | 502 | 828 | | foreach (var template in textTemplates) |
| | | 829 | | { |
| | 17 | 830 | | definitions.Add(new RouteDefinition( |
| | 17 | 831 | | TelegramRouteKind.TextTemplate, |
| | 17 | 832 | | template.Template, |
| | 17 | 833 | | CommandPolicy: new TelegramCommandPolicy(["/"], allowSpaceAfterPrefix: false, template.IgnoreCase), |
| | 17 | 834 | | TextFilters: [], |
| | 17 | 835 | | TelegramTemplateRouteParser.GetRouteValues(template.Template) |
| | 17 | 836 | | .ToDictionary( |
| | 15 | 837 | | static value => value.Name, |
| | 15 | 838 | | static value => new RouteValueDefinition(value.ValueType, value.IsOptional), |
| | 17 | 839 | | StringComparer.Ordinal), |
| | 17 | 840 | | ChatMemberTransitions: [])); |
| | | 841 | | } |
| | | 842 | | |
| | 474 | 843 | | foreach (var regex in textRegexes) |
| | | 844 | | { |
| | 4 | 845 | | var routeValues = GetRegexRouteValueNames(method, regex.Pattern, regex.IgnoreCase) |
| | 4 | 846 | | .ToDictionary( |
| | 4 | 847 | | static name => name, |
| | 4 | 848 | | static _ => new RouteValueDefinition(ValueType: null, IsOptional: false), |
| | 4 | 849 | | StringComparer.Ordinal); |
| | 4 | 850 | | definitions.Add(new RouteDefinition( |
| | 4 | 851 | | TelegramRouteKind.TextRegex, |
| | 4 | 852 | | regex.Pattern, |
| | 4 | 853 | | CommandPolicy: new TelegramCommandPolicy(["/"], allowSpaceAfterPrefix: false, regex.IgnoreCase), |
| | 4 | 854 | | TextFilters: [], |
| | 4 | 855 | | routeValues, |
| | 4 | 856 | | ChatMemberTransitions: [])); |
| | | 857 | | } |
| | | 858 | | |
| | 233 | 859 | | return definitions; |
| | | 860 | | } |
| | | 861 | | |
| | | 862 | | private static RouteDefinition CreateCallbackRouteDefinition(Type? callbackPayloadType) |
| | | 863 | | { |
| | 68 | 864 | | return new RouteDefinition( |
| | 68 | 865 | | TelegramRouteKind.Callback, |
| | 68 | 866 | | Pattern: null, |
| | 68 | 867 | | CommandPolicy: null, |
| | 68 | 868 | | TextFilters: [], |
| | 68 | 869 | | RouteValues: CreateEmptyRouteValues(), |
| | 68 | 870 | | ChatMemberTransitions: []); |
| | | 871 | | } |
| | | 872 | | |
| | | 873 | | private static List<RouteDefinition> BuildChatMemberRouteDefinitions( |
| | | 874 | | Type handlerType, |
| | | 875 | | MethodInfo method, |
| | | 876 | | ChatMemberUpdatedAttribute? chatMemberUpdated, |
| | | 877 | | MyChatMemberUpdatedAttribute? myChatMemberUpdated) |
| | | 878 | | { |
| | 17 | 879 | | var transitions = BuildChatMemberTransitions(handlerType, method); |
| | 17 | 880 | | var definitions = new List<RouteDefinition>(capacity: 2); |
| | | 881 | | |
| | 17 | 882 | | if (chatMemberUpdated is not null) |
| | | 883 | | { |
| | 15 | 884 | | definitions.Add(new RouteDefinition( |
| | 15 | 885 | | TelegramRouteKind.ChatMemberUpdated, |
| | 15 | 886 | | Pattern: null, |
| | 15 | 887 | | CommandPolicy: null, |
| | 15 | 888 | | TextFilters: [], |
| | 15 | 889 | | RouteValues: CreateEmptyRouteValues(), |
| | 15 | 890 | | transitions)); |
| | | 891 | | } |
| | | 892 | | |
| | 17 | 893 | | if (myChatMemberUpdated is not null) |
| | | 894 | | { |
| | 2 | 895 | | definitions.Add(new RouteDefinition( |
| | 2 | 896 | | TelegramRouteKind.MyChatMemberUpdated, |
| | 2 | 897 | | Pattern: null, |
| | 2 | 898 | | CommandPolicy: null, |
| | 2 | 899 | | TextFilters: [], |
| | 2 | 900 | | RouteValues: CreateEmptyRouteValues(), |
| | 2 | 901 | | transitions)); |
| | | 902 | | } |
| | | 903 | | |
| | 17 | 904 | | return definitions; |
| | | 905 | | } |
| | | 906 | | |
| | | 907 | | private static TelegramChatMemberTransitionDescriptor[] BuildChatMemberTransitions( |
| | | 908 | | Type handlerType, |
| | | 909 | | MethodInfo method) |
| | | 910 | | { |
| | 17 | 911 | | return GetChatMemberTransitionAttributes(handlerType, method) |
| | 7 | 912 | | .Select(static attribute => TelegramMemberTransitionFacts.Map(attribute.Transition)) |
| | 17 | 913 | | .Concat(GetChatMemberChangedAttributes(handlerType, method) |
| | 1 | 914 | | .Select(static attribute => new TelegramChatMemberTransitionDescriptor( |
| | 1 | 915 | | attribute.OldStatus, |
| | 1 | 916 | | attribute.NewStatus))) |
| | 17 | 917 | | .ToArray(); |
| | | 918 | | } |
| | | 919 | | |
| | | 920 | | private static IEnumerable<ChatMemberTransitionAttribute> GetChatMemberTransitionAttributes( |
| | | 921 | | Type handlerType, |
| | | 922 | | MethodInfo method) |
| | | 923 | | { |
| | 327 | 924 | | return handlerType |
| | 327 | 925 | | .GetCustomAttributes<ChatMemberTransitionAttribute>(inherit: true) |
| | 327 | 926 | | .Concat(method.GetCustomAttributes<ChatMemberTransitionAttribute>(inherit: true)); |
| | | 927 | | } |
| | | 928 | | |
| | | 929 | | private static IEnumerable<ChatMemberChangedAttribute> GetChatMemberChangedAttributes( |
| | | 930 | | Type handlerType, |
| | | 931 | | MethodInfo method) |
| | | 932 | | { |
| | 327 | 933 | | return handlerType |
| | 327 | 934 | | .GetCustomAttributes<ChatMemberChangedAttribute>(inherit: true) |
| | 327 | 935 | | .Concat(method.GetCustomAttributes<ChatMemberChangedAttribute>(inherit: true)); |
| | | 936 | | } |
| | | 937 | | |
| | | 938 | | private static bool HasChatMemberTransitionAttributes( |
| | | 939 | | Type handlerType, |
| | | 940 | | MethodInfo method) |
| | | 941 | | { |
| | 310 | 942 | | return GetChatMemberTransitionAttributes(handlerType, method).Any() || |
| | 310 | 943 | | GetChatMemberChangedAttributes(handlerType, method).Any(); |
| | | 944 | | } |
| | | 945 | | |
| | | 946 | | private static bool HasStateAttributes( |
| | | 947 | | Type handlerType, |
| | | 948 | | MethodInfo method) |
| | | 949 | | { |
| | 12 | 950 | | return handlerType.GetCustomAttributes<StateAttribute>(inherit: true).Any() || |
| | 12 | 951 | | method.GetCustomAttributes<StateAttribute>(inherit: true).Any() || |
| | 12 | 952 | | HasGenericStateAttributes(handlerType) || |
| | 12 | 953 | | HasGenericStateAttributes(method); |
| | | 954 | | } |
| | | 955 | | |
| | | 956 | | private static bool HasGenericStateAttributes(MemberInfo member) |
| | | 957 | | { |
| | 22 | 958 | | return member |
| | 22 | 959 | | .GetCustomAttributes(inherit: true) |
| | 56 | 960 | | .Select(static attribute => attribute.GetType()) |
| | 22 | 961 | | .Any(static type => |
| | 78 | 962 | | type.IsGenericType && |
| | 78 | 963 | | type.GetGenericTypeDefinition() == typeof(StateAttribute<>)); |
| | | 964 | | } |
| | | 965 | | |
| | | 966 | | private static string ResolveSceneStepState( |
| | | 967 | | Type handlerType, |
| | | 968 | | MethodInfo method, |
| | | 969 | | SceneAttribute scene, |
| | | 970 | | SceneStepAttribute step) |
| | | 971 | | { |
| | 11 | 972 | | var property = handlerType.GetProperty( |
| | 11 | 973 | | step.StateName, |
| | 11 | 974 | | BindingFlags.Public | BindingFlags.Static); |
| | | 975 | | |
| | 11 | 976 | | if (property is null || property.PropertyType != typeof(State)) |
| | | 977 | | { |
| | 1 | 978 | | throw CreateSignatureException( |
| | 1 | 979 | | method, |
| | 1 | 980 | | $"SceneStepAttribute references missing scene state '{handlerType.FullName}.{step.StateName}'."); |
| | | 981 | | } |
| | | 982 | | |
| | 10 | 983 | | if (property.GetValue(null) is not State state || string.IsNullOrWhiteSpace(state.Id)) |
| | | 984 | | { |
| | 0 | 985 | | throw CreateSignatureException( |
| | 0 | 986 | | method, |
| | 0 | 987 | | $"Scene state '{handlerType.FullName}.{step.StateName}' must return a non-empty State value."); |
| | | 988 | | } |
| | | 989 | | |
| | 10 | 990 | | var expectedStateId = $"{scene.Prefix}:{GetSceneStateSegment(property)}"; |
| | | 991 | | |
| | 10 | 992 | | if (!string.Equals(state.Id, expectedStateId, StringComparison.Ordinal)) |
| | | 993 | | { |
| | 1 | 994 | | throw CreateSignatureException( |
| | 1 | 995 | | method, |
| | 1 | 996 | | $"Scene state '{handlerType.FullName}.{step.StateName}' must return canonical state id '{expectedStateId |
| | | 997 | | } |
| | | 998 | | |
| | 9 | 999 | | return state.Id; |
| | | 1000 | | } |
| | | 1001 | | |
| | | 1002 | | private static string GetSceneStateSegment(PropertyInfo property) |
| | | 1003 | | { |
| | 10 | 1004 | | var attribute = property.GetCustomAttribute<StateValueAttribute>(inherit: false); |
| | | 1005 | | |
| | 10 | 1006 | | return attribute is not null && !string.IsNullOrWhiteSpace(attribute.Value) |
| | 10 | 1007 | | ? attribute.Value |
| | 10 | 1008 | | : ToCamelCase(property.Name); |
| | | 1009 | | } |
| | | 1010 | | |
| | | 1011 | | private static string ToCamelCase(string value) |
| | | 1012 | | { |
| | 9 | 1013 | | return value.Length == 0 |
| | 9 | 1014 | | ? value |
| | 9 | 1015 | | : char.ToLowerInvariant(value[0]) + value[1..]; |
| | | 1016 | | } |
| | | 1017 | | |
| | | 1018 | | private static Dictionary<string, TelegramRouteValueDescriptor> ResolveRouteValueTypes( |
| | | 1019 | | MethodInfo method, |
| | | 1020 | | IReadOnlyList<RouteDefinition> routeDefinitions) |
| | | 1021 | | { |
| | 318 | 1022 | | var routeValueDefinitions = routeDefinitions |
| | 325 | 1023 | | .Select(static definition => definition.RouteValues) |
| | 325 | 1024 | | .Where(static values => values.Count > 0) |
| | 318 | 1025 | | .ToArray(); |
| | | 1026 | | |
| | 318 | 1027 | | if (routeValueDefinitions.Length == 0) |
| | | 1028 | | { |
| | 286 | 1029 | | return new Dictionary<string, TelegramRouteValueDescriptor>(StringComparer.Ordinal); |
| | | 1030 | | } |
| | | 1031 | | |
| | 32 | 1032 | | var firstNames = routeValueDefinitions[0].Keys.Order(StringComparer.Ordinal).ToArray(); |
| | | 1033 | | |
| | 64 | 1034 | | foreach (var values in routeValueDefinitions.Skip(1)) |
| | | 1035 | | { |
| | 0 | 1036 | | var names = values.Keys.Order(StringComparer.Ordinal).ToArray(); |
| | | 1037 | | |
| | 0 | 1038 | | if (!firstNames.SequenceEqual(names, StringComparer.Ordinal)) |
| | | 1039 | | { |
| | 0 | 1040 | | throw CreateSignatureException( |
| | 0 | 1041 | | method, |
| | 0 | 1042 | | "Multiple route attributes on one handler method must expose the same route value names."); |
| | | 1043 | | } |
| | | 1044 | | } |
| | | 1045 | | |
| | 64 | 1046 | | if (routeDefinitions.Any(static definition => definition.RouteValues.Count == 0)) |
| | | 1047 | | { |
| | 0 | 1048 | | throw CreateSignatureException( |
| | 0 | 1049 | | method, |
| | 0 | 1050 | | "A handler method with route value parameters cannot also have routes that do not provide route values." |
| | | 1051 | | } |
| | | 1052 | | |
| | 32 | 1053 | | var parameters = method.GetParameters(); |
| | 32 | 1054 | | var routeValues = new Dictionary<string, TelegramRouteValueDescriptor>(StringComparer.Ordinal); |
| | | 1055 | | |
| | 125 | 1056 | | foreach (var name in firstNames) |
| | | 1057 | | { |
| | 95 | 1058 | | var parameter = parameters.FirstOrDefault(parameter => string.Equals(parameter.Name, name, StringComparison. |
| | | 1059 | | |
| | 32 | 1060 | | if (parameter is null) |
| | | 1061 | | { |
| | 1 | 1062 | | throw CreateSignatureException( |
| | 1 | 1063 | | method, |
| | 1 | 1064 | | $"Route value '{name}' must have a matching handler parameter with the same name."); |
| | | 1065 | | } |
| | | 1066 | | |
| | 31 | 1067 | | if (!TryGetRouteValueParameterShape(parameter, out var parameterValueType, out var parameterIsNullable)) |
| | | 1068 | | { |
| | 0 | 1069 | | throw CreateSignatureException( |
| | 0 | 1070 | | method, |
| | 0 | 1071 | | $"Route value parameter '{name}' must be string, string?, int, int?, long, or long?."); |
| | | 1072 | | } |
| | | 1073 | | |
| | 122 | 1074 | | foreach (var values in routeValueDefinitions) |
| | | 1075 | | { |
| | 31 | 1076 | | var definition = values[name]; |
| | | 1077 | | |
| | 31 | 1078 | | if (definition.IsOptional != parameterIsNullable) |
| | | 1079 | | { |
| | 2 | 1080 | | var expectedNullability = definition.IsOptional ? "nullable" : "non-nullable"; |
| | 2 | 1081 | | throw CreateSignatureException( |
| | 2 | 1082 | | method, |
| | 2 | 1083 | | $"Route value parameter '{name}' must be {expectedNullability} for the selected route template." |
| | | 1084 | | } |
| | | 1085 | | |
| | 29 | 1086 | | if (definition.ValueType is { } expectedType && expectedType != parameterValueType) |
| | | 1087 | | { |
| | 0 | 1088 | | throw CreateSignatureException( |
| | 0 | 1089 | | method, |
| | 0 | 1090 | | $"Route value parameter '{name}' must be {expectedType.Name} for the selected route template."); |
| | | 1091 | | } |
| | | 1092 | | } |
| | | 1093 | | |
| | 29 | 1094 | | routeValues.Add(name, new TelegramRouteValueDescriptor(name, parameterValueType, parameterIsNullable)); |
| | | 1095 | | } |
| | | 1096 | | |
| | 29 | 1097 | | return routeValues; |
| | | 1098 | | } |
| | | 1099 | | |
| | | 1100 | | private static List<TelegramHandlerParameterDescriptor> BuildParameterDescriptors( |
| | | 1101 | | MethodInfo method, |
| | | 1102 | | TelegramHandlerKind kind, |
| | | 1103 | | Type? callbackPayloadType, |
| | | 1104 | | Dictionary<string, TelegramRouteValueDescriptor> routeValues) |
| | | 1105 | | { |
| | 314 | 1106 | | var expectedContextType = kind switch |
| | 314 | 1107 | | { |
| | 67 | 1108 | | TelegramHandlerKind.Callback => typeof(CallbackQueryContext), |
| | 17 | 1109 | | TelegramHandlerKind.ChatMember => typeof(ChatMemberUpdatedContext), |
| | 230 | 1110 | | _ => typeof(MessageContext) |
| | 314 | 1111 | | }; |
| | | 1112 | | |
| | 314 | 1113 | | var parameters = method.GetParameters(); |
| | 314 | 1114 | | var contextParameters = parameters |
| | 649 | 1115 | | .Where(parameter => parameter.ParameterType == typeof(MessageContext) || |
| | 649 | 1116 | | parameter.ParameterType == typeof(CallbackQueryContext) || |
| | 649 | 1117 | | parameter.ParameterType == typeof(ChatMemberUpdatedContext) || |
| | 649 | 1118 | | parameter.ParameterType == typeof(TelegramUpdateContext)) |
| | 314 | 1119 | | .ToArray(); |
| | | 1120 | | |
| | 314 | 1121 | | if (contextParameters.Length != 1 || contextParameters[0].ParameterType != expectedContextType) |
| | | 1122 | | { |
| | 0 | 1123 | | throw CreateSignatureException( |
| | 0 | 1124 | | method, |
| | 0 | 1125 | | $"A {FormatHandlerKind(kind)} handler must declare exactly one {expectedContextType.Name} parameter."); |
| | | 1126 | | } |
| | | 1127 | | |
| | 963 | 1128 | | if (parameters.Count(static parameter => parameter.ParameterType == typeof(CancellationToken)) > 1) |
| | | 1129 | | { |
| | 0 | 1130 | | throw CreateSignatureException(method, "A handler method can declare at most one CancellationToken parameter |
| | | 1131 | | } |
| | | 1132 | | |
| | 314 | 1133 | | var descriptors = new List<TelegramHandlerParameterDescriptor>(parameters.Length); |
| | | 1134 | | |
| | 1925 | 1135 | | foreach (var parameter in parameters) |
| | | 1136 | | { |
| | 649 | 1137 | | if (parameter.ParameterType == expectedContextType) |
| | | 1138 | | { |
| | 314 | 1139 | | descriptors.Add(new TelegramHandlerParameterDescriptor(parameter, TelegramHandlerParameterKind.Context)) |
| | 314 | 1140 | | continue; |
| | | 1141 | | } |
| | | 1142 | | |
| | 335 | 1143 | | if (parameter.ParameterType == typeof(CancellationToken)) |
| | | 1144 | | { |
| | 17 | 1145 | | descriptors.Add(new TelegramHandlerParameterDescriptor(parameter, TelegramHandlerParameterKind.Cancellat |
| | 17 | 1146 | | continue; |
| | | 1147 | | } |
| | | 1148 | | |
| | 318 | 1149 | | if (!string.IsNullOrWhiteSpace(parameter.Name) && routeValues.ContainsKey(parameter.Name)) |
| | | 1150 | | { |
| | 29 | 1151 | | descriptors.Add(new TelegramHandlerParameterDescriptor(parameter, TelegramHandlerParameterKind.RouteValu |
| | 29 | 1152 | | continue; |
| | | 1153 | | } |
| | | 1154 | | |
| | 289 | 1155 | | if (kind == TelegramHandlerKind.Callback && callbackPayloadType == parameter.ParameterType) |
| | | 1156 | | { |
| | 18 | 1157 | | descriptors.Add(new TelegramHandlerParameterDescriptor(parameter, TelegramHandlerParameterKind.CallbackP |
| | 18 | 1158 | | continue; |
| | | 1159 | | } |
| | | 1160 | | |
| | 271 | 1161 | | if (kind == TelegramHandlerKind.Callback && |
| | 271 | 1162 | | callbackPayloadType is null && |
| | 271 | 1163 | | IsCallbackDataPayloadType(parameter.ParameterType)) |
| | | 1164 | | { |
| | 1 | 1165 | | throw CreateSignatureException( |
| | 1 | 1166 | | method, |
| | 1 | 1167 | | $"Raw {nameof(CallbackAttribute)} handlers do not bind typed callback payloads. Use {nameof(Callback |
| | | 1168 | | } |
| | | 1169 | | |
| | 270 | 1170 | | descriptors.Add(new TelegramHandlerParameterDescriptor(parameter, TelegramHandlerParameterKind.Service)); |
| | | 1171 | | } |
| | | 1172 | | |
| | 313 | 1173 | | if (kind == TelegramHandlerKind.Callback && callbackPayloadType is not null) |
| | | 1174 | | { |
| | 71 | 1175 | | var payloadParameterCount = parameters.Count(parameter => parameter.ParameterType == callbackPayloadType); |
| | | 1176 | | |
| | 18 | 1177 | | if (payloadParameterCount != 1) |
| | | 1178 | | { |
| | 0 | 1179 | | throw CreateSignatureException( |
| | 0 | 1180 | | method, |
| | 0 | 1181 | | $"{nameof(CallbackAttribute)}<{callbackPayloadType.Name}> handlers must declare exactly one {callbac |
| | | 1182 | | } |
| | | 1183 | | } |
| | | 1184 | | |
| | 313 | 1185 | | return descriptors; |
| | | 1186 | | } |
| | | 1187 | | |
| | | 1188 | | private static TelegramCommandPolicy CreateCommandPolicy( |
| | | 1189 | | IReadOnlyList<string> prefixes, |
| | | 1190 | | bool allowSpaceAfterPrefix, |
| | | 1191 | | bool ignoreCase, |
| | | 1192 | | CommandPrefixMode prefixMode) |
| | | 1193 | | { |
| | 80 | 1194 | | return new TelegramCommandPolicy(prefixes, allowSpaceAfterPrefix, ignoreCase, prefixMode); |
| | | 1195 | | } |
| | | 1196 | | |
| | | 1197 | | private static Dictionary<string, RouteValueDefinition> CreateEmptyRouteValues() |
| | | 1198 | | { |
| | 290 | 1199 | | return new Dictionary<string, RouteValueDefinition>(StringComparer.Ordinal); |
| | | 1200 | | } |
| | | 1201 | | |
| | | 1202 | | private static string[] GetRegexRouteValueNames( |
| | | 1203 | | MethodInfo method, |
| | | 1204 | | string pattern, |
| | | 1205 | | bool ignoreCase) |
| | | 1206 | | { |
| | | 1207 | | Regex regex; |
| | | 1208 | | |
| | | 1209 | | try |
| | | 1210 | | { |
| | 7 | 1211 | | regex = new Regex(pattern, TelegramTemplateRouteParser.GetRegexOptions(ignoreCase)); |
| | 7 | 1212 | | } |
| | 0 | 1213 | | catch (ArgumentException exception) |
| | | 1214 | | { |
| | 0 | 1215 | | throw CreateSignatureException(method, $"Regex route pattern is invalid: {exception.Message}"); |
| | | 1216 | | } |
| | | 1217 | | |
| | 7 | 1218 | | return regex |
| | 7 | 1219 | | .GetGroupNames() |
| | 13 | 1220 | | .Where(static name => !int.TryParse(name, out _)) |
| | 7 | 1221 | | .Distinct(StringComparer.Ordinal) |
| | 7 | 1222 | | .ToArray(); |
| | | 1223 | | } |
| | | 1224 | | |
| | | 1225 | | private static Type? GetGenericCallbackPayloadType( |
| | | 1226 | | Type handlerType, |
| | | 1227 | | MethodInfo method, |
| | | 1228 | | bool includeClassRouteAttributes) |
| | | 1229 | | { |
| | 358 | 1230 | | var attributes = method |
| | 358 | 1231 | | .GetCustomAttributes(inherit: true) |
| | 819 | 1232 | | .Select(static attribute => attribute.GetType()) |
| | 358 | 1233 | | .Where(static type => |
| | 1177 | 1234 | | type.IsGenericType && |
| | 1177 | 1235 | | type.GetGenericTypeDefinition() == typeof(CallbackAttribute<>)); |
| | | 1236 | | |
| | 358 | 1237 | | if (includeClassRouteAttributes) |
| | | 1238 | | { |
| | 8 | 1239 | | attributes = handlerType |
| | 8 | 1240 | | .GetCustomAttributes(inherit: true) |
| | 14 | 1241 | | .Select(static attribute => attribute.GetType()) |
| | 8 | 1242 | | .Where(static type => |
| | 14 | 1243 | | type.IsGenericType && |
| | 14 | 1244 | | type.GetGenericTypeDefinition() == typeof(CallbackAttribute<>)) |
| | 8 | 1245 | | .Concat(attributes); |
| | | 1246 | | } |
| | | 1247 | | |
| | 358 | 1248 | | var payloadTypes = attributes |
| | 18 | 1249 | | .Select(static type => type.GetGenericArguments()[0]) |
| | 358 | 1250 | | .Distinct() |
| | 358 | 1251 | | .ToArray(); |
| | | 1252 | | |
| | 358 | 1253 | | if (payloadTypes.Length > 1) |
| | | 1254 | | { |
| | 0 | 1255 | | throw CreateSignatureException( |
| | 0 | 1256 | | method, |
| | 0 | 1257 | | "CallbackAttribute<TPayload> metadata on one handler must resolve to exactly one payload type."); |
| | | 1258 | | } |
| | | 1259 | | |
| | 358 | 1260 | | return payloadTypes.FirstOrDefault(); |
| | | 1261 | | } |
| | | 1262 | | |
| | | 1263 | | private static bool HasGenericCallbackAttribute(MemberInfo member) |
| | | 1264 | | { |
| | 1674 | 1265 | | return member |
| | 1674 | 1266 | | .GetCustomAttributes(inherit: true) |
| | 746 | 1267 | | .Select(static attribute => attribute.GetType()) |
| | 1674 | 1268 | | .Any(static type => |
| | 2420 | 1269 | | type.IsGenericType && |
| | 2420 | 1270 | | type.GetGenericTypeDefinition() == typeof(CallbackAttribute<>)); |
| | | 1271 | | } |
| | | 1272 | | |
| | | 1273 | | private static void ValidateCallbackPayloadType(MethodInfo method, Type? callbackPayloadType) |
| | | 1274 | | { |
| | 324 | 1275 | | if (callbackPayloadType is null) |
| | | 1276 | | { |
| | 306 | 1277 | | return; |
| | | 1278 | | } |
| | | 1279 | | |
| | 18 | 1280 | | if (callbackPayloadType.IsInterface || |
| | 18 | 1281 | | callbackPayloadType.IsAbstract || |
| | 18 | 1282 | | callbackPayloadType.ContainsGenericParameters) |
| | | 1283 | | { |
| | 0 | 1284 | | throw CreateSignatureException( |
| | 0 | 1285 | | method, |
| | 0 | 1286 | | $"{nameof(CallbackAttribute)} payload type '{callbackPayloadType.FullName}' must be a concrete closed ty |
| | | 1287 | | } |
| | 18 | 1288 | | } |
| | | 1289 | | |
| | | 1290 | | private static bool IsCallbackDataPayloadType(Type type) |
| | | 1291 | | { |
| | 46 | 1292 | | return type.GetCustomAttribute<CallbackDataAttribute>(inherit: false) is not null; |
| | | 1293 | | } |
| | | 1294 | | |
| | | 1295 | | private static void ValidateReturnType(MethodInfo method) |
| | | 1296 | | { |
| | 325 | 1297 | | if (method.ReturnType != typeof(Task) && method.ReturnType != typeof(ValueTask)) |
| | | 1298 | | { |
| | 1 | 1299 | | throw CreateSignatureException( |
| | 1 | 1300 | | method, |
| | 1 | 1301 | | "A Telegram handler method must return Task or ValueTask."); |
| | | 1302 | | } |
| | 324 | 1303 | | } |
| | | 1304 | | |
| | | 1305 | | private static string NormalizeCommand(MethodInfo method, string command) |
| | | 1306 | | { |
| | 62 | 1307 | | var normalized = command.Trim(); |
| | | 1308 | | |
| | 62 | 1309 | | if (normalized.StartsWith('/') || |
| | 62 | 1310 | | normalized.Contains('@', StringComparison.Ordinal) || |
| | 62 | 1311 | | normalized.Any(char.IsWhiteSpace)) |
| | | 1312 | | { |
| | 0 | 1313 | | throw CreateSignatureException( |
| | 0 | 1314 | | method, |
| | 0 | 1315 | | "CommandAttribute value must be a command name without '/', bot username, or whitespace."); |
| | | 1316 | | } |
| | | 1317 | | |
| | 62 | 1318 | | return normalized; |
| | | 1319 | | } |
| | | 1320 | | |
| | | 1321 | | private static string FormatHandlerKind(TelegramHandlerKind kind) |
| | | 1322 | | { |
| | 0 | 1323 | | return kind switch |
| | 0 | 1324 | | { |
| | 0 | 1325 | | TelegramHandlerKind.Command => "command", |
| | 0 | 1326 | | TelegramHandlerKind.Message => "message", |
| | 0 | 1327 | | TelegramHandlerKind.Callback => "callback", |
| | 0 | 1328 | | TelegramHandlerKind.ChatMember => "chat member update", |
| | 0 | 1329 | | _ => kind.ToString() |
| | 0 | 1330 | | }; |
| | | 1331 | | } |
| | | 1332 | | |
| | | 1333 | | private static string NormalizeCommandPattern( |
| | | 1334 | | MethodInfo method, |
| | | 1335 | | string pattern, |
| | | 1336 | | TelegramCommandPolicy policy) |
| | | 1337 | | { |
| | 14 | 1338 | | var normalized = pattern.Trim(); |
| | | 1339 | | |
| | 28 | 1340 | | if (policy.Prefixes.Any(prefix => normalized.StartsWith(prefix, StringComparison.Ordinal))) |
| | | 1341 | | { |
| | 1 | 1342 | | throw CreateSignatureException( |
| | 1 | 1343 | | method, |
| | 1 | 1344 | | "Command route template value must not include a command prefix."); |
| | | 1345 | | } |
| | | 1346 | | |
| | 13 | 1347 | | return normalized; |
| | | 1348 | | } |
| | | 1349 | | |
| | | 1350 | | private static bool TryGetRouteValueParameterShape( |
| | | 1351 | | ParameterInfo parameter, |
| | | 1352 | | out Type valueType, |
| | | 1353 | | out bool isNullable) |
| | | 1354 | | { |
| | 31 | 1355 | | var parameterType = parameter.ParameterType; |
| | 31 | 1356 | | var underlyingType = Nullable.GetUnderlyingType(parameterType); |
| | | 1357 | | |
| | 31 | 1358 | | if (underlyingType is not null) |
| | | 1359 | | { |
| | 5 | 1360 | | valueType = underlyingType; |
| | 5 | 1361 | | isNullable = true; |
| | 5 | 1362 | | return valueType == typeof(int) || valueType == typeof(long); |
| | | 1363 | | } |
| | | 1364 | | |
| | 26 | 1365 | | if (parameterType == typeof(string)) |
| | | 1366 | | { |
| | 5 | 1367 | | valueType = typeof(string); |
| | 5 | 1368 | | isNullable = NullabilityInfoContext.Create(parameter).ReadState == NullabilityState.Nullable; |
| | 5 | 1369 | | return true; |
| | | 1370 | | } |
| | | 1371 | | |
| | 21 | 1372 | | valueType = parameterType; |
| | 21 | 1373 | | isNullable = false; |
| | | 1374 | | |
| | 21 | 1375 | | return valueType == typeof(int) || valueType == typeof(long); |
| | | 1376 | | } |
| | | 1377 | | |
| | | 1378 | | private static InvalidOperationException CreateSignatureException(MethodInfo method, string reason) |
| | | 1379 | | { |
| | 14 | 1380 | | return new InvalidOperationException( |
| | 14 | 1381 | | $"Invalid Telegram handler signature '{method.DeclaringType?.FullName}.{method.Name}': {reason}"); |
| | | 1382 | | } |
| | | 1383 | | |
| | | 1384 | | private sealed record RouteDefinition( |
| | | 1385 | | TelegramRouteKind RouteKind, |
| | | 1386 | | string? Pattern, |
| | | 1387 | | TelegramCommandPolicy? CommandPolicy, |
| | | 1388 | | IReadOnlyList<TelegramTextFilter> TextFilters, |
| | | 1389 | | IReadOnlyDictionary<string, RouteValueDefinition> RouteValues, |
| | | 1390 | | IReadOnlyList<TelegramChatMemberTransitionDescriptor> ChatMemberTransitions); |
| | | 1391 | | |
| | | 1392 | | private readonly record struct RouteValueDefinition( |
| | | 1393 | | Type? ValueType, |
| | | 1394 | | bool IsOptional); |
| | | 1395 | | } |