Skip to content

Commit

Permalink
Merge pull request #98 from NicolasConstant/fix_tweet-parsing
Browse files Browse the repository at this point in the history
Fix tweet parsing
  • Loading branch information
NicolasConstant authored Mar 30, 2021
2 parents a4d62fa + c1f4aed commit 07d09eb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
41 changes: 26 additions & 15 deletions src/BirdsiteLive.Domain/Tools/StatusExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ public StatusExtractor(InstanceSettings instanceSettings, ILogger<StatusExtracto
}

var url = $"https://{_instanceSettings.Domain}/tags/{tag}";
tags.Add(new Tag

if (tags.All(x => x.href != url))
{
name = $"#{tag}",
href = url,
type = "Hashtag"
});
tags.Add(new Tag
{
name = $"#{tag}",
href = url,
type = "Hashtag"
});
}

messageContent = Regex.Replace(messageContent, Regex.Escape(m.Groups[0].ToString()),
$@"{m.Groups[1]}<a href=""{url}"" class=""mention hashtag"" rel=""tag"">#<span>{tag}</span></a>{m.Groups[3]}");
Expand All @@ -96,7 +100,7 @@ public StatusExtractor(InstanceSettings instanceSettings, ILogger<StatusExtracto
if (extractMentions)
{
var mentionMatch = OrderByLength(UserRegexes.Mention.Matches(messageContent));
foreach (Match m in mentionMatch.OrderByDescending(x => x.Length))
foreach (Match m in mentionMatch)
{
var mention = m.Groups[2].ToString();

Expand All @@ -109,27 +113,34 @@ public StatusExtractor(InstanceSettings instanceSettings, ILogger<StatusExtracto
var url = $"https://{_instanceSettings.Domain}/users/{mention}";
var name = $"@{mention}@{_instanceSettings.Domain}";

tags.Add(new Tag
if (tags.All(x => x.href != url))
{
name = name,
href = url,
type = "Mention"
});

tags.Add(new Tag
{
name = name,
href = url,
type = "Mention"
});
}

messageContent = Regex.Replace(messageContent, Regex.Escape(m.Groups[0].ToString()),
$@"{m.Groups[1]}<span class=""h-card""><a href=""https://{_instanceSettings.Domain}/@{mention}"" class=""u-url mention"">@<span>{mention}</span></a></span>{m.Groups[3]}");
}
}

return (messageContent.Trim(), tags.ToArray());
}

private IEnumerable<Match> OrderByLength(MatchCollection matches)
{
var result = new List<Match>();

foreach (Match m in matches) result.Add(m);
result = result.OrderByDescending(x => x.Length).ToList();

result = result
.OrderBy(x => x.Length)
.GroupBy(p => p.Value)
.Select(g => g.First())
.ToList();

return result;
}
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.16.0</Version>
<Version>0.16.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,32 @@ public void Extract_SingleMentionTag_Test()
Assert.IsTrue(result.content.Contains(@"<span class=""h-card""><a href=""https://domain.name/@mynickname"" class=""u-url mention"">@<span>mynickname</span></a></span>"));
#endregion
}


[TestMethod]
public void Extract_MultiMentionTag_MultiOccurrence_Test()
{
#region Stubs
var message = $"[RT @yamenbousrih]{Environment.NewLine}@KiwixOffline @photos_floues Bla. Cc @Pyb75 @photos_floues @KiwixOffline";
#endregion

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

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

#region Validations
logger.VerifyAll();
Assert.AreEqual(4, result.tags.Length);
Assert.AreEqual("Mention", result.tags.First().type);

Assert.IsTrue(result.content.Contains(@"<span class=""h-card""><a href=""https://domain.name/@photos_floues"" class=""u-url mention"">@<span>photos_floues</span></a></span>"));
Assert.IsTrue(result.content.Contains(@"<span class=""h-card""><a href=""https://domain.name/@KiwixOffline"" class=""u-url mention"">@<span>KiwixOffline</span></a></span> <span class=""h-card""><a href=""https://domain.name/@photos_floues"" class=""u-url mention"">@<span>photos_floues</span></a></span>"));
Assert.IsTrue(result.content.Contains(@"Cc <span class=""h-card""><a href=""https://domain.name/@Pyb75"" class=""u-url mention"">@<span>Pyb75</span></a></span> <span class=""h-card""><a href=""https://domain.name/@photos_floues"" class=""u-url mention"">@<span>photos_floues</span></a></span> <span class=""h-card""><a href=""https://domain.name/@KiwixOffline"" class=""u-url mention"">@<span>KiwixOffline</span></a></span>"));
#endregion
}

[TestMethod]
public void Extract_SingleMentionTag_RT_Test()
{
Expand Down

0 comments on commit 07d09eb

Please sign in to comment.