Merge pull request #98 from NicolasConstant/fix_tweet-parsing
Fix tweet parsing
This commit is contained in:
commit
07d09ebdb3
3 changed files with 53 additions and 17 deletions
|
@ -81,12 +81,16 @@ namespace BirdsiteLive.Domain.Tools
|
|||
}
|
||||
|
||||
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]}");
|
||||
|
@ -96,7 +100,7 @@ namespace BirdsiteLive.Domain.Tools
|
|||
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();
|
||||
|
||||
|
@ -109,13 +113,16 @@ namespace BirdsiteLive.Domain.Tools
|
|||
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]}");
|
||||
}
|
||||
|
@ -123,13 +130,17 @@ namespace BirdsiteLive.Domain.Tools
|
|||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -364,7 +364,32 @@ namespace BirdsiteLive.Domain.Tests.Tools
|
|||
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()
|
||||
{
|
||||
|
|
Reference in a new issue