diff options
Diffstat (limited to 'src/slskd/Users/UserService.cs')
| -rw-r--r-- | src/slskd/Users/UserService.cs | 58 |
1 files changed, 53 insertions, 5 deletions
diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index f52f19f1..cb56e6dd 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -15,13 +15,18 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // </copyright> +using Microsoft.Extensions.Options; + namespace slskd.Users { using System; + using System.Collections.Concurrent; + using System.Collections.Generic; + using System.Linq; using System.Net; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; - using Microsoft.Extensions.Logging; + using Serilog; using Soulseek; /// <summary> @@ -36,20 +41,37 @@ namespace slskd.Users /// </summary> /// <param name="soulseekClient"></param> /// <param name="contextFactory">The database context to use.</param> - /// <param name="log">The logger.</param> + /// <param name="optionsMonitor"></param> public UserService( ISoulseekClient soulseekClient, IDbContextFactory<UserDbContext> contextFactory, - ILogger<UserService> log) + IOptionsMonitor<Options> optionsMonitor) { Client = soulseekClient; ContextFactory = contextFactory; - Log = log; + + OptionsMonitor = optionsMonitor; + OptionsMonitor.OnChange(Configure); + + Configure(OptionsMonitor.CurrentValue); } private ISoulseekClient Client { get; } private IDbContextFactory<UserDbContext> ContextFactory { get; } - private ILogger<UserService> Log { get; set; } + private string LastOptionsHash { get; set; } + private ILogger Log { get; set; } = Serilog.Log.ForContext<UserService>(); + private ConcurrentDictionary<string, string> Map { get; set; } = new ConcurrentDictionary<string, string>(); + private IOptionsMonitor<Options> OptionsMonitor { 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> + public string GetGroup(string username) + { + return Map.GetValueOrDefault(username ?? string.Empty, Application.DefaultGroup); + } /// <summary> /// Retrieves peer <see cref="Info"/>. @@ -134,5 +156,31 @@ namespace slskd.Users { await Client.AddUserAsync(username); } + + private void Configure(Options options) + { + var optionsHash = Compute.Sha1Hash(options.Groups.UserDefined.ToJson()); + + if (optionsHash == LastOptionsHash) + { + return; + } + + var map = new ConcurrentDictionary<string, string>(); + + // sort by priority, ascending + foreach (var group in options.Groups.UserDefined.OrderBy(kvp => kvp.Value.Upload.Priority)) + { + foreach (var user in group.Value.Members) + { + // if the key already exists, leave the existing entry. if a user appears in more than one group, the higher + // priority (lower numbered) group is their effective group. + map.TryAdd(user, group.Key); + } + } + + Map = map; + LastOptionsHash = optionsHash; + } } }
\ No newline at end of file |
