| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using TeleFlow.Annotations; |
| | | 3 | | using TeleFlow.Telegram.Internal; |
| | | 4 | | using TeleFlow.Telegram.Schema.Types; |
| | | 5 | | |
| | | 6 | | namespace TeleFlow.Telegram.Internal.Handlers; |
| | | 7 | | |
| | | 8 | | internal static class TelegramFilterEvaluator |
| | | 9 | | { |
| | | 10 | | public static async ValueTask<bool> MatchesAsync( |
| | | 11 | | TelegramUpdateContext context, |
| | | 12 | | TelegramRouteFilterPlan filterPlan, |
| | | 13 | | CancellationToken cancellationToken) |
| | | 14 | | { |
| | 551 | 15 | | ArgumentNullException.ThrowIfNull(filterPlan); |
| | | 16 | | |
| | 551 | 17 | | var builtInFilters = filterPlan.BuiltInFilters; |
| | 551 | 18 | | var facts = builtInFilters.Count > 0 |
| | 551 | 19 | | ? TelegramFilterContextFacts.From(context) |
| | 551 | 20 | | : default; |
| | | 21 | | |
| | 1354 | 22 | | for (var index = 0; index < builtInFilters.Count; index++) |
| | | 23 | | { |
| | 330 | 24 | | var filter = builtInFilters[index]; |
| | | 25 | | |
| | 330 | 26 | | if (!MatchesBuiltInFilter(context, facts, filter)) |
| | | 27 | | { |
| | 204 | 28 | | return false; |
| | | 29 | | } |
| | | 30 | | } |
| | | 31 | | |
| | 347 | 32 | | var customFilters = filterPlan.CustomFilters; |
| | | 33 | | |
| | 708 | 34 | | for (var index = 0; index < customFilters.Count; index++) |
| | | 35 | | { |
| | 29 | 36 | | var customFilter = customFilters[index]; |
| | | 37 | | |
| | 29 | 38 | | if (!await customFilter.MatchesAsync(context, cancellationToken).ConfigureAwait(false)) |
| | | 39 | | { |
| | 20 | 40 | | return false; |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | 325 | 44 | | if (!await MatchesRoleRequirementsAsync(context, filterPlan.RoleRequirements, cancellationToken).ConfigureAwait( |
| | | 45 | | { |
| | 3 | 46 | | return false; |
| | | 47 | | } |
| | | 48 | | |
| | 322 | 49 | | return true; |
| | 549 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static bool MatchesBuiltInFilter( |
| | | 53 | | TelegramUpdateContext context, |
| | | 54 | | TelegramFilterContextFacts facts, |
| | | 55 | | TelegramFilterDescriptor filter) |
| | | 56 | | { |
| | 330 | 57 | | return filter.Kind switch |
| | 330 | 58 | | { |
| | 38 | 59 | | TelegramFilterKind.ChatType => facts.DestinationChat is not null && |
| | 38 | 60 | | ContainsString( |
| | 38 | 61 | | filter.StringValues, |
| | 38 | 62 | | facts.DestinationChat.Type, |
| | 38 | 63 | | StringComparison.Ordinal), |
| | 20 | 64 | | TelegramFilterKind.ChatId => facts.DestinationChat is not null && |
| | 20 | 65 | | ContainsLong(filter.LongValues, facts.DestinationChat.Id), |
| | 16 | 66 | | TelegramFilterKind.ChatUsername => facts.DestinationChat?.Username is { } username && |
| | 16 | 67 | | ContainsString( |
| | 16 | 68 | | filter.StringValues, |
| | 16 | 69 | | username, |
| | 16 | 70 | | StringComparison.OrdinalIgnoreCase), |
| | 20 | 71 | | TelegramFilterKind.FromUser => facts.SenderUser is { IsBot: false } user && |
| | 20 | 72 | | MatchesOptionalId(filter.LongValues, user.Id), |
| | 11 | 73 | | TelegramFilterKind.FromBot => facts.SenderUser is { IsBot: true } bot && |
| | 11 | 74 | | MatchesOptionalId(filter.LongValues, bot.Id), |
| | 3 | 75 | | TelegramFilterKind.FromPremiumUser => facts.SenderUser?.IsPremium == true, |
| | 7 | 76 | | TelegramFilterKind.SenderChatType => facts.SenderChat is not null && |
| | 7 | 77 | | ContainsString( |
| | 7 | 78 | | filter.StringValues, |
| | 7 | 79 | | facts.SenderChat.Type, |
| | 7 | 80 | | StringComparison.Ordinal), |
| | 8 | 81 | | TelegramFilterKind.MessageThreadId => facts.MessageThreadId is { } messageThreadId && |
| | 8 | 82 | | ContainsLong(filter.LongValues, messageThreadId), |
| | 12 | 83 | | TelegramFilterKind.HasMessageThread => facts.MessageThreadId is not null, |
| | 13 | 84 | | TelegramFilterKind.HasCallbackData => context is CallbackQueryContext callbackContext && |
| | 13 | 85 | | !string.IsNullOrWhiteSpace(callbackContext.TelegramCallbackQuery.Data) |
| | 45 | 86 | | TelegramFilterKind.CallbackDataPrefix => context is CallbackQueryContext callbackContext && |
| | 45 | 87 | | callbackContext.TelegramCallbackQuery.Data is { } data && |
| | 45 | 88 | | StartsWithAnyPrefix(data, filter.StringValues), |
| | 137 | 89 | | _ => context is MessageContext messageContext && |
| | 137 | 90 | | MatchesMessageBuiltInFilter(messageContext.TelegramMessage, filter) |
| | 330 | 91 | | }; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | private static bool MatchesMessageBuiltInFilter( |
| | | 95 | | Message message, |
| | | 96 | | TelegramFilterDescriptor filter) |
| | | 97 | | { |
| | 137 | 98 | | return filter.Kind switch |
| | 137 | 99 | | { |
| | 21 | 100 | | TelegramFilterKind.HasText => !string.IsNullOrWhiteSpace(message.Text), |
| | 3 | 101 | | TelegramFilterKind.HasPhoto => message.Photo is { Count: > 0 }, |
| | 2 | 102 | | TelegramFilterKind.HasDocument => message.Document is not null, |
| | 29 | 103 | | TelegramFilterKind.HasCaption => !string.IsNullOrWhiteSpace(message.Caption), |
| | 12 | 104 | | TelegramFilterKind.HasVideo => message.Video is not null, |
| | 11 | 105 | | TelegramFilterKind.HasAnimation => message.Animation is not null, |
| | 10 | 106 | | TelegramFilterKind.HasAudio => message.Audio is not null, |
| | 9 | 107 | | TelegramFilterKind.HasVoice => message.Voice is not null, |
| | 8 | 108 | | TelegramFilterKind.HasVideoNote => message.VideoNote is not null, |
| | 7 | 109 | | TelegramFilterKind.HasSticker => message.Sticker is not null, |
| | 6 | 110 | | TelegramFilterKind.HasContact => message.Contact is not null, |
| | 5 | 111 | | TelegramFilterKind.HasLocation => message.Location is not null, |
| | 4 | 112 | | TelegramFilterKind.HasVenue => message.Venue is not null, |
| | 3 | 113 | | TelegramFilterKind.HasPoll => message.Poll is not null, |
| | 2 | 114 | | TelegramFilterKind.HasDice => message.Dice is not null, |
| | 2 | 115 | | TelegramFilterKind.IsReply => message.ReplyToMessage is not null, |
| | 3 | 116 | | TelegramFilterKind.ReplyToBot => message.ReplyToMessage?.From?.IsBot == true, |
| | 0 | 117 | | _ => false |
| | 137 | 118 | | }; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | private static async ValueTask<bool> MatchesRoleRequirementsAsync( |
| | | 122 | | TelegramUpdateContext context, |
| | | 123 | | IReadOnlyList<TelegramRoleRequirementDescriptor> roleRequirements, |
| | | 124 | | CancellationToken cancellationToken) |
| | | 125 | | { |
| | 325 | 126 | | if (roleRequirements.Count == 0) |
| | | 127 | | { |
| | 312 | 128 | | return true; |
| | | 129 | | } |
| | | 130 | | |
| | 13 | 131 | | if (!TelegramRoleFilterIdentityResolver.TryResolve(context, out var identity)) |
| | | 132 | | { |
| | 1 | 133 | | return false; |
| | | 134 | | } |
| | | 135 | | |
| | 12 | 136 | | var options = context.Services.GetRequiredService<TelegramRoleFilterOptions>(); |
| | 12 | 137 | | var status = await ResolveStatusAsync(context, identity, options, cancellationToken).ConfigureAwait(false); |
| | | 138 | | |
| | 12 | 139 | | if (status is null) |
| | | 140 | | { |
| | 0 | 141 | | return false; |
| | | 142 | | } |
| | | 143 | | |
| | 12 | 144 | | if (!TelegramMemberStatusSetValidator.IsValid(status.Value)) |
| | | 145 | | { |
| | 0 | 146 | | throw new InvalidOperationException("Telegram role resolver returned an invalid member status set."); |
| | | 147 | | } |
| | | 148 | | |
| | 50 | 149 | | foreach (var requirement in roleRequirements) |
| | | 150 | | { |
| | 14 | 151 | | if ((status.Value & requirement.AllowedStatuses) == 0) |
| | | 152 | | { |
| | 2 | 153 | | return false; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | 10 | 157 | | return true; |
| | 325 | 158 | | } |
| | | 159 | | |
| | | 160 | | private static async ValueTask<TelegramMemberStatusSet?> ResolveStatusAsync( |
| | | 161 | | TelegramUpdateContext context, |
| | | 162 | | TelegramRoleFilterIdentity identity, |
| | | 163 | | TelegramRoleFilterOptions options, |
| | | 164 | | CancellationToken cancellationToken) |
| | | 165 | | { |
| | 12 | 166 | | if (options.CacheEnabled) |
| | | 167 | | { |
| | 10 | 168 | | var cache = context.Services.GetRequiredService<ITelegramChatMemberStatusCache>(); |
| | 10 | 169 | | var cachedStatus = await cache.GetAsync( |
| | 10 | 170 | | identity.ChatId, |
| | 10 | 171 | | identity.UserId, |
| | 10 | 172 | | cancellationToken).ConfigureAwait(false); |
| | | 173 | | |
| | 10 | 174 | | if (cachedStatus is not null) |
| | | 175 | | { |
| | 1 | 176 | | return cachedStatus; |
| | | 177 | | } |
| | | 178 | | |
| | 9 | 179 | | var resolvedStatus = await ResolveUncachedStatusAsync(context, identity, cancellationToken).ConfigureAwait(f |
| | | 180 | | |
| | 9 | 181 | | if (resolvedStatus is not null) |
| | | 182 | | { |
| | 9 | 183 | | await cache.SetAsync( |
| | 9 | 184 | | identity.ChatId, |
| | 9 | 185 | | identity.UserId, |
| | 9 | 186 | | resolvedStatus.Value, |
| | 9 | 187 | | options.CacheTtl, |
| | 9 | 188 | | cancellationToken).ConfigureAwait(false); |
| | | 189 | | } |
| | | 190 | | |
| | 9 | 191 | | return resolvedStatus; |
| | | 192 | | } |
| | | 193 | | |
| | 2 | 194 | | return await ResolveUncachedStatusAsync(context, identity, cancellationToken).ConfigureAwait(false); |
| | 12 | 195 | | } |
| | | 196 | | |
| | | 197 | | private static bool MatchesOptionalId( |
| | | 198 | | IReadOnlyList<long> ids, |
| | | 199 | | long id) |
| | | 200 | | { |
| | 19 | 201 | | return ids.Count == 0 || ContainsLong(ids, id); |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | private static bool ContainsString( |
| | | 205 | | IReadOnlyList<string> values, |
| | | 206 | | string value, |
| | | 207 | | StringComparison comparison) |
| | | 208 | | { |
| | 96 | 209 | | for (var index = 0; index < values.Count; index++) |
| | | 210 | | { |
| | 39 | 211 | | if (string.Equals(values[index], value, comparison)) |
| | | 212 | | { |
| | 30 | 213 | | return true; |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | |
| | 9 | 217 | | return false; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | private static bool ContainsLong( |
| | | 221 | | IReadOnlyList<long> values, |
| | | 222 | | long value) |
| | | 223 | | { |
| | 70 | 224 | | for (var index = 0; index < values.Count; index++) |
| | | 225 | | { |
| | 27 | 226 | | if (values[index] == value) |
| | | 227 | | { |
| | 19 | 228 | | return true; |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | |
| | 8 | 232 | | return false; |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | private static bool StartsWithAnyPrefix( |
| | | 236 | | string value, |
| | | 237 | | IReadOnlyList<string> prefixes) |
| | | 238 | | { |
| | 144 | 239 | | for (var index = 0; index < prefixes.Count; index++) |
| | | 240 | | { |
| | 44 | 241 | | if (value.StartsWith(prefixes[index], StringComparison.Ordinal)) |
| | | 242 | | { |
| | 16 | 243 | | return true; |
| | | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | 28 | 247 | | return false; |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | private static ValueTask<TelegramMemberStatusSet?> ResolveUncachedStatusAsync( |
| | | 251 | | TelegramUpdateContext context, |
| | | 252 | | TelegramRoleFilterIdentity identity, |
| | | 253 | | CancellationToken cancellationToken) |
| | | 254 | | { |
| | 11 | 255 | | var resolver = context.Services.GetRequiredService<ITelegramChatMemberStatusResolver>(); |
| | 11 | 256 | | return resolver.ResolveAsync( |
| | 11 | 257 | | context, |
| | 11 | 258 | | identity.ChatId, |
| | 11 | 259 | | identity.UserId, |
| | 11 | 260 | | cancellationToken); |
| | | 261 | | } |
| | | 262 | | } |