aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Application/Messaging/API/Controllers
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-09-21 08:28:11 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-09-21 08:28:11 -0500
commit0eb773dbd4aa2fe971bc55e5c5f12413003296bb (patch)
tree7e36184eb454ed8b0f5f0cbce07c53c191a0a3c1 /src/slskd/Application/Messaging/API/Controllers
parent7387d568548af0898d9fd2b716d3bb16b6d9be2b (diff)
downloadslskd-0eb773dbd4aa2fe971bc55e5c5f12413003296bb.tar.gz
move stuff again
Diffstat (limited to 'src/slskd/Application/Messaging/API/Controllers')
-rw-r--r--src/slskd/Application/Messaging/API/Controllers/ConversationsController.cs219
-rw-r--r--src/slskd/Application/Messaging/API/Controllers/PublicChatController.cs67
-rw-r--r--src/slskd/Application/Messaging/API/Controllers/RoomsController.cs309
3 files changed, 0 insertions, 595 deletions
diff --git a/src/slskd/Application/Messaging/API/Controllers/ConversationsController.cs b/src/slskd/Application/Messaging/API/Controllers/ConversationsController.cs
deleted file mode 100644
index c4341ec1..00000000
--- a/src/slskd/Application/Messaging/API/Controllers/ConversationsController.cs
+++ /dev/null
@@ -1,219 +0,0 @@
-// <copyright file="ConversationsController.cs" company="slskd Team">
-// 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/.
-// </copyright>
-
-namespace slskd.Messaging.API
-{
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using Soulseek;
-
- /// <summary>
- /// Conversations.
- /// </summary>
- [Route("api/v{version:apiVersion}/[controller]")]
- [ApiVersion("0")]
- [ApiController]
- [Produces("application/json")]
- [Consumes("application/json")]
- public class ConversationsController : ControllerBase
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="ConversationsController"/> class.
- /// </summary>
- /// <param name="soulseekClient"></param>
- /// <param name="applicationStateMonotor"></param>
- /// <param name="tracker"></param>
- public ConversationsController(
- ISoulseekClient soulseekClient,
- IStateMonitor<State> applicationStateMonotor,
- IConversationTracker tracker)
- {
- Client = soulseekClient;
- ApplicationStateMonitor = applicationStateMonotor;
- Tracker = tracker;
- }
-
- private ISoulseekClient Client { get; }
- private IStateMonitor<State> ApplicationStateMonitor { get; }
- private IConversationTracker Tracker { get; }
-
- /// <summary>
- /// Acknowledges the given message id for the given username.
- /// </summary>
- /// <param name="username"></param>
- /// <param name="id"></param>
- /// <returns></returns>
- /// <response code="200">The request completed successfully.</response>
- /// <response code="404">
- /// A conversation with the specified username, or a message matching the specified id could not be found.
- /// </response>
- [HttpPut("{username}/{id}")]
- [Authorize]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> Acknowledge([FromRoute]string username, [FromRoute]int id)
- {
- Tracker.Conversations.TryGetValue(username, out var conversation);
-
- if (conversation == default || !conversation.Any(p => p.Id == id))
- {
- return NotFound();
- }
-
- await Client.AcknowledgePrivateMessageAsync(id);
- return StatusCode(200);
- }
-
- /// <summary>
- /// Acknowledges all messages for the given username.
- /// </summary>
- /// <param name="username"></param>
- /// <returns></returns>
- /// <response code="200">The request completed successfully.</response>
- /// <response code="404">A conversation with the specified username could not be found.</response>
- [HttpPut("{username}")]
- [Authorize]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> AcknowledgeAll([FromRoute]string username)
- {
- Tracker.Conversations.TryGetValue(username, out var conversation);
-
- if (conversation == default)
- {
- return NotFound();
- }
-
- var tasks = new List<Task>();
-
- foreach (var message in conversation.Where(p => !p.Acknowledged))
- {
- tasks.Add(Task.Run(async () =>
- {
- await Client.AcknowledgePrivateMessageAsync(message.Id);
- message.Acknowledged = true;
- }));
- }
-
- await Task.WhenAll(tasks);
- return StatusCode(200);
- }
-
- /// <summary>
- /// Deletes the conversation associated with the given username.
- /// </summary>
- /// <returns></returns>
- /// <response code="204">The request completed successfully.</response>
- /// <response code="404">A conversation with the specified username could not be found.</response>
- [HttpDelete("{username}")]
- [Authorize]
- [ProducesResponseType(404)]
- [ProducesResponseType(204)]
- public IActionResult Delete([FromRoute]string username)
- {
- var deleted = Tracker.Conversations.TryRemove(username, out _);
-
- if (deleted)
- {
- return StatusCode(204);
- }
-
- return StatusCode(404);
- }
-
- /// <summary>
- /// Gets all tracked conversations.
- /// </summary>
- /// <returns></returns>
- /// <response code="200">The request completed successfully.</response>
- [HttpGet("")]
- [Authorize]
- [ProducesResponseType(typeof(Dictionary<string, List<PrivateMessageResponse>>), 200)]
- public IActionResult GetAll()
- {
- var response = Tracker.Conversations.ToDictionary(
- entry => entry.Key,
- entry => entry.Value
- .Select(pm => PrivateMessageResponse.FromPrivateMessage(pm, self: pm.Username == ApplicationStateMonitor.CurrentValue.Server.Username))
- .OrderBy(m => m.Timestamp));
-
- return Ok(response);
- }
-
- /// <summary>
- /// Gets the conversation associated with the specified username.
- /// </summary>
- /// <param name="username">The username associated with the desired conversation.</param>
- /// <returns></returns>
- /// <response code="200">The request completed successfully.</response>
- /// <response code="404">A matching search was not found.</response>
- [HttpGet("{username}")]
- [Authorize]
- [ProducesResponseType(typeof(List<PrivateMessageResponse>), 200)]
- [ProducesResponseType(404)]
- public IActionResult GetByUsername([FromRoute]string username)
- {
- if (Tracker.TryGet(username, out var conversation))
- {
- var response = conversation
- .Select(pm => PrivateMessageResponse.FromPrivateMessage(pm, self: pm.Username == ApplicationStateMonitor.CurrentValue.Server.Username))
- .OrderBy(m => m.Timestamp);
-
- return Ok(response);
- }
-
- return NotFound();
- }
-
- /// <summary>
- /// Sends a private message to the specified username.
- /// </summary>
- /// <param name="username"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- /// <response code="201">The request completed successfully.</response>
- /// <response code="400">The specified message is null or empty.</response>
- [HttpPost("{username}")]
- [Authorize]
- [ProducesResponseType(201)]
- [ProducesResponseType(400)]
- public async Task<IActionResult> Send([FromRoute]string username, [FromBody]string message)
- {
- if (string.IsNullOrEmpty(message))
- {
- return BadRequest();
- }
-
- await Client.SendPrivateMessageAsync(username, message);
-
- // append the outgoing message to the tracker
- Tracker.AddOrUpdate(username, new PrivateMessage()
- {
- Username = ApplicationStateMonitor.CurrentValue.Server.Username,
- Timestamp = DateTime.UtcNow,
- Message = message,
- Acknowledged = true,
- });
-
- return StatusCode(201);
- }
- }
-} \ No newline at end of file
diff --git a/src/slskd/Application/Messaging/API/Controllers/PublicChatController.cs b/src/slskd/Application/Messaging/API/Controllers/PublicChatController.cs
deleted file mode 100644
index c30e08a8..00000000
--- a/src/slskd/Application/Messaging/API/Controllers/PublicChatController.cs
+++ /dev/null
@@ -1,67 +0,0 @@
-// <copyright file="PublicChatController.cs" company="slskd Team">
-// 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/.
-// </copyright>
-
-namespace slskd.Messaging.API
-{
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Soulseek;
-
- /// <summary>
- /// Server.
- /// </summary>
- [Route("api/v{version:apiVersion}/[controller]")]
- [ApiVersion("0")]
- [ApiController]
- [Produces("application/json")]
- [Consumes("application/json")]
- public class PublicChatController : ControllerBase
- {
- public PublicChatController(ISoulseekClient soulseekClient)
- {
- Client = soulseekClient;
- }
-
- private ISoulseekClient Client { get; }
-
- /// <summary>
- /// Starts public chat.
- /// </summary>
- /// <returns></returns>
- [HttpPost]
- [Authorize]
- public async Task<IActionResult> Start()
- {
- await Client.StartPublicChatAsync();
- return StatusCode(StatusCodes.Status201Created);
- }
-
- /// <summary>
- /// Stops public chat.
- /// </summary>
- /// <returns></returns>
- [HttpDelete]
- [Authorize]
- public async Task<IActionResult> Stop()
- {
- await Client.StopPublicChatAsync();
- return StatusCode(StatusCodes.Status204NoContent);
- }
- }
-}
diff --git a/src/slskd/Application/Messaging/API/Controllers/RoomsController.cs b/src/slskd/Application/Messaging/API/Controllers/RoomsController.cs
deleted file mode 100644
index 743df527..00000000
--- a/src/slskd/Application/Messaging/API/Controllers/RoomsController.cs
+++ /dev/null
@@ -1,309 +0,0 @@
-// <copyright file="RoomsController.cs" company="slskd Team">
-// 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/.
-// </copyright>
-
-namespace slskd.Messaging.API
-{
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Soulseek;
-
- /// <summary>
- /// Rooms.
- /// </summary>
- [Route("api/v{version:apiVersion}/[controller]")]
- [ApiVersion("0")]
- [ApiController]
- [Produces("application/json")]
- [Consumes("application/json")]
- public class RoomsController : ControllerBase
- {
- public RoomsController(
- ISoulseekClient soulseekClient,
- IRoomService roomService,
- IStateMonitor<State> applicationStateMonitor,
- IRoomTracker tracker)
- {
- Client = soulseekClient;
- ApplicationStateMonitor = applicationStateMonitor;
- Tracker = tracker;
- RoomService = roomService;
- }
-
- private IRoomService RoomService { get; }
- private ISoulseekClient Client { get; }
- private IStateMonitor<State> ApplicationStateMonitor { get; }
- private IRoomTracker Tracker { get; }
-
- /// <summary>
- /// Gets all rooms.
- /// </summary>
- /// <returns></returns>
- /// <response code="200">The request completed successfully.</response>
- [HttpGet("joined")]
- [Authorize]
- [ProducesResponseType(typeof(Dictionary<string, Dictionary<string, Room>>), 200)]
- public IActionResult GetAll()
- {
- return Ok(Tracker.Rooms.Keys);
- }
-
- /// <summary>
- /// Gets the specified room.
- /// </summary>
- /// <param name="roomName"></param>
- /// <returns></returns>
- /// <response code="200">The request completed successfully.</response>
- /// <response code="404">The specified roomName could not be found.</response>
- [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();
- }
-
- /// <summary>
- /// Sends a message to the specified room.
- /// </summary>
- /// <param name="roomName"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- /// <response code="201">The request completed successfully.</response>
- /// <response code="404">The specified roomName could not be found.</response>
- [HttpPost("joined/{roomName}/messages")]
- [Authorize]
- [ProducesResponseType(201)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> SendMessage([FromRoute]string roomName, [FromBody]string message)
- {
- if (Tracker.TryGet(roomName, out var _))
- {
- await Client.SendRoomMessageAsync(roomName, message);
- return StatusCode(StatusCodes.Status201Created);
- }
-
- return NotFound();
- }
-
- /// <summary>
- /// Sets a ticker for the specified room.
- /// </summary>
- /// <param name="roomName"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- /// <response code="201">The request completed successfully.</response>
- /// <response code="404">The specified roomName could not be found.</response>
- [HttpPost("joined/{roomName}/ticker")]
- [Authorize]
- [ProducesResponseType(201)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> SetTicker([FromRoute] string roomName, [FromBody] string message)
- {
- if (Tracker.TryGet(roomName, out var _))
- {
- await Client.SetRoomTickerAsync(roomName, message);
- return StatusCode(StatusCodes.Status201Created);
- }
-
- return NotFound();
- }
-
- /// <summary>
- /// Adds a member to a private room.
- /// </summary>
- /// <param name="roomName"></param>
- /// <param name="username"></param>
- /// <returns></returns>
- /// <response code="201">The request completed successfully.</response>
- /// <response code="404">The specified roomName could not be found.</response>
- [HttpPost("joined/{roomName}/members")]
- [Authorize]
- [ProducesResponseType(201)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> AddRoomMember([FromRoute]string roomName, [FromBody]string username)
- {
- if (Tracker.TryGet(roomName, out var _))
- {
- await Client.AddPrivateRoomMemberAsync(roomName, username);
- return StatusCode(StatusCodes.Status201Created);
- }
-
- return NotFound();
- }
-
- /// <summary>
- /// Gets the current list of users for the specified room.
- /// </summary>
- /// <param name="roomName"></param>
- /// <returns></returns>
- /// <response code="200">The request completed successfully.</response>
- /// <response code="404">The specified roomName could not be found.</response>
- [HttpGet("joined/{roomName}/users")]
- [Authorize]
- [ProducesResponseType(typeof(IList<UserData>), 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 == ApplicationStateMonitor.CurrentValue.Server.Username));
-
- return Ok(response);
- }
-
- return NotFound();
- }
-
- /// <summary>
- /// Gets the current list of messages for the specified room.
- /// </summary>
- /// <param name="roomName"></param>
- /// <returns></returns>
- /// <response code="200">The request completed successfully.</response>
- /// <response code="404">The specified roomName could not be found.</response>
- [HttpGet("joined/{roomName}/messages")]
- [Authorize]
- [ProducesResponseType(typeof(IList<RoomMessage>), 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 == ApplicationStateMonitor.CurrentValue.Server.Username));
-
- return Ok(response);
- }
-
- return NotFound();
- }
-
- /// <summary>
- /// Gets a list of rooms from the server.
- /// </summary>
- /// <returns></returns>
- [HttpGet("available")]
- [Authorize]
- [ProducesResponseType(typeof(List<RoomInfo>), 200)]
- public async Task<IActionResult> GetRooms()
- {
- var list = await Client.GetRoomListAsync();
-
- var response = new List<RoomInfoResponse>();
-
- 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);
- }
-
- /// <summary>
- /// Joins a room.
- /// </summary>
- /// <param name="roomName"></param>
- /// <returns></returns>
- /// <response code="201">The request completed successfully.</response>
- /// <response code="304">The room has already been joined.</response>
- [HttpPost("joined/{roomName}")]
- [Authorize]
- [ProducesResponseType(typeof(Room), 201)]
- [ProducesResponseType(304)]
- public async Task<IActionResult> JoinRoom([FromRoute]string roomName)
- {
- if (Tracker.Rooms.ContainsKey(roomName))
- {
- return StatusCode(StatusCodes.Status304NotModified);
- }
-
- try
- {
- var roomData = await RoomService.JoinAsync(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;
- }
- }
-
- /// <summary>
- /// Leaves a room.
- /// </summary>
- /// <param name="roomName"></param>
- /// <returns></returns>
- /// <response code="204">The request completed successfully.</response>
- /// <response code="404">The room has not been joined.</response>
- [HttpDelete("joined/{roomName}")]
- [Authorize]
- [ProducesResponseType(204)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> LeaveRoom([FromRoute]string roomName)
- {
- if (!Tracker.Rooms.ContainsKey(roomName))
- {
- return StatusCode(StatusCodes.Status404NotFound);
- }
-
- await RoomService.LeaveAsync(roomName);
- Tracker.TryRemove(roomName);
-
- return StatusCode(StatusCodes.Status204NoContent);
- }
-
- private RoomResponse MapRoomToRoomResponse(Room room)
- {
- bool IsSelf(string username)
- {
- return username == ApplicationStateMonitor.CurrentValue.Server.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