| | | 1 | | using TeleFlow.Telegram.Schema.Types; |
| | | 2 | | |
| | | 3 | | namespace TeleFlow.Telegram.Internal; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Classifies one incoming Telegram update and extracts the user and chat identities exposed to scoped application code |
| | | 7 | | /// The explicit property order mirrors the generated <see cref="Update"/> schema and performs no reflection at runtime. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class TelegramUpdateClassifier |
| | | 10 | | { |
| | | 11 | | public static TelegramUpdateClassification Classify(Update update) |
| | | 12 | | { |
| | 70 | 13 | | ArgumentNullException.ThrowIfNull(update); |
| | | 14 | | |
| | 97 | 15 | | if (update.Message is { } message) return FromMessage("message", message); |
| | 45 | 16 | | if (update.EditedMessage is { } editedMessage) return FromMessage("edited_message", editedMessage); |
| | 42 | 17 | | if (update.ChannelPost is { } channelPost) return FromMessage("channel_post", channelPost); |
| | 41 | 18 | | if (update.EditedChannelPost is { } editedChannelPost) return FromMessage("edited_channel_post", editedChannelPo |
| | 39 | 19 | | if (update.BusinessConnection is { } businessConnection) |
| | | 20 | | { |
| | 2 | 21 | | return new TelegramUpdateClassification("business_connection", businessConnection.User, null); |
| | | 22 | | } |
| | | 23 | | |
| | 38 | 24 | | if (update.BusinessMessage is { } businessMessage) return FromMessage("business_message", businessMessage); |
| | 36 | 25 | | if (update.EditedBusinessMessage is { } editedBusinessMessage) |
| | | 26 | | { |
| | 1 | 27 | | return FromMessage("edited_business_message", editedBusinessMessage); |
| | | 28 | | } |
| | | 29 | | |
| | 35 | 30 | | if (update.DeletedBusinessMessages is { } deletedBusinessMessages) |
| | | 31 | | { |
| | 2 | 32 | | return new TelegramUpdateClassification("deleted_business_messages", null, deletedBusinessMessages.Chat); |
| | | 33 | | } |
| | | 34 | | |
| | 34 | 35 | | if (update.GuestMessage is { } guestMessage) return FromMessage("guest_message", guestMessage); |
| | 32 | 36 | | if (update.MessageReaction is { } messageReaction) |
| | | 37 | | { |
| | 2 | 38 | | return new TelegramUpdateClassification("message_reaction", messageReaction.User, messageReaction.Chat); |
| | | 39 | | } |
| | | 40 | | |
| | 30 | 41 | | if (update.MessageReactionCount is { } messageReactionCount) |
| | | 42 | | { |
| | 1 | 43 | | return new TelegramUpdateClassification("message_reaction_count", null, messageReactionCount.Chat); |
| | | 44 | | } |
| | | 45 | | |
| | 29 | 46 | | if (update.InlineQuery is { } inlineQuery) |
| | | 47 | | { |
| | 3 | 48 | | return new TelegramUpdateClassification("inline_query", inlineQuery.From, null); |
| | | 49 | | } |
| | | 50 | | |
| | 26 | 51 | | if (update.ChosenInlineResult is { } chosenInlineResult) |
| | | 52 | | { |
| | 1 | 53 | | return new TelegramUpdateClassification("chosen_inline_result", chosenInlineResult.From, null); |
| | | 54 | | } |
| | | 55 | | |
| | 25 | 56 | | if (update.CallbackQuery is { } callbackQuery) |
| | | 57 | | { |
| | 6 | 58 | | return new TelegramUpdateClassification("callback_query", callbackQuery.From, GetCallbackChat(callbackQuery) |
| | | 59 | | } |
| | | 60 | | |
| | 19 | 61 | | if (update.ShippingQuery is { } shippingQuery) |
| | | 62 | | { |
| | 1 | 63 | | return new TelegramUpdateClassification("shipping_query", shippingQuery.From, null); |
| | | 64 | | } |
| | | 65 | | |
| | 18 | 66 | | if (update.PreCheckoutQuery is { } preCheckoutQuery) |
| | | 67 | | { |
| | 2 | 68 | | return new TelegramUpdateClassification("pre_checkout_query", preCheckoutQuery.From, null); |
| | | 69 | | } |
| | | 70 | | |
| | 16 | 71 | | if (update.PurchasedPaidMedia is { } purchasedPaidMedia) |
| | | 72 | | { |
| | 1 | 73 | | return new TelegramUpdateClassification("purchased_paid_media", purchasedPaidMedia.From, null); |
| | | 74 | | } |
| | | 75 | | |
| | 16 | 76 | | if (update.Poll is not null) return new TelegramUpdateClassification("poll", null, null); |
| | 14 | 77 | | if (update.PollAnswer is { } pollAnswer) |
| | | 78 | | { |
| | 2 | 79 | | return new TelegramUpdateClassification("poll_answer", pollAnswer.User, pollAnswer.VoterChat); |
| | | 80 | | } |
| | | 81 | | |
| | 12 | 82 | | if (update.MyChatMember is { } myChatMember) |
| | | 83 | | { |
| | 1 | 84 | | return new TelegramUpdateClassification("my_chat_member", myChatMember.From, myChatMember.Chat); |
| | | 85 | | } |
| | | 86 | | |
| | 11 | 87 | | if (update.ChatMember is { } chatMember) |
| | | 88 | | { |
| | 2 | 89 | | return new TelegramUpdateClassification("chat_member", chatMember.From, chatMember.Chat); |
| | | 90 | | } |
| | | 91 | | |
| | 9 | 92 | | if (update.ChatJoinRequest is { } chatJoinRequest) |
| | | 93 | | { |
| | 2 | 94 | | return new TelegramUpdateClassification("chat_join_request", chatJoinRequest.From, chatJoinRequest.Chat); |
| | | 95 | | } |
| | | 96 | | |
| | 7 | 97 | | if (update.ChatBoost is { } chatBoost) |
| | | 98 | | { |
| | 2 | 99 | | return new TelegramUpdateClassification( |
| | 2 | 100 | | "chat_boost", |
| | 2 | 101 | | GetChatBoostUser(chatBoost.Boost?.Source), |
| | 2 | 102 | | chatBoost.Chat); |
| | | 103 | | } |
| | | 104 | | |
| | 5 | 105 | | if (update.RemovedChatBoost is { } removedChatBoost) |
| | | 106 | | { |
| | 1 | 107 | | return new TelegramUpdateClassification( |
| | 1 | 108 | | "removed_chat_boost", |
| | 1 | 109 | | GetChatBoostUser(removedChatBoost.Source), |
| | 1 | 110 | | removedChatBoost.Chat); |
| | | 111 | | } |
| | | 112 | | |
| | 4 | 113 | | if (update.ManagedBot is { } managedBot) |
| | | 114 | | { |
| | 2 | 115 | | return new TelegramUpdateClassification("managed_bot", managedBot.User, null); |
| | | 116 | | } |
| | | 117 | | |
| | 2 | 118 | | if (update.Subscription is { } subscription) |
| | | 119 | | { |
| | 2 | 120 | | return new TelegramUpdateClassification("subscription", subscription.User, null); |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | return new TelegramUpdateClassification("unknown", null, null); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private static TelegramUpdateClassification FromMessage(string type, Message message) |
| | | 127 | | { |
| | 34 | 128 | | return new TelegramUpdateClassification(type, message.From, message.Chat); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private static Chat? GetCallbackChat(CallbackQuery callbackQuery) |
| | | 132 | | { |
| | 6 | 133 | | return callbackQuery.Message?.Message?.Chat ?? |
| | 6 | 134 | | callbackQuery.Message?.InaccessibleMessage?.Chat; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | private static User? GetChatBoostUser(ChatBoostSource? source) |
| | | 138 | | { |
| | 3 | 139 | | return source?.ChatBoostSourcePremium?.User ?? |
| | 3 | 140 | | source?.ChatBoostSourceGiftCode?.User ?? |
| | 3 | 141 | | source?.ChatBoostSourceGiveaway?.User; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Carries the Bot API update type and identity selected by <see cref="TelegramUpdateClassifier"/> without allocations. |
| | | 147 | | /// </summary> |
| | | 148 | | internal readonly record struct TelegramUpdateClassification(string Type, User? User, Chat? Chat); |