From bb779904f7a9475ba053638a8675b29813f227ec Mon Sep 17 00:00:00 2001 From: choco Date: Sat, 11 Oct 2025 21:52:14 +0200 Subject: [PATCH] removed temp accept/decline logic from the api layer --- LightlessSync/Services/PairRequestService.cs | 43 ++++++++++++++++++- .../ApiController.Functions.Callbacks.cs | 30 ++----------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/LightlessSync/Services/PairRequestService.cs b/LightlessSync/Services/PairRequestService.cs index 998ea42..9d87244 100644 --- a/LightlessSync/Services/PairRequestService.cs +++ b/LightlessSync/Services/PairRequestService.cs @@ -1,6 +1,8 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; +using LightlessSync.LightlessConfiguration.Models; using LightlessSync.PlayerData.Pairs; using LightlessSync.Services.Mediator; using Microsoft.Extensions.Logging; @@ -11,16 +13,18 @@ public sealed class PairRequestService : DisposableMediatorSubscriberBase { private readonly DalamudUtilService _dalamudUtil; private readonly PairManager _pairManager; + private readonly Lazy _apiController; private readonly object _syncRoot = new(); private readonly List _requests = []; private static readonly TimeSpan Expiration = TimeSpan.FromMinutes(5); - public PairRequestService(ILogger logger, LightlessMediator mediator, DalamudUtilService dalamudUtil, PairManager pairManager) + public PairRequestService(ILogger logger, LightlessMediator mediator, DalamudUtilService dalamudUtil, PairManager pairManager, Lazy apiController) : base(logger, mediator) { _dalamudUtil = dalamudUtil; _pairManager = pairManager; + _apiController = apiController; Mediator.Subscribe(this, _ => { @@ -183,6 +187,41 @@ public sealed class PairRequestService : DisposableMediatorSubscriberBase return _requests.RemoveAll(r => now - r.ReceivedAt > Expiration) > 0; } + public void AcceptPairRequest(string hashedCid) + { + _ = Task.Run(async () => + { + try + { + await _apiController.Value.TryPairWithContentId(hashedCid).ConfigureAwait(false); + RemoveRequest(hashedCid); + + var display = ResolveDisplayName(hashedCid); + var displayText = string.IsNullOrEmpty(display) ? hashedCid : display; + Mediator.Publish(new NotificationMessage( + "Pair request accepted", + $"Sent a pair request back to {displayText}.", + NotificationType.Info, + TimeSpan.FromSeconds(3))); + } + catch (Exception ex) + { + Logger.LogError(ex, "Failed to accept pair request for {HashedCid}", hashedCid); + Mediator.Publish(new NotificationMessage( + "Failed to Accept Pair Request", + ex.Message, + NotificationType.Error, + TimeSpan.FromSeconds(5))); + } + }); + } + + public void DeclinePairRequest(string hashedCid) + { + RemoveRequest(hashedCid); + Logger.LogDebug("Declined pair request from {HashedCid}", hashedCid); + } + private record struct PairRequestEntry(string HashedCid, string MessageTemplate, DateTime ReceivedAt); public readonly record struct PairRequestDisplay(string HashedCid, string DisplayName, string Message, DateTime ReceivedAt); diff --git a/LightlessSync/WebAPI/SignalR/ApiController.Functions.Callbacks.cs b/LightlessSync/WebAPI/SignalR/ApiController.Functions.Callbacks.cs index 38df25a..e3c1fbc 100644 --- a/LightlessSync/WebAPI/SignalR/ApiController.Functions.Callbacks.cs +++ b/LightlessSync/WebAPI/SignalR/ApiController.Functions.Callbacks.cs @@ -111,37 +111,13 @@ public partial class ApiController return Task.CompletedTask; var request = _pairRequestService.RegisterIncomingRequest(dto.myHashedCid, dto.message ?? string.Empty); - - // Use the new interactive notification system for pair requests var senderName = string.IsNullOrEmpty(request.DisplayName) ? "Unknown User" : request.DisplayName; + _lightlessNotificationService.ShowPairRequestNotification( senderName, request.HashedCid, - onAccept: () => - { - // Fire and forget async operation - _ = Task.Run(async () => - { - var myCidHash = (await _dalamudUtil.GetCIDAsync().ConfigureAwait(false)).ToString().GetHash256(); - try - { - await TryPairWithContentId(request.HashedCid, myCidHash).ConfigureAwait(false); - _pairRequestService.RemoveRequest(request.HashedCid); - } - catch (Exception ex) - { - Mediator.Publish(new NotificationMessage( - "Failed to Accept Pair Request", - ex.Message, - NotificationType.Error, - TimeSpan.FromSeconds(5))); - } - }); - }, - onDecline: () => - { - _pairRequestService.RemoveRequest(request.HashedCid); - }); + onAccept: () => _pairRequestService.AcceptPairRequest(request.HashedCid), + onDecline: () => _pairRequestService.DeclinePairRequest(request.HashedCid)); return Task.CompletedTask; }