From e7197f305485b92bc3b1042cad23508c95f83e11 Mon Sep 17 00:00:00 2001 From: Vincent Cloutier Date: Wed, 28 Dec 2022 10:23:46 -0500 Subject: [PATCH] made twitteruser more async --- src/BirdsiteLive.Domain/UserService.cs | 2 +- .../RefreshTwitterUserStatusProcessor.cs | 2 +- .../CachedTwitterService.cs | 5 +- .../TwitterTweetsService.cs | 10 +-- .../TwitterUserService.cs | 6 +- .../Controllers/UsersController.cs | 4 +- .../Controllers/WellKnownController.cs | 4 +- .../RefreshTwitterUserStatusProcessorTests.cs | 68 +++++++++---------- 8 files changed, 49 insertions(+), 52 deletions(-) diff --git a/src/BirdsiteLive.Domain/UserService.cs b/src/BirdsiteLive.Domain/UserService.cs index 5e13ec3..96fdf7f 100644 --- a/src/BirdsiteLive.Domain/UserService.cs +++ b/src/BirdsiteLive.Domain/UserService.cs @@ -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 diff --git a/src/BirdsiteLive.Pipeline/Processors/RefreshTwitterUserStatusProcessor.cs b/src/BirdsiteLive.Pipeline/Processors/RefreshTwitterUserStatusProcessor.cs index 6eb5b7c..50632e5 100644 --- a/src/BirdsiteLive.Pipeline/Processors/RefreshTwitterUserStatusProcessor.cs +++ b/src/BirdsiteLive.Pipeline/Processors/RefreshTwitterUserStatusProcessor.cs @@ -40,7 +40,7 @@ namespace BirdsiteLive.Pipeline.Processors try { - userView = _twitterUserService.GetUser(user.Acct); + userView = await _twitterUserService.GetUserAsync(user.Acct); } catch (UserNotFoundException) { diff --git a/src/BirdsiteLive.Twitter/CachedTwitterService.cs b/src/BirdsiteLive.Twitter/CachedTwitterService.cs index c49104a..68b6bc9 100644 --- a/src/BirdsiteLive.Twitter/CachedTwitterService.cs +++ b/src/BirdsiteLive.Twitter/CachedTwitterService.cs @@ -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 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); } diff --git a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs index a615d23..6577cda 100644 --- a/src/BirdsiteLive.Twitter/TwitterTweetsService.cs +++ b/src/BirdsiteLive.Twitter/TwitterTweetsService.cs @@ -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 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") diff --git a/src/BirdsiteLive.Twitter/TwitterUserService.cs b/src/BirdsiteLive.Twitter/TwitterUserService.cs index e774c66..cb72e67 100644 --- a/src/BirdsiteLive.Twitter/TwitterUserService.cs +++ b/src/BirdsiteLive.Twitter/TwitterUserService.cs @@ -13,7 +13,7 @@ namespace BirdsiteLive.Twitter { public interface ITwitterUserService { - TwitterUser GetUser(string username); + Task 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 GetUserAsync(string username) { await _twitterAuthenticationInitializer.EnsureAuthenticationIsInitialized(); diff --git a/src/BirdsiteLive/Controllers/UsersController.cs b/src/BirdsiteLive/Controllers/UsersController.cs index 73207ea..c4d35cd 100644 --- a/src/BirdsiteLive/Controllers/UsersController.cs +++ b/src/BirdsiteLive/Controllers/UsersController.cs @@ -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 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) { diff --git a/src/BirdsiteLive/Controllers/WellKnownController.cs b/src/BirdsiteLive/Controllers/WellKnownController.cs index 3e14d7d..6d96d3c 100644 --- a/src/BirdsiteLive/Controllers/WellKnownController.cs +++ b/src/BirdsiteLive/Controllers/WellKnownController.cs @@ -142,7 +142,7 @@ namespace BirdsiteLive.Controllers } [Route("/.well-known/webfinger")] - public IActionResult Webfinger(string resource = null) + public async Task 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) { diff --git a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RefreshTwitterUserStatusProcessorTests.cs b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RefreshTwitterUserStatusProcessorTests.cs index cd2d116..6319432 100644 --- a/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RefreshTwitterUserStatusProcessorTests.cs +++ b/src/Tests/BirdsiteLive.Pipeline.Tests/Processors/RefreshTwitterUserStatusProcessorTests.cs @@ -47,8 +47,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.IsAny())) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.IsAny())) + .ReturnsAsync(new TwitterUser { Protected = false }); @@ -95,8 +95,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.IsAny())) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.IsAny())) + .ReturnsAsync(new TwitterUser { Protected = false }); @@ -152,14 +152,14 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct1))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct1))) + .ReturnsAsync(new TwitterUser { Protected = false }); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct2))) + .Setup(x => x.GetUserAsync(It.Is(y => y == acct2))) .Throws(new UserNotFoundException()); var twitterUserDalMock = new Mock(MockBehavior.Strict); @@ -216,14 +216,14 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct1))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct1))) + .ReturnsAsync(new TwitterUser { Protected = false }); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct2))) + .Setup(x => x.GetUserAsync(It.Is(y => y == acct2))) .Throws(new UserHasBeenSuspendedException()); var twitterUserDalMock = new Mock(MockBehavior.Strict); @@ -280,14 +280,14 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct1))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct1))) + .ReturnsAsync(new TwitterUser { Protected = false }); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct2))) + .Setup(x => x.GetUserAsync(It.Is(y => y == acct2))) .Throws(new Exception()); var twitterUserDalMock = new Mock(MockBehavior.Strict); @@ -352,15 +352,15 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct1))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct1))) + .ReturnsAsync(new TwitterUser { Protected = false }); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct2))) - .Returns((TwitterUser)null); + .Setup(x => x.GetUserAsync(It.Is(y => y == acct2))) + .ReturnsAsync((TwitterUser)null); var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock @@ -424,15 +424,15 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct1))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct1))) + .ReturnsAsync(new TwitterUser { Protected = false }); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct2))) - .Returns((TwitterUser)null); + .Setup(x => x.GetUserAsync(It.Is(y => y == acct2))) + .ReturnsAsync((TwitterUser)null); var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock @@ -495,15 +495,15 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct1))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct1))) + .ReturnsAsync(new TwitterUser { Protected = false }); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct2))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct2))) + .ReturnsAsync(new TwitterUser { Protected = true }); @@ -570,15 +570,15 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct1))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct1))) + .ReturnsAsync(new TwitterUser { Protected = false }); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct2))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct2))) + .ReturnsAsync(new TwitterUser { Protected = true }); @@ -639,8 +639,8 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct1))) - .Returns((TwitterUser)null); + .Setup(x => x.GetUserAsync(It.Is(y => y == acct1))) + .ReturnsAsync((TwitterUser)null); var twitterUserDalMock = new Mock(MockBehavior.Strict); twitterUserDalMock @@ -700,14 +700,14 @@ namespace BirdsiteLive.Pipeline.Tests.Processors #region Mocks var twitterUserServiceMock = new Mock(MockBehavior.Strict); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct1))) - .Returns(new TwitterUser + .Setup(x => x.GetUserAsync(It.Is(y => y == acct1))) + .ReturnsAsync(new TwitterUser { Protected = false, }); twitterUserServiceMock - .Setup(x => x.GetUser(It.Is(y => y == acct2))) + .Setup(x => x.GetUserAsync(It.Is(y => y == acct2))) .Throws(new RateLimitExceededException()); var twitterUserDalMock = new Mock(MockBehavior.Strict);