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>
This commit is contained in:
parent
9c4cce6840
commit
9e4f9d55ea
8 changed files with 156 additions and 24 deletions
|
|
@ -56,7 +56,7 @@
|
|||
IsChecked="{Binding AllowVideoCalls, Mode=TwoWay}" />
|
||||
<CheckBox Content="Autoriser les messages prives"
|
||||
IsChecked="{Binding AllowPrivateMessages, Mode=TwoWay}" />
|
||||
<TextBlock Text="Ces preferences sont annoncees au serveur a la connexion et a la reconnexion. Leur application aux appels et aux messages n'est pas encore implementee ; elles ne bloquent donc pas les contacts."
|
||||
<TextBlock Text="Les changements prennent effet a la prochaine connexion au chat. Les autres appareils conservent leurs propres preferences ; les anciens clients sans preferences restent joignables. Les appels audio seuls ne sont pas encore disponibles."
|
||||
TextWrapping="Wrap" />
|
||||
<StackPanel IsVisible="{Binding SupportsBackgroundChatService}" Spacing="6">
|
||||
<CheckBox x:Name="BackgroundChatCheckBox"
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@ namespace Yavsc.Server.Hubs;
|
|||
|
||||
public partial class ChatHub
|
||||
{
|
||||
public void SetContactPreferences(ContactPreferences preferences)
|
||||
public Task SetContactPreferences(ContactPreferences preferences)
|
||||
{
|
||||
CallUser();
|
||||
if (preferences is null)
|
||||
throw new HubException("Contact preferences are required.");
|
||||
_calls.SetContactPreferences(Context.ConnectionId, preferences);
|
||||
return _calls.SetContactPreferencesAsync(Context.ConnectionId, preferences);
|
||||
}
|
||||
|
||||
public Task<CallSession> InviteCall(string roomName, string callee) =>
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -37,31 +37,83 @@ public sealed class CallSignalingService(
|
|||
private readonly ConcurrentDictionary<string, ContactPreferences> _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<CallSession> 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)
|
||||
|
|
|
|||
|
|
@ -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<HubException>(() => spoof.SetContactPreferences(preferences));
|
||||
Assert.Throws<HubException>(() => alice.SetContactPreferences(null!));
|
||||
alice.SetContactPreferences(preferences);
|
||||
await Assert.ThrowsAsync<HubException>(() => spoof.SetContactPreferences(preferences));
|
||||
await Assert.ThrowsAsync<HubException>(() => 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<HubException>(() => blocked.AcceptCall(call.Id));
|
||||
await caller.EndCall(call.Id);
|
||||
|
||||
await caller.SetContactPreferences(new(true, false, true));
|
||||
await Assert.ThrowsAsync<HubException>(() => 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<HubException>(() => 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<HubException>(() => 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<HubException>(() => caller.SendPV("bob", "hello"));
|
||||
await caller.SetContactPreferences(new(true, true, false));
|
||||
await Assert.ThrowsAsync<HubException>(() => 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<string> ids) => throw new NotSupportedException();
|
||||
public IClientProxy Client(string id) => throw new NotSupportedException();
|
||||
public IClientProxy Client(string id) => delivery.Client(id);
|
||||
public IClientProxy Clients(IReadOnlyList<string> ids) => throw new NotSupportedException();
|
||||
public IClientProxy Group(string name) => throw new NotSupportedException();
|
||||
public IClientProxy GroupExcept(string name, IReadOnlyList<string> ids) => throw new NotSupportedException();
|
||||
|
|
|
|||
|
|
@ -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<HubException>(() => 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<CallAccepted>(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<CallEnded>(f.Hub.Messages.Last().Arguments[0]).Reason);
|
||||
await Assert.ThrowsAsync<HubException>(() => 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()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue