Introduce Luna.Identity project with user identity service, pairing code generation, and channel linking. Channels (Telegram, CLI) now verify link status before routing messages and prompt users to pair via the web interface. WebChannel handles /link <channel-type> <code> commands to complete the pairing flow. Fix inverted expiration check in IsPairingCodeExpired that caused codes to be treated as expired immediately after creation.
63 lines
2.2 KiB
C#
63 lines
2.2 KiB
C#
using Luna.Channels.Abstractions;
|
|
using Luna.Core.Abstractions;
|
|
using Luna.Identity.Abstractions;
|
|
using Luna.Shared;
|
|
|
|
namespace Luna.Channels.Cli;
|
|
|
|
public class CliChannel(
|
|
string channelId,
|
|
IChatHubContext hubContext,
|
|
IUserIdentityService identityService)
|
|
: IChannel
|
|
{
|
|
public string ChannelId { get; } = channelId;
|
|
public string ChannelType => Abstractions.ChannelType.Cli;
|
|
public string DisplayName { get; } = "Cli";
|
|
public bool IsConnected { get; }
|
|
|
|
public event ChannelMessageReceivedEventHandler? MessageReceived;
|
|
public event ChannelConnectionChangedEventHandler? ConnectionStateChanged;
|
|
|
|
public async Task<bool> VerifyChannelLinkAsync()
|
|
{
|
|
var currentUser = identityService.GetCurrentUser();
|
|
|
|
if (identityService.IsChannelLinked(currentUser.UserId, ChannelType))
|
|
return true;
|
|
|
|
var pairingCode = identityService.StartChannelLink(currentUser.UserId, ChannelType);
|
|
await SendMessageAsync(new ChannelMessage(
|
|
MessageId: Guid.NewGuid().ToString(),
|
|
ConversationId: Guid.NewGuid().ToString(),
|
|
SenderId: Guid.NewGuid().ToString(),
|
|
SenderName: "luna",
|
|
Content:
|
|
$"{ChannelType} channel is not linked to current user.\nGo to web interface and execute command \"/link [pairing code]\".\nYou will have to provide following PairingCode: {pairingCode}",
|
|
Timestamp: DateTimeOffset.UtcNow));
|
|
|
|
return false;
|
|
}
|
|
|
|
public async Task SendStreamingMessageAsync(IAsyncEnumerable<ChatStreamUpdate> messageStream, CancellationToken cancellationToken = default)
|
|
{
|
|
await hubContext.SendStreamingResponseAsync(ChannelId, messageStream, cancellationToken);
|
|
}
|
|
|
|
public async Task SendMessageAsync(ChannelMessage message, CancellationToken cancellationToken = default)
|
|
{
|
|
await hubContext.SendResponseAsync(ChannelId, message, cancellationToken);
|
|
}
|
|
|
|
public void RaiseMessageReceived(object? sender, ChannelMessage message)
|
|
{
|
|
MessageReceived?.Invoke(sender, new ChannelMessageReceivedEventArgs
|
|
{
|
|
Channel = this,
|
|
Message = message,
|
|
IsHandled = false
|
|
});
|
|
}
|
|
|
|
public void Dispose() { }
|
|
} |