diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-02-13 07:58:15 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-02-13 07:58:15 -0600 |
| commit | 936ed099a6e5258f9fd5a5ac026afc0630359b98 (patch) | |
| tree | f406e1f1b9fc29391e471c65b708ba6d94b72027 /src/slskd/API/Controllers/UserController.cs | |
| parent | 87f024fa2561f751fc26c129bfaa373a966f1c67 (diff) | |
| download | slskd-936ed099a6e5258f9fd5a5ac026afc0630359b98.tar.gz | |
move controllers and DTOs to API namespace
Diffstat (limited to 'src/slskd/API/Controllers/UserController.cs')
| -rw-r--r-- | src/slskd/API/Controllers/UserController.cs | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/slskd/API/Controllers/UserController.cs b/src/slskd/API/Controllers/UserController.cs new file mode 100644 index 00000000..73efca0f --- /dev/null +++ b/src/slskd/API/Controllers/UserController.cs @@ -0,0 +1,151 @@ +namespace slskd.API.Controllers +{ + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + using Soulseek; + using slskd.API.DTO; + using slskd.Trackers; + + /// <summary> + /// Users + /// </summary> + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class UserController : ControllerBase + { + /// <summary> + /// Initializes a new instance of the <see cref="UserController"/> class. + /// </summary> + /// <param name="client"></param> + /// <param name="browseTracker"></param> + public UserController(ISoulseekClient client, IBrowseTracker browseTracker) + { + Client = client; + BrowseTracker = browseTracker; + } + + private ISoulseekClient Client { get; } + private IBrowseTracker BrowseTracker { get; } + + /// <summary> + /// Retrieves the address of the specified <paramref name="username"/>. + /// </summary> + /// <param name="username">The username of the user.</param> + /// <returns></returns> + /// <response code="200">The request completed successfully.</response> + [HttpGet("{username}/address")] + [Authorize] + [ProducesResponseType(typeof(UserAddress), 200)] + [ProducesResponseType(404)] + public async Task<IActionResult> Address([FromRoute, Required]string username) + { + try + { + var endpoint = await Client.GetUserEndPointAsync(username); + return Ok(new UserAddress() { IPAddress = endpoint.Address.ToString(), Port = endpoint.Port }); + } + catch (UserOfflineException ex) + { + return NotFound(ex.Message); + } + } + + /// <summary> + /// Retrieves the files shared by the specified <paramref name="username"/>. + /// </summary> + /// <param name="username">The username of the user.</param> + /// <returns></returns> + [HttpGet("{username}/browse")] + [Authorize] + [ProducesResponseType(typeof(IEnumerable<Directory>), 200)] + [ProducesResponseType(404)] + public async Task<IActionResult> Browse([FromRoute, Required]string username) + { + try + { + var result = await Client.BrowseAsync(username); + + _ = Task.Run(async () => + { + await Task.Delay(5000); + BrowseTracker.TryRemove(username); + }); + + return Ok(result); + } + catch (UserOfflineException ex) + { + return NotFound(ex.Message); + } + } + + /// <summary> + /// Retrieves the status of the current browse operation for the specified <paramref name="username"/>, if any. + /// </summary> + /// <param name="username">The username of the user.</param> + /// <returns></returns> + [HttpGet("{username}/browse/status")] + [Authorize] + [ProducesResponseType(typeof(decimal), 200)] + [ProducesResponseType(404)] + public IActionResult BrowseStatus([FromRoute, Required]string username) + { + if (BrowseTracker.TryGet(username, out var progress)) + { + return Ok(progress); + } + + return NotFound(); + } + + /// <summary> + /// Retrieves information about the specified <paramref name="username"/>. + /// </summary> + /// <param name="username">The username of the user.</param> + /// <returns></returns> + [HttpGet("{username}/info")] + [Authorize] + [ProducesResponseType(typeof(UserInfo), 200)] + [ProducesResponseType(404)] + public async Task<IActionResult> Info([FromRoute, Required]string username) + { + try + { + var response = await Client.GetUserInfoAsync(username); + return Ok(response); + } + catch (UserOfflineException ex) + { + return NotFound(ex.Message); + } + } + + /// <summary> + /// Retrieves status for the specified <paramref name="username"/>. + /// </summary> + /// <param name="username">The username of the user.</param> + /// <returns></returns> + [HttpGet("{username}/status")] + [Authorize] + [ProducesResponseType(typeof(UserStatus), 200)] + [ProducesResponseType(404)] + public async Task<IActionResult> Status([FromRoute, Required]string username) + { + try + { + var response = await Client.GetUserStatusAsync(username); + return Ok(response); + } + catch (UserOfflineException ex) + { + return NotFound(ex.Message); + } + } + } +}
\ No newline at end of file |
