diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-07-30 20:44:58 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-30 20:44:58 -0500 |
| commit | da71a74850b837ce6e831b9212714e510e72ca5e (patch) | |
| tree | 523dc96ed715cf8098758365c6c9277c79b89fa1 /src/slskd/Messaging | |
| parent | 1fade356f1b8b38e269408aeec89e96e5fae5cb6 (diff) | |
| parent | 5beeea03a1bc4f0c05467a7668c7f8a104f69206 (diff) | |
| download | slskd-da71a74850b837ce6e831b9212714e510e72ca5e.tar.gz | |
Merge pull request #1399 from slskd/msg-events
Add chat events for webhooks and scripts
Diffstat (limited to 'src/slskd/Messaging')
| -rw-r--r-- | src/slskd/Messaging/API/Controllers/PublicChatController.cs | 82 | ||||
| -rw-r--r-- | src/slskd/Messaging/ConversationService.cs | 17 | ||||
| -rw-r--r-- | src/slskd/Messaging/RoomService.cs | 13 | ||||
| -rw-r--r-- | src/slskd/Messaging/Types/PrivateMessage.cs | 6 |
4 files changed, 33 insertions, 85 deletions
diff --git a/src/slskd/Messaging/API/Controllers/PublicChatController.cs b/src/slskd/Messaging/API/Controllers/PublicChatController.cs deleted file mode 100644 index d44a5b5a..00000000 --- a/src/slskd/Messaging/API/Controllers/PublicChatController.cs +++ /dev/null @@ -1,82 +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> - -using Microsoft.Extensions.Options; - -namespace slskd.Messaging.API -{ - using System.Threading.Tasks; - using Asp.Versioning; - 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, IOptionsSnapshot<Options> optionsSnapshot) - { - Client = soulseekClient; - OptionsSnapshot = optionsSnapshot; - } - - private ISoulseekClient Client { get; } - private IOptionsSnapshot<Options> OptionsSnapshot { get; } - - /// <summary> - /// Starts public chat. - /// </summary> - /// <returns></returns> - [HttpPost] - [Authorize(Policy = AuthPolicy.Any)] - public async Task<IActionResult> Start() - { - if (Program.IsRelayAgent) - { - return Forbid(); - } - - await Client.StartPublicChatAsync(); - return StatusCode(StatusCodes.Status201Created); - } - - /// <summary> - /// Stops public chat. - /// </summary> - /// <returns></returns> - [HttpDelete] - [Authorize(Policy = AuthPolicy.Any)] - public async Task<IActionResult> Stop() - { - if (Program.IsRelayAgent) - { - return Forbid(); - } - - await Client.StopPublicChatAsync(); - return StatusCode(StatusCodes.Status204NoContent); - } - } -} diff --git a/src/slskd/Messaging/ConversationService.cs b/src/slskd/Messaging/ConversationService.cs index de772ff0..b39162d6 100644 --- a/src/slskd/Messaging/ConversationService.cs +++ b/src/slskd/Messaging/ConversationService.cs @@ -24,6 +24,7 @@ namespace slskd.Messaging using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Serilog; + using slskd.Events; using Soulseek; /// <summary> @@ -112,12 +113,15 @@ namespace slskd.Messaging { public ConversationService( ISoulseekClient soulseekClient, + EventBus eventBus, IDbContextFactory<MessagingDbContext> contextFactory) { SoulseekClient = soulseekClient; + EventBus = eventBus; ContextFactory = contextFactory; } + private EventBus EventBus { get; } private IDbContextFactory<MessagingDbContext> ContextFactory { get; } private ILogger Log { get; } = Serilog.Log.ForContext<ConversationService>(); private ISoulseekClient SoulseekClient { get; } @@ -301,8 +305,12 @@ namespace slskd.Messaging if (existing != null) { - // the message was replayed. ensure the local ack bit is in sync with the server - existing.IsAcknowledged = message.IsAcknowledged; + // the message was replayed. either we haven't ACKed it, or we did and the ACK didn't make it to the + // server. either way we need to ensure that the local db understands the message needs to be ACKed, so + // force the bit false. we shouldn't need to update any other part of the message because IDs are unique + // on the soulseek server + existing.IsAcknowledged = false; + existing.WasReplayed = message.WasReplayed; } else { @@ -312,6 +320,11 @@ namespace slskd.Messaging context.SaveChanges(); + EventBus.Raise(new PrivateMessageReceivedEvent + { + Message = message, + }); + return Task.CompletedTask; } diff --git a/src/slskd/Messaging/RoomService.cs b/src/slskd/Messaging/RoomService.cs index 8dd25431..e85de4a4 100644 --- a/src/slskd/Messaging/RoomService.cs +++ b/src/slskd/Messaging/RoomService.cs @@ -24,6 +24,7 @@ namespace slskd.Messaging using System.Linq; using System.Threading.Tasks; using Serilog; + using slskd.Events; using slskd.Users; using Soulseek; @@ -71,12 +72,14 @@ namespace slskd.Messaging /// <param name="stateMutator"></param> /// <param name="roomTracker"></param> /// <param name="userService"></param> + /// <param name="eventBus"></param> public RoomService( ISoulseekClient soulseekClient, IOptionsMonitor<Options> optionsMonitor, IStateMutator<State> stateMutator, IRoomTracker roomTracker, - IUserService userService) + IUserService userService, + EventBus eventBus) { Client = soulseekClient; @@ -86,6 +89,7 @@ namespace slskd.Messaging RoomTracker = roomTracker; Users = userService; + EventBus = eventBus; Client.LoggedIn += Client_LoggedIn; @@ -100,6 +104,7 @@ namespace slskd.Messaging private IOptionsMonitor<Options> OptionsMonitor { get; } private IRoomTracker RoomTracker { get; set; } private IUserService Users { get; set; } + private EventBus EventBus { get; } /// <summary> /// Joins the specified <paramref name="roomName"/>. @@ -228,6 +233,12 @@ namespace slskd.Messaging var message = RoomMessage.FromEventArgs(args, DateTime.UtcNow); RoomTracker.AddOrUpdateMessage(args.RoomName, message); + + // todo: persist these in the database before raising (why are we not??) + EventBus.Raise<RoomMessageReceivedEvent>(new RoomMessageReceivedEvent + { + Message = message, + }); } } }
\ No newline at end of file diff --git a/src/slskd/Messaging/Types/PrivateMessage.cs b/src/slskd/Messaging/Types/PrivateMessage.cs index a4b1c325..a036520d 100644 --- a/src/slskd/Messaging/Types/PrivateMessage.cs +++ b/src/slskd/Messaging/Types/PrivateMessage.cs @@ -55,6 +55,11 @@ namespace slskd.Messaging /// </summary> public bool IsAcknowledged { get; set; } = false; + /// <summary> + /// A value indicating whether the message has was replayed by the server. + /// </summary> + public bool WasReplayed { get; set; } = false; + public static PrivateMessage FromEventArgs(PrivateMessageReceivedEventArgs eventArgs) { return new PrivateMessage() @@ -64,6 +69,7 @@ namespace slskd.Messaging Username = eventArgs.Username, Message = eventArgs.Message, IsAcknowledged = false, + WasReplayed = eventArgs.Replayed, Direction = MessageDirection.In, }; } |
