< Summary

Information
Class: TeleFlow.Telegram.Internal.TelegramUpdateClassifier
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/Internal/TelegramUpdateClassifier.cs
Line coverage
98%
Covered lines: 58
Uncovered lines: 1
Coverable lines: 59
Total lines: 148
Line coverage: 98.3%
Branch coverage
83%
Covered branches: 67
Total branches: 80
Branch coverage: 83.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Classify(...)98.14%545498.11%
FromMessage(...)100%11100%
GetCallbackChat(...)60%1010100%
GetChatBoostUser(...)50%1616100%

File(s)

/_/src/TeleFlow.Framework/Internal/TelegramUpdateClassifier.cs

#LineLine coverage
 1using TeleFlow.Telegram.Schema.Types;
 2
 3namespace 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>
 9internal static class TelegramUpdateClassifier
 10{
 11    public static TelegramUpdateClassification Classify(Update update)
 12    {
 7013        ArgumentNullException.ThrowIfNull(update);
 14
 9715        if (update.Message is { } message) return FromMessage("message", message);
 4516        if (update.EditedMessage is { } editedMessage) return FromMessage("edited_message", editedMessage);
 4217        if (update.ChannelPost is { } channelPost) return FromMessage("channel_post", channelPost);
 4118        if (update.EditedChannelPost is { } editedChannelPost) return FromMessage("edited_channel_post", editedChannelPo
 3919        if (update.BusinessConnection is { } businessConnection)
 20        {
 221            return new TelegramUpdateClassification("business_connection", businessConnection.User, null);
 22        }
 23
 3824        if (update.BusinessMessage is { } businessMessage) return FromMessage("business_message", businessMessage);
 3625        if (update.EditedBusinessMessage is { } editedBusinessMessage)
 26        {
 127            return FromMessage("edited_business_message", editedBusinessMessage);
 28        }
 29
 3530        if (update.DeletedBusinessMessages is { } deletedBusinessMessages)
 31        {
 232            return new TelegramUpdateClassification("deleted_business_messages", null, deletedBusinessMessages.Chat);
 33        }
 34
 3435        if (update.GuestMessage is { } guestMessage) return FromMessage("guest_message", guestMessage);
 3236        if (update.MessageReaction is { } messageReaction)
 37        {
 238            return new TelegramUpdateClassification("message_reaction", messageReaction.User, messageReaction.Chat);
 39        }
 40
 3041        if (update.MessageReactionCount is { } messageReactionCount)
 42        {
 143            return new TelegramUpdateClassification("message_reaction_count", null, messageReactionCount.Chat);
 44        }
 45
 2946        if (update.InlineQuery is { } inlineQuery)
 47        {
 348            return new TelegramUpdateClassification("inline_query", inlineQuery.From, null);
 49        }
 50
 2651        if (update.ChosenInlineResult is { } chosenInlineResult)
 52        {
 153            return new TelegramUpdateClassification("chosen_inline_result", chosenInlineResult.From, null);
 54        }
 55
 2556        if (update.CallbackQuery is { } callbackQuery)
 57        {
 658            return new TelegramUpdateClassification("callback_query", callbackQuery.From, GetCallbackChat(callbackQuery)
 59        }
 60
 1961        if (update.ShippingQuery is { } shippingQuery)
 62        {
 163            return new TelegramUpdateClassification("shipping_query", shippingQuery.From, null);
 64        }
 65
 1866        if (update.PreCheckoutQuery is { } preCheckoutQuery)
 67        {
 268            return new TelegramUpdateClassification("pre_checkout_query", preCheckoutQuery.From, null);
 69        }
 70
 1671        if (update.PurchasedPaidMedia is { } purchasedPaidMedia)
 72        {
 173            return new TelegramUpdateClassification("purchased_paid_media", purchasedPaidMedia.From, null);
 74        }
 75
 1676        if (update.Poll is not null) return new TelegramUpdateClassification("poll", null, null);
 1477        if (update.PollAnswer is { } pollAnswer)
 78        {
 279            return new TelegramUpdateClassification("poll_answer", pollAnswer.User, pollAnswer.VoterChat);
 80        }
 81
 1282        if (update.MyChatMember is { } myChatMember)
 83        {
 184            return new TelegramUpdateClassification("my_chat_member", myChatMember.From, myChatMember.Chat);
 85        }
 86
 1187        if (update.ChatMember is { } chatMember)
 88        {
 289            return new TelegramUpdateClassification("chat_member", chatMember.From, chatMember.Chat);
 90        }
 91
 992        if (update.ChatJoinRequest is { } chatJoinRequest)
 93        {
 294            return new TelegramUpdateClassification("chat_join_request", chatJoinRequest.From, chatJoinRequest.Chat);
 95        }
 96
 797        if (update.ChatBoost is { } chatBoost)
 98        {
 299            return new TelegramUpdateClassification(
 2100                "chat_boost",
 2101                GetChatBoostUser(chatBoost.Boost?.Source),
 2102                chatBoost.Chat);
 103        }
 104
 5105        if (update.RemovedChatBoost is { } removedChatBoost)
 106        {
 1107            return new TelegramUpdateClassification(
 1108                "removed_chat_boost",
 1109                GetChatBoostUser(removedChatBoost.Source),
 1110                removedChatBoost.Chat);
 111        }
 112
 4113        if (update.ManagedBot is { } managedBot)
 114        {
 2115            return new TelegramUpdateClassification("managed_bot", managedBot.User, null);
 116        }
 117
 2118        if (update.Subscription is { } subscription)
 119        {
 2120            return new TelegramUpdateClassification("subscription", subscription.User, null);
 121        }
 122
 0123        return new TelegramUpdateClassification("unknown", null, null);
 124    }
 125
 126    private static TelegramUpdateClassification FromMessage(string type, Message message)
 127    {
 34128        return new TelegramUpdateClassification(type, message.From, message.Chat);
 129    }
 130
 131    private static Chat? GetCallbackChat(CallbackQuery callbackQuery)
 132    {
 6133        return callbackQuery.Message?.Message?.Chat ??
 6134               callbackQuery.Message?.InaccessibleMessage?.Chat;
 135    }
 136
 137    private static User? GetChatBoostUser(ChatBoostSource? source)
 138    {
 3139        return source?.ChatBoostSourcePremium?.User ??
 3140               source?.ChatBoostSourceGiftCode?.User ??
 3141               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>
 148internal readonly record struct TelegramUpdateClassification(string Type, User? User, Chat? Chat);