From 979acc196f01bf53bf0141cf80f684172433d472 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 22 Feb 2022 09:39:23 -0600 Subject: track and expose watched usernames --- src/slskd/Application.cs | 2 +- src/slskd/Users/IUserService.cs | 12 ++++++ src/slskd/Users/UserService.cs | 93 ++++++++++++++++++++++------------------- 3 files changed, 64 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 47bcd6c9..56d17808 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -593,7 +593,7 @@ namespace slskd // users with uploads must be watched so that we can keep informed of their // online status, privileges, and statistics. this is so that we can accurately // determine their effective group. - if (Users.Users.Any(u => u.Username == username)) + if (!Users.IsWatched(username)) { await Users.WatchAsync(username); } diff --git a/src/slskd/Users/IUserService.cs b/src/slskd/Users/IUserService.cs index 3c716116..e78b1b1c 100644 --- a/src/slskd/Users/IUserService.cs +++ b/src/slskd/Users/IUserService.cs @@ -31,6 +31,11 @@ namespace slskd.Users /// IReadOnlyList Users { get; } + /// + /// Gets the list of watched usernames. + /// + IReadOnlyList WatchedUsernames { get; } + /// /// Gets the name of the group for the specified . /// @@ -75,6 +80,13 @@ namespace slskd.Users /// A value indicating whether the specified peer is privileged. Task IsPrivilegedAsync(string username); + /// + /// Gets a value indicating whether the specified is watched. + /// + /// The username of the peer. + /// A value indicating whether the username is watched. + bool IsWatched(string username); + /// /// Adds the specified username to the server-side user list. /// diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 7532e2a7..39647e0a 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -67,13 +67,19 @@ namespace slskd.Users /// public IReadOnlyList Users => UserDictionary.Values.ToList().AsReadOnly(); + /// + /// Gets the list of watched usernames. + /// + public IReadOnlyList WatchedUsernames => WatchedUsernamesDictionary.Keys.ToList().AsReadOnly(); + private ISoulseekClient Client { get; } - private IStateMutator StateMutator { get; } private IDbContextFactory ContextFactory { get; } private string LastOptionsHash { get; set; } private ILogger Log { get; set; } = Serilog.Log.ForContext(); - private ConcurrentDictionary UserDictionary { get; set; } = new ConcurrentDictionary(); private IOptionsMonitor OptionsMonitor { get; } + private IStateMutator StateMutator { get; } + private ConcurrentDictionary UserDictionary { get; set; } = new ConcurrentDictionary(); + private ConcurrentDictionary WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary(); /// /// Gets the name of the group for the specified . @@ -85,6 +91,11 @@ namespace slskd.Users // note: this is an extremely hot path; keep the work done to an absolute minimum. if (UserDictionary.TryGetValue(username ?? string.Empty, out var user)) { + if (user.Status?.IsPrivileged ?? false) + { + return Application.PrivilegedGroup; + } + return user.Group ?? Application.DefaultGroup; } @@ -137,43 +148,25 @@ namespace slskd.Users } /// - /// Retrieves the current of a peer. + /// Retrieves the current of a peer. /// /// The username of the peer. - /// The retrieved status. - public async Task GetStatusAsync(string username) + /// The retrieved statistics. + public async Task GetStatisticsAsync(string username) { - var soulseekStatus = await Client.GetUserStatusAsync(username); - var status = Status.FromSoulseekUserStatus(soulseekStatus); - - UserDictionary.AddOrUpdate( - key: username, - addValue: new User() { Status = status }, - updateValueFactory: (key, user) => user with { Username = username, Status = status }); - - StateMutator.SetValue(state => state with { Users = Users.ToArray() }); - - return status; + var soulseekStatistics = await Client.GetUserStatisticsAsync(username); + return Statistics.FromSoulseekUserStatistics(soulseekStatistics); } /// - /// Retrieves the current of a peer. + /// Retrieves the current of a peer. /// /// The username of the peer. - /// The retrieved statistics. - public async Task GetStatisticsAsync(string username) + /// The retrieved status. + public async Task GetStatusAsync(string username) { - var soulseekStatistics = await Client.GetUserStatisticsAsync(username); - var statistics = Statistics.FromSoulseekUserStatistics(soulseekStatistics); - - UserDictionary.AddOrUpdate( - key: username, - addValue: new User() { Statistics = statistics }, - updateValueFactory: (key, user) => user with { Username = username, Statistics = statistics }); - - StateMutator.SetValue(state => state with { Users = Users.ToArray() }); - - return statistics; + var soulseekStatus = await Client.GetUserStatusAsync(username); + return Status.FromSoulseekUserStatus(soulseekStatus); } /// @@ -196,6 +189,13 @@ namespace slskd.Users return status.IsPrivileged; } + /// + /// Gets a value indicating whether the specified is watched. + /// + /// The username of the peer. + /// A value indicating whether the username is watched. + public bool IsWatched(string username) => WatchedUsernamesDictionary.ContainsKey(username); + /// /// Adds the specified username to the server-side user list. /// @@ -203,16 +203,26 @@ namespace slskd.Users /// The operation context. public async Task WatchAsync(string username) { - await Client.AddUserAsync(username); - await GetStatusAsync(username); + // note that this isn't strictly synchronized, and this may be executed more than once per user. this is acceptable, + // and has no side effects other than increased round trips to the server. + if (!WatchedUsernamesDictionary.ContainsKey(username)) + { + await Client.AddUserAsync(username); + WatchedUsernamesDictionary.TryAdd(username, true); - UserDictionary.TryAdd(username, new User()); - StateMutator.SetValue(state => state with { Users = Users.ToArray() }); - Log.Information("Added user {Username} to watch list", username); + Log.Information("Added user {Username} to watch list", username); + + // the server does not send a status update when a user is added, but does when that user's status changes. + // request the status explicitly to get the current status. + await GetStatusAsync(username); + } } private void BindClientEvents() { + // it isn't completely clear what all causes the server to send this message. we know it is sent following an explicit + // request, but it may be sent in an unsolicited manner as well. to be safe, we update internal stats for this user + // only on this event, which should cover all bases (both solicited and unsolicited). Client.UserStatisticsChanged += (_, e) => { var statistics = Statistics.FromSoulseekUserStatistics(e); @@ -225,12 +235,12 @@ namespace slskd.Users StateMutator.SetValue(state => state with { Users = Users.ToArray() }); }; + // the server sends this message in both solicited (we request status), and unsolicited (the user's status changes and + // the server informs us without asking). we handle updates in this event handler to cover both cases. Client.UserStatusChanged += (_, e) => { var status = Status.FromSoulseekUserStatus(e); - Console.WriteLine(e.ToJson()); - UserDictionary.AddOrUpdate( key: e.Username, addValue: new User() { Status = status }, @@ -238,12 +248,12 @@ namespace slskd.Users StateMutator.SetValue(state => state with { Users = Users.ToArray() }); - // the server doesn't send statistics events by itself, so - // when a user status changes, fetch stats at the same time. + // the server doesn't send statistics events by itself, so when a user status changes, fetch stats at the same time. _ = GetStatisticsAsync(e.Username); }; Client.LoggedIn += (_, _) => WatchAllUsers(); + Client.Disconnected += (_, _) => WatchedUsernamesDictionary.Clear(); } private void Configure(Options options) @@ -267,8 +277,8 @@ namespace slskd.Users updateValueFactory: (key, user) => user with { Username = username, Group = null }); } - // sort by priority, descending. this will cause the highest priority group for the user - // to be persisted when the operation is complete. + // sort by priority, descending. this will cause the highest priority group for the user to be persisted when the + // operation is complete. foreach (var group in options.Groups.UserDefined.OrderByDescending(kvp => kvp.Value.Upload.Priority)) { foreach (var username in group.Value.Members) @@ -280,7 +290,6 @@ namespace slskd.Users } } - Console.WriteLine($"Updating user state: {Users.ToArray().ToJson()}"); StateMutator.SetValue(state => state with { Users = Users.ToArray() }); WatchAllUsers(); -- cgit v1.2.3