aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/slskd/Users/IUserService.cs28
-rw-r--r--src/slskd/Users/UserService.cs167
2 files changed, 74 insertions, 121 deletions
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
@@ -37,6 +37,13 @@ namespace slskd.Users
IReadOnlyList<string> WatchedUsernames { get; }
/// <summary>
+ /// Gets the name of the group for the specified <paramref name="username"/>.
+ /// </summary>
+ /// <param name="username">The username of the peer.</param>
+ /// <returns>The group for the specified username.</returns>
+ string GetGroup(string username);
+
+ /// <summary>
/// Retrieves peer <see cref="Info"/>.
/// </summary>
/// <param name="username">The username of the peer.</param>
@@ -58,13 +65,6 @@ namespace slskd.Users
Task<Status> GetStatusAsync(string username);
/// <summary>
- /// Retrieves the current <see cref="Statistics"/> of a peer.
- /// </summary>
- /// <param name="username">The username of the peer.</param>
- /// <returns>The retrieved statistics.</returns>
- Task<Statistics> GetStatisticsAsync(string username);
-
- /// <summary>
/// Grants the specified peer the specified number of privilege days.
/// </summary>
/// <param name="username">The username of the peer.</param>
@@ -87,20 +87,6 @@ namespace slskd.Users
bool IsWatched(string username);
/// <summary>
- /// Resolves the name of the group for the specified <paramref name="username"/>.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
- /// <param name="username">The username of the peer.</param>
- /// <param name="status">The optional status for the user.</param>
- /// <param name="statistics">The optional statistics for the user.</param>
- /// <returns>The group for the specified username.</returns>
- string ResolveGroup(string username, Status status = null, Statistics statistics = null);
-
- /// <summary>
/// Adds the specified username to the server-side user list.
/// </summary>
/// <param name="username">The username of the peer.</param>
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) =>
{
@@ -87,6 +87,37 @@ namespace slskd.Users
private ConcurrentDictionary<string, bool> WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary<string, bool>();
/// <summary>
+ /// Gets the name of the group for the specified <paramref name="username"/>.
+ /// </summary>
+ /// <param name="username">The username of the peer.</param>
+ /// <returns>The group for the specified username.</returns>
+ 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;
+ }
+
+ /// <summary>
/// Retrieves peer <see cref="Info"/>.
/// </summary>
/// <param name="username">The username of the peer.</param>
@@ -165,86 +196,50 @@ namespace slskd.Users
public bool IsWatched(string username) => WatchedUsernamesDictionary.ContainsKey(username);
/// <summary>
- /// Resolves the name of the group for the specified <paramref name="username"/>.
+ /// Adds the specified username to the server-side user list.
/// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
/// <param name="username">The username of the peer.</param>
- /// <param name="status">The optional status for the user.</param>
- /// <param name="statistics">The optional statistics for the user.</param>
- /// <returns>The group for the specified username.</returns>
- public string ResolveGroup(string username, Status status = null, Statistics statistics = null)
+ /// <returns>The operation context.</returns>
+ 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() });
}
- /// <summary>
- /// Adds the specified username to the server-side user list.
- /// </summary>
- /// <param name="username">The username of the peer.</param>
- /// <returns>The operation context.</returns>
- 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))