From d409b1e3e371cfe8fd1e59e9ad53b0f7e4edafca Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 1 Jan 2024 14:54:57 -0600 Subject: revert all the changes to User --- src/slskd/Users/IUserService.cs | 28 ++----- src/slskd/Users/UserService.cs | 167 ++++++++++++++++------------------------ 2 files changed, 74 insertions(+), 121 deletions(-) (limited to 'src') diff --git a/src/slskd/Users/IUserService.cs b/src/slskd/Users/IUserService.cs index 1d19277c..88090f9f 100644 --- a/src/slskd/Users/IUserService.cs +++ b/src/slskd/Users/IUserService.cs @@ -36,6 +36,13 @@ namespace slskd.Users /// IReadOnlyList WatchedUsernames { get; } + /// + /// Gets the name of the group for the specified . + /// + /// The username of the peer. + /// The group for the specified username. + string GetGroup(string username); + /// /// Retrieves peer . /// @@ -57,13 +64,6 @@ namespace slskd.Users /// The retrieved status. Task GetStatusAsync(string username); - /// - /// Retrieves the current of a peer. - /// - /// The username of the peer. - /// The retrieved statistics. - Task GetStatisticsAsync(string username); - /// /// Grants the specified peer the specified number of privilege days. /// @@ -86,20 +86,6 @@ namespace slskd.Users /// A value indicating whether the username is watched. bool IsWatched(string username); - /// - /// Resolves the name of the group for the specified . - /// - /// - /// If the user is watched, and therefore the application is tracking their status and statistics, leech and privilege - /// detection works as expected. If the user is not watched, their status and statistics can be specified. If the user - /// is neither watched nor status and statistics are supplied, leech and privilege detection will not work. - /// - /// The username of the peer. - /// The optional status for the user. - /// The optional statistics for the user. - /// The group for the specified username. - string ResolveGroup(string username, Status status = null, Statistics statistics = null); - /// /// 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 91435766..95fed12d 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -50,9 +50,9 @@ namespace slskd.Users OptionsMonitor = optionsMonitor; OptionsMonitor.OnChange(options => Configure(options)); - // updates may be sent unsolicited from the server, so update when we get them. binding these events will cause - // multiple redundant round trips when initially watching a user or when explicitly requesting via - // GetStatus/GetStatistics. this is wasteful, but there's no functional side effect. + // updates may be sent unsolicited from the server, so update when we get them. + // binding these events will cause multiple redundant round trips when initially watching a user + // or when explicitly requesting via GetStatus/GetStatistics. this is wasteful, but there's no functional side effect. Client.UserStatisticsChanged += (_, userStatistics) => UpdateStatistics(userStatistics.Username, userStatistics.ToStatistics()); Client.UserStatusChanged += (sender, userStatus) => { @@ -86,6 +86,37 @@ namespace slskd.Users private ConcurrentDictionary UserDictionary { get; set; } = new ConcurrentDictionary(); private ConcurrentDictionary WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary(); + /// + /// Gets the name of the group for the specified . + /// + /// The username of the peer. + /// The group for the specified username. + public string GetGroup(string username) + { + // 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; + } + + if (user.Group != null) + { + return user.Group; + } + + var thresholds = OptionsMonitor.CurrentValue.Groups.Leechers.Thresholds; + + if (user.Statistics?.FileCount < thresholds.Files || user.Statistics?.DirectoryCount < thresholds.Directories) + { + return Application.LeecherGroup; + } + } + + return Application.DefaultGroup; + } + /// /// Retrieves peer . /// @@ -165,86 +196,50 @@ namespace slskd.Users public bool IsWatched(string username) => WatchedUsernamesDictionary.ContainsKey(username); /// - /// Resolves the name of the group for the specified . + /// Adds the specified username to the server-side user list. /// - /// - /// If the user is watched, and therefore the application is tracking their status and statistics, leech and privilege - /// detection works as expected. If the user is not watched, their status and statistics can be specified. If the user - /// is neither watched nor status and statistics are supplied, leech and privilege detection will not work. - /// /// The username of the peer. - /// The optional status for the user. - /// The optional statistics for the user. - /// The group for the specified username. - public string ResolveGroup(string username, Status status = null, Statistics statistics = null) + /// The operation context. + public async Task WatchAsync(string username) { - /* - * note: this is an extremely hot path; keep the work done to an absolute minimum. - */ - - // we shouldn't be asking, but this could happen in some scenarios. - if (OptionsMonitor.CurrentValue.Groups.Blacklisted.Members.Contains(username)) - { - return Application.BlacklistedGroup; - } - - var thresholds = OptionsMonitor.CurrentValue.Groups.Leechers.Thresholds; - - // any user that's been explicitly added to a group is "watched" and will be in this dictionary, - // along with their latest status and statistics. we *need* to do this lookup to expedite the - // resolution of their group from configuration quickly. - if (UserDictionary.TryGetValue(username ?? string.Empty, out var user)) - { - if (user.Status?.IsPrivileged ?? false) - { - return Application.PrivilegedGroup; - } - - if (user.Group != null) - { - return user.Group; - } + await Client.AddUserAsync(username); + WatchedUsernamesDictionary.TryAdd(username, true); - if (user.Statistics?.FileCount < thresholds.Files || user.Statistics?.DirectoryCount < thresholds.Directories) - { - return Application.LeecherGroup; - } - } + Log.Information("Added user {Username} to watch list", username); - // the user hasn't been added to the dictionary, so we know they aren't part of a user defined group - // the caller may have specified status and statistics. check and see. - if (status?.IsPrivileged ?? false) - { - return Application.PrivilegedGroup; - } + // the server does not automatically send status and statistics information + // when watching a user initially. explicitly fetch both, so that the user has been + // watched and all information populated when this method returns. + await GetStatusAsync(username); + await GetStatisticsAsync(username); + } - if (statistics?.FileCount < thresholds.Files || statistics?.DirectoryCount < thresholds.Directories) - { - return Application.LeecherGroup; - } + private void UpdateStatistics(string username, Statistics statistics) + { + UserDictionary.AddOrUpdate( + key: username, + addValue: new User() { Statistics = statistics }, + updateValueFactory: (key, user) => user with { Username = username, Statistics = statistics }); - return Application.DefaultGroup; + StateMutator.SetValue(state => state with { Users = Users.ToArray() }); } - /// - /// Adds the specified username to the server-side user list. - /// - /// The username of the peer. - /// The operation context. - public async Task WatchAsync(string username) + private void UpdateStatus(string username, Status status) { - await Client.AddUserAsync(username); - WatchedUsernamesDictionary.TryAdd(username, true); + UserDictionary.AddOrUpdate( + key: username, + addValue: new User() { Status = status }, + updateValueFactory: (key, user) => user with { Username = username, Status = status }); - Log.Information("Added user {Username} to watch list", username); + StateMutator.SetValue(state => state with { Users = Users.ToArray() }); + } - // the server does not automatically send status and statistics information when watching a user initially. explicitly - // fetch both, so that the user has been watched and all information populated when this method returns. - var status = await GetStatusAsync(username); - UpdateStatus(username, status); + private void Reset() + { + WatchedUsernamesDictionary.Clear(); + UserDictionary.Clear(); - var statistics = await GetStatisticsAsync(username); - UpdateStatistics(username, statistics); + StateMutator.SetValue(state => state with { Users = Users.ToArray() }); } private void Configure(Options options, bool force = false) @@ -256,8 +251,8 @@ namespace slskd.Users return; } - // get a list of tracked names that haven't been explicitly added to any group, including those that were previlously - // configured but have now been removed + // get a list of tracked names that haven't been explicitly added to any group, including + // those that were previlously configured but have now been removed var usernamesBeforeUpdate = UserDictionary.Keys.ToList(); var usernamesAfterUpdate = options.Groups.UserDefined.SelectMany(g => g.Value.Members); var usernamesRemoved = usernamesBeforeUpdate.Except(usernamesAfterUpdate); @@ -291,34 +286,6 @@ namespace slskd.Users LastOptionsHash = optionsHash; } - private void Reset() - { - WatchedUsernamesDictionary.Clear(); - UserDictionary.Clear(); - - StateMutator.SetValue(state => state with { Users = Users.ToArray() }); - } - - private void UpdateStatistics(string username, Statistics statistics) - { - 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() }); - } - - private void UpdateStatus(string username, Status status) - { - 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() }); - } - private void WatchAllUsers() { if (Client.State.HasFlag(SoulseekClientStates.Connected) && Client.State.HasFlag(SoulseekClientStates.LoggedIn)) -- cgit v1.2.3