aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/API/DTO
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-02-13 17:05:59 -0600
committerGitHub <noreply@github.com>2021-02-13 17:05:59 -0600
commit034eff1ad47a4ac976237d4e13c596ba1404cd2f (patch)
tree8d9650d86237fe511a600ae58016b1ff3de3a389 /src/slskd/API/DTO
parentaec49da74c08865353faae4d97fb4e405ae970e5 (diff)
parent1744c9b2ded05a8a83874864957229a3235fb546 (diff)
downloadslskd-034eff1ad47a4ac976237d4e13c596ba1404cd2f.tar.gz
Merge pull request #40 from slskd/housekeeping
Tidy up
Diffstat (limited to 'src/slskd/API/DTO')
-rw-r--r--src/slskd/API/DTO/ConnectRequest.cs10
-rw-r--r--src/slskd/API/DTO/LoginRequest.cs8
-rw-r--r--src/slskd/API/DTO/PrivateMessageResponse.cs57
-rw-r--r--src/slskd/API/DTO/QueueDownloadRequest.cs20
-rw-r--r--src/slskd/API/DTO/RoomInfoResponse.cs43
-rw-r--r--src/slskd/API/DTO/RoomMessageResponse.cs45
-rw-r--r--src/slskd/API/DTO/RoomResponse.cs58
-rw-r--r--src/slskd/API/DTO/SearchRequest.cs99
-rw-r--r--src/slskd/API/DTO/TokenResponse.cs50
-rw-r--r--src/slskd/API/DTO/Transfer.cs131
-rw-r--r--src/slskd/API/DTO/UserAddress.cs18
-rw-r--r--src/slskd/API/DTO/UserDataResponse.cs68
12 files changed, 607 insertions, 0 deletions
diff --git a/src/slskd/API/DTO/ConnectRequest.cs b/src/slskd/API/DTO/ConnectRequest.cs
new file mode 100644
index 00000000..5d90073f
--- /dev/null
+++ b/src/slskd/API/DTO/ConnectRequest.cs
@@ -0,0 +1,10 @@
+namespace slskd.API.DTO
+{
+ public class ConnectRequest
+ {
+ public string Address { get; set; }
+ public int? Port { get; set; }
+ public string Username { get; set; }
+ public string Password { get; set; }
+ }
+}
diff --git a/src/slskd/API/DTO/LoginRequest.cs b/src/slskd/API/DTO/LoginRequest.cs
new file mode 100644
index 00000000..269a489b
--- /dev/null
+++ b/src/slskd/API/DTO/LoginRequest.cs
@@ -0,0 +1,8 @@
+namespace slskd.API.DTO
+{
+ public class LoginRequest
+ {
+ public string Username { get; set; }
+ public string Password { get; set; }
+ }
+}
diff --git a/src/slskd/API/DTO/PrivateMessageResponse.cs b/src/slskd/API/DTO/PrivateMessageResponse.cs
new file mode 100644
index 00000000..e20ce637
--- /dev/null
+++ b/src/slskd/API/DTO/PrivateMessageResponse.cs
@@ -0,0 +1,57 @@
+namespace slskd.API.DTO
+{
+ using System;
+ using slskd.Entities;
+
+ public class PrivateMessageResponse
+ {
+ /// <summary>
+ /// A value indicating whether the message has been acknowledged.
+ /// </summary>
+ public bool Acknowledged { get; set; } = false;
+
+ /// <summary>
+ /// The unique message id, used to acknowledge receipt.
+ /// </summary>
+ public int Id { get; set; }
+
+ /// <summary>
+ /// The message.
+ /// </summary>
+ public string Message { get; set; }
+
+ /// <summary>
+ /// A value indicating whether the message was replayed.
+ /// </summary>
+ public bool Replayed { get; set; }
+
+ /// <summary>
+ /// The UTC timestamp of the message.
+ /// </summary>
+ public DateTime Timestamp { get; set; } = DateTime.UtcNow;
+
+ /// <summary>
+ /// The username of the user who sent the message.
+ /// </summary>
+ public string Username { get; set; }
+
+ /// <summary>
+ /// A value indicating whether this message was sent by the currently logged in user.
+ /// </summary>
+ public bool? Self { get; set; }
+
+ public static PrivateMessageResponse FromPrivateMessage(PrivateMessage privateMessage, bool self = false)
+ {
+ return new PrivateMessageResponse()
+ {
+ Id = privateMessage.Id,
+ Timestamp = privateMessage.Timestamp,
+ Username = privateMessage.Username,
+ Message = privateMessage.Message,
+ Acknowledged = privateMessage.Acknowledged,
+ Replayed = privateMessage.Replayed,
+ Self = self ? self: (bool?)null
+ };
+ }
+ }
+}
diff --git a/src/slskd/API/DTO/QueueDownloadRequest.cs b/src/slskd/API/DTO/QueueDownloadRequest.cs
new file mode 100644
index 00000000..fcd279ab
--- /dev/null
+++ b/src/slskd/API/DTO/QueueDownloadRequest.cs
@@ -0,0 +1,20 @@
+namespace slskd.API.DTO
+{
+ public class QueueDownloadRequest
+ {
+ /// <summary>
+ /// Gets or sets the filename to download.
+ /// </summary>
+ public string Filename { get; set; }
+
+ /// <summary>
+ /// Gets or sets the size of the file.
+ /// </summary>
+ public long? Size { get; set; }
+
+ /// <summary>
+ /// Gets or sets the optional transfer token.
+ /// </summary>
+ public int? Token { get; set; }
+ }
+}
diff --git a/src/slskd/API/DTO/RoomInfoResponse.cs b/src/slskd/API/DTO/RoomInfoResponse.cs
new file mode 100644
index 00000000..8b9a3cb6
--- /dev/null
+++ b/src/slskd/API/DTO/RoomInfoResponse.cs
@@ -0,0 +1,43 @@
+namespace slskd.API.DTO
+{
+ using Soulseek;
+
+ public class RoomInfoResponse
+ {
+ /// <summary>
+ /// Gets the room name.
+ /// </summary>
+ public string Name { get; init; }
+
+ /// <summary>
+ /// Gets the number of users in the room.
+ /// </summary>
+ public int UserCount { get; init; }
+
+ /// <summary>
+ /// Gets a value indicating whether the room is private.
+ /// </summary>
+ public bool IsPrivate { get; init; }
+
+ /// <summary>
+ /// Gets a value indicating whether the room is owned by the currently logged in user.
+ /// </summary>
+ public bool IsOwned { get; init; }
+
+ /// <summary>
+ /// Gets a value indicating whether the room is moderated by the currently logged in user.
+ /// </summary>
+ public bool IsModerated { get; set; }
+
+ public static RoomInfoResponse FromRoomInfo(RoomInfo info, bool isPrivate = false, bool isOwned = false)
+ {
+ return new RoomInfoResponse()
+ {
+ Name = info.Name,
+ UserCount = info.UserCount,
+ IsPrivate = isPrivate,
+ IsOwned = isOwned
+ };
+ }
+ }
+}
diff --git a/src/slskd/API/DTO/RoomMessageResponse.cs b/src/slskd/API/DTO/RoomMessageResponse.cs
new file mode 100644
index 00000000..3a804b65
--- /dev/null
+++ b/src/slskd/API/DTO/RoomMessageResponse.cs
@@ -0,0 +1,45 @@
+namespace slskd.API.DTO
+{
+ using System;
+ using slskd.Entities;
+
+ public class RoomMessageResponse
+ {
+ /// <summary>
+ /// The timestamp of the message.
+ /// </summary>
+ public DateTime Timestamp { get; set; } = DateTime.UtcNow;
+
+ /// <summary>
+ /// The username of the user who sent the message.
+ /// </summary>
+ public string Username { get; set; }
+
+ /// <summary>
+ /// The message.
+ /// </summary>
+ public string Message { get; set; }
+
+ /// <summary>
+ /// The room to which the message pertains.
+ /// </summary>
+ public string RoomName { get; set; }
+
+ /// <summary>
+ /// A value indicating whether this user data belongs to the currently logged in user.
+ /// </summary>
+ public bool? Self { get; set; }
+
+ public static RoomMessageResponse FromRoomMessage(RoomMessage roomMessage, bool self = false)
+ {
+ return new RoomMessageResponse()
+ {
+ Timestamp = roomMessage.Timestamp,
+ Username = roomMessage.Username,
+ Message = roomMessage.Message,
+ RoomName = roomMessage.RoomName,
+ Self = self ? self : (bool?)null
+ };
+ }
+ }
+}
diff --git a/src/slskd/API/DTO/RoomResponse.cs b/src/slskd/API/DTO/RoomResponse.cs
new file mode 100644
index 00000000..d7708da7
--- /dev/null
+++ b/src/slskd/API/DTO/RoomResponse.cs
@@ -0,0 +1,58 @@
+namespace slskd.API.DTO
+{
+ using slskd.Entities;
+ using System.Collections.Generic;
+ using System.Linq;
+
+ public class RoomResponse
+ {
+ /// <summary>
+ /// The room name.
+ /// </summary>
+ public string Name { get; set; }
+
+ /// <summary>
+ /// A value indicating whether the room is private.
+ /// </summary>
+ public bool IsPrivate { get; set; }
+
+ /// <summary>
+ /// The number of operators in the room, if private.
+ /// </summary>
+ public int? OperatorCount { get; set; }
+
+ /// <summary>
+ /// The operators in the room, if private.
+ /// </summary>
+ public IList<string> Operators { get; set; }
+
+ /// <summary>
+ /// The owner of the room, if private.
+ /// </summary>
+ public string Owner { get; set; }
+
+ /// <summary>
+ /// The list of users in the room.
+ /// </summary>
+ public IEnumerable<UserDataResponse> Users { get; set; } = new List<UserDataResponse>();
+
+ /// <summary>
+ /// The list of messages.
+ /// </summary>
+ public IEnumerable<RoomMessageResponse> Messages { get; set; } = new List<RoomMessageResponse>();
+
+ public static RoomResponse FromRoom(Room room)
+ {
+ return new RoomResponse()
+ {
+ Name = room.Name,
+ IsPrivate = room.IsPrivate,
+ OperatorCount = room.OperatorCount,
+ Operators = room.Operators?.ToList(),
+ Owner = room.Owner,
+ Users = new List<UserDataResponse>(),
+ Messages = new List<RoomMessageResponse>()
+ };
+ }
+ }
+}
diff --git a/src/slskd/API/DTO/SearchRequest.cs b/src/slskd/API/DTO/SearchRequest.cs
new file mode 100644
index 00000000..2e6c61e6
--- /dev/null
+++ b/src/slskd/API/DTO/SearchRequest.cs
@@ -0,0 +1,99 @@
+using Soulseek;
+using System;
+
+namespace slskd.API.DTO
+{
+ /// <summary>
+ /// A search request.
+ /// </summary>
+ public class SearchRequest
+ {
+ /// <summary>
+ /// Gets or sets the unique search identifier
+ /// </summary>
+ public Guid? Id { get; set; }
+
+ /// <summary>
+ /// Gets or sets the maximum number of file results to accept before the search is considered complete. (Default = 10,000).
+ /// </summary>
+ public int? FileLimit { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether responses are to be filtered. (Default = true).
+ /// </summary>
+ public bool? FilterResponses { get; set; }
+
+ /// <summary>
+ /// Gets or sets the maximum queue depth a peer may have in order for a response to be processed. (Default = 1000000).
+ /// </summary>
+ public int? MaximumPeerQueueLength { get; set; }
+
+ /// <summary>
+ /// Gets or sets the minimum number of free upload slots a peer must have in order for a response to be processed.
+ /// (Default = 0).
+ /// </summary>
+ public int? MinimumPeerFreeUploadSlots { get; set; }
+
+ /// <summary>
+ /// Gets or sets the minimum upload speed a peer must have in order for a response to be processed. (Default = 0).
+ /// </summary>
+ public int? MinimumPeerUploadSpeed { get; set; }
+
+ /// <summary>
+ /// Gets or sets the minimum number of files a response must contain in order to be processed. (Default = 1).
+ /// </summary>
+ public int? MinimumResponseFileCount { get; set; }
+
+ /// <summary>
+ /// Gets or sets the maximum number of search results to accept before the search is considered complete. (Default = 100).
+ /// </summary>
+ public int? ResponseLimit { get; set; }
+
+ /// <summary>
+ /// Gets or sets the search text.
+ /// </summary>
+ public string SearchText { get; set; }
+
+ /// <summary>
+ /// Gets or sets the search timeout value, in seconds, used to determine when the search is complete. (Default = 15).
+ /// </summary>
+ /// <remarks>The timeout duration is from the time of the last response.</remarks>
+ public int? SearchTimeout { get; set; }
+
+ /// <summary>
+ /// Gets or sets the search token.
+ /// </summary>
+ public int? Token { get; set; }
+
+ /// <summary>
+ /// Maps to a new instance of <see cref="SearchOptions"/>.
+ /// </summary>
+ /// <param name="responseFilter"></param>
+ /// <param name="fileFilter"></param>
+ /// <param name="stateChanged"></param>
+ /// <param name="responseReceived"></param>
+ /// <returns></returns>
+ public SearchOptions ToSearchOptions(
+ Func<SearchResponse, bool> responseFilter = null,
+ Func<File, bool> fileFilter = null,
+ Action<SearchStateChangedEventArgs> stateChanged = null,
+ Action<SearchResponseReceivedEventArgs> responseReceived = null)
+ {
+ var def = new SearchOptions();
+
+ return new SearchOptions(
+ searchTimeout: SearchTimeout ?? def.SearchTimeout,
+ responseLimit: ResponseLimit ?? def.ResponseLimit,
+ fileLimit: FileLimit ?? def.FileLimit,
+ filterResponses: FilterResponses ?? def.FilterResponses,
+ minimumResponseFileCount: MinimumResponseFileCount ?? def.MinimumResponseFileCount,
+ minimumPeerFreeUploadSlots: MinimumPeerFreeUploadSlots ?? def.MinimumPeerFreeUploadSlots,
+ maximumPeerQueueLength: MaximumPeerQueueLength ?? def.MaximumPeerQueueLength,
+ minimumPeerUploadSpeed: MinimumPeerUploadSpeed ?? def.MinimumPeerUploadSpeed,
+ responseFilter: responseFilter,
+ fileFilter: fileFilter,
+ responseReceived: responseReceived,
+ stateChanged: stateChanged);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/slskd/API/DTO/TokenResponse.cs b/src/slskd/API/DTO/TokenResponse.cs
new file mode 100644
index 00000000..3490d570
--- /dev/null
+++ b/src/slskd/API/DTO/TokenResponse.cs
@@ -0,0 +1,50 @@
+namespace slskd.API.DTO
+{
+ using System;
+ using System.IdentityModel.Tokens.Jwt;
+ using System.Linq;
+ using System.Security.Claims;
+ using System.Text.Json.Serialization;
+ using Microsoft.AspNetCore.Authentication.JwtBearer;
+
+ public class TokenResponse
+ {
+ public TokenResponse(JwtSecurityToken jwtSecurityToken)
+ {
+ JwtSecurityToken = jwtSecurityToken;
+ }
+
+ [JsonIgnore]
+ private JwtSecurityToken JwtSecurityToken { get; }
+
+ /// <summary>
+ /// Gets the Access Token string.
+ /// </summary>
+ public string Token => new JwtSecurityTokenHandler().WriteToken(JwtSecurityToken);
+
+ /// <summary>
+ /// Gets the time at which the Access Token expires.
+ /// </summary>
+ public long Expires => ((DateTimeOffset)JwtSecurityToken.ValidTo).ToUnixTimeSeconds();
+
+ /// <summary>
+ /// Gets the time at which the Access Token was issued.
+ /// </summary>
+ public long Issued => ((DateTimeOffset)JwtSecurityToken.ValidFrom).ToUnixTimeSeconds();
+
+ /// <summary>
+ /// Gets the value of the Name claim from the Access Token.
+ /// </summary>
+ public string Name => JwtSecurityToken.Claims.Where(c => c.Type == ClaimTypes.Name).SingleOrDefault().Value;
+
+ /// <summary>
+ /// Gets the value of the Not Before claim from the Access Token.
+ /// </summary>
+ public long NotBefore => long.Parse(JwtSecurityToken.Claims.Where(c => c.Type == "nbf").SingleOrDefault().Value);
+
+ /// <summary>
+ /// Gets the Token type.
+ /// </summary>
+ public string TokenType => JwtBearerDefaults.AuthenticationScheme;
+ }
+} \ No newline at end of file
diff --git a/src/slskd/API/DTO/Transfer.cs b/src/slskd/API/DTO/Transfer.cs
new file mode 100644
index 00000000..34639a40
--- /dev/null
+++ b/src/slskd/API/DTO/Transfer.cs
@@ -0,0 +1,131 @@
+namespace slskd.API.DTO
+{
+ using Soulseek;
+ using System;
+ using System.Net;
+
+ /// <summary>
+ /// A single file transfer.
+ /// </summary>
+ public class Transfer
+ {
+ /// <summary>
+ /// Gets the current average transfer speed.
+ /// </summary>
+ public double AverageSpeed { get; set; }
+
+ /// <summary>
+ /// Gets the number of remaining bytes to be transferred.
+ /// </summary>
+ public long BytesRemaining { get; set; }
+
+ /// <summary>
+ /// Gets the total number of bytes transferred.
+ /// </summary>
+ public long BytesTransferred { get; set; }
+
+ /// <summary>
+ /// Gets the transfer direction.
+ /// </summary>
+ public TransferDirection Direction { get; set; }
+
+ /// <summary>
+ /// Gets the current duration of the transfer, if it has been started.
+ /// </summary>
+ public double? ElapsedTime { get; set; }
+
+ /// <summary>
+ /// Gets the UTC time at which the transfer transitioned into the <see cref="TransferStates.Completed"/> state.
+ /// </summary>
+ public DateTime? EndTime { get; set; }
+
+ /// <summary>
+ /// Gets the filename of the file to be transferred.
+ /// </summary>
+ public string Filename { get; set; }
+
+ /// <summary>
+ /// Gets the transfer id.
+ /// </summary>
+ public string Id => Filename.Sha1();
+
+ /// <summary>
+ /// Gets the ip endpoint of the remote transfer connection, if one has been established.
+ /// </summary>
+ public IPEndPoint IPEndPoint { get; set; }
+
+ /// <summary>
+ /// Gets the current progress in percent.
+ /// </summary>
+ public double PercentComplete { get; set; }
+
+ /// <summary>
+ /// Gets the current place in queue, if it has been fetched.
+ /// </summary>
+ public int? PlaceInQueue { get; set; }
+
+ /// <summary>
+ /// Gets the projected remaining duration of the transfer.
+ /// </summary>
+ public double? RemainingTime { get; set; }
+
+ /// <summary>
+ /// Gets the remote unique token for the transfer.
+ /// </summary>
+ public int? RemoteToken { get; set; }
+
+ /// <summary>
+ /// Gets the size of the file to be transferred, in bytes.
+ /// </summary>
+ public long Size { get; set; }
+
+ /// <summary>
+ /// Gets the starting offset of the transfer, in bytes.
+ /// </summary>
+ public long StartOffset { get; set; }
+
+ /// <summary>
+ /// Gets the UTC time at which the transfer transitioned into the <see cref="TransferStates.InProgress"/> state.
+ /// </summary>
+ public DateTime? StartTime { get; set; }
+
+ /// <summary>
+ /// Gets the state of the transfer.
+ /// </summary>
+ public TransferStates State { get; set; }
+
+ /// <summary>
+ /// Gets the unique token for the transfer.
+ /// </summary>
+ public int Token { get; set; }
+
+ /// <summary>
+ /// Gets the username of the peer to or from which the file is to be transferred.
+ /// </summary>
+ public string Username { get; set; }
+
+ public static Transfer FromSoulseekTransfer(Soulseek.Transfer transfer)
+ {
+ return new Transfer()
+ {
+ AverageSpeed = transfer.AverageSpeed,
+ BytesRemaining = transfer.BytesRemaining,
+ BytesTransferred = transfer.BytesTransferred,
+ Direction = transfer.Direction,
+ ElapsedTime = transfer.ElapsedTime?.TotalMilliseconds,
+ EndTime = transfer.EndTime,
+ Filename = transfer.Filename,
+ IPEndPoint = transfer.IPEndPoint,
+ PercentComplete = transfer.PercentComplete,
+ RemainingTime = transfer.RemainingTime?.TotalMilliseconds,
+ RemoteToken = transfer.RemoteToken,
+ Size = transfer.Size,
+ StartOffset = transfer.StartOffset,
+ StartTime = transfer.StartTime,
+ State = transfer.State,
+ Token = transfer.Token,
+ Username = transfer.Username
+ };
+ }
+ }
+} \ No newline at end of file
diff --git a/src/slskd/API/DTO/UserAddress.cs b/src/slskd/API/DTO/UserAddress.cs
new file mode 100644
index 00000000..4fe7dc4a
--- /dev/null
+++ b/src/slskd/API/DTO/UserAddress.cs
@@ -0,0 +1,18 @@
+namespace slskd.API.DTO
+{
+ /// <summary>
+ /// A user's IP address and port.
+ /// </summary>
+ public class UserAddress
+ {
+ /// <summary>
+ /// Gets or sets the IP address.
+ /// </summary>
+ public string IPAddress { get; set; }
+
+ /// <summary>
+ /// Gets or sets the port.
+ /// </summary>
+ public int Port { get; set; }
+ }
+}
diff --git a/src/slskd/API/DTO/UserDataResponse.cs b/src/slskd/API/DTO/UserDataResponse.cs
new file mode 100644
index 00000000..5d79639c
--- /dev/null
+++ b/src/slskd/API/DTO/UserDataResponse.cs
@@ -0,0 +1,68 @@
+namespace slskd.API.DTO
+{
+ using Soulseek;
+
+ public class UserDataResponse
+ {
+ /// <summary>
+ /// The average upload speed of the user.
+ /// </summary>
+ public int AverageSpeed { get; set; }
+
+ /// <summary>
+ /// The user's country code, if provided.
+ /// </summary>
+ public string CountryCode { get; set; }
+
+ /// <summary>
+ /// The number of directories shared by the user.
+ /// </summary>
+ public int DirectoryCount { get; set; }
+
+ /// <summary>
+ /// The number of active user downloads.
+ /// </summary>
+ public long DownloadCount { get; set; }
+
+ /// <summary>
+ /// The number of files shared by the user.
+ /// </summary>
+ public int FileCount { get; set; }
+
+ /// <summary>
+ /// A value indicating whether this user data belongs to the currently logged in user.
+ /// </summary>
+ public bool? Self { get; set; }
+
+ /// <summary>
+ /// The number of the user's free download slots, if provided.
+ /// </summary>
+ public int? SlotsFree { get; set; }
+
+ /// <summary>
+ /// The status of the user (0 = offline, 1 = away, 2 = online).
+ /// </summary>
+ public UserPresence Status { get; set; }
+
+ /// <summary>
+ /// The username of the user.
+ /// </summary>
+ public string Username { get; set; }
+
+ public static UserDataResponse FromUserData(UserData userData, bool self = false)
+ {
+ return new UserDataResponse()
+ {
+ AverageSpeed = userData.AverageSpeed,
+ CountryCode = userData.CountryCode,
+ DirectoryCount = userData.DirectoryCount,
+ DownloadCount = userData.DownloadCount,
+ FileCount = userData.FileCount,
+ SlotsFree = userData.SlotsFree,
+ Status = userData.Status,
+ Username = userData.Username,
+ Self = self ? self : (bool?)null
+ };
+ }
+ }
+} \ No newline at end of file