V18 / 8d896b8a86e5
MessageOperations.cs
Lilith/Core/Messaging/MessageOperations.cs
Unchanged from the preceding versionCS
using System.Text.Json;
using Lilith.Core.Workspace;
namespace Lilith.Core.Messaging;
internal static class MessageOperations
{
internal static readonly HashSet<string> Names = ["message_channels_operation", "message_check_operation", "message_status_operation", "message_claim_operation", "message_send_operation"];
internal static bool NeedsApproval(string? receipt)
{
if (receipt is null) return false;
using var doc = JsonDocument.Parse(receipt);
return doc.RootElement.TryGetProperty("status", out var value) && value.GetString() == "needs_approval";
}
internal static async Task<string> ExecuteAsync(ProviderClient provider, string operation, IReadOnlyDictionary<string, string> inputs, CancellationToken ct)
{
string Get(string key) => inputs.GetValueOrDefault(key) ?? "";
string nickname = Get("nickname");
switch (operation)
{
case "message_channels_operation": return await MessageBridgeClient.RequestAsync("channels", new { key = Get("after") }, ct);
case "message_check_operation": return await MessageBridgeClient.RequestAsync("inbox", new { nickname }, ct);
case "message_status_operation": return await MessageBridgeClient.RequestAsync("status", new { nickname }, ct);
case "message_claim_operation": return await MessageBridgeClient.RequestAsync("claim", new { nickname, messageId = Get("message_id") }, ct);
case "message_send_operation":
var workspace = provider.Workspace ?? throw new InvalidOperationException("Configure the boot-fixed access mode first.");
string prepared = await MessageBridgeClient.RequestAsync("prepare", new { nickname, room = Get("room"), text = Get("text"), replyTo = Get("reply_to"), key = Guid.NewGuid().ToString("N") }, ct);
using (var doc = JsonDocument.Parse(prepared))
{
var proposal = doc.RootElement;
string id = proposal.GetProperty("id").GetString()!;
if (workspace.Options.Mode == WorkspaceAccessMode.Yolo)
return await MessageBridgeClient.RequestAsync("send", new { nickname, proposalId = id, signature = proposal.GetProperty("signature").GetString() }, ct);
provider.PendingMessageApproval = true;
return JsonSerializer.Serialize(new { status = "needs_approval", source = "dummy", nickname, proposal = proposal.Clone(), instruction = "Review the exact destination and text. Nothing sent. Enter /message-approve " + nickname + " " + id + " to send, or /message-reject " + nickname + " " + id + "." });
}
default: throw new ArgumentException("Unknown messaging operation.");
}
}
}