mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-04 08:36:11 +02:00
fix: ghost channel when trying to join a channel that doesn't exist
This commit is contained in:
@@ -136,8 +136,10 @@ public sealed class EchoHubConnection : IAsyncDisposable
|
||||
|
||||
public async Task<List<MessageDto>> JoinChannelAsync(string channelName)
|
||||
{
|
||||
var messages = await _connection.InvokeAsync<List<MessageDto>>("JoinChannel", channelName);
|
||||
return DecryptMessages(messages);
|
||||
var result = await _connection.InvokeAsync<JoinChannelResult>("JoinChannel", channelName);
|
||||
if (!result.Success)
|
||||
throw new InvalidOperationException(result.Error ?? "Failed to join channel.");
|
||||
return DecryptMessages(result.History);
|
||||
}
|
||||
|
||||
public async Task LeaveChannelAsync(string channelName)
|
||||
|
||||
@@ -15,6 +15,8 @@ public record MessageDto(
|
||||
long? AttachmentFileSize = null,
|
||||
List<EmbedDto>? Embeds = null);
|
||||
|
||||
public record JoinChannelResult(bool Success, List<MessageDto> History, string? Error = null);
|
||||
|
||||
public record ChannelDto(
|
||||
Guid Id,
|
||||
string Name,
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ChatHub : Hub<IEchoHubClient>
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<MessageDto>> JoinChannel(string channelName)
|
||||
public async Task<JoinChannelResult> JoinChannel(string channelName)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -64,19 +64,15 @@ public class ChatHub : Hub<IEchoHubClient>
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user