mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-04 00:26:07 +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)
|
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)
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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 [];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user