Skip to content

Commit

Permalink
Merge pull request #59 from NicolasConstant/develop
Browse files Browse the repository at this point in the history
0.9.1 PR
  • Loading branch information
NicolasConstant authored Jan 18, 2021
2 parents cd4d30b + 6ae5f06 commit 6e61b6c
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 69 deletions.
10 changes: 5 additions & 5 deletions src/BirdsiteLive.Pipeline/Processors/RetrieveTweetsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace BirdsiteLive.Pipeline.Processors
{
public class RetrieveTweetsProcessor : IRetrieveTweetsProcessor
{
private readonly ITwitterService _twitterService;
private readonly ITwitterTweetsService _twitterTweetsService;
private readonly ITwitterUserDal _twitterUserDal;

#region Ctor
public RetrieveTweetsProcessor(ITwitterService twitterService, ITwitterUserDal twitterUserDal)
public RetrieveTweetsProcessor(ITwitterTweetsService twitterTweetsService, ITwitterUserDal twitterUserDal)
{
_twitterService = twitterService;
_twitterTweetsService = twitterTweetsService;
_twitterUserDal = twitterUserDal;
}
#endregion
Expand Down Expand Up @@ -56,9 +56,9 @@ private ExtractedTweet[] RetrieveNewTweets(SyncTwitterUser user)
{
ExtractedTweet[] tweets;
if (user.LastTweetPostedId == -1)
tweets = _twitterService.GetTimeline(user.Acct, 1);
tweets = _twitterTweetsService.GetTimeline(user.Acct, 1);
else
tweets = _twitterService.GetTimeline(user.Acct, 200, user.LastTweetSynchronizedForAllFollowersId);
tweets = _twitterTweetsService.GetTimeline(user.Acct, 200, user.LastTweetSynchronizedForAllFollowersId);

return tweets;
}
Expand Down
16 changes: 3 additions & 13 deletions src/BirdsiteLive.Twitter/CachedTwitterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace BirdsiteLive.Twitter
{
public class CachedTwitterService : ITwitterService
public class CachedTwitterUserService : ITwitterUserService
{
private readonly ITwitterService _twitterService;
private readonly ITwitterUserService _twitterService;

private MemoryCache _userCache = new MemoryCache(new MemoryCacheOptions()
{
Expand All @@ -22,7 +22,7 @@ public class CachedTwitterService : ITwitterService
.SetAbsoluteExpiration(TimeSpan.FromDays(30));

#region Ctor
public CachedTwitterService(ITwitterService twitterService)
public CachedTwitterUserService(ITwitterUserService twitterService)
{
_twitterService = twitterService;
}
Expand All @@ -38,15 +38,5 @@ public TwitterUser GetUser(string username)

return user;
}

public ExtractedTweet GetTweet(long statusId)
{
return _twitterService.GetTweet(statusId);
}

public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1)
{
return _twitterService.GetTimeline(username, nberTweets, fromTweetId);
}
}
}
1 change: 1 addition & 0 deletions src/BirdsiteLive.Twitter/Models/TwitterUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public class TwitterUser
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Url { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,39 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.Statistics.Domain;
using BirdsiteLive.Twitter.Extractors;
using BirdsiteLive.Twitter.Models;
using Tweetinvi;
using Tweetinvi.Models;
using Tweetinvi.Models.Entities;
using Tweetinvi.Parameters;

namespace BirdsiteLive.Twitter
{
public interface ITwitterService
public interface ITwitterTweetsService
{
TwitterUser GetUser(string username);
ExtractedTweet GetTweet(long statusId);
ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTweetId = -1);
}

public class TwitterService : ITwitterService
public class TwitterTweetsService : ITwitterTweetsService
{
private readonly TwitterSettings _settings;
private readonly ITweetExtractor _tweetExtractor;
private readonly ITwitterStatisticsHandler _statisticsHandler;
private readonly ITwitterUserService _twitterUserService;

#region Ctor
public TwitterService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler)
public TwitterTweetsService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService)
{
_settings = settings;
_tweetExtractor = tweetExtractor;
_statisticsHandler = statisticsHandler;
_twitterUserService = twitterUserService;
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
}
#endregion

public TwitterUser GetUser(string username)
{
var user = User.GetUserFromScreenName(username);
_statisticsHandler.CalledUserApi();
if (user == null) return null;

// Expand URLs
var description = user.Description;
foreach (var descriptionUrl in user.Entities?.Description?.Urls?.OrderByDescending(x => x.URL.Length))
description = description.Replace(descriptionUrl.URL, descriptionUrl.ExpandedURL);

return new TwitterUser
{
Acct = username,
Name = user.Name,
Description = description,
Url = $"https://twitter.com/{username}",
ProfileImageUrl = user.ProfileImageUrlFullSize,
ProfileBackgroundImageUrl = user.ProfileBackgroundImageUrlHttps,
ProfileBannerURL = user.ProfileBannerURL
};
}


public ExtractedTweet GetTweet(long statusId)
{
TweetinviConfig.CurrentThreadSettings.TweetMode = TweetMode.Extended;
Expand All @@ -72,9 +46,9 @@ 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);

var user = User.GetUserFromScreenName(username);
_statisticsHandler.CalledUserApi();
var tweets = new List<ITweet>();
if (fromTweetId == -1)
{
Expand All @@ -97,4 +71,4 @@ public ExtractedTweet[] GetTimeline(string username, int nberTweets, long fromTw
return tweets.Select(_tweetExtractor.Extract).ToArray();
}
}
}
}
55 changes: 55 additions & 0 deletions src/BirdsiteLive.Twitter/TwitterUserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Linq;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.Statistics.Domain;
using BirdsiteLive.Twitter.Extractors;
using BirdsiteLive.Twitter.Models;
using Tweetinvi;

namespace BirdsiteLive.Twitter
{
public interface ITwitterUserService
{
TwitterUser GetUser(string username);
}

public class TwitterUserService : ITwitterUserService
{
private readonly TwitterSettings _settings;
private readonly ITweetExtractor _tweetExtractor;
private readonly ITwitterStatisticsHandler _statisticsHandler;

#region Ctor
public TwitterUserService(TwitterSettings settings, ITweetExtractor tweetExtractor, ITwitterStatisticsHandler statisticsHandler)
{
_settings = settings;
_tweetExtractor = tweetExtractor;
_statisticsHandler = statisticsHandler;
Auth.SetApplicationOnlyCredentials(_settings.ConsumerKey, _settings.ConsumerSecret, true);
}
#endregion

public TwitterUser GetUser(string username)
{
var user = User.GetUserFromScreenName(username);
_statisticsHandler.CalledUserApi();
if (user == null) return null;

// Expand URLs
var description = user.Description;
foreach (var descriptionUrl in user.Entities?.Description?.Urls?.OrderByDescending(x => x.URL.Length))
description = description.Replace(descriptionUrl.URL, descriptionUrl.ExpandedURL);

return new TwitterUser
{
Id = user.Id,
Acct = username,
Name = user.Name,
Description = description,
Url = $"https://twitter.com/{username}",
ProfileImageUrl = user.ProfileImageUrlFullSize,
ProfileBackgroundImageUrl = user.ProfileBackgroundImageUrlHttps,
ProfileBannerURL = user.ProfileBannerURL
};
}
}
}
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.9.0</Version>
<Version>0.9.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 7 additions & 5 deletions src/BirdsiteLive/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ namespace BirdsiteLive.Controllers
{
public class UsersController : Controller
{
private readonly ITwitterService _twitterService;
private readonly ITwitterUserService _twitterUserService;
private readonly ITwitterTweetsService _twitterTweetService;
private readonly IUserService _userService;
private readonly IStatusService _statusService;
private readonly InstanceSettings _instanceSettings;

#region Ctor
public UsersController(ITwitterService twitterService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings)
public UsersController(ITwitterUserService twitterUserService, IUserService userService, IStatusService statusService, InstanceSettings instanceSettings, ITwitterTweetsService twitterTweetService)
{
_twitterService = twitterService;
_twitterUserService = twitterUserService;
_userService = userService;
_statusService = statusService;
_instanceSettings = instanceSettings;
_twitterTweetService = twitterTweetService;
}
#endregion

Expand All @@ -53,7 +55,7 @@ public IActionResult Index()
public IActionResult Index(string id)
{
id = id.Trim(new[] { ' ', '@' }).ToLowerInvariant();
var user = _twitterService.GetUser(id);
var user = _twitterUserService.GetUser(id);

var acceptHeaders = Request.Headers["Accept"];
if (acceptHeaders.Any())
Expand Down Expand Up @@ -96,7 +98,7 @@ public IActionResult Tweet(string id, string statusId)
if (!long.TryParse(statusId, out var parsedStatusId))
return NotFound();

var tweet = _twitterService.GetTweet(parsedStatusId);
var tweet = _twitterTweetService.GetTweet(parsedStatusId);
if (tweet == null)
return NotFound();

Expand Down
8 changes: 4 additions & 4 deletions src/BirdsiteLive/Controllers/WellKnownController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ namespace BirdsiteLive.Controllers
[ApiController]
public class WellKnownController : ControllerBase
{
private readonly ITwitterService _twitterService;
private readonly ITwitterUserService _twitterUserService;
private readonly ITwitterUserDal _twitterUserDal;
private readonly InstanceSettings _settings;

#region Ctor
public WellKnownController(InstanceSettings settings, ITwitterService twitterService, ITwitterUserDal twitterUserDal)
public WellKnownController(InstanceSettings settings, ITwitterUserService twitterUserService, ITwitterUserDal twitterUserDal)
{
_twitterService = twitterService;
_twitterUserService = twitterUserService;
_twitterUserDal = twitterUserDal;
_settings = settings;
}
Expand Down Expand Up @@ -163,7 +163,7 @@ public IActionResult Webfinger(string resource = null)
if (!string.IsNullOrWhiteSpace(domain) && domain != _settings.Domain)
return NotFound();

var user = _twitterService.GetUser(name);
var user = _twitterUserService.GetUser(name);
if (user == null)
return NotFound();

Expand Down
4 changes: 2 additions & 2 deletions src/BirdsiteLive/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void ConfigureContainer(ServiceRegistry services)
throw new NotImplementedException($"{dbSettings.Type} is not supported");
}

services.For<ITwitterService>().DecorateAllWith<CachedTwitterService>();
services.For<ITwitterService>().Use<TwitterService>().Singleton();
services.For<ITwitterUserService>().DecorateAllWith<CachedTwitterUserService>();
services.For<ITwitterUserService>().Use<TwitterUserService>().Singleton();

services.Scan(_ =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task ProcessAsync_UserNotSync_Test()
#endregion

#region Mocks
var twitterServiceMock = new Mock<ITwitterService>(MockBehavior.Strict);
var twitterServiceMock = new Mock<ITwitterTweetsService>(MockBehavior.Strict);
twitterServiceMock
.Setup(x => x.GetTimeline(
It.Is<string>(y => y == user1.Acct),
Expand Down Expand Up @@ -105,7 +105,7 @@ public async Task ProcessAsync_UserSync_Test()
#endregion

#region Mocks
var twitterServiceMock = new Mock<ITwitterService>(MockBehavior.Strict);
var twitterServiceMock = new Mock<ITwitterTweetsService>(MockBehavior.Strict);
twitterServiceMock
.Setup(x => x.GetTimeline(
It.Is<string>(y => y == user1.Acct),
Expand Down Expand Up @@ -165,7 +165,7 @@ public async Task ProcessAsync_UserPartiallySync_Test()
#endregion

#region Mocks
var twitterServiceMock = new Mock<ITwitterService>(MockBehavior.Strict);
var twitterServiceMock = new Mock<ITwitterTweetsService>(MockBehavior.Strict);
twitterServiceMock
.Setup(x => x.GetTimeline(
It.Is<string>(y => y == user1.Acct),
Expand Down

0 comments on commit 6e61b6c

Please sign in to comment.