diff --git a/src/BirdsiteLive/Controllers/UsersController.cs b/src/BirdsiteLive/Controllers/UsersController.cs index 73be8b0..aa4f272 100644 --- a/src/BirdsiteLive/Controllers/UsersController.cs +++ b/src/BirdsiteLive/Controllers/UsersController.cs @@ -13,6 +13,7 @@ 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; @@ -32,9 +33,10 @@ 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) + public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger logger, ITwitterStatisticsHandler twitterStatisticsHandler) { _twitterUserService = twitterUserService; _userService = userService; @@ -42,6 +44,7 @@ namespace BirdsiteLive.Controllers _instanceSettings = instanceSettings; _twitterTweetService = twitterTweetService; _logger = logger; + _twitterStatisticsHandler = twitterStatisticsHandler; } #endregion @@ -72,12 +75,17 @@ 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 acceptHeaders = Request.Headers["Accept"]; if (acceptHeaders.Any()) { var r = acceptHeaders.First(); if (r.Contains("application/activity+json")) { + if (user == null && isSaturated) return new ObjectResult("Too Many Requests") { StatusCode = 429 }; if (user == null) return NotFound(); var apUser = _userService.GetUser(user); var jsonApUser = JsonConvert.SerializeObject(apUser); @@ -85,8 +93,9 @@ namespace BirdsiteLive.Controllers } } + if (user == null && isSaturated) return View("ApiSaturated"); if (user == null) return View("UserNotFound"); - + var displayableUser = new DisplayTwitterUser { Name = user.Name, @@ -190,7 +199,5 @@ namespace BirdsiteLive.Controllers var jsonApUser = JsonConvert.SerializeObject(followers); return Content(jsonApUser, "application/activity+json; charset=utf-8"); } - - } } \ No newline at end of file diff --git a/src/BirdsiteLive/Views/Users/ApiSaturated.cshtml b/src/BirdsiteLive/Views/Users/ApiSaturated.cshtml new file mode 100644 index 0000000..1f807aa --- /dev/null +++ b/src/BirdsiteLive/Views/Users/ApiSaturated.cshtml @@ -0,0 +1,13 @@ +@using BirdsiteLive.Controllers; +@{ + ViewData["Title"] = "Api Saturated"; +} + +
+

429 Too Many Requests

+

+
+ The API is saturated.
+ Please consider using another instance. +

+
\ No newline at end of file