aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-02-27 06:42:21 -0600
committerGitHub <noreply@github.com>2024-02-27 06:42:21 -0600
commit9b2269a5501fd6b971473f6dd08e632f1ee5238d (patch)
tree121b2c6cbb3997a513baf52178f28a6a0504a319 /src
parent4ba90365e4f6f4602280238be6686405b0a27275 (diff)
parent1fd51e9ffadc263942ca36e5e9b1209138bf3e88 (diff)
downloadslskd-9b2269a5501fd6b971473f6dd08e632f1ee5238d.tar.gz
Merge pull request #1089 from slskd/filter
Add compliance with server code 160/ExcludedSearchPhrases
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs15
-rw-r--r--src/slskd/Shares/SqliteShareRepository.cs11
2 files changed, 25 insertions, 1 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index aa6af2bc..75b2eee6 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -181,6 +181,8 @@ namespace slskd
Client.DownloadDenied += (e, args) => Log.Information("Download of {Filename} from {Username} was denied: {Message}", args.Filename, args.Username, args.Message);
Client.DownloadFailed += (e, args) => Log.Information("Download of {Filename} from {Username} failed", args.Filename, args.Username);
+ Client.ExcludedSearchPhrasesReceived += Client_ExcludedSearchPhrasesReceived;
+
ConnectionWatchdog = connectionWatchdog;
Clock.EveryMinute += Clock_EveryMinute;
@@ -221,6 +223,7 @@ namespace slskd
private IEnumerable<Regex> CompiledSearchResponseFilters { get; set; }
private IEnumerable<Guid> ActiveDownloadIdsAtPreviousShutdown { get; set; } = Enumerable.Empty<Guid>();
private Options.FlagsOptions Flags { get; set; }
+ private IReadOnlyCollection<string> ExcludedSearchPhrases { get; set; } = Enumerable.Empty<string>().ToList();
public void CollectGarbage()
{
@@ -1093,6 +1096,12 @@ namespace slskd
}
}
+ private void Client_ExcludedSearchPhrasesReceived(object sender, IReadOnlyCollection<string> e)
+ {
+ Log.Debug("Excluded search phrases: {Phrases}", string.Join(", ", e));
+ ExcludedSearchPhrases = e;
+ }
+
private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args)
{
// no-op. this is really verbose, use for troubleshooting.
@@ -1366,11 +1375,15 @@ namespace slskd
// sometimes clients send search queries consisting only of exclusions; drop them.
// no other clients send search results for these, even though it is technically possible.
- if (!query.Terms.Any())
+ if (query.Terms.Count == 0)
{
return null;
}
+ // append the list of excluded search phrases supplied by the server
+ // see https://github.com/jpdillingham/Soulseek.NET/issues/803
+ query = new SearchQuery(terms: query.Terms, exclusions: query.Exclusions.Concat(ExcludedSearchPhrases).Distinct());
+
try
{
var sw = new Stopwatch();
diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs
index 882ede74..029d74b8 100644
--- a/src/slskd/Shares/SqliteShareRepository.cs
+++ b/src/slskd/Shares/SqliteShareRepository.cs
@@ -539,6 +539,17 @@ namespace slskd.Shares
while (reader.Read())
{
var filename = reader.GetString(0);
+
+ // SQLite takes care of matching on tokens, but unfortunately doesn't work in cases where an exclusion appears as a substring.
+ // example: the token matching works to exclude the result "foo bar baz" given a "bar" exclusion, but does not exclude "foo xbarx baz"
+ // for this reason we must iterate over the exclusion list and ensure none appear as substrings
+ // **CAUTION** performance of this will degrade exponentially as the list of exclusions grows
+ if (query.Exclusions.Any(x => filename.Contains(x, StringComparison.OrdinalIgnoreCase)))
+ {
+ Log.Debug("Dropping excluded filename {File}", filename);
+ continue;
+ }
+
var code = reader.GetInt32(1);
var size = reader.GetInt64(2);
var extension = reader.GetString(3);