< Summary

Information
Class: TeleFlow.Telegram.Internal.TelegramCurrentUpdateAccessor
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/Internal/TelegramCurrentUpdateAccessor.cs
Line coverage
95%
Covered lines: 78
Uncovered lines: 4
Coverable lines: 82
Total lines: 197
Line coverage: 95.1%
Branch coverage
79%
Covered branches: 49
Total branches: 62
Branch coverage: 79%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IsAvailable()100%11100%
get_Current()50%22100%
get_Update()50%22100%
get_User()100%22100%
get_Chat()100%22100%
get_Message()100%22100%
get_CallbackQuery()100%22100%
get_ChatMemberUpdated()75%44100%
get_StateKey()100%44100%
TryGetCurrent(...)83.33%121287.5%
TryGetMessageContext(...)66.66%66100%
TryGetCallbackQueryContext(...)66.66%66100%
TryGetChatMemberUpdatedContext(...)50%9871.42%
TryGetUpdate(...)100%66100%
TryGetClassification(...)100%44100%

File(s)

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

#LineLine coverage
 1using TeleFlow.Framework.States;
 2using TeleFlow.Framework.Updates;
 3using TeleFlow.Telegram.Schema.Types;
 4
 5namespace TeleFlow.Telegram.Internal;
 6
 7/// <summary>
 8/// Adapts the Core current-update accessor to Telegram-specific contexts and identity properties for scoped
 9/// application services reached while a Telegram update is being dispatched.
 10/// </summary>
 11internal sealed class TelegramCurrentUpdateAccessor(
 12    IUpdateContextAccessor currentUpdate,
 13    TelegramContextFactory contextFactory) : ITelegramCurrentUpdateAccessor
 14{
 15    private bool _updateCached;
 16    private Update? _update;
 17    private bool _classificationCached;
 18    private TelegramUpdateClassification _classification;
 19
 420    public bool IsAvailable => TryGetUpdate(out _);
 21
 122    public TelegramUpdateContext Current => TryGetCurrent(out var context)
 123        ? context
 124        : throw new InvalidOperationException(
 125            "No current Telegram update is available. ITelegramCurrentUpdateAccessor can only be used inside TeleFlow Te
 26
 1227    public Update Update => TryGetUpdate(out var update)
 1228        ? update
 1229        : throw new InvalidOperationException(
 1230            "No current Telegram update is available. ITelegramCurrentUpdateAccessor can only be used inside TeleFlow Te
 31
 32    public User? User
 33    {
 34        get
 35        {
 1336            return TryGetClassification(out var classification)
 1337                ? classification.User
 1338                : null;
 39        }
 40    }
 41
 42    public Chat? Chat
 43    {
 44        get
 45        {
 1346            return TryGetClassification(out var classification)
 1347                ? classification.Chat
 1348                : null;
 49        }
 50    }
 51
 452    public Message? Message => TryGetUpdate(out var update) ? update.Message : null;
 53
 454    public CallbackQuery? CallbackQuery => TryGetUpdate(out var update) ? update.CallbackQuery : null;
 55
 56    public ChatMemberUpdated? ChatMemberUpdated
 57    {
 58        get
 59        {
 360            if (!TryGetUpdate(out var update))
 61            {
 162                return null;
 63            }
 64
 265            return update.ChatMember ?? update.MyChatMember;
 66        }
 67    }
 68
 69    public StateKey? StateKey
 70    {
 71        get
 72        {
 273            if (!currentUpdate.TryGetCurrent(out var context) ||
 274                !context.TryGetState(out var state))
 75            {
 176                return null;
 77            }
 78
 179            return state.Key;
 80        }
 81    }
 82
 83    public bool TryGetCurrent(out TelegramUpdateContext context)
 84    {
 585        if (!currentUpdate.TryGetCurrent(out var updateContext) ||
 586            updateContext.Payload is not TelegramUpdatePayload payload)
 87        {
 288            context = null!;
 289            return false;
 90        }
 91
 392        if (payload.Update.Message is not null)
 93        {
 194            context = contextFactory.CreateMessageContext(updateContext);
 195            return true;
 96        }
 97
 298        if (payload.Update.CallbackQuery is not null)
 99        {
 1100            context = contextFactory.CreateCallbackQueryContext(updateContext);
 1101            return true;
 102        }
 103
 1104        if (payload.Update.ChatMember is not null ||
 1105            payload.Update.MyChatMember is not null)
 106        {
 1107            context = contextFactory.CreateChatMemberUpdatedContext(updateContext);
 1108            return true;
 109        }
 110
 0111        context = contextFactory.CreateTelegramContext(updateContext);
 0112        return true;
 113    }
 114
 115    public bool TryGetMessageContext(out MessageContext context)
 116    {
 2117        if (!currentUpdate.TryGetCurrent(out var updateContext) ||
 2118            updateContext.Payload is not TelegramUpdatePayload payload ||
 2119            payload.Update.Message is null)
 120        {
 1121            context = null!;
 1122            return false;
 123        }
 124
 1125        context = contextFactory.CreateMessageContext(updateContext);
 1126        return true;
 127    }
 128
 129    public bool TryGetCallbackQueryContext(out CallbackQueryContext context)
 130    {
 2131        if (!currentUpdate.TryGetCurrent(out var updateContext) ||
 2132            updateContext.Payload is not TelegramUpdatePayload payload ||
 2133            payload.Update.CallbackQuery is null)
 134        {
 1135            context = null!;
 1136            return false;
 137        }
 138
 1139        context = contextFactory.CreateCallbackQueryContext(updateContext);
 1140        return true;
 141    }
 142
 143    public bool TryGetChatMemberUpdatedContext(out ChatMemberUpdatedContext context)
 144    {
 2145        if (!currentUpdate.TryGetCurrent(out var updateContext) ||
 2146            updateContext.Payload is not TelegramUpdatePayload payload ||
 2147            payload.Update.ChatMember is null && payload.Update.MyChatMember is null)
 148        {
 0149            context = null!;
 0150            return false;
 151        }
 152
 2153        context = contextFactory.CreateChatMemberUpdatedContext(updateContext);
 2154        return true;
 155    }
 156
 157    private bool TryGetUpdate(out Update update)
 158    {
 41159        if (_updateCached)
 160        {
 23161            update = _update!;
 23162            return true;
 163        }
 164
 18165        if (!currentUpdate.TryGetCurrent(out var context) ||
 18166            context.Payload is not TelegramUpdatePayload payload)
 167        {
 6168            update = null!;
 6169            return false;
 170        }
 171
 12172        _update = payload.Update;
 12173        _updateCached = true;
 12174        update = _update;
 12175        return true;
 176    }
 177
 178    private bool TryGetClassification(out TelegramUpdateClassification classification)
 179    {
 26180        if (_classificationCached)
 181        {
 12182            classification = _classification;
 12183            return true;
 184        }
 185
 14186        if (!TryGetUpdate(out var update))
 187        {
 2188            classification = default;
 2189            return false;
 190        }
 191
 12192        _classification = TelegramUpdateClassifier.Classify(update);
 12193        _classificationCached = true;
 12194        classification = _classification;
 12195        return true;
 196    }
 197}