added option to prevent mention extraction in user's description

This commit is contained in:
Nicolas Constant 2021-01-15 01:42:05 -05:00
parent f0a0bb9f43
commit a59ed49a9a
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
6 changed files with 40 additions and 35 deletions

View file

@ -4,5 +4,6 @@
{
public string Domain { get; set; }
public string AdminEmail { get; set; }
public bool ResolveMentionsInProfiles { get; set; }
}
}

View file

@ -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;

View file

@ -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<Tag>();
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(),
$@" <span class=""h-card""><a href=""https://{_instanceSettings.Domain}/@{mention}"" class=""u-url mention"">@<span>{mention}</span></a></span>");
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>");
}
}
// Clean up return lines

View file

@ -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"));

View file

@ -11,7 +11,8 @@
"AllowedHosts": "*",
"Instance": {
"Domain": "domain.name",
"AdminEmail": "me@domain.name"
"AdminEmail": "me@domain.name",
"ResolveMentionsInProfiles": true
},
"Db": {
"Type": "postgres",

View file

@ -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);