fix: ghost channel when trying to join a channel that doesn't exist

This commit is contained in:
Stone_Red
2026-02-24 21:45:33 +01:00
parent 5391d2cb1c
commit 24a5bdb9a1
3 changed files with 10 additions and 10 deletions
@@ -136,8 +136,10 @@ public sealed class EchoHubConnection : IAsyncDisposable
public async Task<List<MessageDto>> JoinChannelAsync(string channelName) public async Task<List<MessageDto>> JoinChannelAsync(string channelName)
{ {
var messages = await _connection.InvokeAsync<List<MessageDto>>("JoinChannel", channelName); var result = await _connection.InvokeAsync<JoinChannelResult>("JoinChannel", channelName);
return DecryptMessages(messages); if (!result.Success)
throw new InvalidOperationException(result.Error ?? "Failed to join channel.");
return DecryptMessages(result.History);
} }
public async Task LeaveChannelAsync(string channelName) public async Task LeaveChannelAsync(string channelName)
+2
View File
@@ -15,6 +15,8 @@ public record MessageDto(
long? AttachmentFileSize = null, long? AttachmentFileSize = null,
List<EmbedDto>? Embeds = null); List<EmbedDto>? Embeds = null);
public record JoinChannelResult(bool Success, List<MessageDto> History, string? Error = null);
public record ChannelDto( public record ChannelDto(
Guid Id, Guid Id,
string Name, string Name,
+4 -8
View File
@@ -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 try
{ {
@@ -64,19 +64,15 @@ public class ChatHub : Hub<IEchoHubClient>
Context.ConnectionId, CurrentUserId, CurrentUsername, channelName); Context.ConnectionId, CurrentUserId, CurrentUsername, channelName);
if (error is not null) if (error is not null)
{ return new JoinChannelResult(false, [], error);
await Clients.Caller.Error(error);
return [];
}
await Groups.AddToGroupAsync(Context.ConnectionId, channelName.ToLowerInvariant().Trim()); await Groups.AddToGroupAsync(Context.ConnectionId, channelName.ToLowerInvariant().Trim());
return history; return new JoinChannelResult(true, history);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error joining channel '{Channel}' for {User}", channelName, CurrentUsername); _logger.LogError(ex, "Error joining channel '{Channel}' for {User}", channelName, CurrentUsername);
await Clients.Caller.Error($"Failed to join channel: {ex.Message}"); return new JoinChannelResult(false, [], $"Failed to join channel: {ex.Message}");
return [];
} }
} }