conversion to System.Text.Json part 2

This commit is contained in:
Vincent Cloutier 2023-03-16 11:46:05 -04:00
parent 29ba6baddb
commit 2393563574
9 changed files with 46 additions and 21 deletions

View file

@ -34,12 +34,13 @@ namespace BirdsiteLive.ActivityPub
id = accept.id,
actor = accept.actor,
context = accept.context,
apObject = new NestedActivity()
apObject = new ActivityFollow()
{
id = accept.apObject.id,
type = accept.apObject.type,
actor = accept.apObject.actor,
context = accept.apObject.context,
context = accept.apObject.context?.ToString(),
apObject = accept.apObject.apObject,
}
};

View file

@ -5,6 +5,6 @@ namespace BirdsiteLive.ActivityPub
public class ActivityAcceptFollow : Activity
{
[JsonPropertyName("object")]
public NestedActivity apObject { get; set; }
public ActivityFollow apObject { get; set; }
}
}

View file

@ -83,7 +83,7 @@ namespace BirdsiteLive.Domain
id = $"{activity.apObject}#accepts/follows/{Guid.NewGuid()}",
type = "Accept",
actor = activity.apObject,
apObject = new NestedActivity()
apObject = new ActivityFollow()
{
id = activity.id,
type = activity.type,
@ -100,8 +100,7 @@ namespace BirdsiteLive.Domain
if (!string.IsNullOrWhiteSpace(inbox))
usedInbox = inbox;
var json = JsonSerializer.Serialize(data,
new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
var json = JsonSerializer.Serialize(data);
var date = DateTime.UtcNow.ToUniversalTime();
var httpDate = date.ToString("r");

View file

@ -243,10 +243,11 @@ namespace BirdsiteLive.Domain
actor = activity.apObject.apObject,
apObject = new ActivityUndoFollow()
{
id = activity.id,
type = activity.type,
actor = activity.actor,
apObject = activity.apObject
id = (activity.apObject as dynamic).id?.ToString(),
type = (activity.apObject as dynamic).type?.ToString(),
actor = (activity.apObject as dynamic).actor?.ToString(),
context = (activity.apObject as dynamic).context?.ToString(),
apObject = (activity.apObject as dynamic).@object?.ToString()
}
};
var result = await _activityPubService.PostDataAsync(acceptFollow, followerHost, activity.apObject.apObject);

View file

@ -55,16 +55,14 @@ namespace BirdsiteLive.Twitter
"https://api.twitter.com/graphql/XjlydVWHFIDaAUny86oh2g/TweetDetail?variables=%7B%22focalTweetId%22%3A%22"
+ statusId +
"%22,%22with_rux_injections%22%3Atrue,%22includePromotedContent%22%3Afalse,%22withCommunity%22%3Afalse,%22withQuickPromoteEligibilityTweetFields%22%3Afalse,%22withBirdwatchNotes%22%3Afalse,%22withSuperFollowsUserFields%22%3Afalse,%22withDownvotePerspective%22%3Afalse,%22withReactionsMetadata%22%3Afalse,%22withReactionsPerspective%22%3Afalse,%22withSuperFollowsTweetFields%22%3Afalse,%22withVoice%22%3Atrue,%22withV2Timeline%22%3Atrue%7D&features=%7B%22responsive_web_twitter_blue_verified_badge_is_enabled%22%3Atrue,%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue,%22verified_phone_label_enabled%22%3Afalse,%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue,%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse,%22tweetypie_unmention_optimization_enabled%22%3Atrue,%22vibe_api_enabled%22%3Atrue,%22responsive_web_edit_tweet_api_enabled%22%3Atrue,%22graphql_is_translatable_rweb_tweet_is_translatable_enabled%22%3Afalse,%22view_counts_everywhere_api_enabled%22%3Atrue,%22longform_notetweets_consumption_enabled%22%3Atrue,%22tweet_awards_web_tipping_enabled%22%3Afalse,%22freedom_of_speech_not_reach_fetch_enabled%22%3Afalse,%22standardized_nudges_misinfo%22%3Atrue,%22tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled%22%3Afalse,%22interactive_text_enabled%22%3Atrue,%22responsive_web_text_conversations_enabled%22%3Afalse,%22longform_notetweets_richtext_consumption_enabled%22%3Afalse,%22responsive_web_enhance_cards_enabled%22%3Atrue%7D";
using var request = _twitterAuthenticationInitializer.MakeHttpRequest(new HttpMethod("GET"), reqURL);
try
{
JsonDocument tweet;
using (var request = _twitterAuthenticationInitializer.MakeHttpRequest(new HttpMethod("GET"), reqURL))
{
var httpResponse = await client.SendAsync(request);
httpResponse.EnsureSuccessStatusCode();
var c = await httpResponse.Content.ReadAsStringAsync();
tweet = JsonDocument.Parse(c);
}
var httpResponse = await client.SendAsync(request);
httpResponse.EnsureSuccessStatusCode();
var c = await httpResponse.Content.ReadAsStringAsync();
tweet = JsonDocument.Parse(c);
var timeline = tweet.RootElement.GetProperty("data").GetProperty("threaded_conversation_with_injections_v2")
@ -77,6 +75,7 @@ namespace BirdsiteLive.Twitter
catch (Exception e)
{
_logger.LogError(e, "Error retrieving tweet {TweetId}", statusId);
await _twitterAuthenticationInitializer.RefreshClient(request);
return null;
}
}

View file

@ -6,6 +6,7 @@ using BirdsiteLive.Domain.Factories;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Text.Json;
namespace BirdsiteLive.Domain.Tests
@ -38,8 +39,21 @@ namespace BirdsiteLive.Domain.Tests
var activity = new ActivityAcceptFollow()
{
id = "awef",
context = "https://www.w3.org/ns/activitystreams",
type = "Accept",
actor = "https://mastodon.technology/users/testtest",
apObject = new ActivityFollow()
{
context = "https://www.w3.org/ns/activitystreams",
id = "abc",
type = "Follow",
actor = "https://mastodon.technology/users/testtest2",
apObject = "https://mastodon.technology/users/testtest3",
}
};
var json = "{\"id\":\"awef\"}";
var json =
"""{"object":{"object":"https://mastodon.technology/users/testtest3","@context":"https://www.w3.org/ns/activitystreams","id":"abc","type":"Follow","actor":"https://mastodon.technology/users/testtest2"},"@context":"https://www.w3.org/ns/activitystreams","id":"awef","type":"Accept","actor":"https://mastodon.technology/users/testtest"}""";
#region Validations
var req = service.BuildRequest(activity, "google.com", "tata", "awef");
@ -70,6 +84,8 @@ namespace BirdsiteLive.Domain.Tests
var req = service.BuildAcceptFollow(activity);
string s = JsonSerializer.Serialize(req);
Assert.AreEqual(req.actor, activityRes.actor);
Assert.AreEqual(req.context, activityRes.context);

View file

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net6</TargetFramework>
<LangVersion>11</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

View file

@ -41,6 +41,11 @@ namespace BirdsiteLive.ActivityPub.Tests
ITwitterUserService user = new TwitterUserService(auth, stats.Object, logger2.Object);
ICachedTwitterUserService user2 = new CachedTwitterUserService(user, settings);
_tweetService = new TwitterTweetsService(auth, stats.Object, user2, twitterDal.Object, settings, logger3.Object);
await Task.Delay(1000);
await auth.MakeHttpClient();
await Task.Delay(1000);
await auth.MakeHttpClient();
}
[TestMethod]
@ -48,7 +53,7 @@ namespace BirdsiteLive.ActivityPub.Tests
{
var tweets = await _tweetService.GetTimelineAsync("kobebryant", 1218020971346444288);
Assert.AreEqual(tweets[0].MessageContent, "Continuing to move the game forward @KingJames. Much respect my brother 💪🏾 #33644");
Assert.AreEqual(tweets.Length, 8);
Assert.IsTrue(tweets.Length > 5);
}
[TestMethod]

View file

@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using BirdsiteLive.Twitter;
@ -18,6 +20,7 @@ namespace BirdsiteLive.ActivityPub.Tests
[TestInitialize]
public async Task TestInit()
{
var logger1 = new Mock<ILogger<TwitterAuthenticationInitializer>>(MockBehavior.Strict);
var logger2 = new Mock<ILogger<TwitterUserService>>(MockBehavior.Strict);
var logger3 = new Mock<ILogger<TwitterTweetsService>>();
@ -33,6 +36,7 @@ namespace BirdsiteLive.ActivityPub.Tests
ITwitterUserService user = new TwitterUserService(auth, stats.Object, logger2.Object);
ICachedTwitterUserService user2 = new CachedTwitterUserService(user, settings);
_tweetService = new TwitterTweetsService(auth, stats.Object, user2, twitterDal.Object, settings, logger3.Object);
}
[TestMethod]