| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using TeleFlow.Telegram.Internal.Handlers; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram.Internal; |
| | | 5 | | |
| | | 6 | | internal static partial class TelegramAllowedUpdatesResolver |
| | | 7 | | { |
| | | 8 | | public static IReadOnlyList<string>? Resolve( |
| | | 9 | | TelegramAllowedUpdates allowedUpdates, |
| | | 10 | | IReadOnlyList<TelegramHandlerDescriptor> handlerDescriptors, |
| | | 11 | | ILogger logger) |
| | | 12 | | { |
| | 16 | 13 | | ArgumentNullException.ThrowIfNull(allowedUpdates); |
| | 16 | 14 | | ArgumentNullException.ThrowIfNull(handlerDescriptors); |
| | 16 | 15 | | ArgumentNullException.ThrowIfNull(logger); |
| | | 16 | | |
| | 16 | 17 | | return allowedUpdates.Mode switch |
| | 16 | 18 | | { |
| | 14 | 19 | | TelegramAllowedUpdatesMode.Auto => ResolveAuto(handlerDescriptors, logger), |
| | 27 | 20 | | TelegramAllowedUpdatesMode.All => TelegramUpdateType.AllKnown.Select(static item => item.Value).ToArray(), |
| | 3 | 21 | | TelegramAllowedUpdatesMode.Only => allowedUpdates.UpdateTypes.Select(static item => item.Value).ToArray(), |
| | 0 | 22 | | _ => throw new InvalidOperationException("Unsupported Telegram allowed updates mode.") |
| | 16 | 23 | | }; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | private static List<string>? ResolveAuto( |
| | | 27 | | IReadOnlyList<TelegramHandlerDescriptor> handlerDescriptors, |
| | | 28 | | ILogger logger) |
| | | 29 | | { |
| | 14 | 30 | | if (handlerDescriptors.Count == 0) |
| | | 31 | | { |
| | 10 | 32 | | LogAutoInferenceSkipped(logger); |
| | 10 | 33 | | return null; |
| | | 34 | | } |
| | | 35 | | |
| | 4 | 36 | | var values = new List<string>(capacity: 4); |
| | | 37 | | |
| | 4 | 38 | | if (handlerDescriptors.Any(static descriptor => |
| | 9 | 39 | | descriptor.Kind is TelegramHandlerKind.Command or TelegramHandlerKind.Message)) |
| | | 40 | | { |
| | 2 | 41 | | values.Add(TelegramUpdateType.Message.Value); |
| | | 42 | | } |
| | | 43 | | |
| | 10 | 44 | | if (handlerDescriptors.Any(static descriptor => descriptor.Kind == TelegramHandlerKind.Callback)) |
| | | 45 | | { |
| | 1 | 46 | | values.Add(TelegramUpdateType.CallbackQuery.Value); |
| | | 47 | | } |
| | | 48 | | |
| | 10 | 49 | | if (handlerDescriptors.Any(static descriptor => descriptor.Route.RouteKind == TelegramRouteKind.MyChatMemberUpda |
| | | 50 | | { |
| | 1 | 51 | | values.Add(TelegramUpdateType.MyChatMember.Value); |
| | | 52 | | } |
| | | 53 | | |
| | 9 | 54 | | if (handlerDescriptors.Any(static descriptor => descriptor.Route.RouteKind == TelegramRouteKind.ChatMemberUpdate |
| | | 55 | | { |
| | 1 | 56 | | values.Add(TelegramUpdateType.ChatMember.Value); |
| | | 57 | | } |
| | | 58 | | |
| | 4 | 59 | | return values.Count == 0 ? null : values; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | [LoggerMessage( |
| | | 63 | | EventId = 1, |
| | | 64 | | Level = LogLevel.Information, |
| | | 65 | | Message = "Telegram allowed_updates auto inference has no handler metadata. getUpdates will leave allowed_update |
| | | 66 | | private static partial void LogAutoInferenceSkipped(ILogger logger); |
| | | 67 | | } |