added proper return on TooManyRequest case

This commit is contained in:
Nicolas Constant 2022-02-03 01:06:32 -05:00
parent c371218672
commit bf7baba789
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
2 changed files with 24 additions and 4 deletions

View file

@ -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<UsersController> _logger;
private readonly ITwitterStatisticsHandler _twitterStatisticsHandler;
#region Ctor
public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger<UsersController> logger)
public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService, ILogger<UsersController> 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");
}
}
}

View file

@ -0,0 +1,13 @@
@using BirdsiteLive.Controllers;
@{
ViewData["Title"] = "Api Saturated";
}
<div class="text-center">
<h1 class="display-4">429 Too Many Requests</h1>
<p>
<br />
The API is saturated.<br/>
Please consider using another instance.
</p>
</div>