| | | 1 | | using System.ComponentModel; |
| | | 2 | | using TeleFlow.Annotations; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Infrastructure handler metadata emitted by TeleFlow source generators. |
| | | 8 | | /// This API is not intended to be used by application code. |
| | | 9 | | /// </summary> |
| | | 10 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 11 | | public sealed class TelegramGeneratedHandlerDescriptor |
| | | 12 | | { |
| | | 13 | | public TelegramGeneratedHandlerDescriptor( |
| | | 14 | | Type handlerType, |
| | | 15 | | string methodName, |
| | | 16 | | TelegramGeneratedHandlerKind kind, |
| | | 17 | | int registrationOrder, |
| | | 18 | | string? moduleName, |
| | | 19 | | string? command, |
| | | 20 | | Type? callbackPayloadType, |
| | | 21 | | IReadOnlyList<TelegramGeneratedTextFilterDescriptor> textFilters, |
| | | 22 | | IReadOnlyList<string> states, |
| | | 23 | | IReadOnlyList<TelegramGeneratedHandlerParameterDescriptor> parameters, |
| | | 24 | | TelegramGeneratedHandlerInvoker invoker) |
| | 352 | 25 | | : this( |
| | 352 | 26 | | handlerType, |
| | 352 | 27 | | methodName, |
| | 352 | 28 | | kind, |
| | 352 | 29 | | GetLegacyRouteKind(kind), |
| | 352 | 30 | | routePattern: command, |
| | 352 | 31 | | commandPrefixes: ["/"], |
| | 352 | 32 | | allowSpaceAfterPrefix: false, |
| | 352 | 33 | | ignoreCase: true, |
| | 352 | 34 | | registrationOrder, |
| | 352 | 35 | | moduleName, |
| | 352 | 36 | | command, |
| | 352 | 37 | | callbackPayloadType, |
| | 352 | 38 | | textFilters, |
| | 352 | 39 | | filters: [], |
| | 352 | 40 | | chatMemberTransitions: [], |
| | 352 | 41 | | roleRequirements: [], |
| | 352 | 42 | | states, |
| | 352 | 43 | | parameters, |
| | 352 | 44 | | invoker) |
| | | 45 | | { |
| | 352 | 46 | | } |
| | | 47 | | |
| | | 48 | | public TelegramGeneratedHandlerDescriptor( |
| | | 49 | | Type handlerType, |
| | | 50 | | string methodName, |
| | | 51 | | TelegramGeneratedHandlerKind kind, |
| | | 52 | | TelegramGeneratedRouteKind routeKind, |
| | | 53 | | string? routePattern, |
| | | 54 | | IReadOnlyList<string>? commandPrefixes, |
| | | 55 | | bool allowSpaceAfterPrefix, |
| | | 56 | | bool ignoreCase, |
| | | 57 | | int registrationOrder, |
| | | 58 | | string? moduleName, |
| | | 59 | | string? command, |
| | | 60 | | Type? callbackPayloadType, |
| | | 61 | | IReadOnlyList<TelegramGeneratedTextFilterDescriptor> textFilters, |
| | | 62 | | IReadOnlyList<string> states, |
| | | 63 | | IReadOnlyList<TelegramGeneratedHandlerParameterDescriptor> parameters, |
| | | 64 | | TelegramGeneratedHandlerInvoker invoker) |
| | 0 | 65 | | : this( |
| | 0 | 66 | | handlerType, |
| | 0 | 67 | | methodName, |
| | 0 | 68 | | kind, |
| | 0 | 69 | | routeKind, |
| | 0 | 70 | | routePattern, |
| | 0 | 71 | | commandPrefixes, |
| | 0 | 72 | | allowSpaceAfterPrefix, |
| | 0 | 73 | | ignoreCase, |
| | 0 | 74 | | registrationOrder, |
| | 0 | 75 | | moduleName, |
| | 0 | 76 | | command, |
| | 0 | 77 | | callbackPayloadType, |
| | 0 | 78 | | textFilters, |
| | 0 | 79 | | filters: [], |
| | 0 | 80 | | chatMemberTransitions: [], |
| | 0 | 81 | | roleRequirements: [], |
| | 0 | 82 | | states, |
| | 0 | 83 | | parameters, |
| | 0 | 84 | | invoker) |
| | | 85 | | { |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | public TelegramGeneratedHandlerDescriptor( |
| | | 89 | | Type handlerType, |
| | | 90 | | string methodName, |
| | | 91 | | TelegramGeneratedHandlerKind kind, |
| | | 92 | | TelegramGeneratedRouteKind routeKind, |
| | | 93 | | string? routePattern, |
| | | 94 | | IReadOnlyList<string>? commandPrefixes, |
| | | 95 | | bool allowSpaceAfterPrefix, |
| | | 96 | | bool ignoreCase, |
| | | 97 | | int registrationOrder, |
| | | 98 | | string? moduleName, |
| | | 99 | | string? command, |
| | | 100 | | Type? callbackPayloadType, |
| | | 101 | | IReadOnlyList<TelegramGeneratedTextFilterDescriptor> textFilters, |
| | | 102 | | IReadOnlyList<TelegramGeneratedFilterDescriptor> filters, |
| | | 103 | | IReadOnlyList<string> states, |
| | | 104 | | IReadOnlyList<TelegramGeneratedHandlerParameterDescriptor> parameters, |
| | | 105 | | TelegramGeneratedHandlerInvoker invoker) |
| | 224 | 106 | | : this( |
| | 224 | 107 | | handlerType, |
| | 224 | 108 | | methodName, |
| | 224 | 109 | | kind, |
| | 224 | 110 | | routeKind, |
| | 224 | 111 | | routePattern, |
| | 224 | 112 | | commandPrefixes, |
| | 224 | 113 | | allowSpaceAfterPrefix, |
| | 224 | 114 | | ignoreCase, |
| | 224 | 115 | | registrationOrder, |
| | 224 | 116 | | moduleName, |
| | 224 | 117 | | command, |
| | 224 | 118 | | callbackPayloadType, |
| | 224 | 119 | | textFilters, |
| | 224 | 120 | | filters, |
| | 224 | 121 | | chatMemberTransitions: [], |
| | 224 | 122 | | roleRequirements: [], |
| | 224 | 123 | | states, |
| | 224 | 124 | | parameters, |
| | 224 | 125 | | invoker) |
| | | 126 | | { |
| | 224 | 127 | | } |
| | | 128 | | |
| | | 129 | | public TelegramGeneratedHandlerDescriptor( |
| | | 130 | | Type handlerType, |
| | | 131 | | string methodName, |
| | | 132 | | TelegramGeneratedHandlerKind kind, |
| | | 133 | | TelegramGeneratedRouteKind routeKind, |
| | | 134 | | string? routePattern, |
| | | 135 | | IReadOnlyList<string>? commandPrefixes, |
| | | 136 | | bool allowSpaceAfterPrefix, |
| | | 137 | | bool ignoreCase, |
| | | 138 | | int registrationOrder, |
| | | 139 | | string? moduleName, |
| | | 140 | | string? command, |
| | | 141 | | Type? callbackPayloadType, |
| | | 142 | | IReadOnlyList<TelegramGeneratedTextFilterDescriptor> textFilters, |
| | | 143 | | IReadOnlyList<TelegramGeneratedFilterDescriptor> filters, |
| | | 144 | | IReadOnlyList<TelegramGeneratedChatMemberTransitionDescriptor> chatMemberTransitions, |
| | | 145 | | IReadOnlyList<string> states, |
| | | 146 | | IReadOnlyList<TelegramGeneratedHandlerParameterDescriptor> parameters, |
| | | 147 | | TelegramGeneratedHandlerInvoker invoker) |
| | 32 | 148 | | : this( |
| | 32 | 149 | | handlerType, |
| | 32 | 150 | | methodName, |
| | 32 | 151 | | kind, |
| | 32 | 152 | | routeKind, |
| | 32 | 153 | | routePattern, |
| | 32 | 154 | | commandPrefixes, |
| | 32 | 155 | | allowSpaceAfterPrefix, |
| | 32 | 156 | | ignoreCase, |
| | 32 | 157 | | registrationOrder, |
| | 32 | 158 | | moduleName, |
| | 32 | 159 | | command, |
| | 32 | 160 | | callbackPayloadType, |
| | 32 | 161 | | textFilters, |
| | 32 | 162 | | filters, |
| | 32 | 163 | | chatMemberTransitions, |
| | 32 | 164 | | roleRequirements: [], |
| | 32 | 165 | | states, |
| | 32 | 166 | | parameters, |
| | 32 | 167 | | invoker) |
| | | 168 | | { |
| | 32 | 169 | | } |
| | | 170 | | |
| | | 171 | | public TelegramGeneratedHandlerDescriptor( |
| | | 172 | | Type handlerType, |
| | | 173 | | string methodName, |
| | | 174 | | TelegramGeneratedHandlerKind kind, |
| | | 175 | | TelegramGeneratedRouteKind routeKind, |
| | | 176 | | string? routePattern, |
| | | 177 | | IReadOnlyList<string>? commandPrefixes, |
| | | 178 | | bool allowSpaceAfterPrefix, |
| | | 179 | | bool ignoreCase, |
| | | 180 | | int registrationOrder, |
| | | 181 | | string? moduleName, |
| | | 182 | | string? command, |
| | | 183 | | Type? callbackPayloadType, |
| | | 184 | | IReadOnlyList<TelegramGeneratedTextFilterDescriptor> textFilters, |
| | | 185 | | IReadOnlyList<TelegramGeneratedFilterDescriptor> filters, |
| | | 186 | | IReadOnlyList<TelegramGeneratedChatMemberTransitionDescriptor> chatMemberTransitions, |
| | | 187 | | IReadOnlyList<TelegramGeneratedRoleRequirementDescriptor> roleRequirements, |
| | | 188 | | IReadOnlyList<string> states, |
| | | 189 | | IReadOnlyList<TelegramGeneratedHandlerParameterDescriptor> parameters, |
| | | 190 | | TelegramGeneratedHandlerInvoker invoker, |
| | | 191 | | string? sceneName = null, |
| | | 192 | | IReadOnlyList<TelegramGeneratedRouteValueDescriptor>? routeValues = null, |
| | | 193 | | TelegramGeneratedAutoAnswerCallbackDescriptor? autoAnswerCallback = null, |
| | | 194 | | CommandPrefixMode prefixMode = CommandPrefixMode.Required) |
| | | 195 | | { |
| | 1063 | 196 | | ArgumentNullException.ThrowIfNull(handlerType); |
| | 1063 | 197 | | ArgumentException.ThrowIfNullOrWhiteSpace(methodName); |
| | 1063 | 198 | | ArgumentNullException.ThrowIfNull(textFilters); |
| | 1063 | 199 | | ArgumentNullException.ThrowIfNull(filters); |
| | 1063 | 200 | | ArgumentNullException.ThrowIfNull(chatMemberTransitions); |
| | 1063 | 201 | | ArgumentNullException.ThrowIfNull(roleRequirements); |
| | 1063 | 202 | | ArgumentNullException.ThrowIfNull(states); |
| | 1063 | 203 | | ArgumentNullException.ThrowIfNull(parameters); |
| | 1063 | 204 | | ArgumentNullException.ThrowIfNull(invoker); |
| | | 205 | | |
| | 1063 | 206 | | if (!Enum.IsDefined(prefixMode)) |
| | | 207 | | { |
| | 0 | 208 | | throw new ArgumentOutOfRangeException(nameof(prefixMode), prefixMode, "Unsupported command prefix mode."); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | HandlerType = handlerType; |
| | | 212 | | MethodName = methodName; |
| | | 213 | | Kind = kind; |
| | | 214 | | RouteKind = routeKind; |
| | | 215 | | RoutePattern = routePattern; |
| | 1063 | 216 | | CommandPrefixes = CopyValues(commandPrefixes ?? ["/"]); |
| | | 217 | | AllowSpaceAfterPrefix = allowSpaceAfterPrefix; |
| | | 218 | | PrefixMode = prefixMode; |
| | | 219 | | IgnoreCase = ignoreCase; |
| | | 220 | | RegistrationOrder = registrationOrder; |
| | | 221 | | ModuleName = moduleName; |
| | | 222 | | Command = command; |
| | | 223 | | CallbackPayloadType = callbackPayloadType; |
| | 1063 | 224 | | TextFilters = CopyValues(textFilters); |
| | 1063 | 225 | | Filters = CopyValues(filters); |
| | 1063 | 226 | | ChatMemberTransitions = CopyValues(chatMemberTransitions); |
| | 1063 | 227 | | RoleRequirements = CopyValues(roleRequirements); |
| | 1063 | 228 | | States = CopyValues(states); |
| | 1063 | 229 | | Parameters = CopyValues(parameters); |
| | | 230 | | Invoker = invoker; |
| | | 231 | | SceneName = sceneName; |
| | 1063 | 232 | | RouteValues = CopyValues(routeValues); |
| | | 233 | | AutoAnswerCallback = autoAnswerCallback; |
| | 1063 | 234 | | } |
| | | 235 | | |
| | | 236 | | private static TelegramGeneratedRouteKind GetLegacyRouteKind(TelegramGeneratedHandlerKind kind) |
| | | 237 | | { |
| | 352 | 238 | | return kind switch |
| | 352 | 239 | | { |
| | 224 | 240 | | TelegramGeneratedHandlerKind.Command => TelegramGeneratedRouteKind.CommandExact, |
| | 96 | 241 | | TelegramGeneratedHandlerKind.Message => TelegramGeneratedRouteKind.MessageAny, |
| | 32 | 242 | | TelegramGeneratedHandlerKind.Callback => TelegramGeneratedRouteKind.Callback, |
| | 0 | 243 | | TelegramGeneratedHandlerKind.ChatMember => TelegramGeneratedRouteKind.ChatMemberUpdated, |
| | 0 | 244 | | _ => throw new InvalidOperationException($"Unsupported generated Telegram handler kind '{kind}'.") |
| | 352 | 245 | | }; |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | public Type HandlerType { get; } |
| | | 249 | | |
| | | 250 | | public string MethodName { get; } |
| | | 251 | | |
| | | 252 | | public TelegramGeneratedHandlerKind Kind { get; } |
| | | 253 | | |
| | | 254 | | public TelegramGeneratedRouteKind RouteKind { get; } |
| | | 255 | | |
| | | 256 | | public string? RoutePattern { get; } |
| | | 257 | | |
| | | 258 | | public IReadOnlyList<string> CommandPrefixes { get; } |
| | | 259 | | |
| | | 260 | | public bool AllowSpaceAfterPrefix { get; } |
| | | 261 | | |
| | | 262 | | public CommandPrefixMode PrefixMode { get; } |
| | | 263 | | |
| | | 264 | | public bool IgnoreCase { get; } |
| | | 265 | | |
| | | 266 | | public int RegistrationOrder { get; } |
| | | 267 | | |
| | | 268 | | public string? ModuleName { get; } |
| | | 269 | | |
| | | 270 | | public string? SceneName { get; } |
| | | 271 | | |
| | | 272 | | public string? Command { get; } |
| | | 273 | | |
| | | 274 | | public Type? CallbackPayloadType { get; } |
| | | 275 | | |
| | | 276 | | public IReadOnlyList<TelegramGeneratedTextFilterDescriptor> TextFilters { get; } |
| | | 277 | | |
| | | 278 | | public IReadOnlyList<TelegramGeneratedFilterDescriptor> Filters { get; } |
| | | 279 | | |
| | | 280 | | public IReadOnlyList<TelegramGeneratedChatMemberTransitionDescriptor> ChatMemberTransitions { get; } |
| | | 281 | | |
| | | 282 | | public IReadOnlyList<TelegramGeneratedRoleRequirementDescriptor> RoleRequirements { get; } |
| | | 283 | | |
| | | 284 | | public IReadOnlyList<string> States { get; } |
| | | 285 | | |
| | | 286 | | public IReadOnlyList<TelegramGeneratedHandlerParameterDescriptor> Parameters { get; } |
| | | 287 | | |
| | | 288 | | public TelegramGeneratedHandlerInvoker Invoker { get; } |
| | | 289 | | |
| | | 290 | | public IReadOnlyList<TelegramGeneratedRouteValueDescriptor> RouteValues { get; } |
| | | 291 | | |
| | | 292 | | public TelegramGeneratedAutoAnswerCallbackDescriptor? AutoAnswerCallback { get; } |
| | | 293 | | |
| | | 294 | | private static TValue[] CopyValues<TValue>(IReadOnlyList<TValue>? values) |
| | | 295 | | { |
| | 8504 | 296 | | return values is null || values.Count == 0 |
| | 8504 | 297 | | ? [] |
| | 8504 | 298 | | : values.ToArray(); |
| | | 299 | | } |
| | | 300 | | } |