From 936ed099a6e5258f9fd5a5ac026afc0630359b98 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 13 Feb 2021 07:58:15 -0600 Subject: move controllers and DTOs to API namespace --- src/slskd/API/Controllers/RoomsController.cs | 287 +++++++++++++++++++++++++++ 1 file changed, 287 insertions(+) create mode 100644 src/slskd/API/Controllers/RoomsController.cs (limited to 'src/slskd/API/Controllers/RoomsController.cs') diff --git a/src/slskd/API/Controllers/RoomsController.cs b/src/slskd/API/Controllers/RoomsController.cs new file mode 100644 index 00000000..29693bbc --- /dev/null +++ b/src/slskd/API/Controllers/RoomsController.cs @@ -0,0 +1,287 @@ +namespace slskd.API.Controllers +{ + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Http; + using Microsoft.AspNetCore.Mvc; + using Soulseek; + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading.Tasks; + using slskd.API.DTO; + using slskd.Entities; + using slskd.Trackers; + + /// + /// Rooms. + /// + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class RoomsController : ControllerBase + { + public RoomsController(ISoulseekClient client, IRoomTracker tracker) + { + Client = client; + Tracker = tracker; + } + + private ISoulseekClient Client { get; } + private IRoomTracker Tracker { get; } + + /// + /// Gets all rooms. + /// + /// + /// The request completed successfully. + [HttpGet("joined")] + [Authorize] + [ProducesResponseType(typeof(Dictionary>), 200)] + public IActionResult GetAll() + { + return Ok(Tracker.Rooms.Keys); + } + + /// + /// Gets the specified room. + /// + /// + /// + /// The request completed successfully. + /// The specified roomName could not be found. + [HttpGet("joined/{roomName}")] + [Authorize] + [ProducesResponseType(typeof(Room), 200)] + [ProducesResponseType(404)] + public IActionResult GetByRoomName([FromRoute]string roomName) + { + if (Tracker.TryGet(roomName, out var room)) + { + return Ok(MapRoomToRoomResponse(room)); + } + + return NotFound(); + } + + /// + /// Sends a message to the specified room. + /// + /// + /// + /// + /// The request completed successfully. + /// The specified roomName could not be found. + [HttpPost("joined/{roomName}/messages")] + [Authorize] + [ProducesResponseType(201)] + [ProducesResponseType(404)] + public async Task SendMessage([FromRoute]string roomName, [FromBody]string message) + { + if (Tracker.TryGet(roomName, out var _)) + { + await Client.SendRoomMessageAsync(roomName, message); + return StatusCode(StatusCodes.Status201Created); + } + + return NotFound(); + } + + /// + /// Sets a ticker for the specified room. + /// + /// + /// + /// + /// The request completed successfully. + /// The specified roomName could not be found. + [HttpPost("joined/{roomName}/ticker")] + [Authorize] + [ProducesResponseType(201)] + [ProducesResponseType(404)] + public async Task SetTicker([FromRoute] string roomName, [FromBody] string message) + { + if (Tracker.TryGet(roomName, out var _)) + { + await Client.SetRoomTickerAsync(roomName, message); + return StatusCode(StatusCodes.Status201Created); + } + + return NotFound(); + } + + /// + /// Adds a member to a private room. + /// + /// + /// + /// + /// The request completed successfully. + /// The specified roomName could not be found. + [HttpPost("joined/{roomName}/members")] + [Authorize] + [ProducesResponseType(201)] + [ProducesResponseType(404)] + public async Task AddRoomMember([FromRoute]string roomName, [FromBody]string username) + { + if (Tracker.TryGet(roomName, out var _)) + { + await Client.AddPrivateRoomMemberAsync(roomName, username); + return StatusCode(StatusCodes.Status201Created); + } + + return NotFound(); + } + + /// + /// Gets the current list of users for the specified room. + /// + /// + /// + /// The request completed successfully. + /// The specified roomName could not be found. + [HttpGet("joined/{roomName}/users")] + [Authorize] + [ProducesResponseType(typeof(IList), 200)] + [ProducesResponseType(404)] + public IActionResult GetUsersByRoomName([FromRoute]string roomName) + { + if (Tracker.TryGet(roomName, out var room)) + { + var response = room.Users + .Select(user => UserDataResponse.FromUserData(user, self: user.Username == Client.Username)); + + return Ok(response); + } + + return NotFound(); + } + + /// + /// Gets the current list of messages for the specified room. + /// + /// + /// + /// The request completed successfully. + /// The specified roomName could not be found. + [HttpGet("joined/{roomName}/messages")] + [Authorize] + [ProducesResponseType(typeof(IList), 200)] + [ProducesResponseType(404)] + public IActionResult GetMessagesByRoomName([FromRoute]string roomName) + { + if (Tracker.TryGet(roomName, out var room)) + { + var response = room.Messages + .Select(message => RoomMessageResponse.FromRoomMessage(message, self: message.Username == Client.Username)); + + return Ok(response); + } + + return NotFound(); + } + + /// + /// Gets a list of rooms from the server. + /// + /// + [HttpGet("available")] + [Authorize] + [ProducesResponseType(typeof(List), 200)] + public async Task GetRooms() + { + var list = await Client.GetRoomListAsync(); + + var response = new List(); + + response.AddRange(list.Public.Select(r => RoomInfoResponse.FromRoomInfo(r))); + response.AddRange(list.Private.Select(r => RoomInfoResponse.FromRoomInfo(r, isPrivate: true))); + response.AddRange(list.Owned.Select(r => RoomInfoResponse.FromRoomInfo(r, isPrivate: true, isOwned: true))); + + foreach (var room in response) + { + room.IsModerated = list.ModeratedRoomNames.Contains(room.Name); + } + + return Ok(response); + } + + /// + /// Joins a room. + /// + /// + /// + /// The request completed successfully. + /// The room has already been joined. + [HttpPost("joined/{roomName}")] + [Authorize] + [ProducesResponseType(typeof(Room), 201)] + [ProducesResponseType(304)] + public async Task JoinRoom([FromRoute]string roomName) + { + if (Tracker.Rooms.ContainsKey(roomName)) + { + return StatusCode(StatusCodes.Status304NotModified); + } + + try + { + var roomData = await Client.JoinRoomAsync(roomName); + var room = Room.FromRoomData(roomData); + Tracker.TryAdd(roomName, room); + + return StatusCode(StatusCodes.Status201Created, MapRoomToRoomResponse(room)); + } + catch (Exception ex) + { + if (ex.InnerException is RoomJoinForbiddenException) + { + return StatusCode(StatusCodes.Status403Forbidden, $"The server rejected your request to join {roomName}"); + } + + throw; + } + } + + /// + /// Leaves a room. + /// + /// + /// + /// The request completed successfully. + /// The room has not been joined. + [HttpDelete("joined/{roomName}")] + [Authorize] + [ProducesResponseType(204)] + [ProducesResponseType(404)] + public async Task LeaveRoom([FromRoute]string roomName) + { + if (!Tracker.Rooms.ContainsKey(roomName)) + { + return StatusCode(StatusCodes.Status404NotFound); + } + + await Client.LeaveRoomAsync(roomName); + Tracker.TryRemove(roomName); + + return StatusCode(StatusCodes.Status204NoContent); + } + + private RoomResponse MapRoomToRoomResponse(Room room) + { + bool IsSelf(string username) + { + return username == Client.Username; + } + + var response = RoomResponse.FromRoom(room); + response.Users = room.Users + .Select(user => UserDataResponse.FromUserData(user, self: IsSelf(user.Username))); + response.Messages = room.Messages + .Select(message => RoomMessageResponse.FromRoomMessage(message, self: IsSelf(message.Username))); + + return response; + } + } +} \ No newline at end of file -- cgit v1.2.3