| | | 1 | | using TeleFlow.Framework.States; |
| | | 2 | | using TeleFlow.Framework.Updates; |
| | | 3 | | using TeleFlow.Telegram.Schema.Types; |
| | | 4 | | |
| | | 5 | | namespace 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> |
| | | 11 | | internal 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 | | |
| | 4 | 20 | | public bool IsAvailable => TryGetUpdate(out _); |
| | | 21 | | |
| | 1 | 22 | | public TelegramUpdateContext Current => TryGetCurrent(out var context) |
| | 1 | 23 | | ? context |
| | 1 | 24 | | : throw new InvalidOperationException( |
| | 1 | 25 | | "No current Telegram update is available. ITelegramCurrentUpdateAccessor can only be used inside TeleFlow Te |
| | | 26 | | |
| | 12 | 27 | | public Update Update => TryGetUpdate(out var update) |
| | 12 | 28 | | ? update |
| | 12 | 29 | | : throw new InvalidOperationException( |
| | 12 | 30 | | "No current Telegram update is available. ITelegramCurrentUpdateAccessor can only be used inside TeleFlow Te |
| | | 31 | | |
| | | 32 | | public User? User |
| | | 33 | | { |
| | | 34 | | get |
| | | 35 | | { |
| | 13 | 36 | | return TryGetClassification(out var classification) |
| | 13 | 37 | | ? classification.User |
| | 13 | 38 | | : null; |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public Chat? Chat |
| | | 43 | | { |
| | | 44 | | get |
| | | 45 | | { |
| | 13 | 46 | | return TryGetClassification(out var classification) |
| | 13 | 47 | | ? classification.Chat |
| | 13 | 48 | | : null; |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | 4 | 52 | | public Message? Message => TryGetUpdate(out var update) ? update.Message : null; |
| | | 53 | | |
| | 4 | 54 | | public CallbackQuery? CallbackQuery => TryGetUpdate(out var update) ? update.CallbackQuery : null; |
| | | 55 | | |
| | | 56 | | public ChatMemberUpdated? ChatMemberUpdated |
| | | 57 | | { |
| | | 58 | | get |
| | | 59 | | { |
| | 3 | 60 | | if (!TryGetUpdate(out var update)) |
| | | 61 | | { |
| | 1 | 62 | | return null; |
| | | 63 | | } |
| | | 64 | | |
| | 2 | 65 | | return update.ChatMember ?? update.MyChatMember; |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public StateKey? StateKey |
| | | 70 | | { |
| | | 71 | | get |
| | | 72 | | { |
| | 2 | 73 | | if (!currentUpdate.TryGetCurrent(out var context) || |
| | 2 | 74 | | !context.TryGetState(out var state)) |
| | | 75 | | { |
| | 1 | 76 | | return null; |
| | | 77 | | } |
| | | 78 | | |
| | 1 | 79 | | return state.Key; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | public bool TryGetCurrent(out TelegramUpdateContext context) |
| | | 84 | | { |
| | 5 | 85 | | if (!currentUpdate.TryGetCurrent(out var updateContext) || |
| | 5 | 86 | | updateContext.Payload is not TelegramUpdatePayload payload) |
| | | 87 | | { |
| | 2 | 88 | | context = null!; |
| | 2 | 89 | | return false; |
| | | 90 | | } |
| | | 91 | | |
| | 3 | 92 | | if (payload.Update.Message is not null) |
| | | 93 | | { |
| | 1 | 94 | | context = contextFactory.CreateMessageContext(updateContext); |
| | 1 | 95 | | return true; |
| | | 96 | | } |
| | | 97 | | |
| | 2 | 98 | | if (payload.Update.CallbackQuery is not null) |
| | | 99 | | { |
| | 1 | 100 | | context = contextFactory.CreateCallbackQueryContext(updateContext); |
| | 1 | 101 | | return true; |
| | | 102 | | } |
| | | 103 | | |
| | 1 | 104 | | if (payload.Update.ChatMember is not null || |
| | 1 | 105 | | payload.Update.MyChatMember is not null) |
| | | 106 | | { |
| | 1 | 107 | | context = contextFactory.CreateChatMemberUpdatedContext(updateContext); |
| | 1 | 108 | | return true; |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | context = contextFactory.CreateTelegramContext(updateContext); |
| | 0 | 112 | | return true; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public bool TryGetMessageContext(out MessageContext context) |
| | | 116 | | { |
| | 2 | 117 | | if (!currentUpdate.TryGetCurrent(out var updateContext) || |
| | 2 | 118 | | updateContext.Payload is not TelegramUpdatePayload payload || |
| | 2 | 119 | | payload.Update.Message is null) |
| | | 120 | | { |
| | 1 | 121 | | context = null!; |
| | 1 | 122 | | return false; |
| | | 123 | | } |
| | | 124 | | |
| | 1 | 125 | | context = contextFactory.CreateMessageContext(updateContext); |
| | 1 | 126 | | return true; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public bool TryGetCallbackQueryContext(out CallbackQueryContext context) |
| | | 130 | | { |
| | 2 | 131 | | if (!currentUpdate.TryGetCurrent(out var updateContext) || |
| | 2 | 132 | | updateContext.Payload is not TelegramUpdatePayload payload || |
| | 2 | 133 | | payload.Update.CallbackQuery is null) |
| | | 134 | | { |
| | 1 | 135 | | context = null!; |
| | 1 | 136 | | return false; |
| | | 137 | | } |
| | | 138 | | |
| | 1 | 139 | | context = contextFactory.CreateCallbackQueryContext(updateContext); |
| | 1 | 140 | | return true; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | public bool TryGetChatMemberUpdatedContext(out ChatMemberUpdatedContext context) |
| | | 144 | | { |
| | 2 | 145 | | if (!currentUpdate.TryGetCurrent(out var updateContext) || |
| | 2 | 146 | | updateContext.Payload is not TelegramUpdatePayload payload || |
| | 2 | 147 | | payload.Update.ChatMember is null && payload.Update.MyChatMember is null) |
| | | 148 | | { |
| | 0 | 149 | | context = null!; |
| | 0 | 150 | | return false; |
| | | 151 | | } |
| | | 152 | | |
| | 2 | 153 | | context = contextFactory.CreateChatMemberUpdatedContext(updateContext); |
| | 2 | 154 | | return true; |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | private bool TryGetUpdate(out Update update) |
| | | 158 | | { |
| | 41 | 159 | | if (_updateCached) |
| | | 160 | | { |
| | 23 | 161 | | update = _update!; |
| | 23 | 162 | | return true; |
| | | 163 | | } |
| | | 164 | | |
| | 18 | 165 | | if (!currentUpdate.TryGetCurrent(out var context) || |
| | 18 | 166 | | context.Payload is not TelegramUpdatePayload payload) |
| | | 167 | | { |
| | 6 | 168 | | update = null!; |
| | 6 | 169 | | return false; |
| | | 170 | | } |
| | | 171 | | |
| | 12 | 172 | | _update = payload.Update; |
| | 12 | 173 | | _updateCached = true; |
| | 12 | 174 | | update = _update; |
| | 12 | 175 | | return true; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | private bool TryGetClassification(out TelegramUpdateClassification classification) |
| | | 179 | | { |
| | 26 | 180 | | if (_classificationCached) |
| | | 181 | | { |
| | 12 | 182 | | classification = _classification; |
| | 12 | 183 | | return true; |
| | | 184 | | } |
| | | 185 | | |
| | 14 | 186 | | if (!TryGetUpdate(out var update)) |
| | | 187 | | { |
| | 2 | 188 | | classification = default; |
| | 2 | 189 | | return false; |
| | | 190 | | } |
| | | 191 | | |
| | 12 | 192 | | _classification = TelegramUpdateClassifier.Classify(update); |
| | 12 | 193 | | _classificationCached = true; |
| | 12 | 194 | | classification = _classification; |
| | 12 | 195 | | return true; |
| | | 196 | | } |
| | | 197 | | } |