diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2022-02-23 09:28:32 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2022-02-23 09:28:32 -0600 |
| commit | a6508a390021f82ffd3c81f8001c914889f23f28 (patch) | |
| tree | 0931a5ba3a8d6ab1c00a3c01b88c9254f67540d8 /src | |
| parent | ec21875fb748bc91b4df85ea62c4813815e25b4a (diff) | |
| download | slskd-a6508a390021f82ffd3c81f8001c914889f23f28.tar.gz | |
use extensions over static factory methods
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Users/Domain/Statistics.cs | 15 | ||||
| -rw-r--r-- | src/slskd/Users/Domain/Status.cs | 21 | ||||
| -rw-r--r-- | src/slskd/Users/Extensions.cs | 51 | ||||
| -rw-r--r-- | src/slskd/Users/UserService.cs | 22 |
4 files changed, 74 insertions, 35 deletions
diff --git a/src/slskd/Users/Domain/Statistics.cs b/src/slskd/Users/Domain/Statistics.cs index 8dbcaaef..a415d3d5 100644 --- a/src/slskd/Users/Domain/Statistics.cs +++ b/src/slskd/Users/Domain/Statistics.cs @@ -17,9 +17,7 @@ namespace slskd.Users { - using Soulseek; - - public class Statistics + public record Statistics { /// <summary> /// Gets the average upload speed of the user. @@ -40,16 +38,5 @@ namespace slskd.Users /// Gets the number of uploads tracked by the server for this user. /// </summary> public long UploadCount { get; init; } - - public static Statistics FromSoulseekUserStatistics(UserStatistics statistics) - { - return new Statistics() - { - AverageSpeed = statistics.AverageSpeed, - DirectoryCount = statistics.DirectoryCount, - FileCount = statistics.FileCount, - UploadCount = statistics.UploadCount, - }; - } } } diff --git a/src/slskd/Users/Domain/Status.cs b/src/slskd/Users/Domain/Status.cs index 41452e66..890afb74 100644 --- a/src/slskd/Users/Domain/Status.cs +++ b/src/slskd/Users/Domain/Status.cs @@ -19,18 +19,19 @@ namespace slskd.Users { using Soulseek; - public class Status + /// <summary> + /// User status. + /// </summary> + public record Status { + /// <summary> + /// Gets a value indicating whether the user is privileged. + /// </summary> public bool IsPrivileged { get; init; } - public UserPresence Presence { get; init; } - public static Status FromSoulseekUserStatus(UserStatus status) - { - return new Status() - { - IsPrivileged = status.IsPrivileged, - Presence = status.Presence, - }; - } + /// <summary> + /// Gets the user's presence. + /// </summary> + public UserPresence Presence { get; init; } } } diff --git a/src/slskd/Users/Extensions.cs b/src/slskd/Users/Extensions.cs new file mode 100644 index 00000000..f485df34 --- /dev/null +++ b/src/slskd/Users/Extensions.cs @@ -0,0 +1,51 @@ +// <copyright file="Extensions.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd.Users +{ + using Soulseek; + + /// <summary> + /// Users extensions. + /// </summary> + public static class Extensions + { + /// <summary> + /// Converts <see cref="Soulseek.UserStatistics"/> to <see cref="Statistics"/>. + /// </summary> + /// <param name="u">The UserStatistics instance to convert.</param> + /// <returns>The converted instance.</returns> + public static Statistics AsStatistics(this UserStatistics u) => new() + { + AverageSpeed = u.AverageSpeed, + DirectoryCount = u.DirectoryCount, + FileCount = u.FileCount, + UploadCount = u.UploadCount, + }; + + /// <summary> + /// Converts <see cref="Soulseek.UserStatus"/> to <see cref="Status"/>. + /// </summary> + /// <param name="s">The UserStatus instance to convert.</param> + /// <returns>The converted instance.</returns> + public static Status AsStatus(this UserStatus s) => new() + { + IsPrivileged = s.IsPrivileged, + Presence = s.Presence, + }; + } +} diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index 4e252f40..58b14cec 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -155,7 +155,7 @@ namespace slskd.Users public async Task<Statistics> GetStatisticsAsync(string username) { var soulseekStatistics = await Client.GetUserStatisticsAsync(username); - return Statistics.FromSoulseekUserStatistics(soulseekStatistics); + return soulseekStatistics.AsStatistics(); } /// <summary> @@ -166,7 +166,7 @@ namespace slskd.Users public async Task<Status> GetStatusAsync(string username) { var soulseekStatus = await Client.GetUserStatusAsync(username); - return Status.FromSoulseekUserStatus(soulseekStatus); + return soulseekStatus.AsStatus(); } /// <summary> @@ -223,33 +223,33 @@ namespace slskd.Users // 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) => + Client.UserStatisticsChanged += (_, userStatistics) => { - var statistics = Statistics.FromSoulseekUserStatistics(e); + var statistics = userStatistics.AsStatistics(); UserDictionary.AddOrUpdate( - key: e.Username, + key: userStatistics.Username, addValue: new User() { Statistics = statistics }, - updateValueFactory: (key, user) => user with { Username = e.Username, Statistics = statistics }); + updateValueFactory: (key, user) => user with { Username = userStatistics.Username, Statistics = statistics }); 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) => + Client.UserStatusChanged += (_, userStatus) => { - var status = Status.FromSoulseekUserStatus(e); + var status = userStatus.AsStatus(); UserDictionary.AddOrUpdate( - key: e.Username, + key: userStatus.Username, addValue: new User() { Status = status }, - updateValueFactory: (key, user) => user with { Username = e.Username, Status = status }); + updateValueFactory: (key, user) => user with { Username = userStatus.Username, Status = status }); 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. - _ = GetStatisticsAsync(e.Username); + _ = GetStatisticsAsync(userStatus.Username); }; Client.LoggedIn += (_, _) => WatchAllUsers(); |
