From 058c9514244d970b7d7223f9155dbbff432ecd8a Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sat, 9 Jan 2021 22:26:17 -0500 Subject: [PATCH 1/3] fix case issue --- .../Converters/UrlFactory.cs | 17 ++++++++++++++ src/BirdsiteLive.Domain/ActivityPubService.cs | 16 +++++--------- src/BirdsiteLive.Domain/StatusService.cs | 12 ++++++---- src/BirdsiteLive.Domain/UserService.cs | 22 +++++++++++-------- .../Controllers/UsersController.cs | 6 ++--- .../Controllers/WellKnownController.cs | 16 ++++++++++---- 6 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 src/BirdsiteLive.ActivityPub/Converters/UrlFactory.cs diff --git a/src/BirdsiteLive.ActivityPub/Converters/UrlFactory.cs b/src/BirdsiteLive.ActivityPub/Converters/UrlFactory.cs new file mode 100644 index 0000000..8abe5e2 --- /dev/null +++ b/src/BirdsiteLive.ActivityPub/Converters/UrlFactory.cs @@ -0,0 +1,17 @@ +using System.Runtime.CompilerServices; + +namespace BirdsiteLive.ActivityPub.Converters +{ + public class UrlFactory + { + public static string GetActorUrl(string domain, string username) + { + return $"https://{domain.ToLowerInvariant()}/users/{username.ToLowerInvariant()}"; + } + + public static string GetNoteUrl(string domain, string username, string noteId) + { + return $"https://{domain.ToLowerInvariant()}/users/{username.ToLowerInvariant()}/statuses/{noteId}"; + } + } +} \ No newline at end of file diff --git a/src/BirdsiteLive.Domain/ActivityPubService.cs b/src/BirdsiteLive.Domain/ActivityPubService.cs index bbabf07..16d9610 100644 --- a/src/BirdsiteLive.Domain/ActivityPubService.cs +++ b/src/BirdsiteLive.Domain/ActivityPubService.cs @@ -6,6 +6,7 @@ using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using BirdsiteLive.ActivityPub; +using BirdsiteLive.ActivityPub.Converters; using BirdsiteLive.ActivityPub.Models; using BirdsiteLive.Common.Settings; using Newtonsoft.Json; @@ -47,18 +48,11 @@ namespace BirdsiteLive.Domain public async Task PostNewNoteActivity(Note note, string username, string noteId, string targetHost, string targetInbox) { - //var username = "gra"; - var actor = $"https://{_instanceSettings.Domain}/users/{username}"; - //var targetHost = "mastodon.technology"; - //var target = $"{targetHost}/users/testtest"; - //var inbox = $"/users/testtest/inbox"; + //var actor = $"https://{_instanceSettings.Domain}/users/{username}"; + var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username); - //var noteGuid = Guid.NewGuid(); - var noteUri = $"https://{_instanceSettings.Domain}/users/{username}/statuses/{noteId}"; - - //var noteUrl = $"https://{_instanceSettings.Domain}/@{username}/{noteId}"; - //var to = $"{actor}/followers"; - //var apPublic = "https://www.w3.org/ns/activitystreams#Public"; + //var noteUri = $"https://{_instanceSettings.Domain}/users/{username}/statuses/{noteId}"; + var noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, noteId); var now = DateTime.UtcNow; var nowString = now.ToString("s") + "Z"; diff --git a/src/BirdsiteLive.Domain/StatusService.cs b/src/BirdsiteLive.Domain/StatusService.cs index 54a95ca..d86512a 100644 --- a/src/BirdsiteLive.Domain/StatusService.cs +++ b/src/BirdsiteLive.Domain/StatusService.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Text.RegularExpressions; using BirdsiteLive.ActivityPub; +using BirdsiteLive.ActivityPub.Converters; using BirdsiteLive.ActivityPub.Models; using BirdsiteLive.Common.Settings; using BirdsiteLive.Domain.Tools; @@ -33,9 +34,12 @@ namespace BirdsiteLive.Domain public Note GetStatus(string username, ExtractedTweet tweet) { - var actorUrl = $"https://{_instanceSettings.Domain}/users/{username}"; - var noteId = $"https://{_instanceSettings.Domain}/users/{username}/statuses/{tweet.Id}"; - var noteUrl = $"https://{_instanceSettings.Domain}/@{username}/{tweet.Id}"; + //var actorUrl = $"https://{_instanceSettings.Domain}/users/{username}"; + var actorUrl = UrlFactory.GetActorUrl(_instanceSettings.Domain, username); + //var noteId = $"https://{_instanceSettings.Domain}/users/{username}/statuses/{tweet.Id}"; + var noteId = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, tweet.Id.ToString()); + //var noteUrl = $"https://{_instanceSettings.Domain}/@{username}/{tweet.Id}"; + var noteUrl = noteId; var to = $"{actorUrl}/followers"; var apPublic = "https://www.w3.org/ns/activitystreams#Public"; @@ -44,7 +48,7 @@ namespace BirdsiteLive.Domain string inReplyTo = null; if (tweet.InReplyToStatusId != default) - inReplyTo = $"https://{_instanceSettings.Domain}/users/{tweet.InReplyToAccount}/statuses/{tweet.InReplyToStatusId}"; + inReplyTo = $"https://{_instanceSettings.Domain}/users/{tweet.InReplyToAccount.ToLowerInvariant()}/statuses/{tweet.InReplyToStatusId}"; var note = new Note { diff --git a/src/BirdsiteLive.Domain/UserService.cs b/src/BirdsiteLive.Domain/UserService.cs index 02323ea..c203977 100644 --- a/src/BirdsiteLive.Domain/UserService.cs +++ b/src/BirdsiteLive.Domain/UserService.cs @@ -6,6 +6,7 @@ using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using BirdsiteLive.ActivityPub; +using BirdsiteLive.ActivityPub.Converters; using BirdsiteLive.Common.Settings; using BirdsiteLive.Cryptography; using BirdsiteLive.Domain.BusinessUseCases; @@ -45,21 +46,24 @@ namespace BirdsiteLive.Domain public Actor GetUser(TwitterUser twitterUser) { + var actorUrl = UrlFactory.GetActorUrl(_instanceSettings.Domain, twitterUser.Acct); + var acct = twitterUser.Acct.ToLowerInvariant(); + var user = new Actor { - id = $"https://{_instanceSettings.Domain}/users/{twitterUser.Acct}", - type = "Service", //Person Service - followers = $"https://{_instanceSettings.Domain}/users/{twitterUser.Acct}/followers", - preferredUsername = twitterUser.Acct, + id = actorUrl, + type = "Service", + followers = $"{actorUrl}/followers", + preferredUsername = acct, name = twitterUser.Name, - inbox = $"https://{_instanceSettings.Domain}/users/{twitterUser.Acct}/inbox", + inbox = $"{actorUrl}/inbox", summary = twitterUser.Description, - url = $"https://{_instanceSettings.Domain}/@{twitterUser.Acct}", + url = actorUrl, publicKey = new PublicKey() { - id = $"https://{_instanceSettings.Domain}/users/{twitterUser.Acct}#main-key", - owner = $"https://{_instanceSettings.Domain}/users/{twitterUser.Acct}", - publicKeyPem = _cryptoService.GetUserPem(twitterUser.Acct) + id = $"{actorUrl}#main-key", + owner = actorUrl, + publicKeyPem = _cryptoService.GetUserPem(acct) }, icon = new Image { diff --git a/src/BirdsiteLive/Controllers/UsersController.cs b/src/BirdsiteLive/Controllers/UsersController.cs index bcb3f7f..78941d5 100644 --- a/src/BirdsiteLive/Controllers/UsersController.cs +++ b/src/BirdsiteLive/Controllers/UsersController.cs @@ -48,7 +48,7 @@ namespace BirdsiteLive.Controllers [Route("/users/{id}")] public IActionResult Index(string id) { - id = id.Trim(new[] {' ', '@'}); + id = id.Trim(new[] {' ', '@'}).ToLowerInvariant(); var user = _twitterService.GetUser(id); var r = Request.Headers["Accept"].First(); @@ -66,11 +66,11 @@ namespace BirdsiteLive.Controllers { Name = user.Name, Description = user.Description, - Acct = user.Acct, + Acct = user.Acct.ToLowerInvariant(), Url = user.Url, ProfileImageUrl = user.ProfileImageUrl, - InstanceHandle = $"@{user.Acct}@{_instanceSettings.Domain}" + InstanceHandle = $"@{user.Acct.ToLowerInvariant()}@{_instanceSettings.Domain}" }; return View(displayableUser); } diff --git a/src/BirdsiteLive/Controllers/WellKnownController.cs b/src/BirdsiteLive/Controllers/WellKnownController.cs index 5be47be..543eb06 100644 --- a/src/BirdsiteLive/Controllers/WellKnownController.cs +++ b/src/BirdsiteLive/Controllers/WellKnownController.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using BirdsiteLive.ActivityPub.Converters; using BirdsiteLive.Common.Settings; using BirdsiteLive.DAL.Contracts; using BirdsiteLive.Models; @@ -156,6 +157,8 @@ namespace BirdsiteLive.Controllers return BadRequest(); } + name = name.ToLowerInvariant(); + if (!string.IsNullOrWhiteSpace(domain) && domain != _settings.Domain) return NotFound(); @@ -163,13 +166,16 @@ namespace BirdsiteLive.Controllers if (user == null) return NotFound(); + var actorUrl = UrlFactory.GetActorUrl(_settings.Domain, name); + var result = new WebFingerResult() { subject = $"acct:{name}@{_settings.Domain}", aliases = new[] { - $"https://{_settings.Domain}/@{name}", - $"https://{_settings.Domain}/users/{name}" + //$"https://{_settings.Domain}/@{name}", + //$"https://{_settings.Domain}/users/{name}" + actorUrl }, links = new List { @@ -177,13 +183,15 @@ namespace BirdsiteLive.Controllers { rel = "http://webfinger.net/rel/profile-page", type = "text/html", - href = $"https://{_settings.Domain}/@{name}" + //href = $"https://{_settings.Domain}/@{name}" + href = actorUrl }, new WebFingerLink() { rel = "self", type = "application/activity+json", - href = $"https://{_settings.Domain}/users/{name}" + //href = $"https://{_settings.Domain}/users/{name}" + href = actorUrl } } }; From 6aef12e8258de195cc13800a1c2b1e5bf28c5c04 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sat, 9 Jan 2021 22:52:52 -0500 Subject: [PATCH 2/3] clean up --- src/BirdsiteLive.Domain/ActivityPubService.cs | 3 --- src/BirdsiteLive.Domain/StatusService.cs | 10 ++-------- src/BirdsiteLive/Controllers/WellKnownController.cs | 5 +---- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/BirdsiteLive.Domain/ActivityPubService.cs b/src/BirdsiteLive.Domain/ActivityPubService.cs index 16d9610..3fc17e6 100644 --- a/src/BirdsiteLive.Domain/ActivityPubService.cs +++ b/src/BirdsiteLive.Domain/ActivityPubService.cs @@ -48,10 +48,7 @@ namespace BirdsiteLive.Domain public async Task PostNewNoteActivity(Note note, string username, string noteId, string targetHost, string targetInbox) { - //var actor = $"https://{_instanceSettings.Domain}/users/{username}"; var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username); - - //var noteUri = $"https://{_instanceSettings.Domain}/users/{username}/statuses/{noteId}"; var noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, noteId); var now = DateTime.UtcNow; diff --git a/src/BirdsiteLive.Domain/StatusService.cs b/src/BirdsiteLive.Domain/StatusService.cs index d86512a..ab48e92 100644 --- a/src/BirdsiteLive.Domain/StatusService.cs +++ b/src/BirdsiteLive.Domain/StatusService.cs @@ -34,12 +34,8 @@ namespace BirdsiteLive.Domain public Note GetStatus(string username, ExtractedTweet tweet) { - //var actorUrl = $"https://{_instanceSettings.Domain}/users/{username}"; var actorUrl = UrlFactory.GetActorUrl(_instanceSettings.Domain, username); - //var noteId = $"https://{_instanceSettings.Domain}/users/{username}/statuses/{tweet.Id}"; - var noteId = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, tweet.Id.ToString()); - //var noteUrl = $"https://{_instanceSettings.Domain}/@{username}/{tweet.Id}"; - var noteUrl = noteId; + var noteUrl = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, tweet.Id.ToString()); var to = $"{actorUrl}/followers"; var apPublic = "https://www.w3.org/ns/activitystreams#Public"; @@ -52,8 +48,7 @@ namespace BirdsiteLive.Domain var note = new Note { - //id = $"{noteId}/activity", - id = $"{noteId}", + id = noteUrl, published = tweet.CreatedAt.ToString("s") + "Z", url = noteUrl, @@ -72,7 +67,6 @@ namespace BirdsiteLive.Domain attachment = Convert(tweet.Media), tag = extractedTags.tags }; - return note; } diff --git a/src/BirdsiteLive/Controllers/WellKnownController.cs b/src/BirdsiteLive/Controllers/WellKnownController.cs index 543eb06..255981d 100644 --- a/src/BirdsiteLive/Controllers/WellKnownController.cs +++ b/src/BirdsiteLive/Controllers/WellKnownController.cs @@ -157,6 +157,7 @@ namespace BirdsiteLive.Controllers return BadRequest(); } + // Ensure lowercase name = name.ToLowerInvariant(); if (!string.IsNullOrWhiteSpace(domain) && domain != _settings.Domain) @@ -173,8 +174,6 @@ namespace BirdsiteLive.Controllers subject = $"acct:{name}@{_settings.Domain}", aliases = new[] { - //$"https://{_settings.Domain}/@{name}", - //$"https://{_settings.Domain}/users/{name}" actorUrl }, links = new List @@ -183,14 +182,12 @@ namespace BirdsiteLive.Controllers { rel = "http://webfinger.net/rel/profile-page", type = "text/html", - //href = $"https://{_settings.Domain}/@{name}" href = actorUrl }, new WebFingerLink() { rel = "self", type = "application/activity+json", - //href = $"https://{_settings.Domain}/users/{name}" href = actorUrl } } From 4b9f7346877e9daaf0a4f1803083efa07054393d Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sat, 9 Jan 2021 23:57:45 -0500 Subject: [PATCH 3/3] road to 0.3.3 --- src/BirdsiteLive/BirdsiteLive.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BirdsiteLive/BirdsiteLive.csproj b/src/BirdsiteLive/BirdsiteLive.csproj index 283c3c8..b20520f 100644 --- a/src/BirdsiteLive/BirdsiteLive.csproj +++ b/src/BirdsiteLive/BirdsiteLive.csproj @@ -4,7 +4,7 @@ netcoreapp3.1 d21486de-a812-47eb-a419-05682bb68856 Linux - 0.3.2 + 0.3.3