< Summary

Information
Class: TeleFlow.Telegram.Internal.EphemeralMessageTargetResolver
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/Internal/EphemeralMessageTargetResolver.cs
Line coverage
96%
Covered lines: 24
Uncovered lines: 1
Coverable lines: 25
Total lines: 75
Line coverage: 96%
Branch coverage
62%
Covered branches: 10
Total branches: 16
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ResolveForMessage(...)50%44100%
ResolveForCallback(...)100%11100%
ResolveForEphemeralMessage(...)50%22100%
ResolveReceiverUserId(...)50%22100%
CreateTarget(...)75%8887.5%

File(s)

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

#LineLine coverage
 1using TeleFlow.Telegram.Schema.Abstractions;
 2using TeleFlow.Telegram.Schema.Constants;
 3using TeleFlow.Telegram.Schema.Types;
 4
 5namespace TeleFlow.Telegram.Internal;
 6
 7/// <summary>
 8/// Derives the Bot API target for ephemeral actions from message and callback updates.
 9/// It keeps Telegram's group-only recipient rules out of the public action classes.
 10/// </summary>
 11internal static class EphemeralMessageTargetResolver
 12{
 13    public static EphemeralMessageTarget ResolveForMessage(Message message)
 14    {
 215        ArgumentNullException.ThrowIfNull(message);
 16
 217        return CreateTarget(
 218            message.Chat,
 219            message.ReceiverUser?.Id ?? message.From?.Id,
 220            "The current message does not identify a user who can receive an ephemeral message.");
 21    }
 22
 23    public static EphemeralMessageTarget ResolveForCallback(
 24        CallbackQuery callbackQuery,
 25        TelegramMessageTarget messageTarget)
 26    {
 127        ArgumentNullException.ThrowIfNull(callbackQuery);
 28
 129        return CreateTarget(
 130            messageTarget.Chat,
 131            callbackQuery.From.Id,
 132            "The current callback query does not identify a user who can receive an ephemeral message.");
 33    }
 34
 35    public static EphemeralMessageTarget ResolveForEphemeralMessage(Message message)
 36    {
 337        ArgumentNullException.ThrowIfNull(message);
 38
 339        return CreateTarget(
 340            message.Chat,
 341            message.ReceiverUser?.Id,
 342            "The current ephemeral message does not identify its receiver.");
 43    }
 44
 45    public static long ResolveReceiverUserId(
 46        CallbackQuery callbackQuery,
 47        TelegramMessageTarget messageTarget)
 48    {
 349        ArgumentNullException.ThrowIfNull(callbackQuery);
 50
 351        return messageTarget.ReceiverUserId ?? callbackQuery.From.Id;
 52    }
 53
 54    private static EphemeralMessageTarget CreateTarget(
 55        Chat chat,
 56        long? receiverUserId,
 57        string missingReceiverMessage)
 58    {
 659        ArgumentNullException.ThrowIfNull(chat);
 60
 661        if (!string.Equals(chat.Type, ChatTypes.Group, StringComparison.Ordinal) &&
 662            !string.Equals(chat.Type, ChatTypes.Supergroup, StringComparison.Ordinal))
 63        {
 164            throw new InvalidOperationException(
 165                "Ephemeral messages can only be sent in a group or supergroup chat.");
 66        }
 67
 568        if (receiverUserId is not long userId || userId <= 0)
 69        {
 070            throw new InvalidOperationException(missingReceiverMessage);
 71        }
 72
 573        return new EphemeralMessageTarget(IntegerString.From(chat.Id), userId);
 74    }
 75}