made twitteruser more async

This commit is contained in:
Vincent Cloutier 2022-12-28 10:23:46 -05:00
parent 944dfc7254
commit e7197f3054
8 changed files with 49 additions and 52 deletions

View file

@ -162,7 +162,7 @@ namespace BirdsiteLive.Domain
}
// Validate User Protected
var user = _twitterUserService.GetUser(twitterUser);
var user = await _twitterUserService.GetUserAsync(twitterUser);
if (!user.Protected)
{
// Execute

View file

@ -40,7 +40,7 @@ namespace BirdsiteLive.Pipeline.Processors
try
{
userView = _twitterUserService.GetUser(user.Acct);
userView = await _twitterUserService.GetUserAsync(user.Acct);
}
catch (UserNotFoundException)
{

View file

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using BirdsiteLive.Common.Settings;
using BirdsiteLive.Twitter.Models;
using Microsoft.Extensions.Caching.Memory;
@ -36,11 +37,11 @@ namespace BirdsiteLive.Twitter
}
#endregion
public TwitterUser GetUser(string username)
public async Task<TwitterUser> GetUserAsync(string username)
{
if (!_userCache.TryGetValue(username, out TwitterUser user))
{
user = _twitterService.GetUser(username);
user = await _twitterService.GetUserAsync(username);
if(user != null) _userCache.Set(username, user, _cacheEntryOptions);
}

View file

@ -65,7 +65,7 @@ namespace BirdsiteLive.Twitter
var timeline = tweet.RootElement.GetProperty("data").GetProperty("threaded_conversation_with_injections_v2")
.GetProperty("instructions").EnumerateArray().First().GetProperty("entries").EnumerateArray();
return Extract( timeline.First() );
return await Extract( timeline.First() );
}
catch (Exception e)
{
@ -84,7 +84,7 @@ namespace BirdsiteLive.Twitter
var client = await _twitterAuthenticationInitializer.MakeHttpClient();
var user = _twitterUserService.GetUser(username);
var user = await _twitterUserService.GetUserAsync(username);
if (user == null || user.Protected) return new ExtractedTweet[0];
@ -129,7 +129,7 @@ namespace BirdsiteLive.Twitter
try
{
var extractedTweet = Extract(tweet);
var extractedTweet = await Extract(tweet);
extractedTweets.Add(extractedTweet);
}
@ -145,7 +145,7 @@ namespace BirdsiteLive.Twitter
return extractedTweets.ToArray();
}
private ExtractedTweet Extract(JsonElement tweet)
private async Task<ExtractedTweet> Extract(JsonElement tweet)
{
JsonElement retweet;
@ -188,7 +188,7 @@ namespace BirdsiteLive.Twitter
.GetProperty("retweeted_status_result").GetProperty("result")
.GetProperty("core").GetProperty("user_results").GetProperty("result")
.GetProperty("legacy").GetProperty("screen_name").GetString();
OriginalAuthor = _twitterUserService.GetUser(OriginalAuthorUsername);
OriginalAuthor = await _twitterUserService.GetUserAsync(OriginalAuthorUsername);
}
string creationTime = tweet.GetProperty("content").GetProperty("itemContent")

View file

@ -13,7 +13,7 @@ namespace BirdsiteLive.Twitter
{
public interface ITwitterUserService
{
TwitterUser GetUser(string username);
Task<TwitterUser> GetUserAsync(string username);
bool IsUserApiRateLimited();
}
@ -35,10 +35,6 @@ namespace BirdsiteLive.Twitter
}
#endregion
public TwitterUser GetUser(string username)
{
return GetUserAsync(username).Result;
}
public async Task<TwitterUser> GetUserAsync(string username)
{
await _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized();

View file

@ -59,7 +59,7 @@ namespace BirdsiteLive.Controllers
[Route("/@{id}")]
[Route("/users/{id}")]
[Route("/users/{id}/remote_follow")]
public IActionResult Index(string id)
public async Task<IActionResult> Index(string id)
{
_logger.LogTrace("User Index: {Id}", id);
@ -75,7 +75,7 @@ namespace BirdsiteLive.Controllers
{
try
{
user = _twitterUserService.GetUser(id);
user = await _twitterUserService.GetUserAsync(id);
}
catch (UserNotFoundException)
{

View file

@ -142,7 +142,7 @@ namespace BirdsiteLive.Controllers
}
[Route("/.well-known/webfinger")]
public IActionResult Webfinger(string resource = null)
public async Task<IActionResult> Webfinger(string resource = null)
{
if (string.IsNullOrWhiteSpace(resource))
return BadRequest();
@ -203,7 +203,7 @@ namespace BirdsiteLive.Controllers
try
{
_twitterUserService.GetUser(name);
await _twitterUserService.GetUserAsync(name);
}
catch (UserNotFoundException)
{

View file

@ -47,8 +47,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.IsAny<string>()))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.IsAny<string>()))
.ReturnsAsync(new TwitterUser
{
Protected = false
});
@ -95,8 +95,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.IsAny<string>()))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.IsAny<string>()))
.ReturnsAsync(new TwitterUser
{
Protected = false
});
@ -152,14 +152,14 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync(new TwitterUser
{
Protected = false
});
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct2)))
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct2)))
.Throws(new UserNotFoundException());
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
@ -216,14 +216,14 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync(new TwitterUser
{
Protected = false
});
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct2)))
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct2)))
.Throws(new UserHasBeenSuspendedException());
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
@ -280,14 +280,14 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync(new TwitterUser
{
Protected = false
});
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct2)))
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct2)))
.Throws(new Exception());
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
@ -352,15 +352,15 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync(new TwitterUser
{
Protected = false
});
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct2)))
.Returns((TwitterUser)null);
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct2)))
.ReturnsAsync((TwitterUser)null);
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
@ -424,15 +424,15 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync(new TwitterUser
{
Protected = false
});
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct2)))
.Returns((TwitterUser)null);
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct2)))
.ReturnsAsync((TwitterUser)null);
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
@ -495,15 +495,15 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync(new TwitterUser
{
Protected = false
});
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct2)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct2)))
.ReturnsAsync(new TwitterUser
{
Protected = true
});
@ -570,15 +570,15 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync(new TwitterUser
{
Protected = false
});
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct2)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct2)))
.ReturnsAsync(new TwitterUser
{
Protected = true
});
@ -639,8 +639,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns((TwitterUser)null);
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync((TwitterUser)null);
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);
twitterUserDalMock
@ -700,14 +700,14 @@ namespace BirdsiteLive.Pipeline.Tests.Processors
#region Mocks
var twitterUserServiceMock = new Mock<ICachedTwitterUserService>(MockBehavior.Strict);
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct1)))
.Returns(new TwitterUser
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct1)))
.ReturnsAsync(new TwitterUser
{
Protected = false,
});
twitterUserServiceMock
.Setup(x => x.GetUser(It.Is<string>(y => y == acct2)))
.Setup(x => x.GetUserAsync(It.Is<string>(y => y == acct2)))
.Throws(new RateLimitExceededException());
var twitterUserDalMock = new Mock<ITwitterUserDal>(MockBehavior.Strict);