XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
namespace XFEWorkflow.Plugins.WeCom.Native.Core;

/// <summary>
/// An adapter for a separately verified WeCom native Hook or cache reader.
/// Implementations must not capture the screen or drive the client UI.
/// This interface does not implement or provide any WeCom memory offsets.
/// </summary>
public interface IWeComTransport : IAsyncDisposable
{
    // Getting metadata and constructing the adapter must not attach/inject/send.
    ProviderInfo Info { get; }
    ValueTask<ClientSession> ConnectAsync(ClientTarget target, CancellationToken cancellationToken);
    ValueTask<IReadOnlyList<WeComGroup>> GetGroupsAsync(CancellationToken cancellationToken);
    ValueTask<IReadOnlyList<WeComMember>> GetMembersAsync(string conversationId, CancellationToken cancellationToken);

    // Establish a live baseline when called. Never label cache/history replay as live.
    // All events must carry real, stable message IDs, original sender IDs and the
    // native message kind. MergedForward must not be inferred from visible text.
    // Cancellation must stop subscription; disconnection must terminate this stream.
    IAsyncEnumerable<WeComMessage> WatchMessagesAsync(DateTimeOffset listenSinceUtc,
        CancellationToken cancellationToken);

    // Invoke the verified client's internal send path, with no automatic retry.
    // Accepted means the native send operation was accepted, not server delivery.
    // If the outcome is uncertain, return Unknown rather than retrying.
    ValueTask<SendReceipt> SendTextAsync(TextReply reply, CancellationToken cancellationToken);
}

public interface IAttemptStore : IAsyncDisposable
{
    // Must durably reserve before returning true. Includes failed/uncertain attempts.
    ValueTask<bool> TryReserveAsync(AttemptKey key, CancellationToken cancellationToken);
}

public static class TransportValidation
{
    public static void ValidateProvider(ProviderInfo info, string clientVersion)
    {
        if (string.IsNullOrWhiteSpace(info.Name) || info.SupportedVersions is null ||
            !info.SupportedVersions.Contains(clientVersion, StringComparer.Ordinal))
            throw new NotSupportedException($"接口未声明支持本机企业微信 {clientVersion},不能连接。");
        if (info.ReceiveMethod is not (ReceiveMethod.MemoryHook or ReceiveMethod.LocalCache) ||
            info.SendMethod != SendMethod.ClientInternalCall || !info.ProvidesLiveMessageIds)
            throw new NotSupportedException("接口必须提供内存/缓存消息、真实消息 ID 和客户端内部发送能力。");
    }

    public static void ValidateSession(ClientSession session, string clientVersion)
    {
        if (!ReplySettings.ValidId(session.AccountId) || !ReplySettings.ValidId(session.SelfUserId))
            throw new InvalidOperationException("接口没有返回有效的登录账号和自身成员 ID。");
        if (!string.Equals(session.ClientVersion, clientVersion, StringComparison.Ordinal))
            throw new NotSupportedException("接口返回的客户端版本与本机不一致,已停止连接。");
    }
}