Skip to content

Commit

Permalink
Implement account ignore list
Browse files Browse the repository at this point in the history
Fixes #99
  • Loading branch information
prplecake committed Aug 13, 2023
1 parent b438b13 commit 50749ee
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/BookmarkSync.CLI/appsettings.Example.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"Service": "LinkAce",
"ApiToken": "",
"LinkAceUri": ""
}
},
"IgnoredAccounts": []
}
}
7 changes: 7 additions & 0 deletions src/BookmarkSync.Core/Configuration/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Configuration;
using BookmarkSync.Core.Entities.Config;
using BookmarkSync.Core.Extensions;
using Microsoft.Extensions.Configuration;

namespace BookmarkSync.Core.Configuration;
Expand All @@ -21,6 +22,8 @@ public ConfigManager(
App = Configuration.GetSection("App").Get<App>() ?? throw new InvalidOperationException();
Instances = Configuration.GetSection("Instances").Get<List<Instance>>();

if (App.IgnoredAccounts is not null) CleanUpIgnoredAccounts();

if (!App.IsValid())
{
_logger.Error("App configuration is invalid");
Expand All @@ -46,4 +49,8 @@ public void SaveToFile()
Console.WriteLine(Directory.GetCurrentDirectory());
throw new NotImplementedException();
}
private void CleanUpIgnoredAccounts()
{
App.IgnoredAccounts = (from account in App.IgnoredAccounts select account.RemoveLeadingAt()).ToList();
}
}
1 change: 1 addition & 0 deletions src/BookmarkSync.Core/Entities/Config/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ namespace BookmarkSync.Core.Entities.Config;
public class App : ConfigurationBase
{
[ConfigRequired] public Bookmarking Bookmarking { get; set; } = null!;
public List<string>? IgnoredAccounts { get; set; }
public DateTime LastSynced { get; set; }
}
15 changes: 15 additions & 0 deletions src/BookmarkSync.Core/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using BookmarkSync.Core.Entities;

namespace BookmarkSync.Core.Extensions;

public static class ListExtensions
{
public static List<Bookmark>? RemoveAllFromIgnoredAccounts(
this List<Bookmark>? bookmarks,
List<string> ignoredAccounts)
{
if (bookmarks is null) return bookmarks;
bookmarks.RemoveAll(b => ignoredAccounts.Contains(b.Account.Name));

Check warning on line 12 in src/BookmarkSync.Core/Extensions/ListExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 12 in src/BookmarkSync.Core/Extensions/ListExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'bool List<string>.Contains(string item)'.
return bookmarks;
}
}
4 changes: 4 additions & 0 deletions src/BookmarkSync.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ namespace BookmarkSync.Core.Extensions;

public static class StringExtensions
{
public static bool HasLeadingAt(this string str)
=> str.StartsWith("@");
public static string RemoveLeadingAt(this string str)
=> str.HasLeadingAt() ? str[1..] : str;
public static string ToSnakeCase(this string str)
=> string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x : x.ToString())).ToLower();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BookmarkSync.Core.Entities.Config;
using BookmarkSync.Core.Extensions;
using BookmarkSync.Infrastructure.Services.Mastodon;
using Microsoft.Extensions.Hosting;

Expand All @@ -9,14 +10,16 @@ public class BookmarkSyncService : IHostedService
private static readonly ILogger _logger = Log.ForContext<BookmarkSyncService>();
private readonly IBookmarkingService _bookmarkingService;
private readonly IHostApplicationLifetime _host;
private readonly List<string> _ignoredAccounts;
private readonly List<Instance>? _instances;
public BookmarkSyncService(IHostApplicationLifetime host, IConfigManager configManager)

Check warning on line 15 in src/BookmarkSync.Infrastructure/Services/Bookmarking/BookmarkSyncService.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_ignoredAccounts' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_bookmarkingService = BookmarkingService.GetBookmarkingService(configManager);
_host = host;
_instances = configManager.Instances;
_ignoredAccounts = configManager.App.IgnoredAccounts;

Check warning on line 20 in src/BookmarkSync.Infrastructure/Services/Bookmarking/BookmarkSyncService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
}
/// <inheritdoc />
/// <inheritdoc/>
public async Task StartAsync(CancellationToken stoppingToken)
{
if (_instances == null || _instances.Count == 0)
Expand Down Expand Up @@ -47,6 +50,10 @@ public async Task StartAsync(CancellationToken stoppingToken)
_logger.Information("No bookmarks received");
continue;
}

// Remove any bookmarks from accounts in the IgnoredAccounts list
bookmarks.RemoveAllFromIgnoredAccounts(_ignoredAccounts);

foreach (var bookmark in bookmarks)
{
// Save bookmarks to bookmarking service
Expand All @@ -72,6 +79,6 @@ public async Task StartAsync(CancellationToken stoppingToken)
// Finish task
_host.StopApplication();
}
/// <inheritdoc />
/// <inheritdoc/>
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
Expand Down
47 changes: 40 additions & 7 deletions tests/BookmarkSync.Core.Tests/Configuration/ConfigManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Configuration;
using System.Text;
using BookmarkSync.Core.Configuration;
using BookmarkSync.Core.Extensions;
using Microsoft.Extensions.Configuration;

namespace BookmarkSync.Core.Tests.Configuration;
Expand Down Expand Up @@ -38,6 +40,18 @@ public void ConfigManager_HasProperties()
Assert.IsTrue(_configManager?.HasMethod("GetConfigValue"));
}
[TestMethod]
[ExpectedException(typeof(ConfigurationErrorsException))]
public void GetConfigValue_InvalidKey()
{
// Arrange
var expected = "Pinner";

Check warning on line 47 in tests/BookmarkSync.Core.Tests/Configuration/ConfigManagerTests.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'expected' is assigned but its value is never used

// Act
string? actual = _configManager?.GetConfigValue("Apps:Bookmarking:Service");

// Assert - Exception
}
[TestMethod]
public void GetConfigValue_Success()
{
// Arrange
Expand All @@ -50,15 +64,34 @@ public void GetConfigValue_Success()
Assert.AreEqual(expected, actual);
}
[TestMethod]
[ExpectedException(typeof(ConfigurationErrorsException))]
public void GetConfigValue_InvalidKey()
public void TestConfigManagerWithIgnoredAccountsConfigured()
{
// Arrange
var expected = "Pinner";
var jsonString = @"
{
""App"": {
""Bookmarking"": {
""Service"": ""LinkAce""
},
""IgnoredAccounts"": [
""@[email protected]"",
""[email protected]""
],
},
""Instances"": [
""https://compostintraining.club""
]
}";
var config = new ConfigurationBuilder()
.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
.Build();

// Act
string? actual = _configManager?.GetConfigValue("Apps:Bookmarking:Service");
var configManager = new ConfigManager(config);

// Assert - Exception
// Assert
Assert.IsNotNull(configManager.App.IgnoredAccounts);
foreach (string? account in configManager.App.IgnoredAccounts)
{
Assert.IsFalse(account.HasLeadingAt());
}
}
}
3 changes: 2 additions & 1 deletion tests/BookmarkSync.Core.Tests/Entities/Config/AppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public void App_HasProperties()
App obj = new();

// Assert
Assert.AreEqual(2, obj.PropertyCount());
Assert.AreEqual(3, obj.PropertyCount());
Assert.IsTrue(obj.HasProperty("Bookmarking"));
Assert.IsTrue(obj.HasProperty("IgnoredAccounts"));
Assert.IsTrue(obj.HasProperty("LastSynced"));

Assert.IsTrue(obj.HasMethod("IsValid"));
Expand Down
22 changes: 22 additions & 0 deletions tests/BookmarkSync.Core.Tests/Extensions/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ namespace BookmarkSync.Core.Tests.Extensions;
[TestClass]
public class StringExtensionsTests
{
[DataTestMethod]
[DataRow("@prplecake", true)]
[DataRow("flipper", false)]
public void HasLeadingAt_Success(string input, bool expected)
{
// Act
bool actual = input.HasLeadingAt();

// Assert
Assert.AreEqual(expected, actual);
}
[DataTestMethod]
[DataRow("@[email protected]", "[email protected]")]
[DataRow("[email protected]", "[email protected]")]
public void RemoveLeadingAt_Success(string input, string expected)
{
// Act
string actual = input.RemoveLeadingAt();

// Assert
Assert.AreEqual(expected, actual);
}
[DataTestMethod]
[DataRow("Example", "example")]
[DataRow("TestCase", "test_case")]
Expand Down

0 comments on commit 50749ee

Please sign in to comment.