From 0d145a7255676f7a81d344dcaaf81e0e12df3b32 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 29 Jun 2022 08:27:02 -0500 Subject: stop sorting info and options --- src/web/src/components/System/Info/index.js | 2 +- src/web/src/components/System/Options/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/web/src/components/System/Info/index.js b/src/web/src/components/System/Info/index.js index 7068e047..a3342ea0 100644 --- a/src/web/src/components/System/Info/index.js +++ b/src/web/src/components/System/Info/index.js @@ -17,7 +17,7 @@ const Info = ({ state }) => { useEffect(() => { setTimeout(() => { - setContents(YAML.stringify(state, { simpleKeys: true, sortMapEntries: true })); + setContents(YAML.stringify(state, { simpleKeys: true, sortMapEntries: false })); }, 250); }, [state]); diff --git a/src/web/src/components/System/Options/index.js b/src/web/src/components/System/Options/index.js index 16928a29..d0872960 100644 --- a/src/web/src/components/System/Options/index.js +++ b/src/web/src/components/System/Options/index.js @@ -19,7 +19,7 @@ const Index = ({ options }) => { useEffect(() => { setTimeout(() => { - setContents(YAML.stringify(options, { simpleKeys: true, sortMapEntries: true })); + setContents(YAML.stringify(options, { simpleKeys: true, sortMapEntries: false })); }, 250); }, [options]); -- cgit v1.2.3 From ac6f113e36476fe25016a5ae073bc18be91301fd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 29 Jun 2022 08:27:30 -0500 Subject: rename extensions, add a mapper for stats --- src/slskd/Core/CoreExtensions.cs | 75 ---------------------------------- src/slskd/Core/Extensions.cs | 88 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 75 deletions(-) delete mode 100644 src/slskd/Core/CoreExtensions.cs create mode 100644 src/slskd/Core/Extensions.cs (limited to 'src') diff --git a/src/slskd/Core/CoreExtensions.cs b/src/slskd/Core/CoreExtensions.cs deleted file mode 100644 index aef187b8..00000000 --- a/src/slskd/Core/CoreExtensions.cs +++ /dev/null @@ -1,75 +0,0 @@ -// -// 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/. -// - -namespace slskd -{ - using System; - using System.Net.Sockets; - using Soulseek; - - /// - /// Core extensions; extensions for types specific to Soulseek or slskd. - /// - public static class CoreExtensions - { - /// - /// Redacts this instance of Options, replacing properties marked with with '*****'. - /// - /// - /// Creates a deep clone before redacting. - /// - /// The Options instance to redact. - /// A redacted instance. - public static Options Redact(this Options options) - { - var redacted = options.ToJson().FromJson(); - Redactor.Redact(redacted, redactWith: "*****"); - return redacted; - } - - /// - /// Creates a copy of this instance with the specified parameters changed. - /// - /// The options instance to copy. - /// The read buffer size for underlying TCP connections. - /// The write buffer size for underlying TCP connections. - /// The size of the write queue for double buffered writes. - /// The connection timeout, in milliseconds, for client and peer TCP connections. - /// The inactivity timeout, in milliseconds, for peer TCP connections. - /// Optional SOCKS 5 proxy configuration options. - /// - /// The delegate invoked during instantiation to configure the server Socket instance. - /// - /// The new instance. - public static ConnectionOptions With( - this ConnectionOptions o, - int? readBufferSize = null, - int? writeBufferSize = null, - int? writeQueueSize = null, - int? connectTimeout = null, - int? inactivityTimeout = null, - ProxyOptions proxyOptions = null, - Action configureSocketAction = null) => new ConnectionOptions( - readBufferSize: readBufferSize ?? o.ReadBufferSize, - writeBufferSize: writeBufferSize ?? o.WriteBufferSize, - writeQueueSize: writeQueueSize ?? o.WriteQueueSize, - connectTimeout: connectTimeout ?? o.ConnectTimeout, - inactivityTimeout: inactivityTimeout ?? o.InactivityTimeout, - proxyOptions: proxyOptions ?? o.ProxyOptions, - configureSocket: configureSocketAction ?? o.ConfigureSocket); - } -} diff --git a/src/slskd/Core/Extensions.cs b/src/slskd/Core/Extensions.cs new file mode 100644 index 00000000..d818d387 --- /dev/null +++ b/src/slskd/Core/Extensions.cs @@ -0,0 +1,88 @@ +// +// 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/. +// + +namespace slskd +{ + using System; + using System.Net.Sockets; + using Soulseek; + + /// + /// Core extensions; extensions for types specific to Soulseek or slskd. + /// + public static class Extensions + { + /// + /// Redacts this instance of Options, replacing properties marked with with '*****'. + /// + /// + /// Creates a deep clone before redacting. + /// + /// The Options instance to redact. + /// A redacted instance. + public static Options Redact(this Options options) + { + var redacted = options.ToJson().FromJson(); + Redactor.Redact(redacted, redactWith: "*****"); + return redacted; + } + + /// + /// Creates a copy of this instance with the specified parameters changed. + /// + /// The options instance to copy. + /// The read buffer size for underlying TCP connections. + /// The write buffer size for underlying TCP connections. + /// The size of the write queue for double buffered writes. + /// The connection timeout, in milliseconds, for client and peer TCP connections. + /// The inactivity timeout, in milliseconds, for peer TCP connections. + /// Optional SOCKS 5 proxy configuration options. + /// + /// The delegate invoked during instantiation to configure the server Socket instance. + /// + /// The new instance. + public static ConnectionOptions With( + this ConnectionOptions o, + int? readBufferSize = null, + int? writeBufferSize = null, + int? writeQueueSize = null, + int? connectTimeout = null, + int? inactivityTimeout = null, + ProxyOptions proxyOptions = null, + Action configureSocketAction = null) => new ConnectionOptions( + readBufferSize: readBufferSize ?? o.ReadBufferSize, + writeBufferSize: writeBufferSize ?? o.WriteBufferSize, + writeQueueSize: writeQueueSize ?? o.WriteQueueSize, + connectTimeout: connectTimeout ?? o.ConnectTimeout, + inactivityTimeout: inactivityTimeout ?? o.InactivityTimeout, + proxyOptions: proxyOptions ?? o.ProxyOptions, + configureSocket: configureSocketAction ?? o.ConfigureSocket); + + /// + /// Creates a new instance of from this instance of . + /// + /// The UserStatistics instance from which to copy data + /// The new instance. + public static UserStatisticsState ToUserStatisticsState(this UserStatistics stats) => new() + { + AverageSpeed = stats.AverageSpeed, + DirectoryCount = stats.DirectoryCount, + FileCount = stats.FileCount, + UploadCount = stats.UploadCount, + }; + } +} -- cgit v1.2.3 From 941c79e26fe45a2fd75ccb30c9c90a39b72c5562 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 29 Jun 2022 08:27:47 -0500 Subject: add UserState and move some things around --- src/slskd/Core/State.cs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/State.cs b/src/slskd/Core/State.cs index b15ce7fa..e009805a 100644 --- a/src/slskd/Core/State.cs +++ b/src/slskd/Core/State.cs @@ -34,6 +34,7 @@ namespace slskd public bool PendingReconnect { get; init; } public bool PendingRestart { get; init; } public ServerState Server { get; init; } = new ServerState(); + public UserState User { get; init; } = new UserState(); public DistributedNetworkState DistributedNetwork { get; init; } = new DistributedNetworkState(); public ShareState Shares { get; init; } = new ShareState(); public string[] Rooms { get; init; } = Array.Empty(); @@ -57,13 +58,32 @@ namespace slskd [JsonConverter(typeof(IPEndPointConverter))] public IPEndPoint IPEndPoint { get; init; } public SoulseekClientStates State { get; init; } - public string Username { get; init; } - public UserStatistics Statistics { get; init; } public bool IsConnected => State.HasFlag(SoulseekClientStates.Connected); public bool IsLoggedIn => State.HasFlag(SoulseekClientStates.LoggedIn); public bool IsTransitioning => State.HasFlag(SoulseekClientStates.Connecting) || State.HasFlag(SoulseekClientStates.Disconnecting) || State.HasFlag(SoulseekClientStates.LoggingIn); } + public record UserState + { + public string Username { get; init; } + public UserPrivilegeState Privileges { get; init; } = new UserPrivilegeState(); + public UserStatisticsState Statistics { get; init; } = new UserStatisticsState(); + } + + public record UserPrivilegeState + { + public bool IsPrivileged { get; init; } + public int PrivilegesRemaining { get; init; } + } + + public record UserStatisticsState + { + public int AverageSpeed { get; init; } + public int DirectoryCount { get; init; } + public int FileCount { get; init; } + public long UploadCount { get; init; } + } + public record DistributedNetworkState { public int BranchLevel { get; init; } -- cgit v1.2.3 From e1fffd3167e63229319f9c5abd98ef14676e1bc5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 29 Jun 2022 08:28:13 -0500 Subject: fix downstream of UserState change --- src/slskd/Messaging/API/Controllers/ConversationsController.cs | 6 +++--- src/slskd/Messaging/API/Controllers/RoomsController.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Messaging/API/Controllers/ConversationsController.cs b/src/slskd/Messaging/API/Controllers/ConversationsController.cs index c4341ec1..492340f0 100644 --- a/src/slskd/Messaging/API/Controllers/ConversationsController.cs +++ b/src/slskd/Messaging/API/Controllers/ConversationsController.cs @@ -152,7 +152,7 @@ namespace slskd.Messaging.API var response = Tracker.Conversations.ToDictionary( entry => entry.Key, entry => entry.Value - .Select(pm => PrivateMessageResponse.FromPrivateMessage(pm, self: pm.Username == ApplicationStateMonitor.CurrentValue.Server.Username)) + .Select(pm => PrivateMessageResponse.FromPrivateMessage(pm, self: pm.Username == ApplicationStateMonitor.CurrentValue.User.Username)) .OrderBy(m => m.Timestamp)); return Ok(response); @@ -174,7 +174,7 @@ namespace slskd.Messaging.API if (Tracker.TryGet(username, out var conversation)) { var response = conversation - .Select(pm => PrivateMessageResponse.FromPrivateMessage(pm, self: pm.Username == ApplicationStateMonitor.CurrentValue.Server.Username)) + .Select(pm => PrivateMessageResponse.FromPrivateMessage(pm, self: pm.Username == ApplicationStateMonitor.CurrentValue.User.Username)) .OrderBy(m => m.Timestamp); return Ok(response); @@ -207,7 +207,7 @@ namespace slskd.Messaging.API // append the outgoing message to the tracker Tracker.AddOrUpdate(username, new PrivateMessage() { - Username = ApplicationStateMonitor.CurrentValue.Server.Username, + Username = ApplicationStateMonitor.CurrentValue.User.Username, Timestamp = DateTime.UtcNow, Message = message, Acknowledged = true, diff --git a/src/slskd/Messaging/API/Controllers/RoomsController.cs b/src/slskd/Messaging/API/Controllers/RoomsController.cs index f22a1354..8a3e5f3f 100644 --- a/src/slskd/Messaging/API/Controllers/RoomsController.cs +++ b/src/slskd/Messaging/API/Controllers/RoomsController.cs @@ -172,7 +172,7 @@ namespace slskd.Messaging.API if (Tracker.TryGet(roomName, out var room)) { var response = room.Users - .Select(user => UserDataResponse.FromUserData(user, self: user.Username == ApplicationStateMonitor.CurrentValue.Server.Username)); + .Select(user => UserDataResponse.FromUserData(user, self: user.Username == ApplicationStateMonitor.CurrentValue.User.Username)); return Ok(response); } @@ -196,7 +196,7 @@ namespace slskd.Messaging.API if (Tracker.TryGet(roomName, out var room)) { var response = room.Messages - .Select(message => RoomMessageResponse.FromRoomMessage(message, self: message.Username == ApplicationStateMonitor.CurrentValue.Server.Username)); + .Select(message => RoomMessageResponse.FromRoomMessage(message, self: message.Username == ApplicationStateMonitor.CurrentValue.User.Username)); return Ok(response); } @@ -293,7 +293,7 @@ namespace slskd.Messaging.API { bool IsSelf(string username) { - return username == ApplicationStateMonitor.CurrentValue.Server.Username; + return username == ApplicationStateMonitor.CurrentValue.User.Username; } var response = RoomResponse.FromRoom(room); -- cgit v1.2.3 From bbbb5385bb6ffae42f2880fc44f455f4c0a0c987 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 29 Jun 2022 08:28:44 -0500 Subject: refresh privileges along with stats, refresh both after a share scan completes --- src/slskd/Application.cs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index d7078bde..477cc572 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -445,17 +445,23 @@ namespace slskd } } - private async Task RefreshUserStatisticsIfNeeded(bool force = false) + private async Task RefreshUserStatistics(bool force = false) { if (force || !Cache.TryGetValue(CacheKeys.UserStatisticsToken, out _)) { var stats = await Client.GetUserStatisticsAsync(OptionsAtStartup.Soulseek.Username); + var privileges = await Client.GetPrivilegesAsync(); State.SetValue(state => state with { - Server = state.Server with + User = state.User with { - Statistics = stats, + Statistics = stats.ToUserStatisticsState(), + Privileges = new UserPrivilegeState() + { + IsPrivileged = privileges > 0, + PrivilegesRemaining = privileges, + }, }, }); @@ -472,7 +478,7 @@ namespace slskd _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Directories, State.CurrentValue.Shares.Files); // fetch our average upload speed from the server, so we can provide it along with search results - _ = RefreshUserStatisticsIfNeeded(force: true); + _ = RefreshUserStatistics(force: true); } private void Client_PrivateMessageRecieved(object sender, PrivateMessageReceivedEventArgs args) @@ -515,6 +521,9 @@ namespace slskd Address = Client.Address, IPEndPoint = Client.IPEndPoint, State = Client.State, + }, + User = state.User with + { Username = Client.Username, }, }); @@ -866,7 +875,7 @@ namespace slskd if (results.Any()) { - await RefreshUserStatisticsIfNeeded(); + await RefreshUserStatistics(); var forecastedPosition = Transfers.Uploads.Queue.ForecastPosition(username); @@ -875,7 +884,7 @@ namespace slskd return new SearchResponse( Client.Username, token, - uploadSpeed: State.CurrentValue.Server.Statistics.AverageSpeed, + uploadSpeed: State.CurrentValue.User.Statistics.AverageSpeed, freeUploadSlots: forecastedPosition == 0 ? 1 : 0, queueLength: forecastedPosition, fileList: results); @@ -924,7 +933,11 @@ namespace slskd if (Client.State.HasFlag(SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn)) { - _ = Client.SetSharedCountsAsync(State.CurrentValue.Shares.Directories, State.CurrentValue.Shares.Files); + _ = Task.Run(async () => + { + await Client.SetSharedCountsAsync(State.CurrentValue.Shares.Directories, State.CurrentValue.Shares.Files); + await RefreshUserStatistics(force: true); + }); } } } -- cgit v1.2.3 From 17416b81dd4f454f8e1e55de875f17402addbf37 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 29 Jun 2022 09:27:49 -0500 Subject: add privilege button and icon --- src/web/src/components/App.js | 8 ++++++-- src/web/src/components/System/Info/index.js | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/web/src/components/App.js b/src/web/src/components/App.js index 206cea34..ea24924a 100644 --- a/src/web/src/components/App.js +++ b/src/web/src/components/App.js @@ -116,7 +116,7 @@ class App extends Component { render = () => { const { login, applicationState = {}, applicationOptions = {}, error, initialized, retriesExhausted } = this.state; - const { version = {}, server, pendingReconnect, pendingRestart, shares = {} } = applicationState; + const { version = {}, server, pendingReconnect, pendingRestart, shares = {}, user } = applicationState; const { isUpdateAvailable, current, latest } = version; const { scanPending: pendingShareRescan } = shares; @@ -203,7 +203,11 @@ class App extends Component { {server?.isConnected && disconnect()} > - Connected + + + {user?.privileges?.isPrivileged && + } + Connected } {(!server?.isConnected) && connect()} diff --git a/src/web/src/components/System/Info/index.js b/src/web/src/components/System/Info/index.js index a3342ea0..a703d4bc 100644 --- a/src/web/src/components/System/Info/index.js +++ b/src/web/src/components/System/Info/index.js @@ -29,19 +29,29 @@ const Info = ({ state }) => {
getVersion({ forceCheck: true })} > Check for Updates + + window.open(`http://www.slsknet.org/qtlogin.php?username=${state?.user?.username}`, '_blank')} + > + Get Privileges +
@@ -58,7 +68,7 @@ const Info = ({ state }) => { trigger={