From 9aff760ef3f7d1f8db925a8a5078d1fb7e7184bb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 28 Feb 2022 07:41:55 -0600 Subject: add threshold options for the leecher group --- src/slskd/Options.cs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 5bebeb68..83e72586 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -480,7 +480,7 @@ namespace slskd /// These options apply to users that have been identified as leechers, and have not been added as a member of any group. /// [Validate] - public BuiltInOptions Leechers { get; init; } = new BuiltInOptions(); + public LeecherOptions Leechers { get; init; } = new LeecherOptions(); /// /// Gets user defined groups and options. @@ -513,6 +513,42 @@ namespace slskd public UploadOptions Upload { get; init; } = new UploadOptions(); } + /// + /// Built in leecher group options. + /// + public class LeecherOptions + { + /// + /// Gets leecher threshold options. + /// + [Validate] + public ThresholdOptions Thresholds { get; init } = new ThresholdOptions(); + + /// + /// Gets upload options. + /// + [Validate] + public UploadOptions Upload { get; init; } = new UploadOptions(); + } + + /// + /// Leecher threshold options. + /// + public class ThresholdOptions + { + /// + /// Gets the minimum number of shared files required to avoid being classified as a leecher. + /// + [Range(1, int.MaxValue)] + public int Files { get; init; } = 1; + + /// + /// Gets the minimum number of shared directories required to avoid being classified as a leecher. + /// + [Range(1, int.MaxValue)] + public int Directories { get; init; } = 1; + } + /// /// User defined user group options. /// -- cgit v1.2.3 From 7dc85d07a4d57c97d4350fccd68ac305061ae801 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 28 Feb 2022 08:06:51 -0600 Subject: typo --- src/slskd/Options.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Options.cs b/src/slskd/Options.cs index 83e72586..64ca494f 100644 --- a/src/slskd/Options.cs +++ b/src/slskd/Options.cs @@ -522,7 +522,7 @@ namespace slskd /// Gets leecher threshold options. /// [Validate] - public ThresholdOptions Thresholds { get; init } = new ThresholdOptions(); + public ThresholdOptions Thresholds { get; init; } = new ThresholdOptions(); /// /// Gets upload options. -- cgit v1.2.3 From 93b0910027f7424c3ed3b6af3af51399439232d9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 2 Mar 2022 20:58:54 -0600 Subject: designate leechers --- src/slskd/Users/UserService.cs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 5adc7c71..62ce2a3f 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -110,7 +110,17 @@ namespace slskd.Users return Application.PrivilegedGroup; } - return user.Group ?? Application.DefaultGroup; + 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; @@ -225,21 +235,16 @@ namespace slskd.Users /// The operation context. public async Task WatchAsync(string 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); + await Client.AddUserAsync(username); + WatchedUsernamesDictionary.TryAdd(username, true); - Log.Information("Added user {Username} to watch list", username); + Log.Information("Added user {Username} to watch list", username); - // 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); - } + // 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); } private void UpdateStatistics(string username, Statistics statistics) @@ -279,10 +284,14 @@ 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 var usernamesBeforeUpdate = UserDictionary.Keys.ToList(); var usernamesAfterUpdate = options.Groups.UserDefined.SelectMany(g => g.Value.Members); var usernamesRemoved = usernamesBeforeUpdate.Except(usernamesAfterUpdate); + // clear the configured group for anyone that was removed from config foreach (var username in usernamesRemoved) { UserDictionary.AddOrUpdate( -- cgit v1.2.3 From d6a6c3baf27dbffc9f014ed23e4af1b99a7ef5b3 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 2 Mar 2022 21:02:31 -0600 Subject: docs --- src/slskd/Users/UserService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 62ce2a3f..1854944f 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -291,7 +291,7 @@ namespace slskd.Users var usernamesAfterUpdate = options.Groups.UserDefined.SelectMany(g => g.Value.Members); var usernamesRemoved = usernamesBeforeUpdate.Except(usernamesAfterUpdate); - // clear the configured group for anyone that was removed from config + // clear the configured group for anyone that was removed from config, or that was added transiently foreach (var username in usernamesRemoved) { UserDictionary.AddOrUpdate( -- cgit v1.2.3