Skip to content

Commit

Permalink
Merge pull request #86 from NicolasConstant/develop
Browse files Browse the repository at this point in the history
0.14.1 PR
  • Loading branch information
NicolasConstant authored Feb 10, 2021
2 parents fcc7bba + 567f02a commit e73d76b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/BirdsiteLive.Common/Regexes/UrlRegexes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace BirdsiteLive.Common.Regexes
{
public class UrlRegexes
{
public static readonly Regex Url = new Regex(@"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)");
public static readonly Regex Url = new Regex(@"(.?)(((http|ftp|https):\/\/)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)");
}
}
9 changes: 3 additions & 6 deletions src/BirdsiteLive.Domain/Tools/StatusExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ public StatusExtractor(InstanceSettings instanceSettings, ILogger<StatusExtracto
var urlMatch = UrlRegexes.Url.Matches(messageContent);
foreach (Match m in urlMatch)
{
var url = m.ToString().Replace("\n", string.Empty).Trim();

var protocol = "https://";
if (url.StartsWith("http://")) protocol = "http://";
else if (url.StartsWith("ftp://")) protocol = "ftp://";
var url = m.Groups[2].ToString();
var protocol = m.Groups[3].ToString();

var truncatedUrl = url.Replace(protocol, string.Empty);

Expand All @@ -68,7 +65,7 @@ public StatusExtractor(InstanceSettings instanceSettings, ILogger<StatusExtracto
}

messageContent = Regex.Replace(messageContent, m.ToString(),
$@" <a href=""{url}"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">{protocol}</span><span class=""ellipsis"">{firstPart}</span><span class=""invisible"">{secondPart}</span></a>");
$@"{m.Groups[1]}<a href=""{url}"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">{protocol}</span><span class=""ellipsis"">{firstPart}</span><span class=""invisible"">{secondPart}</span></a>");
}

// Extract Hashtags
Expand Down
2 changes: 1 addition & 1 deletion src/BirdsiteLive/BirdsiteLive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>0.14.0</Version>
<Version>0.14.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void Extract_FormatUrl_Exact_Test()
}

[TestMethod]
public void Extract_MultiUrls__Test()
public void Extract_MultiUrls_Test()
{
#region Stubs
var message = $"https://t.co/L8BpyHgg25 Bla!{Environment.NewLine}https://www.eff.org/deeplinks/2020/07/pact-act-not-solution-problem-harmful-online-content";
Expand All @@ -160,6 +160,63 @@ public void Extract_MultiUrls__Test()
#endregion
}

[TestMethod]
public void Extract_SmallUrl_Test()
{
#region Stubs
var message = @"πŸš€ test http://GOV.UK date πŸŽ‰ data http://GOV.UK woopsi.";
#endregion

#region Mocks
var logger = new Mock<ILogger<StatusExtractor>>();
#endregion

var service = new StatusExtractor(_settings, logger.Object);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(@"πŸš€ test <a href=""http://GOV.UK"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">http://</span><span class=""ellipsis"">GOV.UK</span><span class=""invisible""></span></a> date πŸŽ‰ data <a href=""http://GOV.UK"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">http://</span><span class=""ellipsis"">GOV.UK</span><span class=""invisible""></span></a> woopsi.", result.content);
#endregion
}

[TestMethod]
public void Extract_SmallUrl_2_Test()
{
#region Stubs
var message = @"πŸš€http://GOV.UK";
#endregion

#region Mocks
var logger = new Mock<ILogger<StatusExtractor>>();
#endregion

var service = new StatusExtractor(_settings, logger.Object);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(@"πŸš€<a href=""http://GOV.UK"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">http://</span><span class=""ellipsis"">GOV.UK</span><span class=""invisible""></span></a>", result.content);
#endregion
}

[TestMethod]
public void Extract_SmallUrl_3_Test()
{
#region Stubs
var message = @"πŸš€http://GOV.UK.";
#endregion

#region Mocks
var logger = new Mock<ILogger<StatusExtractor>>();
#endregion

var service = new StatusExtractor(_settings, logger.Object);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(@"πŸš€<a href=""http://GOV.UK"" rel=""nofollow noopener noreferrer"" target=""_blank""><span class=""invisible"">http://</span><span class=""ellipsis"">GOV.UK</span><span class=""invisible""></span></a>.", result.content);
#endregion
}

[TestMethod]
public void Extract_SingleHashTag_Test()
{
Expand Down

0 comments on commit e73d76b

Please sign in to comment.