Skip to content

Commit

Permalink
Merge pull request #76 from NicolasConstant/develop
Browse files Browse the repository at this point in the history
0.12.2 PR
  • Loading branch information
NicolasConstant authored Jan 30, 2021
2 parents e78d37e + 52e2868 commit 3445465
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/BirdsiteLive.Twitter/BirdsiteLive.Twitter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
<ProjectReference Include="..\BirdsiteLive.Common\BirdsiteLive.Common.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Tools\" />
</ItemGroup>

</Project>
64 changes: 64 additions & 0 deletions src/BirdsiteLive.Twitter/Tools/TwitterAuthenticationInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using Microsoft.Extensions.Logging;
using Tweetinvi;

namespace BirdsiteLive.Twitter.Tools
{
public interface ITwitterAuthenticationInitializer
{
void EnsureAuthenticationIsInitialized();
}

public class TwitterAuthenticationInitializer : ITwitterAuthenticationInitializer
{
private readonly TwitterSettings _settings;
private readonly ILogger<TwitterAuthenticationInitializer> _logger;
private static bool _initialized;
private readonly SemaphoreSlim _semaphoregate = new SemaphoreSlim(1);

#region Ctor
public TwitterAuthenticationInitializer(TwitterSettings settings, ILogger<TwitterAuthenticationInitializer> logger)
{
_settings = settings;
_logger = logger;
}
#endregion

public void EnsureAuthenticationIsInitialized()
{
if (_initialized) return;
_semaphoregate.Wait();

try
{
if (_initialized) return;
InitTwitterCredentials();
}
finally
{
_semaphoregate.Release();
}
}

private void InitTwitterCredentials()
{
for (;;)
{
try
{
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
_initialized = true;
return;
}
catch (Exception e)
{
_logger.LogError(e, "Twitter Authentication Failed");
Thread.Sleep(250);
}
}
}
}
}
24 changes: 14 additions & 10 deletions src/BirdsiteLive.Twitter/TwitterTweetsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using BirdsiteLive.Statistics.Domain;
using BirdsiteLive.Twitter.Extractors;
using BirdsiteLive.Twitter.Models;
using BirdsiteLive.Twitter.Tools;
using Microsoft.Extensions.Logging;
using Tweetinvi;
using Tweetinvi.Models;
Expand All @@ -20,30 +21,31 @@ public interface ITwitterTweetsService

public class TwitterTweetsService : ITwitterTweetsService
{
private readonly TwitterSettings _settings;
private readonly ITwitterAuthenticationInitializer _twitterAuthenticationInitializer;
private readonly ITweetExtractor _tweetExtractor;
private readonly ITwitterStatisticsHandler _statisticsHandler;
private readonly ITwitterUserService _twitterUserService;
private readonly ILogger<TwitterTweetsService> _logger;

#region Ctor
public TwitterTweetsService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger<TwitterTweetsService> logger)
public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger<TwitterTweetsService> logger)
{
_settings = settings;
_twitterAuthenticationInitializer = twitterAuthenticationInitializer;
_tweetExtractor = tweetExtractor;
_statisticsHandler = statisticsHandler;
_twitterUserService = twitterUserService;
_logger = logger;
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
ExceptionHandler.SwallowWebExceptions = false;
}
#endregion

public ExtractedTweet GetTweet(long statusId)
{
try
{
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
ExceptionHandler.SwallowWebExceptions = false;
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;

var tweet = Tweet.GetTweet(statusId);
_statisticsHandler.CalledTweetApi();
if (tweet == null) return null; //TODO: test this
Expand All @@ -58,14 +60,16 @@ public ExtractedTweet GetTweet(long statusId)

public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1)
{
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;

var user = _twitterUserService.GetUser(username);
if (user.Protected) return new ExtractedTweet[0];

var tweets = new List<ITweet>();
try
{
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
ExceptionHandler.SwallowWebExceptions = false;
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;

var user = _twitterUserService.GetUser(username);
if (user == null || user.Protected) return new ExtractedTweet[0];

if (fromTweetId == -1)
{
var timeline = Timeline.GetUserTimeline(user.Id, nberTweets);
Expand Down
18 changes: 12 additions & 6 deletions src/BirdsiteLive.Twitter/TwitterUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BirdsiteLive.Common.Settings;
using BirdsiteLive.Statistics.Domain;
using BirdsiteLive.Twitter.Models;
using BirdsiteLive.Twitter.Tools;
using Microsoft.Extensions.Logging;
using Tweetinvi;
using Tweetinvi.Models;
Expand All @@ -16,29 +17,34 @@ public interface ITwitterUserService

public class TwitterUserService : ITwitterUserService
{
private readonly TwitterSettings _settings;
private readonly ITwitterAuthenticationInitializer _twitterAuthenticationInitializer;
private readonly ITwitterStatisticsHandler _statisticsHandler;
private readonly ILogger<TwitterUserService> _logger;

#region Ctor
public TwitterUserService(TwitterSettings settings, ITwitterStatisticsHandler statisticsHandler, ILogger<TwitterUserService> logger)
public TwitterUserService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ILogger<TwitterUserService> logger)
{
_settings = settings;
_twitterAuthenticationInitializer = twitterAuthenticationInitializer;
_statisticsHandler = statisticsHandler;
_logger = logger;
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
ExceptionHandler.SwallowWebExceptions = false;
}
#endregion

public TwitterUser GetUser(string username)
{
_twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();
ExceptionHandler.SwallowWebExceptions = false;

IUser user;
try
{
user = User.GetUserFromScreenName(username);
_statisticsHandler.CalledUserApi();
if (user == null) return null;
if (user == null)
{
_logger.LogWarning("User {username} not found", username);
return null;
}
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/BirdsiteLive/BirdsiteLive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>0.12.1</Version>
<Version>0.12.2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 9 additions & 1 deletion src/BirdsiteLive/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net.Mime;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using BirdsiteLive.ActivityPub;
Expand All @@ -12,6 +13,7 @@
using BirdsiteLive.Domain;
using BirdsiteLive.Models;
using BirdsiteLive.Twitter;
using BirdsiteLive.Twitter.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
Expand All @@ -26,6 +28,7 @@ public class UsersController : Controller
private readonly IUserService _userService;
private readonly IStatusService _statusService;
private readonly InstanceSettings _instanceSettings;
private readonly Regex _twitterAccountRegex = new Regex(@"^[a-zA-Z0-9_]+$");

#region Ctor
public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService)
Expand Down Expand Up @@ -55,7 +58,12 @@ public IActionResult Index()
public IActionResult Index(string id)
{
id = id.Trim(new[] { ' ', '@' }).ToLowerInvariant();
var user = _twitterUserService.GetUser(id);

// Ensure valid username
// https://help.twitter.com/en/managing-your-account/twitter-username-rules
TwitterUser user = null;
if (!string.IsNullOrWhiteSpace(id) && _twitterAccountRegex.IsMatch(id) && id.Length <= 15)
user = _twitterUserService.GetUser(id);

var acceptHeaders = Request.Headers["Accept"];
if (acceptHeaders.Any())
Expand Down
7 changes: 7 additions & 0 deletions src/BirdsiteLive/Controllers/WellKnownController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BirdsiteLive.ActivityPub.Converters;
using BirdsiteLive.Common.Settings;
Expand All @@ -19,6 +20,7 @@ public class WellKnownController : ControllerBase
private readonly ITwitterUserService _twitterUserService;
private readonly ITwitterUserDal _twitterUserDal;
private readonly InstanceSettings _settings;
private readonly Regex _twitterAccountRegex = new Regex(@"^[a-zA-Z0-9_]+$");

#region Ctor
public WellKnownController(InstanceSettings settings, ITwitterUserService twitterUserService, ITwitterUserDal twitterUserDal)
Expand Down Expand Up @@ -160,6 +162,11 @@ public IActionResult Webfinger(string resource = null)
// Ensure lowercase
name = name.ToLowerInvariant();

// Ensure valid username
// https://help.twitter.com/en/managing-your-account/twitter-username-rules
if (string.IsNullOrWhiteSpace(name) || !_twitterAccountRegex.IsMatch(name) || name.Length > 15 )
return NotFound();

if (!string.IsNullOrWhiteSpace(domain) && domain != _settings.Domain)
return NotFound();

Expand Down
3 changes: 3 additions & 0 deletions src/BirdsiteLive/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using BirdsiteLive.DAL.Postgres.Settings;
using BirdsiteLive.Models;
using BirdsiteLive.Twitter;
using BirdsiteLive.Twitter.Tools;
using Lamar;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -87,6 +88,8 @@ public void ConfigureContainer(ServiceRegistry services)
services.For<ITwitterUserService>().DecorateAllWith<CachedTwitterUserService>();
services.For<ITwitterUserService>().Use<TwitterUserService>().Singleton();

services.For<ITwitterAuthenticationInitializer>().Use<TwitterAuthenticationInitializer>().Singleton();

services.Scan(_ =>
{
_.Assembly("BirdsiteLive.Twitter");
Expand Down

0 comments on commit 3445465

Please sign in to comment.