Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazot provider improvements. #201

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/TagzApp.Providers.Blazot/BlazotProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using TagzApp.Providers.Blazot.Interfaces;
using TagzApp.Providers.Blazot.Models;
using TagzApp.Providers.Blazot.Configuration;
using TagzApp.Common.Models;

namespace TagzApp.Providers.Blazot;

Expand Down
4 changes: 2 additions & 2 deletions src/TagzApp.Providers.Blazot/Converters/ContentConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public IEnumerable<Content> ConvertToContent(List<Transmission>? transmissions,
.Combine(BlazotConstants.BaseAppAddress, "transmission", transmission.TransmissionId.ToString())
.Replace(@"\", "/")),
Text = body,
Timestamp = new DateTimeOffset(transmission.DateTransmitted, TimeSpan.Zero).ToLocalTime(),
Timestamp = new DateTimeOffset(transmission.DateTransmitted, TimeSpan.Zero),
HashtagSought = tag?.Text ?? string.Empty,
Type = ContentType.Message
};
Expand Down Expand Up @@ -118,4 +118,4 @@ public IEnumerable<Content> ConvertToContent(List<Transmission>? transmissions,

return null;
}
}
}
26 changes: 18 additions & 8 deletions src/TagzApp.Providers.Blazot/Formatters/LinkFormatters.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
using System.Text.RegularExpressions;
using System.Web;

namespace TagzApp.Providers.Blazot.Formatters;

internal static class LinkFormatters
{
private static Regex LinkRegex => new(@"(?:(?:https?):\/\/)?[\w/\.-]+(?<!\.)(\.)(?!\.)[a-zA-Z]+(?<!\?)[a-zA-Z0-9/\-&?.=%#_]+(?<!\.)");
private static Regex HashTagRegex => new(@"(^|[^&\p{L}\p{M}\p{Nd}_\u200c\u200d\ua67e\u05be\u05f3\u05f4\u309b\u309c\u30a0\u30fb\u3003\u0f0b\u0f0c\u00b7])(#|\uFF03)(?!\uFE0F|\u20E3)([\p{L}\p{M}\p{Nd}_\u200c\u200d\ua67e\u05be\u05f3\u05f4\u309b\u309c\u30a0\u30fb\u3003\u0f0b\u0f0c\u00b7]*[\p{L}\p{M}][\p{L}\p{M}\p{Nd}_\u200c\u200d\ua67e\u05be\u05f3\u05f4\u309b\u309c\u30a0\u30fb\u3003\u0f0b\u0f0c\u00b7]*)");
private static Regex UserMentionRegex => new(@"\B@\w+");
private static Regex HashTagRegex => new(@"\B#\w\w+");
private static Regex UserMentionRegex => new(@"\B@\w+");

// This must be performed first, since it involves HTML Decoding and Encoding.
public static string AddHashTagLinks(string bodyText)
{
return HashTagRegex.Replace(bodyText, delegate(Match m)
{
var noHash = m.Value.Trim().TrimStart('#');
var hashed = m.Value.Trim();
return $" <a class=\"b-hashtag-link\" href=\"https://blazot.com/hashtag/{noHash}\">{hashed}</a>";
});
bodyText = HttpUtility.HtmlDecode(bodyText);
var matches = HashTagRegex.Matches(bodyText);
bodyText = HttpUtility.HtmlEncode(bodyText);

foreach (var match in matches)
{
if (match == null) continue;

var m = match.ToString()!.Trim();
var noHash = m.TrimStart('#');
bodyText = bodyText.Replace(m, $"<a class=\"b-hashtag-link\" target=\"_blank\" href=\"https://blazot.com/hashtag/{noHash}\">{m}</a>");
}

return bodyText;
}

public static string AddWebLinks(string bodyText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="System.Net.Http.Json" Version="7.0.1" />
<ProjectReference Include="..\TagzApp.Common\TagzApp.Common.csproj" />
<ProjectReference Include="..\TagzApp.Communication\TagzApp.Communication.csproj" />
<InternalsVisibleTo Include="TagzApp.UnitTest" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions src/TagzApp.UnitTest/Blazot/Formatters/LinkFormattersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using TagzApp.UnitTest.Blazot.TestData;

namespace TagzApp.UnitTest.Blazot.Formatters;

public class LinkFormattersTests
{
[Theory]
[ClassData(typeof(HashTagLinkData))]
private void AddHashTagLinks_FormatsProperly_Test(string encodedText, string expectedText)
{
// Act
var response = TagzApp.Providers.Blazot.Formatters.LinkFormatters.AddHashTagLinks(encodedText);

// Assert
Assert.Equal(expectedText, response);
}
}
30 changes: 30 additions & 0 deletions src/TagzApp.UnitTest/Blazot/TestData/HashTagLinkData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections;
using System.Web;

namespace TagzApp.UnitTest.Blazot.TestData;

public class HashTagLinkData : IEnumerable<object[]>
{
private static readonly string _SimplePunctuationString = "I'm a test message";
private static readonly string _HashTagTouching = "I'm a test message <br/>#dotnet";
private static readonly string _TypicalString = "I'm a test message #dotnet";
private static readonly string _EmoticonString = "I'm a test message 😊 #dotnet";

private readonly List<object[]> _BodyText = new()
{
new object[] {HttpUtility.HtmlEncode(_SimplePunctuationString), "I&#39;m a test message"},
new object[] {HttpUtility.HtmlEncode(_HashTagTouching), "I&#39;m a test message &lt;br/&gt;<a class=\"b-hashtag-link\" target=\"_blank\" href=\"https://blazot.com/hashtag/dotnet\">#dotnet</a>" },
new object[] {HttpUtility.HtmlEncode(_TypicalString), "I&#39;m a test message <a class=\"b-hashtag-link\" target=\"_blank\" href=\"https://blazot.com/hashtag/dotnet\">#dotnet</a>" },
new object[] {HttpUtility.HtmlEncode(_EmoticonString), "I&#39;m a test message &#128522; <a class=\"b-hashtag-link\" target=\"_blank\" href=\"https://blazot.com/hashtag/dotnet\">#dotnet</a>" }
};

public IEnumerator<object[]> GetEnumerator()
{
return _BodyText.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
1 change: 1 addition & 0 deletions src/TagzApp.UnitTest/TagzApp.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

<ItemGroup>
<ProjectReference Include="..\TagzApp.Common\TagzApp.Common.csproj" />
<ProjectReference Include="..\TagzApp.Providers.Blazot\TagzApp.Providers.Blazot.csproj" />
<ProjectReference Include="..\TagzApp.Providers.Mastodon\TagzApp.Providers.Mastodon.csproj" />
<ProjectReference Include="..\TagzApp.Providers.Youtube\TagzApp.Providers.Youtube.csproj" />
</ItemGroup>
Expand Down