Skip to content

Commit

Permalink
Merge pull request #99 from NicolasConstant/develop
Browse files Browse the repository at this point in the history
0.16.1 PR
  • Loading branch information
NicolasConstant authored Mar 30, 2021
2 parents 3a50512 + 07d09eb commit 1a578f3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 35 deletions.
47 changes: 29 additions & 18 deletions src/BirdsiteLive.Domain/ActivityPubService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using BirdsiteLive.ActivityPub.Converters;
using BirdsiteLive.ActivityPub.Models;
using BirdsiteLive.Common.Settings;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Org.BouncyCastle.Bcpg;

Expand All @@ -27,13 +28,15 @@ public class ActivityPubService : IActivityPubService
private readonly InstanceSettings _instanceSettings;
private readonly IHttpClientFactory _httpClientFactory;
private readonly ICryptoService _cryptoService;
private readonly ILogger<ActivityPubService> _logger;

#region Ctor
public ActivityPubService(ICryptoService cryptoService, InstanceSettings instanceSettings, IHttpClientFactory httpClientFactory)
public ActivityPubService(ICryptoService cryptoService, InstanceSettings instanceSettings, IHttpClientFactory httpClientFactory, ILogger<ActivityPubService> logger)
{
_cryptoService = cryptoService;
_instanceSettings = instanceSettings;
_httpClientFactory = httpClientFactory;
_logger = logger;
}
#endregion

Expand All @@ -48,26 +51,34 @@ public async Task<Actor> GetUser(string objectId)

public async Task PostNewNoteActivity(Note note, string username, string noteId, string targetHost, string targetInbox)
{
var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username);
var noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, noteId);
try
{
var actor = UrlFactory.GetActorUrl(_instanceSettings.Domain, username);
var noteUri = UrlFactory.GetNoteUrl(_instanceSettings.Domain, username, noteId);

var now = DateTime.UtcNow;
var nowString = now.ToString("s") + "Z";
var now = DateTime.UtcNow;
var nowString = now.ToString("s") + "Z";

var noteActivity = new ActivityCreateNote()
var noteActivity = new ActivityCreateNote()
{
context = "https://www.w3.org/ns/activitystreams",
id = $"{noteUri}/activity",
type = "Create",
actor = actor,
published = nowString,

to = note.to,
cc = note.cc,
apObject = note
};

await PostDataAsync(noteActivity, targetHost, actor, targetInbox);
}
catch (Exception e)
{
context = "https://www.w3.org/ns/activitystreams",
id = $"{noteUri}/activity",
type = "Create",
actor = actor,
published = nowString,

to = note.to,
cc = note.cc,
apObject = note
};

await PostDataAsync(noteActivity, targetHost, actor, targetInbox);
_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)
Expand Down
41 changes: 26 additions & 15 deletions src/BirdsiteLive.Domain/Tools/StatusExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ public StatusExtractor(InstanceSettings instanceSettings, ILogger<StatusExtracto
}

var url = $"https://{_instanceSettings.Domain}/tags/{tag}";
tags.Add(new Tag

if (tags.All(x => x.href != url))
{
name = $"#{tag}",
href = url,
type = "Hashtag"
});
tags.Add(new Tag
{
name = $"#{tag}",
href = url,
type = "Hashtag"
});
}

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]}");
Expand All @@ -96,7 +100,7 @@ public StatusExtractor(InstanceSettings instanceSettings, ILogger<StatusExtracto
if (extractMentions)
{
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();

Expand All @@ -109,27 +113,34 @@ public StatusExtractor(InstanceSettings instanceSettings, ILogger<StatusExtracto
var url = $"https://{_instanceSettings.Domain}/users/{mention}";
var name = $"@{mention}@{_instanceSettings.Domain}";

tags.Add(new Tag
if (tags.All(x => x.href != url))
{
name = name,
href = url,
type = "Mention"
});

tags.Add(new Tag
{
name = name,
href = url,
type = "Mention"
});
}

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]}");
}
}

return (messageContent.Trim(), tags.ToArray());
}

private IEnumerable<Match> OrderByLength(MatchCollection matches)
{
var result = new List<Match>();

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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/BirdsiteLive/BirdsiteLive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>d21486de-a812-47eb-a419-05682bb68856</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>0.16.0</Version>
<Version>0.16.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,32 @@ public void Extract_SingleMentionTag_Test()
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_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]
public void Extract_SingleMentionTag_RT_Test()
{
Expand Down

0 comments on commit 1a578f3

Please sign in to comment.