From 7f7e226813e54b767839bc195017276e3b2ac9fb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Dec 2022 11:17:19 -0600 Subject: swap tracker for service --- .../API/Controllers/ConversationsController.cs | 118 ++++++++++----------- 1 file changed, 56 insertions(+), 62 deletions(-) (limited to 'src/slskd/Messaging/API/Controllers/ConversationsController.cs') diff --git a/src/slskd/Messaging/API/Controllers/ConversationsController.cs b/src/slskd/Messaging/API/Controllers/ConversationsController.cs index d39c2c10..5b67ca86 100644 --- a/src/slskd/Messaging/API/Controllers/ConversationsController.cs +++ b/src/slskd/Messaging/API/Controllers/ConversationsController.cs @@ -19,14 +19,11 @@ using Microsoft.Extensions.Options; 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 slskd.Relay; - using Soulseek; /// /// Conversations. @@ -41,24 +38,21 @@ namespace slskd.Messaging.API /// /// Initializes a new instance of the class. /// - /// /// - /// + /// + /// public ConversationsController( - ISoulseekClient soulseekClient, IStateMonitor applicationStateMonotor, - IConversationTracker tracker, + IMessagingService messagingService, IOptionsSnapshot optionsSnapshot) { - Client = soulseekClient; ApplicationStateMonitor = applicationStateMonotor; - Tracker = tracker; + Messages = messagingService; OptionsSnapshot = optionsSnapshot; } - private ISoulseekClient Client { get; } private IStateMonitor ApplicationStateMonitor { get; } - private IConversationTracker Tracker { get; } + private IMessagingService Messages { get; } private IOptionsSnapshot OptionsSnapshot { get; } /// @@ -82,19 +76,20 @@ namespace slskd.Messaging.API return Forbid(); } - Tracker.Conversations.TryGetValue(username, out var conversation); + var message = Messages.Conversations.FindMessageAsync(username, id); - if (conversation == default || !conversation.Any(p => p.Id == id)) + if (message == default) { return NotFound(); } - await Client.AcknowledgePrivateMessageAsync(id); - return StatusCode(200); + await Messages.Conversations.AcknowledgeMessageAsync(username, id); + + return Ok(); } /// - /// Acknowledges all messages for the given username. + /// Acknowledges all messages from the given username. /// /// /// @@ -111,30 +106,20 @@ namespace slskd.Messaging.API return Forbid(); } - Tracker.Conversations.TryGetValue(username, out var conversation); + var conversation = Messages.Conversations.FindAsync(username); if (conversation == default) { return NotFound(); } - var tasks = new List(); - - foreach (var message in conversation.Where(p => !p.Acknowledged)) - { - tasks.Add(Task.Run(async () => - { - await Client.AcknowledgePrivateMessageAsync(message.Id); - message.Acknowledged = true; - })); - } + await Messages.Conversations.AcknowledgeAsync(username); - await Task.WhenAll(tasks); - return StatusCode(200); + return Ok(); } /// - /// Deletes the conversation associated with the given username. + /// Closes the conversation associated with the given username. /// /// /// The request completed successfully. @@ -143,75 +128,93 @@ namespace slskd.Messaging.API [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(404)] [ProducesResponseType(204)] - public IActionResult Delete([FromRoute]string username) + public async Task Close([FromRoute]string username) { if (Program.IsRelayAgent) { return Forbid(); } - var deleted = Tracker.Conversations.TryRemove(username, out _); + var conversation = Messages.Conversations.FindAsync(username, includeInactive: false); - if (deleted) + if (conversation == default) { - return StatusCode(204); + return NotFound(); } - return StatusCode(404); + await Messages.Conversations.RemoveAsync(username); + + return StatusCode(204); } /// - /// Gets all tracked conversations. + /// Gets all active conversations. /// /// /// The request completed successfully. [HttpGet("")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(Dictionary>), 200)] - public IActionResult GetAll() + [ProducesResponseType(typeof(List), 200)] + public async Task GetAll([FromQuery]bool unAckedOnly = false) { if (Program.IsRelayAgent) { return Forbid(); } - var response = Tracker.Conversations.ToDictionary( - entry => entry.Key, - entry => entry.Value - .Select(pm => PrivateMessageResponse.FromPrivateMessage(pm, self: pm.Username == ApplicationStateMonitor.CurrentValue.User.Username)) - .OrderBy(m => m.Timestamp)); + var conversations = await Messages.Conversations.ListAsync(c => c.IsActive && (!unAckedOnly || c.HasUnAcknowledgedMessages)); - return Ok(response); + return Ok(conversations); } /// /// Gets the conversation associated with the specified username. /// /// The username associated with the desired conversation. + /// /// /// The request completed successfully. /// A matching search was not found. [HttpGet("{username}")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(Conversation), 200)] [ProducesResponseType(404)] - public IActionResult GetByUsername([FromRoute]string username) + public async Task GetByUsername([FromRoute]string username, [FromQuery]bool includeMessages = true) { if (Program.IsRelayAgent) { return Forbid(); } - if (Tracker.TryGet(username, out var conversation)) + var conversation = await Messages.Conversations.FindAsync(username, includeMessages); + + if (conversation == default) + { + return NotFound(); + } + + return Ok(conversation); + } + + [HttpGet("{username}/messages")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(404)] + public async Task GetMessagesByUsername([FromRoute]string username) + { + if (Program.IsRelayAgent) { - var response = conversation - .Select(pm => PrivateMessageResponse.FromPrivateMessage(pm, self: pm.Username == ApplicationStateMonitor.CurrentValue.User.Username)) - .OrderBy(m => m.Timestamp); + return Forbid(); + } + + var messages = await Messages.Conversations.ListMessagesAsync(m => m.Username == username); - return Ok(response); + if (!messages.Any()) + { + return NotFound(); } - return NotFound(); + return Ok(messages); } /// @@ -238,16 +241,7 @@ namespace slskd.Messaging.API return BadRequest(); } - await Client.SendPrivateMessageAsync(username, message); - - // append the outgoing message to the tracker - Tracker.AddOrUpdate(username, new PrivateMessage() - { - Username = ApplicationStateMonitor.CurrentValue.User.Username, - Timestamp = DateTime.UtcNow, - Message = message, - Acknowledged = true, - }); + await Messages.Conversations.SendMessageAsync(username, message); return StatusCode(201); } -- cgit v1.2.3