diff --git a/src/BirdsiteLive.Common/Settings/InstanceSettings.cs b/src/BirdsiteLive.Common/Settings/InstanceSettings.cs index ba2a517..2ce9715 100644 --- a/src/BirdsiteLive.Common/Settings/InstanceSettings.cs +++ b/src/BirdsiteLive.Common/Settings/InstanceSettings.cs @@ -4,5 +4,6 @@ { public string Domain { get; set; } public string AdminEmail { get; set; } + public bool ResolveMentionsInProfiles { get; set; } } } \ No newline at end of file diff --git a/src/BirdsiteLive.Domain/StatusService.cs b/src/BirdsiteLive.Domain/StatusService.cs index a1b3d38..c669779 100644 --- a/src/BirdsiteLive.Domain/StatusService.cs +++ b/src/BirdsiteLive.Domain/StatusService.cs @@ -43,7 +43,7 @@ namespace BirdsiteLive.Domain var to = $"{actorUrl}/followers"; var apPublic = "https://www.w3.org/ns/activitystreams#Public"; - var extractedTags = _statusExtractor.ExtractTags(tweet.MessageContent); + var extractedTags = _statusExtractor.Extract(tweet.MessageContent); _statisticsHandler.ExtractedStatus(extractedTags.tags.Count(x => x.type == "Mention")); string inReplyTo = null; diff --git a/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs b/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs index ed401a6..778f6b5 100644 --- a/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs +++ b/src/BirdsiteLive.Domain/Tools/StatusExtractor.cs @@ -8,7 +8,7 @@ namespace BirdsiteLive.Domain.Tools { public interface IStatusExtractor { - (string content, Tag[] tags) ExtractTags(string messageContent); + (string content, Tag[] tags) Extract(string messageContent, bool extractMentions = true); } public class StatusExtractor : IStatusExtractor @@ -36,7 +36,7 @@ namespace BirdsiteLive.Domain.Tools } #endregion - public (string content, Tag[] tags) ExtractTags(string messageContent) + public (string content, Tag[] tags) Extract(string messageContent, bool extractMentions = true) { var tags = new List(); messageContent = $" {messageContent} "; @@ -102,22 +102,25 @@ namespace BirdsiteLive.Domain.Tools } // Extract Mentions - var mentionMatch = OrderByLength(_mentionRegex.Matches(messageContent)); - foreach (Match m in mentionMatch.OrderByDescending(x => x.Length)) + if (extractMentions) { - 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 + var mentionMatch = OrderByLength(_mentionRegex.Matches(messageContent)); + foreach (Match m in mentionMatch.OrderByDescending(x => x.Length)) { - name = name, - href = url, - type = "Mention" - }); + var mention = m.ToString().Replace("@", string.Empty).Replace("\n", string.Empty).Trim(); + var url = $"https://{_instanceSettings.Domain}/users/{mention}"; + var name = $"@{mention}@{_instanceSettings.Domain}"; - messageContent = Regex.Replace(messageContent, m.ToString(), - $@" @{mention}"); + tags.Add(new Tag + { + name = name, + href = url, + type = "Mention" + }); + + messageContent = Regex.Replace(messageContent, m.ToString(), + $@" @{mention}"); + } } // Clean up return lines diff --git a/src/BirdsiteLive.Domain/UserService.cs b/src/BirdsiteLive.Domain/UserService.cs index 6e498af..6131ca9 100644 --- a/src/BirdsiteLive.Domain/UserService.cs +++ b/src/BirdsiteLive.Domain/UserService.cs @@ -59,7 +59,7 @@ namespace BirdsiteLive.Domain var description = twitterUser.Description; if (!string.IsNullOrWhiteSpace(description)) { - var extracted = _statusExtractor.ExtractTags(description); + var extracted = _statusExtractor.Extract(description, _instanceSettings.ResolveMentionsInProfiles); description = extracted.content; _statisticsHandler.ExtractedDescription(extracted.tags.Count(x => x.type == "Mention")); diff --git a/src/BirdsiteLive/appsettings.json b/src/BirdsiteLive/appsettings.json index f240094..d0c49bf 100644 --- a/src/BirdsiteLive/appsettings.json +++ b/src/BirdsiteLive/appsettings.json @@ -11,7 +11,8 @@ "AllowedHosts": "*", "Instance": { "Domain": "domain.name", - "AdminEmail": "me@domain.name" + "AdminEmail": "me@domain.name", + "ResolveMentionsInProfiles": true }, "Db": { "Type": "postgres", diff --git a/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs b/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs index 32d1faa..f5dc91a 100644 --- a/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs +++ b/src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs @@ -30,7 +30,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.IsTrue(result.content.Contains("Bla.")); @@ -46,7 +46,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.IsTrue(result.content.Contains("Bla.")); @@ -62,7 +62,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(0, result.tags.Length); @@ -80,7 +80,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(0, result.tags.Length); @@ -98,7 +98,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(0, result.tags.Length); @@ -116,7 +116,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(0, result.tags.Length); @@ -136,7 +136,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(1, result.tags.Length); @@ -157,7 +157,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(1, result.tags.Length); @@ -178,7 +178,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(1, result.tags.Length); @@ -199,7 +199,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(4, result.tags.Length); @@ -219,7 +219,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(1, result.tags.Length); @@ -240,7 +240,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(1, result.tags.Length); @@ -261,7 +261,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(1, result.tags.Length); @@ -282,7 +282,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(4, result.tags.Length); @@ -302,7 +302,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(5, result.tags.Length); @@ -325,7 +325,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(1, result.tags.Length); @@ -345,7 +345,7 @@ namespace BirdsiteLive.Domain.Tests.Tools #endregion var service = new StatusExtractor(_settings); - var result = service.ExtractTags(message); + var result = service.Extract(message); #region Validations Assert.AreEqual(1, result.tags.Length);