aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-06-29 17:54:45 -0500
committerGitHub <noreply@github.com>2022-06-29 17:54:45 -0500
commit610582ea53ace5c309b18df5570b12c73a43541a (patch)
tree84248ffd4e3b5c261e2fdbd8f52ddc767e469418 /src
parent77df3572c381150dc60454be03ca3176a940a628 (diff)
parent6fd167cca6be37e2c88c811cdbcfa64081aeb268 (diff)
downloadslskd-610582ea53ace5c309b18df5570b12c73a43541a.tar.gz
Merge pull request #538 from slskd/privileges
Track user privileges
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs27
-rw-r--r--src/slskd/Core/Extensions.cs (renamed from src/slskd/Core/CoreExtensions.cs)17
-rw-r--r--src/slskd/Core/State.cs24
-rw-r--r--src/slskd/Messaging/API/Controllers/ConversationsController.cs6
-rw-r--r--src/slskd/Messaging/API/Controllers/RoomsController.cs6
-rw-r--r--src/web/src/components/App.js8
-rw-r--r--src/web/src/components/System/Info/index.js18
-rw-r--r--src/web/src/components/System/Options/index.js2
8 files changed, 84 insertions, 24 deletions
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);
+ });
}
}
}
diff --git a/src/slskd/Core/CoreExtensions.cs b/src/slskd/Core/Extensions.cs
index aef187b8..d818d387 100644
--- a/src/slskd/Core/CoreExtensions.cs
+++ b/src/slskd/Core/Extensions.cs
@@ -1,4 +1,4 @@
-// <copyright file="CoreExtensions.cs" company="slskd Team">
+// <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
@@ -24,7 +24,7 @@ namespace slskd
/// <summary>
/// Core extensions; extensions for types specific to Soulseek or slskd.
/// </summary>
- public static class CoreExtensions
+ public static class Extensions
{
/// <summary>
/// Redacts this instance of Options, replacing properties marked with <see cref="SecretAttribute"/> with '*****'.
@@ -71,5 +71,18 @@ namespace slskd
inactivityTimeout: inactivityTimeout ?? o.InactivityTimeout,
proxyOptions: proxyOptions ?? o.ProxyOptions,
configureSocket: configureSocketAction ?? o.ConfigureSocket);
+
+ /// <summary>
+ /// Creates a new instance of <see cref="UserStatisticsState"/> from this instance of <see cref="UserStatistics"/>.
+ /// </summary>
+ /// <param name="stats">The UserStatistics instance from which to copy data</param>
+ /// <returns>The new instance.</returns>
+ public static UserStatisticsState ToUserStatisticsState(this UserStatistics stats) => new()
+ {
+ AverageSpeed = stats.AverageSpeed,
+ DirectoryCount = stats.DirectoryCount,
+ FileCount = stats.FileCount,
+ UploadCount = stats.UploadCount,
+ };
}
}
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<string>();
@@ -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; }
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);
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 && <Menu.Item
onClick={() => disconnect()}
>
- <Icon name='plug' color={pendingReconnect ? 'yellow' : 'green'}/>Connected
+ <Icon.Group className='menu-icon-group'>
+ <Icon name='plug' color={pendingReconnect ? 'yellow' : 'green'}/>
+ {user?.privileges?.isPrivileged &&
+ <Icon name='star' color='yellow' corner className='menu-icon-no-shadow'/>}
+ </Icon.Group>Connected
</Menu.Item>}
{(!server?.isConnected) && <Menu.Item
onClick={() => connect()}
diff --git a/src/web/src/components/System/Info/index.js b/src/web/src/components/System/Info/index.js
index 7068e047..a703d4bc 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]);
@@ -29,19 +29,29 @@ const Info = ({ state }) => {
<div style={{float: 'left'}}>
<ShrinkableButton
icon='refresh'
- mediaQuery='(max-width: 516px)'
+ mediaQuery='(max-width: 686px)'
primary
disabled={!contents}
onClick={() => getVersion({ forceCheck: true })}
>
Check for Updates
</ShrinkableButton>
+ <ShrinkableButton
+ icon='star'
+ mediaQuery='(max-width: 686px)'
+ color='yellow'
+ disabled={!contents}
+ onClick={() =>
+ window.open(`http://www.slsknet.org/qtlogin.php?username=${state?.user?.username}`, '_blank')}
+ >
+ Get Privileges
+ </ShrinkableButton>
</div>
<Modal
trigger={
<ShrinkableButton
icon='shutdown'
- mediaQuery='(max-width: 516px)'
+ mediaQuery='(max-width: 686px)'
negative
disabled={!contents}
>
@@ -58,7 +68,7 @@ const Info = ({ state }) => {
trigger={
<ShrinkableButton
icon='redo'
- mediaQuery='(max-width: 516px)'
+ mediaQuery='(max-width: 686px)'
negative={!pendingRestart}
color={pendingRestart ? 'yellow' : undefined}
disabled={!contents}
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]);