commit
1a578f3bbc
4 changed files with 82 additions and 35 deletions
|
@ -9,6 +9,7 @@ using BirdsiteLive.ActivityPub;
|
||||||
using BirdsiteLive.ActivityPub.Converters;
|
using BirdsiteLive.ActivityPub.Converters;
|
||||||
using BirdsiteLive.ActivityPub.Models;
|
using BirdsiteLive.ActivityPub.Models;
|
||||||
using BirdsiteLive.Common.Settings;
|
using BirdsiteLive.Common.Settings;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Org.BouncyCastle.Bcpg;
|
using Org.BouncyCastle.Bcpg;
|
||||||
|
|
||||||
|
@ -27,13 +28,15 @@ namespace BirdsiteLive.Domain
|
||||||
private readonly InstanceSettings _instanceSettings;
|
private readonly InstanceSettings _instanceSettings;
|
||||||
private readonly IHttpClientFactory _httpClientFactory;
|
private readonly IHttpClientFactory _httpClientFactory;
|
||||||
private readonly ICryptoService _cryptoService;
|
private readonly ICryptoService _cryptoService;
|
||||||
|
private readonly ILogger<ActivityPubService> _logger;
|
||||||
|
|
||||||
#region Ctor
|
#region Ctor
|
||||||
public ActivityPubService(ICryptoService cryptoService, InstanceSettings instanceSettings, IHttpClientFactory httpClientFactory)
|
public ActivityPubService(ICryptoService cryptoService, InstanceSettings instanceSettings, IHttpClientFactory httpClientFactory, ILogger<ActivityPubService> logger)
|
||||||
{
|
{
|
||||||
_cryptoService = cryptoService;
|
_cryptoService = cryptoService;
|
||||||
_instanceSettings = instanceSettings;
|
_instanceSettings = instanceSettings;
|
||||||
_httpClientFactory = httpClientFactory;
|
_httpClientFactory = httpClientFactory;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
@ -47,6 +50,8 @@ namespace BirdsiteLive.Domain
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task PostNewNoteActivity(Note note, string username, string noteId, string targetHost, string targetInbox)
|
public async Task PostNewNoteActivity(Note note, string username, string noteId, string targetHost, string targetInbox)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username);
|
var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username);
|
||||||
var noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, noteId);
|
var noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, noteId);
|
||||||
|
@ -69,6 +74,12 @@ namespace BirdsiteLive.Domain
|
||||||
|
|
||||||
await PostDataAsync(noteActivity, targetHost, actor, targetInbox);
|
await PostDataAsync(noteActivity, targetHost, actor, targetInbox);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Error sending {Username} post ({NoteId}) to {Host}{Inbox}", username, noteId, targetHost, targetInbox);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl, string inbox = null)
|
public async Task<HttpStatusCode> PostDataAsync<T>(T data, string targetHost, string actorUrl, string inbox = null)
|
||||||
{
|
{
|
||||||
|
|
|
@ -81,12 +81,16 @@ namespace BirdsiteLive.Domain.Tools
|
||||||
}
|
}
|
||||||
|
|
||||||
var url = $"https://{_instanceSettings.Domain}/tags/{tag}";
|
var url = $"https://{_instanceSettings.Domain}/tags/{tag}";
|
||||||
|
|
||||||
|
if (tags.All(x => x.href != url))
|
||||||
|
{
|
||||||
tags.Add(new Tag
|
tags.Add(new Tag
|
||||||
{
|
{
|
||||||
name = $"#{tag}",
|
name = $"#{tag}",
|
||||||
href = url,
|
href = url,
|
||||||
type = "Hashtag"
|
type = "Hashtag"
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
messageContent = Regex.Replace(messageContent, Regex.Escape(m.Groups[0].ToString()),
|
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]}");
|
$@"{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)
|
if (extractMentions)
|
||||||
{
|
{
|
||||||
var mentionMatch = OrderByLength(UserRegexes.Mention.Matches(messageContent));
|
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();
|
var mention = m.Groups[2].ToString();
|
||||||
|
|
||||||
|
@ -109,12 +113,15 @@ namespace BirdsiteLive.Domain.Tools
|
||||||
var url = $"https://{_instanceSettings.Domain}/users/{mention}";
|
var url = $"https://{_instanceSettings.Domain}/users/{mention}";
|
||||||
var name = $"@{mention}@{_instanceSettings.Domain}";
|
var name = $"@{mention}@{_instanceSettings.Domain}";
|
||||||
|
|
||||||
|
if (tags.All(x => x.href != url))
|
||||||
|
{
|
||||||
tags.Add(new Tag
|
tags.Add(new Tag
|
||||||
{
|
{
|
||||||
name = name,
|
name = name,
|
||||||
href = url,
|
href = url,
|
||||||
type = "Mention"
|
type = "Mention"
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
messageContent = Regex.Replace(messageContent, Regex.Escape(m.Groups[0].ToString()),
|
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]}");
|
$@"{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]}");
|
||||||
|
@ -127,9 +134,13 @@ namespace BirdsiteLive.Domain.Tools
|
||||||
private IEnumerable<Match> OrderByLength(MatchCollection matches)
|
private IEnumerable<Match> OrderByLength(MatchCollection matches)
|
||||||
{
|
{
|
||||||
var result = new List<Match>();
|
var result = new List<Match>();
|
||||||
|
|
||||||
foreach (Match m in matches) result.Add(m);
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
|
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
|
||||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
<Version>0.16.0</Version>
|
<Version>0.16.1</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -365,6 +365,31 @@ namespace BirdsiteLive.Domain.Tests.Tools
|
||||||
#endregion
|
#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]
|
[TestMethod]
|
||||||
public void Extract_SingleMentionTag_RT_Test()
|
public void Extract_SingleMentionTag_RT_Test()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue