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 329e981
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 2 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>>();

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

Check failure on line 54 in src/BookmarkSync.Core/Configuration/ConfigManager.cs

View workflow job for this annotation

GitHub Actions / .NET Test Results

BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests ► GetBookmarkingService_Exception

Failed test found in: TestResults/_fv-az616-849_2023-08-13_19_52_56.trx Error: Test method threw exception System.ArgumentNullException, but exception System.InvalidOperationException was expected. Exception message: System.ArgumentNullException: Value cannot be null. (Parameter 'source')
Raw output
Test method threw exception System.ArgumentNullException, but exception System.InvalidOperationException was expected. Exception message: System.ArgumentNullException: Value cannot be null. (Parameter 'source')
   at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
   at BookmarkSync.Core.Configuration.ConfigManager.CleanUpIgnoredAccounts() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 54
   at BookmarkSync.Core.Configuration.ConfigManager..ctor(IConfiguration configuration) in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 25
   at BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_Exception() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/tests/BookmarkSync.Infrastructure.Tests/Services/BookmarkingServiceTests.cs:line 23
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Check failure on line 54 in src/BookmarkSync.Core/Configuration/ConfigManager.cs

View workflow job for this annotation

GitHub Actions / .NET Test Results

BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests ► GetBookmarkingService_LinkAce

Failed test found in: TestResults/_fv-az616-849_2023-08-13_19_52_56.trx Error: Test method BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_LinkAce threw exception: System.ArgumentNullException: Value cannot be null. (Parameter 'source')
Raw output
Test method BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_LinkAce threw exception: 
System.ArgumentNullException: Value cannot be null. (Parameter 'source')
    at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
   at BookmarkSync.Core.Configuration.ConfigManager.CleanUpIgnoredAccounts() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 54
   at BookmarkSync.Core.Configuration.ConfigManager..ctor(IConfiguration configuration) in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 25
   at BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_LinkAce() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/tests/BookmarkSync.Infrastructure.Tests/Services/BookmarkingServiceTests.cs:line 50
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Check failure on line 54 in src/BookmarkSync.Core/Configuration/ConfigManager.cs

View workflow job for this annotation

GitHub Actions / .NET Test Results

BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests ► GetBookmarkingService_Linkding

Failed test found in: TestResults/_fv-az616-849_2023-08-13_19_52_56.trx Error: Test method BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_Linkding threw exception: System.ArgumentNullException: Value cannot be null. (Parameter 'source')
Raw output
Test method BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_Linkding threw exception: 
System.ArgumentNullException: Value cannot be null. (Parameter 'source')
    at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
   at BookmarkSync.Core.Configuration.ConfigManager.CleanUpIgnoredAccounts() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 54
   at BookmarkSync.Core.Configuration.ConfigManager..ctor(IConfiguration configuration) in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 25
   at BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_Linkding() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/tests/BookmarkSync.Infrastructure.Tests/Services/BookmarkingServiceTests.cs:line 79
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Check failure on line 54 in src/BookmarkSync.Core/Configuration/ConfigManager.cs

View workflow job for this annotation

GitHub Actions / .NET Test Results

BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests ► GetBookmarkingService_Pinboard

Failed test found in: TestResults/_fv-az616-849_2023-08-13_19_52_56.trx Error: Test method BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_Pinboard threw exception: System.ArgumentNullException: Value cannot be null. (Parameter 'source')
Raw output
Test method BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_Pinboard threw exception: 
System.ArgumentNullException: Value cannot be null. (Parameter 'source')
    at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
   at BookmarkSync.Core.Configuration.ConfigManager.CleanUpIgnoredAccounts() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 54
   at BookmarkSync.Core.Configuration.ConfigManager..ctor(IConfiguration configuration) in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 25
   at BookmarkSync.Infrastructure.Tests.Services.BookmarkingServiceTests.GetBookmarkingService_Pinboard() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/tests/BookmarkSync.Infrastructure.Tests/Services/BookmarkingServiceTests.cs:line 105
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Check failure on line 54 in src/BookmarkSync.Core/Configuration/ConfigManager.cs

View workflow job for this annotation

GitHub Actions / .NET Test Results

BookmarkSync.Core.Tests.Configuration.ConfigManagerTests ► ConfigManager_HasProperties

Failed test found in: TestResults/_fv-az616-849_2023-08-13_19_52_56[1].trx Error: Initialization method BookmarkSync.Core.Tests.Configuration.ConfigManagerTests.Init threw exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source').
Raw output
Initialization method BookmarkSync.Core.Tests.Configuration.ConfigManagerTests.Init threw exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source').
    at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
   at BookmarkSync.Core.Configuration.ConfigManager.CleanUpIgnoredAccounts() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 54
   at BookmarkSync.Core.Configuration.ConfigManager..ctor(IConfiguration configuration) in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 25
   at BookmarkSync.Core.Tests.Configuration.ConfigManagerTests.Init() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/tests/BookmarkSync.Core.Tests/Configuration/ConfigManagerTests.cs:line 26
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Check failure on line 54 in src/BookmarkSync.Core/Configuration/ConfigManager.cs

View workflow job for this annotation

GitHub Actions / .NET Test Results

BookmarkSync.Core.Tests.Configuration.ConfigManagerTests ► GetConfigValue_InvalidKey

Failed test found in: TestResults/_fv-az616-849_2023-08-13_19_52_56[1].trx TestResults/_fv-az616-849_2023-08-13_19_52_56[1].trx Error: Initialization method BookmarkSync.Core.Tests.Configuration.ConfigManagerTests.Init threw exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source').
Raw output
Initialization method BookmarkSync.Core.Tests.Configuration.ConfigManagerTests.Init threw exception. System.ArgumentNullException: Value cannot be null. (Parameter 'source').
    at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
   at BookmarkSync.Core.Configuration.ConfigManager.CleanUpIgnoredAccounts() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 54
   at BookmarkSync.Core.Configuration.ConfigManager..ctor(IConfiguration configuration) in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/src/BookmarkSync.Core/Configuration/ConfigManager.cs:line 25
   at BookmarkSync.Core.Tests.Configuration.ConfigManagerTests.Init() in /home/runner/work/mastodon-bookmark-sync/mastodon-bookmark-sync/tests/BookmarkSync.Core.Tests/Configuration/ConfigManagerTests.cs:line 26
   at InvokeStub_ConfigManagerTests.Init(Object, Object, IntPtr*)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
}
}
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; }

Check warning on line 6 in src/BookmarkSync.Core/Entities/Config/App.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'IgnoredAccounts' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in src/BookmarkSync.Core/Entities/Config/App.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'IgnoredAccounts' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
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 @@ -10,11 +11,13 @@ public class BookmarkSyncService : IHostedService
private readonly IBookmarkingService _bookmarkingService;
private readonly IHostApplicationLifetime _host;
private readonly List<Instance>? _instances;
private readonly List<string> _ignoredAccounts;
public BookmarkSyncService(IHostApplicationLifetime host, IConfigManager configManager)
{
_bookmarkingService = BookmarkingService.GetBookmarkingService(configManager);
_host = host;
_instances = configManager.Instances;
_ignoredAccounts = configManager.App.IgnoredAccounts;
}
/// <inheritdoc />
public async Task StartAsync(CancellationToken stoppingToken)
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 Down
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
var 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
var actual = input.RemoveLeadingAt();

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

0 comments on commit 329e981

Please sign in to comment.