Merge pull request #131 from NicolasConstant/topic_prevent-twitter-api-spam
Topic prevent twitter api spam
This commit is contained in:
commit
25ba19bc4f
4 changed files with 38 additions and 8 deletions
|
@ -13,6 +13,8 @@ namespace BirdsiteLive.Statistics.Domain
|
|||
void CalledTweetApi();
|
||||
void CalledTimelineApi();
|
||||
ApiStatistics GetStatistics();
|
||||
|
||||
int GetCurrentUserCalls();
|
||||
}
|
||||
|
||||
//Rate limits: https://developer.twitter.com/en/docs/twitter-api/v1/rate-limits
|
||||
|
@ -60,7 +62,12 @@ namespace BirdsiteLive.Statistics.Domain
|
|||
foreach (var old in oldSnapshots) _snapshots.TryRemove(old, out var data);
|
||||
}
|
||||
|
||||
public void CalledUserApi() //GET users/show - 900/15mins
|
||||
public int GetCurrentUserCalls()
|
||||
{
|
||||
return _userCalls;
|
||||
}
|
||||
|
||||
public void CalledUserApi() //GET users/show - 300/15mins
|
||||
{
|
||||
Interlocked.Increment(ref _userCalls);
|
||||
}
|
||||
|
|
|
@ -32,6 +32,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;
|
||||
|
||||
//Proceed to account retrieval
|
||||
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
|
||||
ExceptionHandler.SwallowWebExceptions = false;
|
||||
|
||||
|
@ -49,9 +55,6 @@ namespace BirdsiteLive.Twitter
|
|||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error retrieving user {Username}", username);
|
||||
|
||||
// TODO keep track of error, see where to remove user if too much errors
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
13
src/BirdsiteLive/Views/Users/ApiSaturated.cshtml
Normal file
13
src/BirdsiteLive/Views/Users/ApiSaturated.cshtml
Normal 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>
|
Reference in a new issue