| | | 1 | | using TeleFlow.Framework.States; |
| | | 2 | | using TeleFlow.Framework.Updates; |
| | | 3 | | using TeleFlow.Telegram.Internal.Options; |
| | | 4 | | using TeleFlow.Telegram.Schema.Types; |
| | | 5 | | |
| | | 6 | | namespace TeleFlow.Telegram.Internal; |
| | | 7 | | |
| | | 8 | | internal sealed class TelegramStateKeyFactory : IStateKeyFactory |
| | | 9 | | { |
| | | 10 | | private readonly string? _botPartitionSegment; |
| | | 11 | | |
| | | 12 | | public TelegramStateKeyFactory(TelegramStateKeyOptions options) |
| | | 13 | | { |
| | 20 | 14 | | ArgumentNullException.ThrowIfNull(options); |
| | | 15 | | |
| | 20 | 16 | | _botPartitionSegment = options.BotId is { } botId ? $"bot:{botId}" : null; |
| | 20 | 17 | | } |
| | | 18 | | |
| | | 19 | | public bool TryCreateStateKey(UpdateContext context, out StateKey key) |
| | | 20 | | { |
| | 41 | 21 | | ArgumentNullException.ThrowIfNull(context); |
| | | 22 | | |
| | 41 | 23 | | if (context.Payload is not TelegramUpdatePayload payload) |
| | | 24 | | { |
| | 0 | 25 | | key = default; |
| | 0 | 26 | | return false; |
| | | 27 | | } |
| | | 28 | | |
| | 41 | 29 | | if (TryCreateFromMessage(payload.Update.Message, out key) || |
| | 41 | 30 | | TryCreateFromCallback(payload.Update.CallbackQuery, out key)) |
| | | 31 | | { |
| | 41 | 32 | | return true; |
| | | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | key = default; |
| | 0 | 36 | | return false; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | private bool TryCreateFromMessage(Message? message, out StateKey key) |
| | | 40 | | { |
| | 41 | 41 | | if (message?.From is null) |
| | | 42 | | { |
| | 3 | 43 | | key = default; |
| | 3 | 44 | | return false; |
| | | 45 | | } |
| | | 46 | | |
| | 38 | 47 | | key = StateKey.Create( |
| | 38 | 48 | | scope: "telegram", |
| | 38 | 49 | | subject: CreateUserSubject(message.From.Id), |
| | 38 | 50 | | partition: CreateMessagePartition(message)); |
| | 38 | 51 | | return true; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private bool TryCreateFromCallback(CallbackQuery? callbackQuery, out StateKey key) |
| | | 55 | | { |
| | 3 | 56 | | if (callbackQuery is null) |
| | | 57 | | { |
| | 0 | 58 | | key = default; |
| | 0 | 59 | | return false; |
| | | 60 | | } |
| | | 61 | | |
| | 3 | 62 | | key = StateKey.Create( |
| | 3 | 63 | | scope: "telegram", |
| | 3 | 64 | | subject: CreateUserSubject(callbackQuery.From.Id), |
| | 3 | 65 | | partition: ResolveCallbackPartition(callbackQuery)); |
| | 3 | 66 | | return true; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | private string ResolveCallbackPartition(CallbackQuery callbackQuery) |
| | | 70 | | { |
| | 3 | 71 | | if (callbackQuery.Message is not null) |
| | | 72 | | { |
| | 1 | 73 | | if (callbackQuery.Message.TryGetMessage(out var message) && message is not null) |
| | | 74 | | { |
| | 1 | 75 | | return CreateMessagePartition(message); |
| | | 76 | | } |
| | | 77 | | |
| | 0 | 78 | | if (callbackQuery.Message.TryGetInaccessibleMessage(out var inaccessibleMessage) && |
| | 0 | 79 | | inaccessibleMessage is not null) |
| | | 80 | | { |
| | 0 | 81 | | return CreateChatPartition(inaccessibleMessage.Chat.Id); |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | 2 | 85 | | return string.IsNullOrWhiteSpace(callbackQuery.ChatInstance) |
| | 2 | 86 | | ? CreateInlinePartition("unknown") |
| | 2 | 87 | | : CreateInlinePartition(callbackQuery.ChatInstance); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private static string CreateUserSubject(long userId) |
| | | 91 | | { |
| | 41 | 92 | | return $"user:{userId}"; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | private string CreateMessagePartition(Message message) |
| | | 96 | | { |
| | 39 | 97 | | var segments = CreatePartitionSegments(capacity: 4); |
| | | 98 | | |
| | 39 | 99 | | if (!string.IsNullOrWhiteSpace(message.BusinessConnectionId)) |
| | | 100 | | { |
| | 1 | 101 | | segments.Add($"business:{message.BusinessConnectionId}"); |
| | | 102 | | } |
| | | 103 | | |
| | 39 | 104 | | segments.Add($"chat:{message.Chat.Id}"); |
| | | 105 | | |
| | 39 | 106 | | if (message.MessageThreadId is not null) |
| | | 107 | | { |
| | 1 | 108 | | segments.Add($"thread:{message.MessageThreadId.Value}"); |
| | | 109 | | } |
| | | 110 | | |
| | 39 | 111 | | return string.Join(':', segments); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | private string CreateChatPartition(long chatId) |
| | | 115 | | { |
| | 0 | 116 | | var segments = CreatePartitionSegments(capacity: 2); |
| | | 117 | | |
| | 0 | 118 | | segments.Add($"chat:{chatId}"); |
| | | 119 | | |
| | 0 | 120 | | return string.Join(':', segments); |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | private string CreateInlinePartition(string chatInstance) |
| | | 124 | | { |
| | 2 | 125 | | var segments = CreatePartitionSegments(capacity: 2); |
| | | 126 | | |
| | 2 | 127 | | segments.Add($"inline:{chatInstance}"); |
| | | 128 | | |
| | 2 | 129 | | return string.Join(':', segments); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | private List<string> CreatePartitionSegments(int capacity) |
| | | 133 | | { |
| | 41 | 134 | | var segments = new List<string>(capacity); |
| | | 135 | | |
| | 41 | 136 | | if (_botPartitionSegment is not null) |
| | | 137 | | { |
| | 2 | 138 | | segments.Add(_botPartitionSegment); |
| | | 139 | | } |
| | | 140 | | |
| | 41 | 141 | | return segments; |
| | | 142 | | } |
| | | 143 | | } |