diff --git a/src/EchoHub.Client/Services/EchoHubConnection.cs b/src/EchoHub.Client/Services/EchoHubConnection.cs index d59ec6b..c79fccd 100644 --- a/src/EchoHub.Client/Services/EchoHubConnection.cs +++ b/src/EchoHub.Client/Services/EchoHubConnection.cs @@ -136,8 +136,10 @@ public sealed class EchoHubConnection : IAsyncDisposable public async Task> JoinChannelAsync(string channelName) { - var messages = await _connection.InvokeAsync>("JoinChannel", channelName); - return DecryptMessages(messages); + var result = await _connection.InvokeAsync("JoinChannel", channelName); + if (!result.Success) + throw new InvalidOperationException(result.Error ?? "Failed to join channel."); + return DecryptMessages(result.History); } public async Task LeaveChannelAsync(string channelName) diff --git a/src/EchoHub.Core/DTOs/ChatDtos.cs b/src/EchoHub.Core/DTOs/ChatDtos.cs index 32cd71f..6a73e4d 100644 --- a/src/EchoHub.Core/DTOs/ChatDtos.cs +++ b/src/EchoHub.Core/DTOs/ChatDtos.cs @@ -15,6 +15,8 @@ public record MessageDto( long? AttachmentFileSize = null, List? Embeds = null); +public record JoinChannelResult(bool Success, List History, string? Error = null); + public record ChannelDto( Guid Id, string Name, diff --git a/src/EchoHub.Server/Hubs/ChatHub.cs b/src/EchoHub.Server/Hubs/ChatHub.cs index ee8b516..eb73a7a 100644 --- a/src/EchoHub.Server/Hubs/ChatHub.cs +++ b/src/EchoHub.Server/Hubs/ChatHub.cs @@ -56,7 +56,7 @@ public class ChatHub : Hub } } - public async Task> JoinChannel(string channelName) + public async Task JoinChannel(string channelName) { try { @@ -64,19 +64,15 @@ public class ChatHub : Hub Context.ConnectionId, CurrentUserId, CurrentUsername, channelName); if (error is not null) - { - await Clients.Caller.Error(error); - return []; - } + return new JoinChannelResult(false, [], error); await Groups.AddToGroupAsync(Context.ConnectionId, channelName.ToLowerInvariant().Trim()); - return history; + return new JoinChannelResult(true, history); } catch (Exception ex) { _logger.LogError(ex, "Error joining channel '{Channel}' for {User}", channelName, CurrentUsername); - await Clients.Caller.Error($"Failed to join channel: {ex.Message}"); - return []; + return new JoinChannelResult(false, [], $"Failed to join channel: {ex.Message}"); } }