Files
wx-win-agent/node-agent/WxAgent.Core/WechatManagement.cs
T

62 lines
2.4 KiB
C#

namespace WxAgent.Core;
public sealed record WechatContactSnapshot(
string DisplayName,
string? WechatId = null,
IReadOnlyList<string>? Tags = null,
string? Signature = null,
string? Source = null,
int? CommonGroupCount = null,
string? Username = null);
public sealed record WechatNewFriendSnapshot(string DisplayName, string Status);
public sealed record WechatGroupMemberSnapshot(string DisplayName, string? GroupNickname = null, bool IsOwner = false);
public sealed record WechatSubWindowSnapshot(string Title, string Kind, int ProcessId);
public sealed record WechatAccountSnapshot(string DisplayName, string? WechatId = null, string? Region = null);
public sealed record WechatOperationResult(bool Success, WxAgentErrorCode? Code, string Message)
{
public static WechatOperationResult Ok(string message = "ok") => new(true, null, message);
public static WechatOperationResult Unconfirmed(string operation) => new(false, WxAgentErrorCode.ResultUnconfirmed,
$"{operation} was submitted but its business result was not verified. Do not retry automatically.");
}
public static class WechatOperationPolicy
{
public const string Confirmation = "CONFIRM";
public static void RequireConfirmation(string? confirmation, string operation)
{
if (!string.Equals(confirmation, Confirmation, StringComparison.Ordinal))
{
throw new WxAgentException(
WxAgentErrorCode.InvalidArgument,
$"{operation} requires explicit confirmation '{Confirmation}'.");
}
}
public static void ValidateNames(IEnumerable<string> names, string parameterName)
{
ArgumentNullException.ThrowIfNull(names);
var values = names.ToArray();
if (values.Length == 0 || values.Any(string.IsNullOrWhiteSpace) ||
values.Distinct(StringComparer.Ordinal).Count() != values.Length)
{
throw new WxAgentException(WxAgentErrorCode.InvalidArgument, $"{parameterName} must contain unique, non-empty names.");
}
}
public static T RequireUnique<T>(IEnumerable<T> candidates, string controlDescription)
{
var matches = candidates.Take(2).ToArray();
if (matches.Length != 1)
throw new WxAgentException(WxAgentErrorCode.ControlNotFound,
$"A unique {controlDescription} was not found; no target was selected.");
return matches[0];
}
}