diff --git a/src/BirdsiteLive.Twitter/CachedTwitterService.cs b/src/BirdsiteLive.Twitter/CachedTwitterService.cs index ac20a62..4ee3e04 100644 --- a/src/BirdsiteLive.Twitter/CachedTwitterService.cs +++ b/src/BirdsiteLive.Twitter/CachedTwitterService.cs @@ -44,6 +44,11 @@ namespace BirdsiteLive.Twitter return user; } + public bool IsUserApiRateLimited() + { + return _twitterService.IsUserApiRateLimited(); + } + public void PurgeUser(string username) { _userCache.Remove(username); diff --git a/src/BirdsiteLive.Twitter/TwitterUserService.cs b/src/BirdsiteLive.Twitter/TwitterUserService.cs index df6e5ad..adc8d6b 100644 --- a/src/BirdsiteLive.Twitter/TwitterUserService.cs +++ b/src/BirdsiteLive.Twitter/TwitterUserService.cs @@ -13,6 +13,7 @@ namespace BirdsiteLive.Twitter public interface ITwitterUserService { TwitterUser GetUser(string username); + bool IsUserApiRateLimited(); } public class TwitterUserService : ITwitterUserService @@ -33,13 +34,12 @@ namespace BirdsiteLive.Twitter public TwitterUser GetUser(string username) { //Check if API is saturated - var currentCalls = _statisticsHandler.GetCurrentUserCalls(); - var maxCalls = _statisticsHandler.GetStatistics().UserCallsMax; - if (currentCalls > maxCalls) return null; + if (IsUserApiRateLimited()) return null; //Proceed to account retrieval _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized(); ExceptionHandler.SwallowWebExceptions = false; + RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly; IUser user; try @@ -76,5 +76,32 @@ namespace BirdsiteLive.Twitter Protected = user.Protected }; } + + public bool IsUserApiRateLimited() + { + // Retrieve limit from tooling + _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized(); + ExceptionHandler.SwallowWebExceptions = false; + RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackOnly; + + try + { + var queryRateLimits = RateLimit.GetQueryRateLimit("https://api.twitter.com/1.1/users/show.json?screen_name=mastodon"); + + if (queryRateLimits != null) + { + return queryRateLimits.Remaining <= 0; + } + } + catch (Exception e) + { + _logger.LogError(e, "Error retrieving rate limits"); + } + + // Fallback + var currentCalls = _statisticsHandler.GetCurrentUserCalls(); + var maxCalls = _statisticsHandler.GetStatistics().UserCallsMax; + return currentCalls >= maxCalls; + } } } \ No newline at end of file diff --git a/src/BirdsiteLive/Controllers/UsersController.cs b/src/BirdsiteLive/Controllers/UsersController.cs index aa4f272..965f988 100644 --- a/src/BirdsiteLive/Controllers/UsersController.cs +++ b/src/BirdsiteLive/Controllers/UsersController.cs @@ -13,7 +13,6 @@ using BirdsiteLive.Common.Regexes; using BirdsiteLive.Common.Settings; using BirdsiteLive.Domain; using BirdsiteLive.Models; -using BirdsiteLive.Statistics.Domain; using BirdsiteLive.Tools; using BirdsiteLive.Twitter; using BirdsiteLive.Twitter.Models; @@ -33,10 +32,9 @@ namespace BirdsiteLive.Controllers private readonly IStatusService _statusService; private readonly InstanceSettings _instanceSettings; private readonly ILogger _logger; - private readonly ITwitterStatisticsHandler _twitterStatisticsHandler; #region Ctor - public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger logger, ITwitterStatisticsHandler twitterStatisticsHandler) + public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger logger) { _twitterUserService = twitterUserService; _userService = userService; @@ -44,7 +42,6 @@ namespace BirdsiteLive.Controllers _instanceSettings = instanceSettings; _twitterTweetService = twitterTweetService; _logger = logger; - _twitterStatisticsHandler = twitterStatisticsHandler; } #endregion @@ -75,9 +72,7 @@ namespace BirdsiteLive.Controllers if (!string.IsNullOrWhiteSpace(id) && UserRegexes.TwitterAccount.IsMatch(id) && id.Length <= 15) user = _twitterUserService.GetUser(id); - var isSaturated = user == null - && _twitterStatisticsHandler.GetCurrentUserCalls() >= - _twitterStatisticsHandler.GetStatistics().UserCallsMax; + var isSaturated = _twitterUserService.IsUserApiRateLimited(); var acceptHeaders = Request.Headers["Accept"]; if (acceptHeaders.Any())