mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-05 23:34:09 +02:00
feat: implement notification sound functionality with user customization options
This commit is contained in:
@@ -21,6 +21,7 @@ public sealed class AppOrchestrator : IDisposable
|
||||
private readonly IApplication _app;
|
||||
private readonly MainWindow _mainWindow;
|
||||
private readonly CommandHandler _commandHandler;
|
||||
private readonly NotificationSoundService _notificationSound;
|
||||
|
||||
private EchoHubConnection? _connection;
|
||||
private ApiClient? _apiClient;
|
||||
@@ -41,6 +42,7 @@ public sealed class AppOrchestrator : IDisposable
|
||||
_config = config;
|
||||
_mainWindow = new MainWindow(app);
|
||||
_commandHandler = new CommandHandler();
|
||||
_notificationSound = new NotificationSoundService(config.Notifications);
|
||||
|
||||
WireMainWindowEvents();
|
||||
WireCommandHandlerEvents();
|
||||
@@ -572,7 +574,9 @@ public sealed class AppOrchestrator : IDisposable
|
||||
var editResult = ProfileEditDialog.Show(_app,
|
||||
currentProfile?.DisplayName,
|
||||
currentProfile?.Bio,
|
||||
currentProfile?.NicknameColor);
|
||||
currentProfile?.NicknameColor,
|
||||
_config.Notifications.Enabled,
|
||||
_config.Notifications.Volume);
|
||||
|
||||
if (editResult is null) return;
|
||||
|
||||
@@ -633,6 +637,18 @@ public sealed class AppOrchestrator : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
if (editResult.NotificationSoundEnabled.HasValue)
|
||||
{
|
||||
_config.Notifications.Enabled = editResult.NotificationSoundEnabled.Value;
|
||||
_notificationSound.SetEnabled(editResult.NotificationSoundEnabled.Value);
|
||||
}
|
||||
|
||||
if (editResult.NotificationVolume.HasValue)
|
||||
{
|
||||
_config.Notifications.Volume = editResult.NotificationVolume.Value;
|
||||
_notificationSound.SetVolume(editResult.NotificationVolume.Value);
|
||||
}
|
||||
|
||||
_config.DefaultPreset = new AccountPreset
|
||||
{
|
||||
DisplayName = editResult.DisplayName,
|
||||
@@ -768,8 +784,17 @@ public sealed class AppOrchestrator : IDisposable
|
||||
private void WireConnectionEvents(EchoHubConnection connection)
|
||||
{
|
||||
connection.OnMessageReceived += message =>
|
||||
{
|
||||
InvokeUI(() => _mainWindow.AddMessage(message));
|
||||
|
||||
if (!string.IsNullOrEmpty(_currentUsername)
|
||||
&& !message.SenderUsername.Equals(_currentUsername, StringComparison.OrdinalIgnoreCase)
|
||||
&& message.Content.Contains($"@{_currentUsername}", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_ = _notificationSound.PlayAsync();
|
||||
}
|
||||
};
|
||||
|
||||
connection.OnUserJoined += (channelName, username) =>
|
||||
{
|
||||
InvokeUI(() => _mainWindow.AddSystemMessage(channelName, $"{username} joined the channel"));
|
||||
|
||||
@@ -10,7 +10,8 @@ public class ClientConfig
|
||||
|
||||
public class NotificationConfig
|
||||
{
|
||||
public bool Enabled { get; set; } = true;
|
||||
public bool Enabled { get; set; } = false;
|
||||
public byte Volume { get; set; } = 30;
|
||||
public string? SoundFile { get; set; }
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,10 @@ public class NotificationSoundService
|
||||
ResolveSoundPath();
|
||||
}
|
||||
|
||||
public void SetEnabled(bool enabled) => _config.Enabled = enabled;
|
||||
|
||||
public void SetVolume(byte volume) => _config.Volume = Math.Min(volume, (byte)100);
|
||||
|
||||
public async Task PlayAsync()
|
||||
{
|
||||
if (!_config.Enabled || _resolvedSoundPath is null)
|
||||
@@ -26,6 +30,7 @@ public class NotificationSoundService
|
||||
if (_player.Playing)
|
||||
await _player.Stop();
|
||||
|
||||
await _player.SetVolume(_config.Volume);
|
||||
await _player.Play(_resolvedSoundPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace EchoHub.Client.UI;
|
||||
/// <summary>
|
||||
/// Result returned from the profile edit dialog.
|
||||
/// </summary>
|
||||
public record ProfileEditResult(string? DisplayName, string? Bio, string? NicknameColor, string? AvatarPath);
|
||||
public record ProfileEditResult(string? DisplayName, string? Bio, string? NicknameColor, string? AvatarPath, bool? NotificationSoundEnabled, byte? NotificationVolume);
|
||||
|
||||
/// <summary>
|
||||
/// A Terminal.Gui dialog for editing the user's profile (display name, bio, nickname color).
|
||||
@@ -19,11 +19,11 @@ public sealed class ProfileEditDialog
|
||||
/// <summary>
|
||||
/// Shows the profile edit dialog and returns the result, or null if cancelled.
|
||||
/// </summary>
|
||||
public static ProfileEditResult? Show(IApplication app, string? currentDisplayName, string? currentBio, string? currentColor)
|
||||
public static ProfileEditResult? Show(IApplication app, string? currentDisplayName, string? currentBio, string? currentColor, bool notificationSoundEnabled = false, byte notificationVolume = 30)
|
||||
{
|
||||
ProfileEditResult? result = null;
|
||||
|
||||
var dialog = new Dialog { Title = "Edit Profile", Width = 60, Height = 22 };
|
||||
var dialog = new Dialog { Title = "Edit Profile", Width = 60, Height = 26 };
|
||||
|
||||
// Display Name
|
||||
var nameLabel = new Label
|
||||
@@ -148,20 +148,53 @@ public sealed class ProfileEditDialog
|
||||
}
|
||||
};
|
||||
|
||||
// Notification Sound
|
||||
var notifCheckbox = new CheckBox
|
||||
{
|
||||
Text = "Notification sound on @mention",
|
||||
X = 1,
|
||||
Y = 13,
|
||||
Value = notificationSoundEnabled ? CheckState.Checked : CheckState.UnChecked
|
||||
};
|
||||
|
||||
var volumeLabel = new Label
|
||||
{
|
||||
Text = "Volume:",
|
||||
X = 1,
|
||||
Y = 15
|
||||
};
|
||||
var volumeField = new TextField
|
||||
{
|
||||
Text = notificationVolume.ToString(),
|
||||
X = 17,
|
||||
Y = 15,
|
||||
Width = 6
|
||||
};
|
||||
var volumeHintLabel = new Label
|
||||
{
|
||||
Text = "(0-100)",
|
||||
X = 24,
|
||||
Y = 15
|
||||
};
|
||||
volumeHintLabel.SetScheme(new Scheme
|
||||
{
|
||||
Normal = new Attribute(Color.DarkGray, Color.Blue)
|
||||
});
|
||||
|
||||
// Buttons
|
||||
var saveButton = new Button
|
||||
{
|
||||
Text = "Save",
|
||||
IsDefault = true,
|
||||
X = Pos.Center() - 10,
|
||||
Y = 14
|
||||
Y = 18
|
||||
};
|
||||
|
||||
var cancelButton = new Button
|
||||
{
|
||||
Text = "Cancel",
|
||||
X = Pos.Center() + 5,
|
||||
Y = 14
|
||||
Y = 18
|
||||
};
|
||||
|
||||
saveButton.Accepting += (s, e) =>
|
||||
@@ -171,7 +204,8 @@ public sealed class ProfileEditDialog
|
||||
var nicknameColor = NullIfEmpty(colorField.Text?.Trim());
|
||||
var avatarPath = NullIfEmpty(avatarField.Text?.Trim());
|
||||
|
||||
result = new ProfileEditResult(displayName, bio, nicknameColor, avatarPath);
|
||||
byte? volume = byte.TryParse(volumeField.Text, out var v) ? Math.Min(v, (byte)100) : null;
|
||||
result = new ProfileEditResult(displayName, bio, nicknameColor, avatarPath, notifCheckbox.Value == CheckState.Checked, volume);
|
||||
e.Handled = true;
|
||||
app.RequestStop();
|
||||
};
|
||||
@@ -186,6 +220,7 @@ public sealed class ProfileEditDialog
|
||||
dialog.Add(nameLabel, nameField, bioLabel, bioField, colorLabel, colorField,
|
||||
colorHintLabel, previewLabel, colorPreview,
|
||||
avatarLabel, avatarField, browseButton, avatarHintLabel,
|
||||
notifCheckbox, volumeLabel, volumeField, volumeHintLabel,
|
||||
saveButton, cancelButton);
|
||||
|
||||
nameField.SetFocus();
|
||||
|
||||
@@ -14,4 +14,5 @@ public interface IChatBroadcaster
|
||||
Task SendMessageDeletedAsync(string channelName, Guid messageId);
|
||||
Task SendChannelNukedAsync(string channelName);
|
||||
Task SendErrorAsync(string connectionId, string message);
|
||||
Task ForceDisconnectUserAsync(List<string> connectionIds, string reason);
|
||||
}
|
||||
|
||||
@@ -16,5 +16,6 @@ public interface IEchoHubClient
|
||||
Task UserBanned(string username, string? reason);
|
||||
Task MessageDeleted(string channelName, Guid messageId);
|
||||
Task ChannelNuked(string channelName);
|
||||
Task ForceDisconnect(string reason);
|
||||
Task Error(string message);
|
||||
}
|
||||
|
||||
@@ -106,4 +106,22 @@ public class IrcBroadcaster : IChatBroadcaster
|
||||
await conn.SendAsync($":{_gateway.Options.ServerName} NOTICE {conn.Nickname ?? "*"} :{message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ForceDisconnectUserAsync(List<string> connectionIds, string reason)
|
||||
{
|
||||
foreach (var connId in connectionIds)
|
||||
{
|
||||
if (!connId.StartsWith("irc-")) continue;
|
||||
|
||||
if (_gateway.Connections.TryGetValue(connId, out var conn))
|
||||
{
|
||||
try
|
||||
{
|
||||
await conn.SendAsync($"ERROR :Closing Link: {reason}");
|
||||
await conn.DisposeAsync();
|
||||
}
|
||||
catch { /* connection may already be closed */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,13 +72,17 @@ public class ModerationController : ControllerBase
|
||||
if (target.Role >= caller!.Role)
|
||||
return BadRequest(new ErrorResponse("Cannot kick a user with equal or higher role."));
|
||||
|
||||
// Broadcast kick to all channels the user is in
|
||||
// Broadcast kick to all channels the user is in, then clean up presence
|
||||
var channels = _presenceTracker.GetChannelsForUser(target.Username);
|
||||
foreach (var channel in channels)
|
||||
{
|
||||
await BroadcastToAllAsync(b => b.SendUserKickedAsync(channel, target.Username, request?.Reason));
|
||||
}
|
||||
|
||||
// Remove from presence tracker and force disconnect all connections
|
||||
var reason = request?.Reason ?? "You have been kicked from the server.";
|
||||
await ForceDisconnectAndCleanupAsync(target.Username, reason);
|
||||
|
||||
return Ok(new { Message = $"{target.Username} has been kicked." });
|
||||
}
|
||||
|
||||
@@ -98,8 +102,12 @@ public class ModerationController : ControllerBase
|
||||
target.IsBanned = true;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
// Broadcast ban notification, then force disconnect
|
||||
await BroadcastToAllAsync(b => b.SendUserBannedAsync(target.Username, request?.Reason));
|
||||
|
||||
var reason = request?.Reason ?? "You have been banned from this server.";
|
||||
await ForceDisconnectAndCleanupAsync(target.Username, reason);
|
||||
|
||||
return Ok(new { Message = $"{target.Username} has been banned." });
|
||||
}
|
||||
|
||||
@@ -217,6 +225,36 @@ public class ModerationController : ControllerBase
|
||||
return (caller, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove user from presence tracking, broadcast their departure from all channels,
|
||||
/// send a ForceDisconnect signal, and update their DB status.
|
||||
/// </summary>
|
||||
private async Task ForceDisconnectAndCleanupAsync(string username, string reason)
|
||||
{
|
||||
var (connectionIds, channels) = _presenceTracker.ForceRemoveUser(username);
|
||||
|
||||
// Notify remaining users that this person left each channel
|
||||
foreach (var channel in channels)
|
||||
{
|
||||
await BroadcastToAllAsync(b => b.SendUserLeftAsync(channel, username));
|
||||
}
|
||||
|
||||
// Signal the user's clients to disconnect
|
||||
if (connectionIds.Count > 0)
|
||||
{
|
||||
await BroadcastToAllAsync(b => b.ForceDisconnectUserAsync(connectionIds, reason));
|
||||
}
|
||||
|
||||
// Mark user offline in DB
|
||||
var user = await _db.Users.FirstOrDefaultAsync(u => u.Username == username);
|
||||
if (user is not null)
|
||||
{
|
||||
user.Status = UserStatus.Invisible;
|
||||
user.LastSeenAt = DateTimeOffset.UtcNow;
|
||||
await _db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task BroadcastToAllAsync(Func<IChatBroadcaster, Task> action)
|
||||
{
|
||||
foreach (var broadcaster in _broadcasters)
|
||||
|
||||
@@ -151,4 +151,27 @@ public class PresenceTracker
|
||||
{
|
||||
return _userConnections.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Forcibly remove a user from all tracking. Returns their connection IDs and channels
|
||||
/// so the caller can broadcast departures and force-disconnect connections.
|
||||
/// </summary>
|
||||
public (List<string> ConnectionIds, List<string> Channels) ForceRemoveUser(string username)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
var channels = _userChannels.TryRemove(username, out var ch)
|
||||
? ch.ToList()
|
||||
: [];
|
||||
|
||||
var connectionIds = _userConnections.TryRemove(username, out var conns)
|
||||
? conns.ToList()
|
||||
: [];
|
||||
|
||||
foreach (var connId in connectionIds)
|
||||
_connections.TryRemove(connId, out _);
|
||||
|
||||
return (connectionIds, channels);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,4 +73,13 @@ public class SignalRBroadcaster : IChatBroadcaster
|
||||
|
||||
return HubContext.Clients.Client(connectionId).Error(message);
|
||||
}
|
||||
|
||||
public Task ForceDisconnectUserAsync(List<string> connectionIds, string reason)
|
||||
{
|
||||
var signalRIds = connectionIds.Where(c => !c.StartsWith("irc-")).ToList();
|
||||
if (signalRIds.Count == 0)
|
||||
return Task.CompletedTask;
|
||||
|
||||
return HubContext.Clients.Clients(signalRIds).ForceDisconnect(reason);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user