extract mentions
This commit is contained in:
parent
fdfe4c703c
commit
6073ee1ba4
2 changed files with 74 additions and 11 deletions
|
@ -12,6 +12,8 @@ namespace BirdsiteLive.Domain.Tools
|
|||
|
||||
public class StatusExtractor : IStatusExtractor
|
||||
{
|
||||
private readonly Regex _hastagRegex = new Regex(@"\W(\#[a-zA-Z0-9]+\b)(?!;)");
|
||||
private readonly Regex _mentionRegex = new Regex(@"\W(\@[a-zA-Z0-9]+\b)(?!;)");
|
||||
private readonly InstanceSettings _instanceSettings;
|
||||
|
||||
#region Ctor
|
||||
|
@ -23,11 +25,10 @@ namespace BirdsiteLive.Domain.Tools
|
|||
|
||||
public (string content, Tag[] tags) ExtractTags(string messageContent)
|
||||
{
|
||||
var regex = new Regex(@"\W(\#[a-zA-Z0-9]+\b)(?!;)");
|
||||
var match = regex.Matches(messageContent);
|
||||
|
||||
var tags = new List<Tag>();
|
||||
foreach (var m in match)
|
||||
|
||||
var hashtagMatch = _hastagRegex.Matches(messageContent);
|
||||
foreach (var m in hashtagMatch)
|
||||
{
|
||||
var tag = m.ToString().Replace("#", string.Empty).Replace("\n", string.Empty).Trim();
|
||||
var url = $"https://{_instanceSettings.Domain}/tags/{tag}";
|
||||
|
@ -41,13 +42,27 @@ namespace BirdsiteLive.Domain.Tools
|
|||
|
||||
messageContent = Regex.Replace(messageContent, m.ToString(),
|
||||
$@"<a href=""{url}"" class=""mention hashtag"" rel=""tag"">#<span>{tag}</span></a>");
|
||||
|
||||
//messageContent = messageContent.Replace(
|
||||
// $"#{tag}",
|
||||
// $@"<a href=""{url}"" class=""mention hashtag"" rel=""tag"">#<span>{tag}</span></a>");
|
||||
}
|
||||
|
||||
return (messageContent, new Tag[0]);
|
||||
var mentionMatch = _mentionRegex.Matches(messageContent);
|
||||
foreach (var m in mentionMatch)
|
||||
{
|
||||
var mention = m.ToString().Replace("@", string.Empty).Replace("\n", string.Empty).Trim();
|
||||
var url = $"https://{_instanceSettings.Domain}/users/{mention}";
|
||||
var name = $"@{mention}@{_instanceSettings.Domain}";
|
||||
|
||||
tags.Add(new Tag
|
||||
{
|
||||
name = name,
|
||||
href = url,
|
||||
type = "Mention"
|
||||
});
|
||||
|
||||
messageContent = Regex.Replace(messageContent, m.ToString(),
|
||||
$@"<span class=""h-card""><a href=""https://{_instanceSettings.Domain}/@{mention}"" class=""u-url mention"">@<span>{mention}</span></a></span>");
|
||||
}
|
||||
|
||||
return (messageContent, tags.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using BirdsiteLive.Common.Settings;
|
||||
using BirdsiteLive.Domain.Tools;
|
||||
using BirdsiteLive.Twitter.Models;
|
||||
|
@ -22,7 +23,7 @@ namespace BirdsiteLive.Domain.Tests.Tools
|
|||
#endregion
|
||||
|
||||
[TestMethod]
|
||||
public void Extract_SingleTag_Test()
|
||||
public void Extract_SingleHashTag_Test()
|
||||
{
|
||||
#region Stubs
|
||||
var message = $"Bla!{Environment.NewLine}#mytag";
|
||||
|
@ -32,13 +33,18 @@ namespace BirdsiteLive.Domain.Tests.Tools
|
|||
var result = service.ExtractTags(message);
|
||||
|
||||
#region Validations
|
||||
Assert.AreEqual(1, result.tags.Length);
|
||||
Assert.AreEqual("#mytag", result.tags.First().name);
|
||||
Assert.AreEqual("Hashtag", result.tags.First().type);
|
||||
Assert.AreEqual("https://domain.name/tags/mytag", result.tags.First().href);
|
||||
|
||||
Assert.IsTrue(result.content.Contains("Bla!"));
|
||||
Assert.IsTrue(result.content.Contains(@"<a href=""https://domain.name/tags/mytag"" class=""mention hashtag"" rel=""tag"">#<span>mytag</span></a>"));
|
||||
#endregion
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Extract_MultiTags_Test()
|
||||
public void Extract_MultiHashTags_Test()
|
||||
{
|
||||
#region Stubs
|
||||
var message = $"Bla!{Environment.NewLine}#mytag #mytag2 #mytag3{Environment.NewLine}Test #bal Test";
|
||||
|
@ -48,6 +54,7 @@ namespace BirdsiteLive.Domain.Tests.Tools
|
|||
var result = service.ExtractTags(message);
|
||||
|
||||
#region Validations
|
||||
Assert.AreEqual(4, result.tags.Length);
|
||||
Assert.IsTrue(result.content.Contains("Bla!"));
|
||||
Assert.IsTrue(result.content.Contains(@"<a href=""https://domain.name/tags/mytag"" class=""mention hashtag"" rel=""tag"">#<span>mytag</span></a>"));
|
||||
Assert.IsTrue(result.content.Contains(@"<a href=""https://domain.name/tags/mytag2"" class=""mention hashtag"" rel=""tag"">#<span>mytag2</span></a>"));
|
||||
|
@ -55,5 +62,46 @@ namespace BirdsiteLive.Domain.Tests.Tools
|
|||
Assert.IsTrue(result.content.Contains(@"<a href=""https://domain.name/tags/bal"" class=""mention hashtag"" rel=""tag"">#<span>bal</span></a>"));
|
||||
#endregion
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Extract_SingleMentionTag_Test()
|
||||
{
|
||||
#region Stubs
|
||||
var message = $"Bla!{Environment.NewLine}@mynickname";
|
||||
#endregion
|
||||
|
||||
var service = new StatusExtractor(_settings);
|
||||
var result = service.ExtractTags(message);
|
||||
|
||||
#region Validations
|
||||
Assert.AreEqual(1, result.tags.Length);
|
||||
Assert.AreEqual("@mynickname@domain.name", result.tags.First().name);
|
||||
Assert.AreEqual("Mention", result.tags.First().type);
|
||||
Assert.AreEqual("https://domain.name/users/mynickname", result.tags.First().href);
|
||||
|
||||
Assert.IsTrue(result.content.Contains("Bla!"));
|
||||
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_Test()
|
||||
{
|
||||
#region Stubs
|
||||
var message = $"Bla!{Environment.NewLine}@mynickname @mynickname2 @mynickname3{Environment.NewLine}Test @dada Test";
|
||||
#endregion
|
||||
|
||||
var service = new StatusExtractor(_settings);
|
||||
var result = service.ExtractTags(message);
|
||||
|
||||
#region Validations
|
||||
Assert.AreEqual(4, result.tags.Length);
|
||||
Assert.IsTrue(result.content.Contains("Bla!"));
|
||||
Assert.IsTrue(result.content.Contains(@"<span class=""h-card""><a href=""https://domain.name/@mynickname"" class=""u-url mention"">@<span>mynickname</span></a></span>"));
|
||||
Assert.IsTrue(result.content.Contains(@"<span class=""h-card""><a href=""https://domain.name/@mynickname2"" class=""u-url mention"">@<span>mynickname2</span></a></span>"));
|
||||
Assert.IsTrue(result.content.Contains(@"<span class=""h-card""><a href=""https://domain.name/@mynickname3"" class=""u-url mention"">@<span>mynickname3</span></a></span>"));
|
||||
Assert.IsTrue(result.content.Contains(@"<span class=""h-card""><a href=""https://domain.name/@dada"" class=""u-url mention"">@<span>dada</span></a></span>"));
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue