| | | 1 | | using System.Reflection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Stores a validated custom filter invocation path prepared while the handler table is built. |
| | | 8 | | /// Incoming updates reuse this call site so custom filter evaluation does not discover interfaces, |
| | | 9 | | /// inspect attributes, or create reflection calls on the dispatcher hot path. |
| | | 10 | | /// </summary> |
| | | 11 | | internal sealed class TelegramCustomFilterCallSite |
| | | 12 | | { |
| | 1 | 13 | | private static readonly MethodInfo UntypedInvokerMethod = typeof(TelegramCustomFilterCallSite) |
| | 1 | 14 | | .GetMethod(nameof(InvokeUntypedFilterAsync), BindingFlags.NonPublic | BindingFlags.Static)!; |
| | | 15 | | |
| | 1 | 16 | | private static readonly MethodInfo TypedInvokerMethod = typeof(TelegramCustomFilterCallSite) |
| | 1 | 17 | | .GetMethod(nameof(InvokeTypedFilterAsync), BindingFlags.NonPublic | BindingFlags.Static)!; |
| | | 18 | | |
| | | 19 | | private readonly TelegramCustomFilterInvoker _invoker; |
| | | 20 | | |
| | | 21 | | private TelegramCustomFilterCallSite( |
| | | 22 | | Type filterType, |
| | | 23 | | Attribute? attribute, |
| | | 24 | | TelegramCustomFilterInvoker invoker) |
| | | 25 | | { |
| | | 26 | | FilterType = filterType; |
| | | 27 | | Attribute = attribute; |
| | 61 | 28 | | _invoker = invoker; |
| | 61 | 29 | | } |
| | | 30 | | |
| | | 31 | | public Type FilterType { get; } |
| | | 32 | | |
| | | 33 | | public Attribute? Attribute { get; } |
| | | 34 | | |
| | | 35 | | public static TelegramCustomFilterCallSite Create( |
| | | 36 | | Type filterType, |
| | | 37 | | Type? contextType, |
| | | 38 | | Attribute? attribute, |
| | | 39 | | TelegramHandlerKind handlerKind) |
| | | 40 | | { |
| | 61 | 41 | | ValidateConcreteFilterType(filterType); |
| | | 42 | | |
| | 61 | 43 | | var expectedContextType = GetExpectedContextType(handlerKind); |
| | | 44 | | |
| | 61 | 45 | | if (attribute is null) |
| | | 46 | | { |
| | 36 | 47 | | var resolvedContextType = contextType ?? ResolveUntypedFilterContextType(filterType, expectedContextType); |
| | | 48 | | |
| | 36 | 49 | | ValidateResolvedContextType(filterType, resolvedContextType, expectedContextType); |
| | | 50 | | |
| | 36 | 51 | | var invoker = CreateUntypedInvoker(resolvedContextType); |
| | | 52 | | |
| | 36 | 53 | | return new TelegramCustomFilterCallSite(filterType, attribute: null, invoker); |
| | | 54 | | } |
| | | 55 | | |
| | 25 | 56 | | var attributeType = attribute.GetType(); |
| | | 57 | | |
| | 25 | 58 | | if (attributeType.IsGenericType) |
| | | 59 | | { |
| | 0 | 60 | | throw new InvalidOperationException( |
| | 0 | 61 | | $"Custom Telegram filter attribute type '{attributeType.FullName}' must be non-generic."); |
| | | 62 | | } |
| | | 63 | | |
| | 25 | 64 | | var typedContextType = contextType ?? ResolveTypedFilterContextType(filterType, expectedContextType, attributeTy |
| | | 65 | | |
| | 25 | 66 | | ValidateResolvedContextType(filterType, typedContextType, expectedContextType); |
| | | 67 | | |
| | 25 | 68 | | var typedInvoker = CreateTypedInvoker(typedContextType, attributeType); |
| | | 69 | | |
| | 25 | 70 | | return new TelegramCustomFilterCallSite(filterType, attribute, typedInvoker); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public ValueTask<bool> MatchesAsync( |
| | | 74 | | TelegramUpdateContext context, |
| | | 75 | | CancellationToken cancellationToken) |
| | | 76 | | { |
| | 29 | 77 | | var filter = context.Services.GetRequiredService(FilterType); |
| | 28 | 78 | | return _invoker(filter, context, Attribute, cancellationToken); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static Type ResolveUntypedFilterContextType( |
| | | 82 | | Type filterType, |
| | | 83 | | Type expectedContextType) |
| | | 84 | | { |
| | 36 | 85 | | var contextType = ResolveContextType( |
| | 36 | 86 | | filterType, |
| | 36 | 87 | | expectedContextType, |
| | 36 | 88 | | static type => type.IsGenericType && |
| | 36 | 89 | | type.GetGenericTypeDefinition() == typeof(ITelegramFilter<>), |
| | 72 | 90 | | static type => type.GetGenericArguments()[0]); |
| | | 91 | | |
| | 36 | 92 | | if (contextType is null) |
| | | 93 | | { |
| | 0 | 94 | | throw new InvalidOperationException( |
| | 0 | 95 | | $"Custom Telegram filter type '{filterType.FullName}' must implement ITelegramFilter<TContext> compatibl |
| | | 96 | | } |
| | | 97 | | |
| | 36 | 98 | | return contextType; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | private static Type ResolveTypedFilterContextType( |
| | | 102 | | Type filterType, |
| | | 103 | | Type expectedContextType, |
| | | 104 | | Type attributeType) |
| | | 105 | | { |
| | 1 | 106 | | var contextType = ResolveContextType( |
| | 1 | 107 | | filterType, |
| | 1 | 108 | | expectedContextType, |
| | 1 | 109 | | type => type.IsGenericType && |
| | 1 | 110 | | type.GetGenericTypeDefinition() == typeof(ITelegramFilter<,>) && |
| | 1 | 111 | | type.GetGenericArguments()[1] == attributeType, |
| | 2 | 112 | | static type => type.GetGenericArguments()[0]); |
| | | 113 | | |
| | 1 | 114 | | if (contextType is null) |
| | | 115 | | { |
| | 0 | 116 | | throw new InvalidOperationException( |
| | 0 | 117 | | $"Parameterized custom Telegram filter type '{filterType.FullName}' must implement ITelegramFilter<TCont |
| | | 118 | | } |
| | | 119 | | |
| | 1 | 120 | | return contextType; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | private static Type? ResolveContextType( |
| | | 124 | | Type filterType, |
| | | 125 | | Type expectedContextType, |
| | | 126 | | Func<Type, bool> interfacePredicate, |
| | | 127 | | Func<Type, Type> contextSelector) |
| | | 128 | | { |
| | 37 | 129 | | Type? updateContextType = null; |
| | | 130 | | |
| | 112 | 131 | | foreach (var interfaceType in filterType.GetInterfaces()) |
| | | 132 | | { |
| | 37 | 133 | | if (!interfacePredicate(interfaceType)) |
| | | 134 | | { |
| | | 135 | | continue; |
| | | 136 | | } |
| | | 137 | | |
| | 37 | 138 | | var contextType = contextSelector(interfaceType); |
| | | 139 | | |
| | 37 | 140 | | if (contextType == expectedContextType) |
| | | 141 | | { |
| | 36 | 142 | | return contextType; |
| | | 143 | | } |
| | | 144 | | |
| | 1 | 145 | | if (contextType == typeof(TelegramUpdateContext)) |
| | | 146 | | { |
| | 1 | 147 | | updateContextType = contextType; |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | 1 | 151 | | return updateContextType; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | private static void ValidateConcreteFilterType(Type filterType) |
| | | 155 | | { |
| | 61 | 156 | | ArgumentNullException.ThrowIfNull(filterType); |
| | | 157 | | |
| | 61 | 158 | | if (filterType.IsInterface || |
| | 61 | 159 | | filterType.IsAbstract || |
| | 61 | 160 | | filterType.ContainsGenericParameters) |
| | | 161 | | { |
| | 0 | 162 | | throw new InvalidOperationException( |
| | 0 | 163 | | $"Custom Telegram filter type '{filterType.FullName}' must be a concrete closed type."); |
| | | 164 | | } |
| | 61 | 165 | | } |
| | | 166 | | |
| | | 167 | | private static void ValidateResolvedContextType( |
| | | 168 | | Type filterType, |
| | | 169 | | Type contextType, |
| | | 170 | | Type expectedContextType) |
| | | 171 | | { |
| | 61 | 172 | | ArgumentNullException.ThrowIfNull(contextType); |
| | | 173 | | |
| | 61 | 174 | | if (contextType != expectedContextType && |
| | 61 | 175 | | contextType != typeof(TelegramUpdateContext)) |
| | | 176 | | { |
| | 0 | 177 | | throw new InvalidOperationException( |
| | 0 | 178 | | $"Custom Telegram filter type '{filterType.FullName}' is not compatible with {expectedContextType.Name} |
| | | 179 | | } |
| | 61 | 180 | | } |
| | | 181 | | |
| | | 182 | | private static Type GetExpectedContextType(TelegramHandlerKind handlerKind) |
| | | 183 | | { |
| | 61 | 184 | | return handlerKind switch |
| | 61 | 185 | | { |
| | 2 | 186 | | TelegramHandlerKind.Callback => typeof(CallbackQueryContext), |
| | 0 | 187 | | TelegramHandlerKind.ChatMember => typeof(ChatMemberUpdatedContext), |
| | 59 | 188 | | _ => typeof(MessageContext) |
| | 61 | 189 | | }; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | private static TelegramCustomFilterInvoker CreateUntypedInvoker(Type contextType) |
| | | 193 | | { |
| | 36 | 194 | | return (TelegramCustomFilterInvoker)UntypedInvokerMethod |
| | 36 | 195 | | .MakeGenericMethod(contextType) |
| | 36 | 196 | | .CreateDelegate(typeof(TelegramCustomFilterInvoker)); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | private static TelegramCustomFilterInvoker CreateTypedInvoker( |
| | | 200 | | Type contextType, |
| | | 201 | | Type attributeType) |
| | | 202 | | { |
| | 25 | 203 | | return (TelegramCustomFilterInvoker)TypedInvokerMethod |
| | 25 | 204 | | .MakeGenericMethod(contextType, attributeType) |
| | 25 | 205 | | .CreateDelegate(typeof(TelegramCustomFilterInvoker)); |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | private static ValueTask<bool> InvokeUntypedFilterAsync<TContext>( |
| | | 209 | | object filter, |
| | | 210 | | TelegramUpdateContext context, |
| | | 211 | | Attribute? attribute, |
| | | 212 | | CancellationToken cancellationToken) |
| | | 213 | | where TContext : TelegramUpdateContext |
| | | 214 | | { |
| | 10 | 215 | | return ((ITelegramFilter<TContext>)filter).MatchesAsync((TContext)context, cancellationToken); |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | private static ValueTask<bool> InvokeTypedFilterAsync<TContext, TAttribute>( |
| | | 219 | | object filter, |
| | | 220 | | TelegramUpdateContext context, |
| | | 221 | | Attribute? attribute, |
| | | 222 | | CancellationToken cancellationToken) |
| | | 223 | | where TContext : TelegramUpdateContext |
| | | 224 | | where TAttribute : Attribute |
| | | 225 | | { |
| | 18 | 226 | | return ((ITelegramFilter<TContext, TAttribute>)filter).MatchesAsync( |
| | 18 | 227 | | (TContext)context, |
| | 18 | 228 | | (TAttribute)attribute!, |
| | 18 | 229 | | cancellationToken); |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | private delegate ValueTask<bool> TelegramCustomFilterInvoker( |
| | | 233 | | object filter, |
| | | 234 | | TelegramUpdateContext context, |
| | | 235 | | Attribute? attribute, |
| | | 236 | | CancellationToken cancellationToken); |
| | | 237 | | } |