| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using TeleFlow.Framework.Callbacks; |
| | | 3 | | using TeleFlow.Framework.Updates; |
| | | 4 | | |
| | | 5 | | namespace TeleFlow.Telegram.Internal; |
| | | 6 | | |
| | | 7 | | internal sealed class TelegramContextFactory |
| | | 8 | | { |
| | 1 | 9 | | private static readonly object TelegramContextKey = new(); |
| | 1 | 10 | | private static readonly object MessageContextKey = new(); |
| | 1 | 11 | | private static readonly object CallbackQueryContextKey = new(); |
| | 1 | 12 | | private static readonly object ChatMemberUpdatedContextKey = new(); |
| | | 13 | | |
| | | 14 | | [System.Diagnostics.CodeAnalysis.SuppressMessage( |
| | | 15 | | "Performance", |
| | | 16 | | "CA1822:Mark members as static", |
| | | 17 | | Justification = "Context factory methods remain instance methods because the factory is resolved through DI as o |
| | | 18 | | public TelegramUpdateContext CreateTelegramContext(UpdateContext context) |
| | | 19 | | { |
| | 414 | 20 | | ArgumentNullException.ThrowIfNull(context); |
| | | 21 | | |
| | 414 | 22 | | if (context.Items.TryGetValue(TelegramContextKey, out var cachedContext) && |
| | 414 | 23 | | cachedContext is TelegramUpdateContext telegramContext) |
| | | 24 | | { |
| | 2 | 25 | | return telegramContext; |
| | | 26 | | } |
| | | 27 | | |
| | 412 | 28 | | var payload = GetPayload(context); |
| | 412 | 29 | | var rootBot = context.Services.GetRequiredService<ITelegramClient>(); |
| | 412 | 30 | | var bot = new UpdateScopedTelegramClient(rootBot, context.CancellationToken); |
| | 412 | 31 | | var callbackData = context.Services.GetRequiredService<ICallbackDataSerializer>(); |
| | 412 | 32 | | telegramContext = new TelegramUpdateContext(context, bot, callbackData, payload); |
| | 412 | 33 | | context.Items[TelegramContextKey] = telegramContext; |
| | 412 | 34 | | return telegramContext; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public MessageContext CreateMessageContext(UpdateContext context) |
| | | 38 | | { |
| | 305 | 39 | | ArgumentNullException.ThrowIfNull(context); |
| | | 40 | | |
| | 305 | 41 | | if (context.Items.TryGetValue(MessageContextKey, out var cachedContext) && |
| | 305 | 42 | | cachedContext is MessageContext messageContext) |
| | | 43 | | { |
| | 4 | 44 | | return messageContext; |
| | | 45 | | } |
| | | 46 | | |
| | 301 | 47 | | var telegramContext = CreateTelegramContext(context); |
| | 301 | 48 | | var telegramMessage = telegramContext.Update.Message |
| | 301 | 49 | | ?? throw new InvalidOperationException("The current update does not contain a Telegram message."); |
| | | 50 | | |
| | 300 | 51 | | messageContext = new MessageContext( |
| | 300 | 52 | | context, |
| | 300 | 53 | | telegramContext.Bot, |
| | 300 | 54 | | telegramContext.CallbackData, |
| | 300 | 55 | | telegramContext.Payload, |
| | 300 | 56 | | telegramMessage); |
| | 300 | 57 | | context.Items[MessageContextKey] = messageContext; |
| | 300 | 58 | | return messageContext; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public CallbackQueryContext CreateCallbackQueryContext(UpdateContext context) |
| | | 62 | | { |
| | 91 | 63 | | ArgumentNullException.ThrowIfNull(context); |
| | | 64 | | |
| | 91 | 65 | | if (context.Items.TryGetValue(CallbackQueryContextKey, out var cachedContext) && |
| | 91 | 66 | | cachedContext is CallbackQueryContext callbackQueryContext) |
| | | 67 | | { |
| | 2 | 68 | | return callbackQueryContext; |
| | | 69 | | } |
| | | 70 | | |
| | 89 | 71 | | var telegramContext = CreateTelegramContext(context); |
| | 89 | 72 | | var telegramCallbackQuery = telegramContext.Update.CallbackQuery |
| | 89 | 73 | | ?? throw new InvalidOperationException("The current update does not contain a Telegram callback query."); |
| | | 74 | | |
| | 88 | 75 | | callbackQueryContext = new CallbackQueryContext( |
| | 88 | 76 | | context, |
| | 88 | 77 | | telegramContext.Bot, |
| | 88 | 78 | | telegramContext.CallbackData, |
| | 88 | 79 | | telegramContext.Payload, |
| | 88 | 80 | | telegramCallbackQuery); |
| | | 81 | | |
| | 88 | 82 | | context.Items[CallbackQueryContextKey] = callbackQueryContext; |
| | 88 | 83 | | return callbackQueryContext; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public ChatMemberUpdatedContext CreateChatMemberUpdatedContext(UpdateContext context) |
| | | 87 | | { |
| | 18 | 88 | | ArgumentNullException.ThrowIfNull(context); |
| | | 89 | | |
| | 18 | 90 | | if (context.Items.TryGetValue(ChatMemberUpdatedContextKey, out var cachedContext) && |
| | 18 | 91 | | cachedContext is ChatMemberUpdatedContext chatMemberUpdatedContext) |
| | | 92 | | { |
| | 3 | 93 | | return chatMemberUpdatedContext; |
| | | 94 | | } |
| | | 95 | | |
| | 15 | 96 | | var telegramContext = CreateTelegramContext(context); |
| | 15 | 97 | | var telegramChatMemberUpdated = telegramContext.Update.ChatMember ?? |
| | 15 | 98 | | telegramContext.Update.MyChatMember ?? |
| | 15 | 99 | | throw new InvalidOperationException("The current update does not contain a Teleg |
| | | 100 | | |
| | 15 | 101 | | chatMemberUpdatedContext = new ChatMemberUpdatedContext( |
| | 15 | 102 | | context, |
| | 15 | 103 | | telegramContext.Bot, |
| | 15 | 104 | | telegramContext.CallbackData, |
| | 15 | 105 | | telegramContext.Payload, |
| | 15 | 106 | | telegramChatMemberUpdated); |
| | | 107 | | |
| | 15 | 108 | | context.Items[ChatMemberUpdatedContextKey] = chatMemberUpdatedContext; |
| | 15 | 109 | | return chatMemberUpdatedContext; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | private static TelegramUpdatePayload GetPayload(UpdateContext context) |
| | | 113 | | { |
| | 412 | 114 | | if (context.Payload is TelegramUpdatePayload payload) |
| | | 115 | | { |
| | 412 | 116 | | return payload; |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | throw new InvalidOperationException("The current update payload is not a Telegram update payload."); |
| | | 120 | | } |
| | | 121 | | } |