diff --git a/ArchiSteamFarm/Actions.cs b/ArchiSteamFarm/Actions.cs index f832c91d4..3c866ec07 100644 --- a/ArchiSteamFarm/Actions.cs +++ b/ArchiSteamFarm/Actions.cs @@ -103,17 +103,16 @@ namespace ArchiSteamFarm { } [PublicAPI] - public async Task<(bool Success, string Message)> HandleTwoFactorAuthenticationConfirmations(bool accept, MobileAuthenticator.Confirmation.EType? acceptedType = null, IReadOnlyCollection? acceptedCreatorIDs = null, bool waitIfNeeded = false) { + public async Task<(bool Success, IReadOnlyCollection? HandledConfirmations, string Message)> HandleTwoFactorAuthenticationConfirmations(bool accept, MobileAuthenticator.Confirmation.EType? acceptedType = null, IReadOnlyCollection? acceptedCreatorIDs = null, bool waitIfNeeded = false) { if (Bot.BotDatabase.MobileAuthenticator == null) { - return (false, Strings.BotNoASFAuthenticator); + return (false, null, Strings.BotNoASFAuthenticator); } if (!Bot.IsConnectedAndLoggedOn) { - return (false, Strings.BotNotConnected); + return (false, null, Strings.BotNotConnected); } - ushort handledConfirmationsCount = 0; - HashSet? handledCreatorIDs = null; + Dictionary? handledConfirmations = null; for (byte i = 0; (i == 0) || ((i < WebBrowser.MaxTries) && waitIfNeeded); i++) { if (i > 0) { @@ -143,28 +142,24 @@ namespace ArchiSteamFarm { } if (!await Bot.BotDatabase.MobileAuthenticator.HandleConfirmations(confirmations, accept).ConfigureAwait(false)) { - return (false, Strings.WarningFailed); + return (false, handledConfirmations?.Values, Strings.WarningFailed); } - handledConfirmationsCount += (ushort) confirmations.Count; + handledConfirmations ??= new Dictionary(); + + foreach (MobileAuthenticator.Confirmation? confirmation in confirmations) { + handledConfirmations[confirmation.Creator] = confirmation; + } if (acceptedCreatorIDs?.Count > 0) { - IEnumerable handledCreatorIDsThisRound = confirmations.Select(confirmation => confirmation.Creator).Where(acceptedCreatorIDs.Contains!); - - if (handledCreatorIDs != null) { - handledCreatorIDs.UnionWith(handledCreatorIDsThisRound); - } else { - handledCreatorIDs = handledCreatorIDsThisRound.ToHashSet(); - } - // Check if those are all that we were expected to confirm - if (handledCreatorIDs.SetEquals(acceptedCreatorIDs)) { - return (true, string.Format(CultureInfo.CurrentCulture, Strings.BotHandledConfirmations, handledConfirmationsCount)); + if ((handledConfirmations.Count >= acceptedCreatorIDs.Count) && acceptedCreatorIDs.All(handledConfirmations.ContainsKey)) { + return (true, handledConfirmations.Values, string.Format(CultureInfo.CurrentCulture, Strings.BotHandledConfirmations, handledConfirmations.Count)); } } } - return (!waitIfNeeded, !waitIfNeeded ? string.Format(CultureInfo.CurrentCulture, Strings.BotHandledConfirmations, handledConfirmationsCount) : string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)); + return (!waitIfNeeded, handledConfirmations?.Values, !waitIfNeeded ? string.Format(CultureInfo.CurrentCulture, Strings.BotHandledConfirmations, handledConfirmations?.Count ?? 0) : string.Format(CultureInfo.CurrentCulture, Strings.ErrorRequestFailedTooManyTimes, WebBrowser.MaxTries)); } [PublicAPI] @@ -309,7 +304,7 @@ namespace ArchiSteamFarm { (bool success, HashSet? mobileTradeOfferIDs) = await Bot.ArchiWebHandler.SendTradeOffer(targetSteamID, items, token: tradeToken, itemsPerTrade: itemsPerTrade).ConfigureAwait(false); if ((mobileTradeOfferIDs?.Count > 0) && Bot.HasMobileAuthenticator) { - (bool twoFactorSuccess, _) = await HandleTwoFactorAuthenticationConfirmations(true, MobileAuthenticator.Confirmation.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false); + (bool twoFactorSuccess, _, _) = await HandleTwoFactorAuthenticationConfirmations(true, MobileAuthenticator.Confirmation.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false); if (!twoFactorSuccess) { return (false, Strings.BotLootingFailed); diff --git a/ArchiSteamFarm/Commands.cs b/ArchiSteamFarm/Commands.cs index 4f873555e..fc10c3822 100644 --- a/ArchiSteamFarm/Commands.cs +++ b/ArchiSteamFarm/Commands.cs @@ -541,7 +541,7 @@ namespace ArchiSteamFarm { return FormatBotResponse(Strings.BotNoASFAuthenticator); } - (bool success, string message) = await Bot.Actions.HandleTwoFactorAuthenticationConfirmations(confirm).ConfigureAwait(false); + (bool success, _, string message) = await Bot.Actions.HandleTwoFactorAuthenticationConfirmations(confirm).ConfigureAwait(false); return FormatBotResponse(success ? message : string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, message)); } diff --git a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs index 798e02b87..82324d2f5 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs @@ -457,7 +457,7 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { /// Handles 2FA confirmations of given bots, requires ASF 2FA module to be active on them. /// [HttpPost("{botNames:required}/TwoFactorAuthentication/Confirmations")] - [ProducesResponseType(typeof(GenericResponse>), (int) HttpStatusCode.OK)] + [ProducesResponseType(typeof(GenericResponse>>>), (int) HttpStatusCode.OK)] [ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.BadRequest)] public async Task> TwoFactorAuthenticationConfirmationsPost(string botNames, [FromBody] TwoFactorAuthenticationConfirmationsRequest request) { if (string.IsNullOrEmpty(botNames)) { @@ -475,19 +475,19 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { HashSet? bots = Bot.GetBots(botNames); if ((bots == null) || (bots.Count == 0)) { - return BadRequest(new GenericResponse>(false, string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botNames))); + return BadRequest(new GenericResponse>>>(false, string.Format(CultureInfo.CurrentCulture, Strings.BotNotFound, botNames))); } - IList<(bool Success, string Message)> results = await Utilities.InParallel(bots.Select(bot => bot.Actions.HandleTwoFactorAuthenticationConfirmations(request.Accept, request.AcceptedType, request.AcceptedCreatorIDs.Count > 0 ? request.AcceptedCreatorIDs : null, request.WaitIfNeeded))).ConfigureAwait(false); + IList<(bool Success, IReadOnlyCollection? HandledConfirmations, string Message)> results = await Utilities.InParallel(bots.Select(bot => bot.Actions.HandleTwoFactorAuthenticationConfirmations(request.Accept, request.AcceptedType, request.AcceptedCreatorIDs.Count > 0 ? request.AcceptedCreatorIDs : null, request.WaitIfNeeded))).ConfigureAwait(false); - Dictionary result = new(bots.Count, Bot.BotsComparer); + Dictionary>> result = new(bots.Count, Bot.BotsComparer); foreach (Bot bot in bots) { - (bool success, string message) = results[result.Count]; - result[bot.BotName] = new GenericResponse(success, message); + (bool success, IReadOnlyCollection? handledConfirmations, string message) = results[result.Count]; + result[bot.BotName] = new GenericResponse>(success, message, handledConfirmations); } - return Ok(new GenericResponse>(result)); + return Ok(new GenericResponse>>>(result)); } /// diff --git a/ArchiSteamFarm/Statistics.cs b/ArchiSteamFarm/Statistics.cs index dcbb585f1..282df766e 100644 --- a/ArchiSteamFarm/Statistics.cs +++ b/ArchiSteamFarm/Statistics.cs @@ -647,7 +647,7 @@ namespace ArchiSteamFarm { (bool success, HashSet? mobileTradeOfferIDs) = await Bot.ArchiWebHandler.SendTradeOffer(listedUser.SteamID, itemsToGive, itemsToReceive, listedUser.TradeToken, true).ConfigureAwait(false); if ((mobileTradeOfferIDs?.Count > 0) && Bot.HasMobileAuthenticator) { - (bool twoFactorSuccess, _) = await Bot.Actions.HandleTwoFactorAuthenticationConfirmations(true, MobileAuthenticator.Confirmation.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false); + (bool twoFactorSuccess, _, _) = await Bot.Actions.HandleTwoFactorAuthenticationConfirmations(true, MobileAuthenticator.Confirmation.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false); if (!twoFactorSuccess) { Bot.ArchiLogger.LogGenericTrace(Strings.WarningFailed); diff --git a/ArchiSteamFarm/Trading.cs b/ArchiSteamFarm/Trading.cs index 70c750372..1523869b9 100644 --- a/ArchiSteamFarm/Trading.cs +++ b/ArchiSteamFarm/Trading.cs @@ -420,7 +420,7 @@ namespace ArchiSteamFarm { HashSet mobileTradeOfferIDs = results.Where(result => (result.TradeResult?.Result == ParseTradeResult.EResult.Accepted) && result.RequiresMobileConfirmation).Select(result => result.TradeResult!.TradeOfferID).ToHashSet(); if (mobileTradeOfferIDs.Count > 0) { - (bool twoFactorSuccess, _) = await Bot.Actions.HandleTwoFactorAuthenticationConfirmations(true, MobileAuthenticator.Confirmation.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false); + (bool twoFactorSuccess, _, _) = await Bot.Actions.HandleTwoFactorAuthenticationConfirmations(true, MobileAuthenticator.Confirmation.EType.Trade, mobileTradeOfferIDs, true).ConfigureAwait(false); if (!twoFactorSuccess) { HandledTradeOfferIDs.ExceptWith(mobileTradeOfferIDs);