diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Users/IUserService.cs | 12 | ||||
| -rw-r--r-- | src/slskd/Users/UserService.cs | 93 |
3 files changed, 64 insertions, 43 deletions
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 @@ -32,6 +32,11 @@ namespace slskd.Users IReadOnlyList<User> Users { get; } /// <summary> + /// Gets the list of watched usernames. + /// </summary> + 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> @@ -76,6 +81,13 @@ namespace slskd.Users Task<bool> IsPrivilegedAsync(string username); /// <summary> + /// Gets a value indicating whether the specified <paramref name="username"/> is watched. + /// </summary> + /// <param name="username">The username of the peer.</param> + /// <returns>A value indicating whether the username is watched.</returns> + bool IsWatched(string username); + + /// <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 7532e2a7..39647e0a 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -67,13 +67,19 @@ namespace slskd.Users /// </summary> public IReadOnlyList<User> Users => UserDictionary.Values.ToList().AsReadOnly(); + /// <summary> + /// Gets the list of watched usernames. + /// </summary> + public IReadOnlyList<string> WatchedUsernames => WatchedUsernamesDictionary.Keys.ToList().AsReadOnly(); + private ISoulseekClient Client { get; } - private IStateMutator<State> StateMutator { get; } private IDbContextFactory<UserDbContext> ContextFactory { get; } private string LastOptionsHash { get; set; } private ILogger Log { get; set; } = Serilog.Log.ForContext<UserService>(); - private ConcurrentDictionary<string, User> UserDictionary { get; set; } = new ConcurrentDictionary<string, User>(); private IOptionsMonitor<Options> OptionsMonitor { get; } + private IStateMutator<State> StateMutator { get; } + private ConcurrentDictionary<string, User> UserDictionary { get; set; } = new ConcurrentDictionary<string, User>(); + private ConcurrentDictionary<string, bool> WatchedUsernamesDictionary { get; set; } = new ConcurrentDictionary<string, bool>(); /// <summary> /// Gets the name of the group for the specified <paramref name="username"/>. @@ -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 } /// <summary> - /// Retrieves the current <see cref="Status"/> of a peer. + /// Retrieves the current <see cref="Statistics"/> of a peer. /// </summary> /// <param name="username">The username of the peer.</param> - /// <returns>The retrieved status.</returns> - public async Task<Status> GetStatusAsync(string username) + /// <returns>The retrieved statistics.</returns> + public async Task<Statistics> 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); } /// <summary> - /// Retrieves the current <see cref="Statistics"/> of a peer. + /// Retrieves the current <see cref="Status"/> of a peer. /// </summary> /// <param name="username">The username of the peer.</param> - /// <returns>The retrieved statistics.</returns> - public async Task<Statistics> GetStatisticsAsync(string username) + /// <returns>The retrieved status.</returns> + public async Task<Status> 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); } /// <summary> @@ -197,22 +190,39 @@ namespace slskd.Users } /// <summary> + /// Gets a value indicating whether the specified <paramref name="username"/> is watched. + /// </summary> + /// <param name="username">The username of the peer.</param> + /// <returns>A value indicating whether the username is watched.</returns> + public bool IsWatched(string username) => WatchedUsernamesDictionary.ContainsKey(username); + + /// <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) { - 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(); |
