| | | 1 | | using TeleFlow.Telegram.Schema.Types; |
| | | 2 | | |
| | | 3 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Captures destination, sender, and thread facts once before built-in filters evaluate an update. |
| | | 7 | | /// It keeps Telegram's destination chat, actual sender user, and <c>sender_chat</c> provenance as separate concepts. |
| | | 8 | | /// </summary> |
| | | 9 | | internal readonly record struct TelegramFilterContextFacts( |
| | | 10 | | Chat? DestinationChat, |
| | | 11 | | User? SenderUser, |
| | | 12 | | Chat? SenderChat, |
| | | 13 | | long? MessageThreadId) |
| | | 14 | | { |
| | | 15 | | public static TelegramFilterContextFacts From(TelegramUpdateContext context) |
| | | 16 | | { |
| | 266 | 17 | | return context switch |
| | 266 | 18 | | { |
| | 167 | 19 | | MessageContext messageContext => FromMessage(messageContext.TelegramMessage), |
| | 266 | 20 | | |
| | 97 | 21 | | CallbackQueryContext callbackContext => FromCallback(callbackContext), |
| | 266 | 22 | | |
| | 2 | 23 | | ChatMemberUpdatedContext chatMemberContext => new TelegramFilterContextFacts( |
| | 2 | 24 | | chatMemberContext.TelegramChat, |
| | 2 | 25 | | SenderUser: null, |
| | 2 | 26 | | SenderChat: null, |
| | 2 | 27 | | MessageThreadId: null), |
| | 266 | 28 | | |
| | 0 | 29 | | _ => default |
| | 266 | 30 | | }; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | private static TelegramFilterContextFacts FromMessage(Message message) |
| | | 34 | | { |
| | 167 | 35 | | var senderChat = message.SenderChat; |
| | | 36 | | |
| | 167 | 37 | | return new TelegramFilterContextFacts( |
| | 167 | 38 | | message.Chat, |
| | 167 | 39 | | senderChat is null ? message.From : null, |
| | 167 | 40 | | senderChat, |
| | 167 | 41 | | message.MessageThreadId); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | private static TelegramFilterContextFacts FromCallback(CallbackQueryContext context) |
| | | 45 | | { |
| | 97 | 46 | | if (context.TelegramCallbackQuery.Message is not { } maybeMessage) |
| | | 47 | | { |
| | 39 | 48 | | return new TelegramFilterContextFacts( |
| | 39 | 49 | | DestinationChat: null, |
| | 39 | 50 | | context.TelegramCallbackQuery.From, |
| | 39 | 51 | | SenderChat: null, |
| | 39 | 52 | | MessageThreadId: null); |
| | | 53 | | } |
| | | 54 | | |
| | 58 | 55 | | if (maybeMessage.TryGetMessage(out var message) && |
| | 58 | 56 | | message is not null) |
| | | 57 | | { |
| | 29 | 58 | | return new TelegramFilterContextFacts( |
| | 29 | 59 | | message.Chat, |
| | 29 | 60 | | context.TelegramCallbackQuery.From, |
| | 29 | 61 | | SenderChat: null, |
| | 29 | 62 | | message.MessageThreadId); |
| | | 63 | | } |
| | | 64 | | |
| | 29 | 65 | | return new TelegramFilterContextFacts( |
| | 29 | 66 | | DestinationChat: null, |
| | 29 | 67 | | context.TelegramCallbackQuery.From, |
| | 29 | 68 | | SenderChat: null, |
| | 29 | 69 | | MessageThreadId: null); |
| | | 70 | | } |
| | | 71 | | } |