From 9e4f9d55ea356bfe97d04009b348059bcbb8cf3d Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 16 Nov 2026 01:16:08 +0000 Subject: [PATCH] Enforce chat contact preferences Filter video calls and private messages per connection and prepare mode-aware call signaling. Audio-only media remains unimplemented. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 2 + doc/architecture/audio-video-calls.md | 7 +- .../PostIt/Views/Layout/SettingsPage.axaml | 2 +- src/Yavsc.Server/Hubs/ChatHub.Calls.cs | 4 +- src/Yavsc.Server/Hubs/ChatHub.cs | 14 ++-- .../Services/CallSignalingService.cs | 62 +++++++++++++++-- src/test/Yavsc.Server.Tests/CallHubTests.cs | 66 +++++++++++++++++-- .../Yavsc.Server.Tests/CallSignalingTests.cs | 23 +++++++ 8 files changed, 156 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d8716342..10da53fa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### Added +* [PostIt/Chat] Filter video invitations and private messages by announced per-connection preferences, stop affected calls when preferences change, and prepare mode-aware audio signaling. Audio-only calls and static images are not implemented yet; legacy clients without preferences remain reachable. + * [PostIt/Chat] Persist audio, audio-video and private-message contact preferences and announce them to ChatHub on connection and reconnection. Add the audio-only call display contract; filtering contacts and audio-only media are not implemented yet. ### Fixed diff --git a/doc/architecture/audio-video-calls.md b/doc/architecture/audio-video-calls.md index 66ba850e3..7c62af27a 100644 --- a/doc/architecture/audio-video-calls.md +++ b/doc/architecture/audio-video-calls.md @@ -7,8 +7,11 @@ l'historique du transport direct. La préférence de confidentialité est persistée et éditable dans le profil. PostIt conserve aussi trois activations de contact locales (audio, audio-video, messages prives) et les annonce pour chaque connexion ChatHub, y compris apres -reconnexion. Ces activations ne filtrent pas encore les appels ni les messages : -elles ne constituent pas encore une protection de la vie privee. +reconnexion. Les modifications prennent effet a la prochaine connexion. Le +serveur filtre les invitations video et les messages prives par appareil ; +une autre connexion peut avoir des preferences differentes et les anciens +clients sans annonce restent autorises. Le mode audio seul n'est pas encore +implemente. La signalisation est intégrée au chat, désactivée par défaut. Une page d'appel separee propose **WebRTC natif** et le transport direct VLC/SRTP sur Android et Desktop Linux. WebRTC utilise ICE/STUN/TURN et le masquage IP impose par la diff --git a/src/PostIt/PostIt/Views/Layout/SettingsPage.axaml b/src/PostIt/PostIt/Views/Layout/SettingsPage.axaml index fcea45e48..510315c76 100644 --- a/src/PostIt/PostIt/Views/Layout/SettingsPage.axaml +++ b/src/PostIt/PostIt/Views/Layout/SettingsPage.axaml @@ -56,7 +56,7 @@ IsChecked="{Binding AllowVideoCalls, Mode=TwoWay}" /> - InviteCall(string roomName, string callee) => diff --git a/src/Yavsc.Server/Hubs/ChatHub.cs b/src/Yavsc.Server/Hubs/ChatHub.cs index d548354cd..8034c3eb8 100644 --- a/src/Yavsc.Server/Hubs/ChatHub.cs +++ b/src/Yavsc.Server/Hubs/ChatHub.cs @@ -365,17 +365,11 @@ namespace Yavsc.Server.Hubs } _logger.LogInformation("getting cx id´s"); - var cxIds = _cxManager.GetConnexionIds(userName); - if (cxIds == null || cxIds.Count() == 0) + var cxIds = _cxManager.GetConnexionIds(userName)?.Distinct().ToArray() ?? []; + if (cxIds.Length == 0) _logger.LogError($"No such connected user : {userName}"); - else foreach (var connectionId in cxIds) - { - _logger.LogInformation($"cx: {connectionId}"); - var cli = Clients.Client(connectionId); - _logger.LogInformation($"cli: {cli.ToString()}"); - await cli.SendAsync("addPV", identityUserName, message); - _logger.LogInformation($"Sent pv to cx {connectionId}"); - } + else + await _calls.SendPrivateMessageAsync(Context.ConnectionId, identityUserName, cxIds, message); } public async Task SendStream(string connectionId, long streamId, string message) diff --git a/src/Yavsc.Server/Services/CallSignalingService.cs b/src/Yavsc.Server/Services/CallSignalingService.cs index 62ce36e1e..928851009 100644 --- a/src/Yavsc.Server/Services/CallSignalingService.cs +++ b/src/Yavsc.Server/Services/CallSignalingService.cs @@ -37,31 +37,83 @@ public sealed class CallSignalingService( private readonly ConcurrentDictionary _contactPreferences = new(); private readonly AudioVideoCallOptions _options = options.Value; - public void SetContactPreferences(string connection, ContactPreferences preferences) => - _contactPreferences[connection] = preferences; + public async Task SetContactPreferencesAsync(string connection, ContactPreferences preferences) + { + await _gate.WaitAsync(); + try + { + _contactPreferences[connection] = preferences; + foreach (var call in _calls.Values.Where(c => !AllowsCallMode(connection, c.Session.DisplayMode)).ToArray()) + { + if (call.CallerConnection == connection || call.AcceptedConnection == connection) + await FinishCoreAsync(call, "contact-preference-changed"); + else if (call.AcceptedConnection == null && call.CalleeConnections.Remove(connection)) + { + await hub.Clients.Client(connection).SendAsync( + CallHubMethods.Ended, new CallEnded(call.Session.Id, "contact-preference-changed")); + if (call.CalleeConnections.Count == 0) + await FinishCoreAsync(call, "contact-preference-changed"); + } + } + } + finally { _gate.Release(); } + } public ContactPreferences? GetContactPreferences(string connection) => _contactPreferences.TryGetValue(connection, out var preferences) ? preferences : null; + private bool AllowsCallMode(string connection, CallDisplayMode mode) => mode switch + { + CallDisplayMode.AudioVideo => GetContactPreferences(connection)?.AllowVideoCalls != false, + CallDisplayMode.AudioOnly => GetContactPreferences(connection)?.AllowAudioCalls == true, + _ => false + }; + + public async Task SendPrivateMessageAsync( + string senderConnection, string sender, string[] recipients, string message) + { + await _gate.WaitAsync(); + try + { + if (GetContactPreferences(senderConnection)?.AllowPrivateMessages == false) + throw new HubException("Private messages are disabled on this connection."); + var allowed = recipients.Where(id => + GetContactPreferences(id)?.AllowPrivateMessages != false).ToArray(); + if (allowed.Length == 0) + throw new HubException("The participant has no device accepting private messages."); + await hub.Clients.Clients(allowed).SendAsync("addPV", sender, message); + } + finally { _gate.Release(); } + } + public async Task InviteAsync( string room, string caller, string callee, string callerConnection, - string[] calleeConnections, bool relayOnly, string transport = CallTransports.WebRtc) + string[] calleeConnections, bool relayOnly, string transport = CallTransports.WebRtc, + CallDisplayMode displayMode = CallDisplayMode.AudioVideo) { EnsureEnabled(); if (caller == callee || calleeConnections.Length == 0 || calleeConnections.Contains(callerConnection)) throw new HubException("Call participants are unavailable."); ValidateTransport(transport, relayOnly); + if (displayMode is not (CallDisplayMode.AudioVideo or CallDisplayMode.AudioOnly)) + throw new HubException("Unknown call mode."); await _gate.WaitAsync(); try { await ExpireCoreAsync(); + if (!AllowsCallMode(callerConnection, displayMode)) + throw new HubException("This call mode is unavailable on the calling connection."); + calleeConnections = calleeConnections + .Where(connection => AllowsCallMode(connection, displayMode)).ToArray(); + if (calleeConnections.Length == 0) + throw new HubException("The participant has no device accepting this call mode."); if (_calls.Count >= 128 || _calls.Values.Any(c => c.Session.Caller == caller || c.Session.Callee == caller || c.Session.Caller == callee || c.Session.Callee == callee)) throw new HubException("A participant is already in a call."); var session = new CallSession(Guid.NewGuid(), room, caller, callee, relayOnly, - clock.GetUtcNow().AddSeconds(45), transport); + clock.GetUtcNow().AddSeconds(45), transport, displayMode); var call = new PendingCall(session, callerConnection, calleeConnections); _calls.Add(session.Id, call); try @@ -215,10 +267,10 @@ public sealed class CallSignalingService( public async Task DisconnectAsync(string connection) { - _contactPreferences.TryRemove(connection, out _); await _gate.WaitAsync(); try { + _contactPreferences.TryRemove(connection, out _); foreach (var call in _calls.Values.ToArray()) { if (call.CallerConnection == connection || call.AcceptedConnection == connection) diff --git a/src/test/Yavsc.Server.Tests/CallHubTests.cs b/src/test/Yavsc.Server.Tests/CallHubTests.cs index 1dab340f3..2dd3be719 100644 --- a/src/test/Yavsc.Server.Tests/CallHubTests.cs +++ b/src/test/Yavsc.Server.Tests/CallHubTests.cs @@ -67,9 +67,9 @@ public sealed class CallHubTests using var spoof = f.Hub("bob", "alice-1"); var preferences = new ContactPreferences(true, false, false); - Assert.Throws(() => spoof.SetContactPreferences(preferences)); - Assert.Throws(() => alice.SetContactPreferences(null!)); - alice.SetContactPreferences(preferences); + await Assert.ThrowsAsync(() => spoof.SetContactPreferences(preferences)); + await Assert.ThrowsAsync(() => alice.SetContactPreferences(null!)); + await alice.SetContactPreferences(preferences); Assert.Equal(preferences, f.Calls.GetContactPreferences("alice-1")); Assert.Null(f.Calls.GetContactPreferences("bob-1")); @@ -77,6 +77,64 @@ public sealed class CallHubTests Assert.Null(f.Calls.GetContactPreferences("alice-1")); } + [Fact] + public async Task Video_invitation_targets_only_devices_that_allow_it_and_rejects_disabled_caller() + { + using var f = new Fixture(); + f.Connections.OnConnected("bob-2", "bob", false); + using var caller = f.Hub("alice", "alice-1"); + using var blocked = f.Hub("bob", "bob-1"); + await blocked.SetContactPreferences(new(true, false, true)); + + var call = await caller.InviteCall("General", "bob"); + Assert.Equal(["bob-2"], Assert.Single(f.Delivery.Messages).Targets); + await Assert.ThrowsAsync(() => blocked.AcceptCall(call.Id)); + await caller.EndCall(call.Id); + + await caller.SetContactPreferences(new(true, false, true)); + await Assert.ThrowsAsync(() => caller.InviteCall("General", "bob")); + } + + [Fact] + public async Task Disabling_video_removes_pending_invitation_or_ends_accepted_call() + { + using var f = new Fixture(); + using var caller = f.Hub("alice", "alice-1"); + using var callee = f.Hub("bob", "bob-1"); + var pending = await caller.InviteCall("General", "bob"); + await callee.SetContactPreferences(new(true, false, true)); + await Assert.ThrowsAsync(() => callee.AcceptCall(pending.Id)); + Assert.Contains(f.Delivery.Messages, m => m.Targets.Contains("alice-1") && + m.Arguments[0] is CallEnded { Reason: "contact-preference-changed" }); + + await callee.SetContactPreferences(new(true, true, true)); + var active = await caller.InviteCall("General", "bob"); + await callee.AcceptCall(active.Id); + await caller.SetContactPreferences(new(true, false, true)); + await Assert.ThrowsAsync(() => callee.AcceptCall(active.Id)); + Assert.Contains(f.Delivery.Messages, m => m.Targets.Contains("bob-1") && + m.Arguments[0] is CallEnded { Reason: "contact-preference-changed" }); + } + + [Fact] + public async Task Private_messages_are_delivered_only_to_enabled_devices() + { + using var f = new Fixture(); + f.Connections.OnConnected("bob-2", "bob", false); + using var caller = f.Hub("alice", "alice-1"); + using var blocked = f.Hub("bob", "bob-1"); + using var other = f.Hub("bob", "bob-2"); + caller.Clients = new RoomClients(f.Delivery); + await blocked.SetContactPreferences(new(true, true, false)); + + await caller.SendPV("bob", "hello"); + Assert.Equal(["bob-2"], Assert.Single(f.Delivery.Messages, m => m.Method == "addPV").Targets); + await other.SetContactPreferences(new(true, true, false)); + await Assert.ThrowsAsync(() => caller.SendPV("bob", "hello")); + await caller.SetContactPreferences(new(true, true, false)); + await Assert.ThrowsAsync(() => caller.SendPV("bob", "hello")); + } + [Fact] public async Task Lan_requires_https_at_invitation_acceptance_and_key_exchange() { @@ -142,7 +200,7 @@ public sealed class CallHubTests public IClientProxy All => throw new NotSupportedException(); public IClientProxy Others => throw new NotSupportedException(); public IClientProxy AllExcept(IReadOnlyList ids) => throw new NotSupportedException(); - public IClientProxy Client(string id) => throw new NotSupportedException(); + public IClientProxy Client(string id) => delivery.Client(id); public IClientProxy Clients(IReadOnlyList ids) => throw new NotSupportedException(); public IClientProxy Group(string name) => throw new NotSupportedException(); public IClientProxy GroupExcept(string name, IReadOnlyList ids) => throw new NotSupportedException(); diff --git a/src/test/Yavsc.Server.Tests/CallSignalingTests.cs b/src/test/Yavsc.Server.Tests/CallSignalingTests.cs index 140d016a4..d9a3fe140 100644 --- a/src/test/Yavsc.Server.Tests/CallSignalingTests.cs +++ b/src/test/Yavsc.Server.Tests/CallSignalingTests.cs @@ -55,6 +55,29 @@ public sealed class CallSignalingTests Assert.Empty(f.Hub.Messages); } + [Fact] + public async Task Audio_mode_reaches_only_devices_that_announced_audio_and_is_preserved_on_acceptance() + { + using var f = new Fixture(); + await f.Service.SetContactPreferencesAsync("alice-device", new(true, true, true)); + await f.Service.SetContactPreferencesAsync("bob-1", new(true, false, true)); + await Assert.ThrowsAsync(() => f.Service.InviteAsync( + "General", "alice", "bob", "alice-device", ["bob-2"], false, + displayMode: CallDisplayMode.AudioOnly)); + var call = await f.Service.InviteAsync("General", "alice", "bob", "alice-device", ["bob-1", "bob-2"], + false, displayMode: CallDisplayMode.AudioOnly); + Assert.Equal(CallDisplayMode.AudioOnly, call.DisplayMode); + Assert.Equal(["bob-1"], Assert.Single(f.Hub.Messages).Targets); + await f.Service.AcceptAsync(call.Id, "bob", "bob-1", false); + var accepted = Assert.IsType(f.Hub.Messages.Last().Arguments[0]); + Assert.Equal(CallDisplayMode.AudioOnly, accepted.Session.DisplayMode); + await f.Service.SetContactPreferencesAsync("bob-1", new(false, true, true)); + Assert.Equal("contact-preference-changed", Assert.IsType(f.Hub.Messages.Last().Arguments[0]).Reason); + await Assert.ThrowsAsync(() => f.Service.InviteAsync( + "General", "alice", "bob", "alice-device", ["bob-1"], false, + displayMode: (CallDisplayMode)42)); + } + [Fact] public async Task Invitation_does_not_grant_media_signaling_or_disclose_turn_credentials() {