Twitter user cache refactoring
This commit is contained in:
parent
90be1b58bf
commit
bdb4b86ae8
6 changed files with 58 additions and 24 deletions
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using BirdsiteLive.Common.Settings;
|
||||
using BirdsiteLive.Twitter.Models;
|
||||
|
@ -9,6 +10,7 @@ namespace BirdsiteLive.Twitter
|
|||
public interface ICachedTwitterUserService : ITwitterUserService
|
||||
{
|
||||
void PurgeUser(string username);
|
||||
void AddUser(TwitterUser user);
|
||||
}
|
||||
|
||||
public class CachedTwitterUserService : ICachedTwitterUserService
|
||||
|
@ -53,9 +55,18 @@ namespace BirdsiteLive.Twitter
|
|||
return _twitterService.IsUserApiRateLimited();
|
||||
}
|
||||
|
||||
public TwitterUser Extract(JsonElement result)
|
||||
{
|
||||
return _twitterService.Extract(result);
|
||||
}
|
||||
public void PurgeUser(string username)
|
||||
{
|
||||
_userCache.Remove(username);
|
||||
}
|
||||
public void AddUser(TwitterUser user)
|
||||
{
|
||||
|
||||
_userCache.Set(user.Acct, user, _cacheEntryOptions);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,12 +25,12 @@ namespace BirdsiteLive.Twitter
|
|||
{
|
||||
private readonly ITwitterAuthenticationInitializer _twitterAuthenticationInitializer;
|
||||
private readonly ITwitterStatisticsHandler _statisticsHandler;
|
||||
private readonly ITwitterUserService _twitterUserService;
|
||||
private readonly ICachedTwitterUserService _twitterUserService;
|
||||
private readonly ILogger<TwitterTweetsService> _logger;
|
||||
private HttpClient _httpClient = new HttpClient();
|
||||
|
||||
#region Ctor
|
||||
public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ITwitterUserService twitterUserService, ILogger<TwitterTweetsService> logger)
|
||||
public TwitterTweetsService(ITwitterAuthenticationInitializer twitterAuthenticationInitializer, ITwitterStatisticsHandler statisticsHandler, ICachedTwitterUserService twitterUserService, ILogger<TwitterTweetsService> logger)
|
||||
{
|
||||
_twitterAuthenticationInitializer = twitterAuthenticationInitializer;
|
||||
_statisticsHandler = statisticsHandler;
|
||||
|
@ -126,7 +126,7 @@ namespace BirdsiteLive.Twitter
|
|||
{
|
||||
var extractedTweet = await Extract(tweet);
|
||||
extractedTweets.Add(extractedTweet);
|
||||
|
||||
|
||||
if (extractedTweet.Id == fromTweetId)
|
||||
break;
|
||||
|
||||
|
@ -137,6 +137,17 @@ namespace BirdsiteLive.Twitter
|
|||
+ JsonObject.Create(tweet).ToJsonString(new JsonSerializerOptions { WriteIndented = true }));
|
||||
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
JsonElement userDoc = tweet.GetProperty("content").GetProperty("itemContent")
|
||||
.GetProperty("tweet_results").GetProperty("core").GetProperty("user_results");
|
||||
|
||||
TwitterUser tweetUser = _twitterUserService.Extract(userDoc);
|
||||
_twitterUserService.AddUser(tweetUser);
|
||||
}
|
||||
catch (Exception e)
|
||||
{}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ namespace BirdsiteLive.Twitter
|
|||
public interface ITwitterUserService
|
||||
{
|
||||
Task<TwitterUser> GetUserAsync(string username);
|
||||
TwitterUser Extract (JsonElement result);
|
||||
bool IsUserApiRateLimited();
|
||||
}
|
||||
|
||||
|
@ -53,25 +54,7 @@ namespace BirdsiteLive.Twitter
|
|||
res = JsonDocument.Parse(c);
|
||||
}
|
||||
var result = res.RootElement.GetProperty("data").GetProperty("user").GetProperty("result");
|
||||
string profileBannerURL = null;
|
||||
JsonElement profileBannerURLObject;
|
||||
if (result.GetProperty("legacy").TryGetProperty("profile_banner_url", out profileBannerURLObject))
|
||||
{
|
||||
profileBannerURL = profileBannerURLObject.GetString();
|
||||
}
|
||||
|
||||
return new TwitterUser
|
||||
{
|
||||
Id = long.Parse(result.GetProperty("rest_id").GetString()),
|
||||
Acct = username,
|
||||
Name = result.GetProperty("legacy").GetProperty("name").GetString(), //res.RootElement.GetProperty("data").GetProperty("name").GetString(),
|
||||
Description = "", //res.RootElement.GetProperty("data").GetProperty("description").GetString(),
|
||||
Url = "", //res.RootElement.GetProperty("data").GetProperty("url").GetString(),
|
||||
ProfileImageUrl = result.GetProperty("legacy").GetProperty("profile_image_url_https").GetString().Replace("normal", "400x400"),
|
||||
ProfileBackgroundImageUrl = profileBannerURL,
|
||||
ProfileBannerURL = profileBannerURL,
|
||||
Protected = false, //res.RootElement.GetProperty("data").GetProperty("protected").GetBoolean(),
|
||||
};
|
||||
return Extract(result);
|
||||
}
|
||||
catch (System.Collections.Generic.KeyNotFoundException)
|
||||
{
|
||||
|
@ -106,6 +89,30 @@ namespace BirdsiteLive.Twitter
|
|||
|
||||
}
|
||||
|
||||
public TwitterUser Extract(JsonElement result)
|
||||
{
|
||||
string profileBannerURL = null;
|
||||
JsonElement profileBannerURLObject;
|
||||
if (result.GetProperty("legacy").TryGetProperty("profile_banner_url", out profileBannerURLObject))
|
||||
{
|
||||
profileBannerURL = profileBannerURLObject.GetString();
|
||||
}
|
||||
|
||||
return new TwitterUser
|
||||
{
|
||||
Id = long.Parse(result.GetProperty("rest_id").GetString()),
|
||||
Acct = result.GetProperty("legacy").GetProperty("screen_name").GetString(),
|
||||
Name = result.GetProperty("legacy").GetProperty("name").GetString(), //res.RootElement.GetProperty("data").GetProperty("name").GetString(),
|
||||
Description = "", //res.RootElement.GetProperty("data").GetProperty("description").GetString(),
|
||||
Url = "", //res.RootElement.GetProperty("data").GetProperty("url").GetString(),
|
||||
ProfileImageUrl = result.GetProperty("legacy").GetProperty("profile_image_url_https").GetString().Replace("normal", "400x400"),
|
||||
ProfileBackgroundImageUrl = profileBannerURL,
|
||||
ProfileBannerURL = profileBannerURL,
|
||||
Protected = false, //res.RootElement.GetProperty("data").GetProperty("protected").GetBoolean(),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public bool IsUserApiRateLimited()
|
||||
{
|
||||
return false;
|
||||
|
|
|
@ -19,9 +19,11 @@ namespace BirdsiteLive.ActivityPub.Tests
|
|||
var logger2 = new Mock<ILogger<TwitterUserService>>(MockBehavior.Strict);
|
||||
var logger3 = new Mock<ILogger<TwitterTweetsService>>();
|
||||
var stats = new Mock<ITwitterStatisticsHandler>();
|
||||
var settings = new Mock<Common.Settings.InstanceSettings>();
|
||||
ITwitterAuthenticationInitializer auth = new TwitterAuthenticationInitializer(logger1.Object);
|
||||
ITwitterUserService user = new TwitterUserService(auth, stats.Object, logger2.Object);
|
||||
_tweetService = new TwitterTweetsService(auth, stats.Object, user, logger3.Object);
|
||||
ICachedTwitterUserService user2 = new CachedTwitterUserService(user, settings.Object);
|
||||
_tweetService = new TwitterTweetsService(auth, stats.Object, user2, logger3.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
|
@ -18,10 +18,12 @@ namespace BirdsiteLive.ActivityPub.Tests
|
|||
var logger1 = new Mock<ILogger<TwitterAuthenticationInitializer>>(MockBehavior.Strict);
|
||||
var logger2 = new Mock<ILogger<TwitterUserService>>(MockBehavior.Strict);
|
||||
var logger3 = new Mock<ILogger<TwitterTweetsService>>();
|
||||
var settings = new Mock<Common.Settings.InstanceSettings>();
|
||||
var stats = new Mock<ITwitterStatisticsHandler>();
|
||||
ITwitterAuthenticationInitializer auth = new TwitterAuthenticationInitializer(logger1.Object);
|
||||
ITwitterUserService user = new TwitterUserService(auth, stats.Object, logger2.Object);
|
||||
_tweetService = new TwitterTweetsService(auth, stats.Object, user, logger3.Object);
|
||||
ICachedTwitterUserService user2 = new CachedTwitterUserService(user, settings.Object);
|
||||
_tweetService = new TwitterTweetsService(auth, stats.Object, user2, logger3.Object);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
|
@ -28,6 +28,7 @@ namespace BirdsiteLive.ActivityPub.Tests
|
|||
{
|
||||
var user = await _tweetService.GetUserAsync("kobebryant");
|
||||
Assert.AreEqual(user.Name, "Kobe Bryant");
|
||||
Assert.AreEqual(user.Acct, "kobebryant");
|
||||
}
|
||||
|
||||
|
||||
|
|
Reference in a new issue