diff --git a/src/BirdsiteLive.Common/Regexes/UserRegexes.cs b/src/BirdsiteLive.Common/Regexes/UserRegexes.cs index 74c8b2e..a9f1053 100644 --- a/src/BirdsiteLive.Common/Regexes/UserRegexes.cs +++ b/src/BirdsiteLive.Common/Regexes/UserRegexes.cs @@ -5,6 +5,6 @@ namespace BirdsiteLive.Common.Regexes public class UserRegexes { public static readonly Regex TwitterAccount = new Regex(@"^[a-zA-Z0-9_]+$"); - public static readonly Regex Mention = new Regex(@"(.)(@[a-zA-Z0-9_]+)(\s|$|[,;:!?/|-]|(. ))"); + public static readonly Regex Mention = new Regex(@"(.?)@([a-zA-Z0-9_]+)(\s|$|[<,;:!?/|-]|(. ))"); } } \ No newline at end of file diff --git a/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs b/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs index 9429096..0d4c1f3 100644 --- a/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs +++ b/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs @@ -21,7 +21,7 @@ namespace BirdsiteLive.Domain.Tools //private readonly Regex _hastagRegex = new Regex(@"(?<=[\s>]|^)#(\w*[a-zA-Z0-9_ー]+\w*)\b(?!;)"); //private readonly Regex _hastagRegex = new Regex(@"(?<=[\s>]|^)#(\w*[a-zA-Z0-9_ー]+)\b(?!;)"); - private readonly Regex _mentionRegex = new Regex(@"\W(\@[a-zA-Z0-9_ー]+\b)(?!;)"); + //private readonly Regex _mentionRegex = new Regex(@"\W(\@[a-zA-Z0-9_ー]+\b)(?!;)"); //private readonly Regex _mentionRegex = new Regex(@"@\w+"); //private readonly Regex _mentionRegex = new Regex(@"(?<=[\s>]|^)@(\w*[a-zA-Z0-9_ー]+\w*)\b(?!;)"); //private readonly Regex _mentionRegex = new Regex(@"(?<=[\s>]|^)@(\w*[a-zA-Z0-9_ー]+)\b(?!;)"); @@ -110,17 +110,25 @@ namespace BirdsiteLive.Domain.Tools //messageContent = Regex.Replace(messageContent, m.ToString(), // $@" #{tag}"); - messageContent = Regex.Replace(messageContent, m.Groups[0].ToString(), + messageContent = Regex.Replace(messageContent, Regex.Escape(m.Groups[0].ToString()), $@"{m.Groups[1]}#{tag}{m.Groups[3]}"); } // Extract Mentions if (extractMentions) { - var mentionMatch = OrderByLength(_mentionRegex.Matches(messageContent)); + var mentionMatch = OrderByLength(UserRegexes.Mention.Matches(messageContent)); foreach (Match m in mentionMatch.OrderByDescending(x => x.Length)) { - var mention = m.ToString().Replace("@", string.Empty).Replace("\n", string.Empty).Trim(); + var mention = m.Groups[2].ToString(); + //var mention = m.ToString().Replace("@", string.Empty).Replace("\n", string.Empty).Trim(); + + if (!UserRegexes.TwitterAccount.IsMatch(mention)) + { + _logger.LogError("Parsing Mention failed: {Mention} on {Content}", mention, messageContent); + continue; + } + var url = $"https://{_instanceSettings.Domain}/users/{mention}"; var name = $"@{mention}@{_instanceSettings.Domain}"; @@ -131,16 +139,19 @@ namespace BirdsiteLive.Domain.Tools type = "Mention" }); - messageContent = Regex.Replace(messageContent, m.ToString(), - $@" @{mention}"); + //messageContent = Regex.Replace(messageContent, m.ToString(), + // $@" @{mention}"); + + messageContent = Regex.Replace(messageContent, Regex.Escape(m.Groups[0].ToString()), + $@"{m.Groups[1]}@{mention}{m.Groups[3]}"); } } - // Clean up return lines - messageContent = Regex.Replace(messageContent, @"

", "

"); - messageContent = Regex.Replace(messageContent, @"
", "
"); - messageContent = Regex.Replace(messageContent, @" ", " "); - messageContent = Regex.Replace(messageContent, @" ", " "); + //// Clean up return lines + //messageContent = Regex.Replace(messageContent, @"

", "

"); + //messageContent = Regex.Replace(messageContent, @"
", "
"); + //messageContent = Regex.Replace(messageContent, @" ", " "); + //messageContent = Regex.Replace(messageContent, @" ", " "); return (messageContent.Trim(), tags.ToArray()); } diff --git a/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs b/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs index f126b12..4ed8ee8 100644 --- a/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs +++ b/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs @@ -267,7 +267,7 @@ namespace BirdsiteLive.Domain.Tests.Tools public void Extract_SingleMentionTag_Test() { #region Stubs - var message = $"Bla!{Environment.NewLine}@mynickname⁠"; + var message = $"Bla!{Environment.NewLine}@mynickname"; #endregion #region Mocks @@ -293,7 +293,7 @@ namespace BirdsiteLive.Domain.Tests.Tools public void Extract_SingleMentionTag_SpecialChar_Test() { #region Stubs - var message = $"Bla!{Environment.NewLine}@my___nickname⁠"; + var message = $"Bla!{Environment.NewLine}@my___nickname"; #endregion #region Mocks @@ -391,8 +391,7 @@ namespace BirdsiteLive.Domain.Tests.Tools Assert.IsTrue(result.content.Contains(@"#dada")); #endregion } - - + [TestMethod] public void Extract_Emoji_Test() { @@ -412,9 +411,9 @@ namespace BirdsiteLive.Domain.Tests.Tools logger.VerifyAll(); Assert.AreEqual(1, result.tags.Length); Assert.IsTrue(result.content.Contains( - @"😤 @mynickname")); + @"😤@mynickname")); - Assert.IsTrue(result.content.Contains(@"😎 😍 🤗 🤩 😘")); + Assert.IsTrue(result.content.Contains(@"😎😍🤗🤩😘")); #endregion } @@ -436,7 +435,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #region Validations logger.VerifyAll(); Assert.AreEqual(1, result.tags.Length); - Assert.IsTrue(result.content.Equals(@"bla ( @mynickname test)")); + Assert.IsTrue(result.content.Equals(@"bla (@mynickname test)")); #endregion } }