Skip to content

Commit

Permalink
Merge pull request #51 from NicolasConstant/develop
Browse files Browse the repository at this point in the history
0.8.0 PR
  • Loading branch information
NicolasConstant authored Jan 15, 2021
2 parents 72f6337 + 3dad498 commit 9729af9
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/BirdsiteLive.Common/Settings/InstanceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class InstanceSettings
{
public string Domain { get; set; }
public string AdminEmail { get; set; }
public bool ResolveMentionsInProfiles { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/BirdsiteLive.Domain/StatusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Note GetStatus(string username, ExtractedTweet tweet)
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;
Expand Down
35 changes: 19 additions & 16 deletions src/BirdsiteLive.Domain/Tools/StatusExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -36,7 +36,7 @@ public StatusExtractor(InstanceSettings instanceSettings)
}
#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} ";
Expand Down Expand Up @@ -102,22 +102,25 @@ public StatusExtractor(InstanceSettings instanceSettings)
}

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

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>");
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
{
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
Expand Down
2 changes: 1 addition & 1 deletion src/BirdsiteLive.Domain/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Actor GetUser(TwitterUser twitterUser)
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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public async Task GetTwitterUsersAsync(BufferBlock<SyncTwitterUser[]> twitterUse
catch (Exception e)
{
Console.WriteLine(e);
//TODO handle error
}

await Task.Delay(SyncPeriod * 1000 * 60, ct);
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.7.0</Version>
<Version>0.8.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 1 addition & 4 deletions src/BirdsiteLive/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ public async Task<IActionResult> Inbox()
var activity = ApDeserializer.ProcessActivity(body);
// Do something
var signature = r.Headers["Signature"].First();

Console.WriteLine(body);
Console.WriteLine();


switch (activity?.type)
{
case "Follow":
Expand Down
3 changes: 2 additions & 1 deletion src/BirdsiteLive/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"AllowedHosts": "*",
"Instance": {
"Domain": "domain.name",
"AdminEmail": "[email protected]"
"AdminEmail": "[email protected]",
"ResolveMentionsInProfiles": true
},
"Db": {
"Type": "postgres",
Expand Down
34 changes: 17 additions & 17 deletions src/Tests/BirdsiteLive.Domain.Tests/Tools/StatusExtractorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Extract_ReturnLines_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.IsTrue(result.content.Contains("Bla."));
Expand All @@ -46,7 +46,7 @@ public void Extract_ReturnSingleLines_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.IsTrue(result.content.Contains("Bla."));
Expand All @@ -62,7 +62,7 @@ public void Extract_FormatUrl_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(0, result.tags.Length);
Expand All @@ -80,7 +80,7 @@ public void Extract_FormatUrl_Long_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(0, result.tags.Length);
Expand All @@ -98,7 +98,7 @@ public void Extract_FormatUrl_Exact_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(0, result.tags.Length);
Expand All @@ -116,7 +116,7 @@ public void Extract_MultiUrls__Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(0, result.tags.Length);
Expand All @@ -136,7 +136,7 @@ public void Extract_SingleHashTag_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(1, result.tags.Length);
Expand All @@ -157,7 +157,7 @@ public void Extract_SingleHashTag_AtStart_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(1, result.tags.Length);
Expand All @@ -178,7 +178,7 @@ public void Extract_SingleHashTag_SpecialChar_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(1, result.tags.Length);
Expand All @@ -199,7 +199,7 @@ public void Extract_MultiHashTags_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(4, result.tags.Length);
Expand All @@ -219,7 +219,7 @@ public void Extract_SingleMentionTag_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(1, result.tags.Length);
Expand All @@ -240,7 +240,7 @@ public void Extract_SingleMentionTag_SpecialChar_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(1, result.tags.Length);
Expand All @@ -261,7 +261,7 @@ public void Extract_SingleMentionTag_AtStart_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(1, result.tags.Length);
Expand All @@ -282,7 +282,7 @@ public void Extract_MultiMentionTag_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(4, result.tags.Length);
Expand All @@ -302,7 +302,7 @@ public void Extract_HeterogeneousTag_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(5, result.tags.Length);
Expand All @@ -325,7 +325,7 @@ public void Extract_Emoji_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(1, result.tags.Length);
Expand All @@ -345,7 +345,7 @@ public void Extract_Parenthesis_Test()
#endregion

var service = new StatusExtractor(_settings);
var result = service.ExtractTags(message);
var result = service.Extract(message);

#region Validations
Assert.AreEqual(1, result.tags.Length);
Expand Down

0 comments on commit 9729af9

Please sign in to comment.