| | | 1 | | using System.Text; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using TeleFlow.Annotations; |
| | | 4 | | |
| | | 5 | | namespace TeleFlow.Telegram.Internal; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Packs and unpacks compact Telegram callback payloads used by typed callback handlers |
| | | 9 | | /// and inline keyboard builders before data is sent to or received from Telegram. |
| | | 10 | | /// </summary> |
| | | 11 | | internal static class CallbackDataCodec |
| | | 12 | | { |
| | | 13 | | public const int MaxTelegramCallbackDataBytes = 64; |
| | | 14 | | |
| | | 15 | | public static string PackRequired(object payload, Type payloadType) |
| | | 16 | | { |
| | 10 | 17 | | ArgumentNullException.ThrowIfNull(payload); |
| | 10 | 18 | | ArgumentNullException.ThrowIfNull(payloadType); |
| | | 19 | | |
| | 10 | 20 | | if (GeneratedCallbackDataCodecRegistry.TryGet(payloadType, out var generatedCodec)) |
| | | 21 | | { |
| | 1 | 22 | | return generatedCodec.Pack(payload); |
| | | 23 | | } |
| | | 24 | | |
| | 9 | 25 | | if (!CallbackDataMetadata.TryCreate(payloadType, out var metadata)) |
| | | 26 | | { |
| | 1 | 27 | | throw new InvalidOperationException( |
| | 1 | 28 | | $"Inline keyboard typed callback payload '{payloadType.FullName}' must be marked with " + |
| | 1 | 29 | | $"{nameof(CallbackDataAttribute)}. Add [CallbackData(\"prefix\")] to the payload type or pass raw string |
| | | 30 | | } |
| | | 31 | | |
| | 8 | 32 | | return Pack(payload, metadata); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public static bool TryGetGenerated(Type payloadType, out GeneratedCallbackDataCodec codec) |
| | | 36 | | { |
| | 81 | 37 | | return GeneratedCallbackDataCodecRegistry.TryGet(payloadType, out codec); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public static string Pack(object payload, CallbackDataMetadata metadata) |
| | | 41 | | { |
| | 10 | 42 | | ArgumentNullException.ThrowIfNull(payload); |
| | 10 | 43 | | ArgumentNullException.ThrowIfNull(metadata); |
| | | 44 | | |
| | 10 | 45 | | var serializedPayload = metadata.Pack(payload); |
| | 10 | 46 | | ValidateCallbackData(serializedPayload, "Serialized Telegram callback data"); |
| | 9 | 47 | | return serializedPayload; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public static object Unpack(string serializedPayload, CallbackDataMetadata metadata) |
| | | 51 | | { |
| | 5 | 52 | | ArgumentNullException.ThrowIfNull(serializedPayload); |
| | 5 | 53 | | ArgumentNullException.ThrowIfNull(metadata); |
| | | 54 | | |
| | 5 | 55 | | var parts = serializedPayload.Split(':'); |
| | | 56 | | |
| | 5 | 57 | | if (parts.Length == 0 || |
| | 5 | 58 | | !string.Equals(parts[0], metadata.Prefix, StringComparison.Ordinal)) |
| | | 59 | | { |
| | 0 | 60 | | throw new JsonException( |
| | 0 | 61 | | $"Telegram callback data prefix does not match payload type '{metadata.PayloadType.FullName}'."); |
| | | 62 | | } |
| | | 63 | | |
| | 5 | 64 | | if (parts.Length - 1 != metadata.Fields.Count) |
| | | 65 | | { |
| | 0 | 66 | | throw new JsonException( |
| | 0 | 67 | | $"Telegram callback data field count does not match payload type '{metadata.PayloadType.FullName}'."); |
| | | 68 | | } |
| | | 69 | | |
| | 5 | 70 | | var values = metadata.Fields |
| | 10 | 71 | | .Select((field, index) => metadata.ParseField(parts[index + 1], field.Property.PropertyType)) |
| | 5 | 72 | | .ToArray(); |
| | | 73 | | |
| | 4 | 74 | | if (metadata.Constructor is not null) |
| | | 75 | | { |
| | 4 | 76 | | return metadata.Constructor.Invoke(values); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | var payload = Activator.CreateInstance(metadata.PayloadType) |
| | 0 | 80 | | ?? throw new JsonException($"Unable to create callback data payload type '{metadata.PayloadType.FullName}'." |
| | | 81 | | |
| | 0 | 82 | | for (var index = 0; index < metadata.Fields.Count; index++) |
| | | 83 | | { |
| | 0 | 84 | | metadata.Fields[index].Property.SetValue(payload, values[index]); |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | return payload; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public static void ValidateCallbackData(string callbackData, string subject) |
| | | 91 | | { |
| | 23 | 92 | | ArgumentNullException.ThrowIfNull(callbackData); |
| | 23 | 93 | | ArgumentException.ThrowIfNullOrWhiteSpace(subject); |
| | | 94 | | |
| | 23 | 95 | | if (string.IsNullOrWhiteSpace(callbackData)) |
| | | 96 | | { |
| | 0 | 97 | | throw new InvalidOperationException($"{subject} must not be empty."); |
| | | 98 | | } |
| | | 99 | | |
| | 23 | 100 | | if (Encoding.UTF8.GetByteCount(callbackData) > MaxTelegramCallbackDataBytes) |
| | | 101 | | { |
| | 3 | 102 | | throw new InvalidOperationException( |
| | 3 | 103 | | $"{subject} must be at most {MaxTelegramCallbackDataBytes} UTF-8 bytes."); |
| | | 104 | | } |
| | 20 | 105 | | } |
| | | 106 | | |
| | | 107 | | public static bool IsPayloadDeserializationFailure(Exception exception) |
| | | 108 | | { |
| | 2 | 109 | | return exception is JsonException or FormatException or OverflowException; |
| | | 110 | | } |
| | | 111 | | } |