From bb5f2cdfb27b3e1773d622ee92b05aa6b8f1950b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 25 Aug 2021 08:52:19 -0500 Subject: filter search results --- src/slskd/Application/Search/SearchService.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/slskd/Application/Search/SearchService.cs b/src/slskd/Application/Search/SearchService.cs index 84cfd756..87ac5e22 100644 --- a/src/slskd/Application/Search/SearchService.cs +++ b/src/slskd/Application/Search/SearchService.cs @@ -15,6 +15,8 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // +using Microsoft.Extensions.Options; + namespace slskd.Search { using System; @@ -22,6 +24,7 @@ namespace slskd.Search using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; + using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; @@ -41,14 +44,17 @@ namespace slskd.Search /// /// Initializes a new instance of the class. /// + /// /// The client instance to use. /// The database context to use. /// The logger. public SearchService( + IOptionsMonitor optionsMonitor, ISoulseekClient client, IDbContextFactory contextFactory, ILogger log) { + OptionsMonitor = optionsMonitor; Client = client; ContextFactory = contextFactory; Log = log; @@ -57,6 +63,7 @@ namespace slskd.Search private ConcurrentDictionary CancellationTokens { get; } = new ConcurrentDictionary(); + private IOptionsMonitor OptionsMonitor { get; } private ISoulseekClient Client { get; } private IDbContextFactory ContextFactory { get; } private ILogger Log { get; set; } @@ -83,10 +90,15 @@ namespace slskd.Search StartedAt = DateTime.UtcNow, }; + var filters = OptionsMonitor.CurrentValue.Filters.File + .Select(filter => new Regex(filter, RegexOptions.Compiled)); + options ??= new SearchOptions(); options = options.WithActions( stateChanged: (args) => UpdateSearchState(search, args.Search), responseReceived: (args) => UpdateSearchState(search, args.Search)); + options = options.WithFilters( + fileFilter: (file) => !filters.Any(filter => filter.IsMatch(file.Filename))); try { -- cgit v1.2.3