From a62f2376004de9ec6c727f4096747b4967296974 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 17 Dec 2021 09:32:14 -0600 Subject: add Search hub --- src/slskd/Search/API/Hubs/SearchHub.cs | 83 ++++++++++++++++++++++++++++++++++ src/slskd/Search/SearchService.cs | 18 +++++++- src/slskd/Startup.cs | 2 + 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/slskd/Search/API/Hubs/SearchHub.cs (limited to 'src') diff --git a/src/slskd/Search/API/Hubs/SearchHub.cs b/src/slskd/Search/API/Hubs/SearchHub.cs new file mode 100644 index 00000000..1bb45574 --- /dev/null +++ b/src/slskd/Search/API/Hubs/SearchHub.cs @@ -0,0 +1,83 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +using Microsoft.Extensions.Options; + +namespace slskd.Search.API +{ + using System; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.SignalR; + + public static class SearchHubMethods + { + public static readonly string Response = "RESPONSE"; + public static readonly string Update = "UPDATE"; + } + + /// + /// Extension methods for the search SignalR hub. + /// + public static class SearchHubExtensions + { + /// + /// Broadcast an update for a search. + /// + /// The hub. + /// The search to broadcast. + /// The operation context. + public static Task BroadcastUpdateAsync(this IHubContext hub, Search search) + { + return hub.Clients.All.SendAsync(SearchHubMethods.Update, search); + } + + /// + /// Broadcast the present application options. + /// + /// The hub. + /// The ID of the search associated with the response. + /// The response to broadcast. + /// The operation context. + public static Task BroadcastResponseAsync(this IHubContext hub, Guid searchId, Soulseek.SearchResponse response) + { + return hub.Clients.All.SendAsync(SearchHubMethods.Response, new { searchId, response }); + } + } + + /// + /// The search SignalR hub. + /// + [Authorize] + public class SearchHub : Hub + { + public SearchHub( + IStateMonitor stateMonitor, + IOptionsMonitor optionsMonitor) + { + StateMonitor = stateMonitor; + OptionsMonitor = optionsMonitor; + } + + private IStateMonitor StateMonitor { get; } + private IOptionsMonitor OptionsMonitor { get; } + + public override async Task OnConnectedAsync() + { + } + } +} \ No newline at end of file diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index b934e86b..1eb92bc2 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -26,8 +26,10 @@ namespace slskd.Search using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; + using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; + using slskd.Search.API; using Soulseek; using SearchOptions = Soulseek.SearchOptions; using SearchQuery = Soulseek.SearchQuery; @@ -43,18 +45,21 @@ namespace slskd.Search /// /// Initializes a new instance of the class. /// + /// /// /// /// /// The database context to use. /// The logger. public SearchService( + IHubContext searchHub, IOptionsMonitor optionsMonitor, ISoulseekClient soulseekClient, IApplication application, IDbContextFactory contextFactory, ILogger log) { + SearchHub = searchHub; OptionsMonitor = optionsMonitor; Client = soulseekClient; Application = application; @@ -70,6 +75,7 @@ namespace slskd.Search private ISoulseekClient Client { get; } private IDbContextFactory ContextFactory { get; } private ILogger Log { get; set; } + private IHubContext SearchHub { get; set; } /// /// Performs a search for the specified and . @@ -96,7 +102,7 @@ namespace slskd.Search options ??= new SearchOptions(); options = options.WithActions( stateChanged: (args) => UpdateSearchState(search, args.Search), - responseReceived: (args) => UpdateSearchState(search, args.Search)); + responseReceived: (args) => UpdateSearchResponses(search, args.Search, args.Response)); try { @@ -201,10 +207,20 @@ namespace slskd.Search search.ResponseCount = soulseekSearch.ResponseCount; search.State = soulseekSearch.State; + SearchHub.BroadcastUpdateAsync(search); SaveSearchState(search); } } + private void UpdateSearchResponses(Search search, SoulseekSearch soulseekSearch, Soulseek.SearchResponse response) + { + if (CancellationTokens.ContainsKey(search.Id)) + { + SearchHub.BroadcastResponseAsync(search.Id, response); + UpdateSearchState(search, soulseekSearch); + } + } + private void SaveSearchState(Search search) { var context = ContextFactory.CreateDbContext(); diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index ac4a2789..07c75fd3 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -41,6 +41,7 @@ namespace slskd using slskd.Integrations.Pushbullet; using slskd.Messaging; using slskd.Search; + using slskd.Search.API; using slskd.Shares; using slskd.Transfers; using slskd.Users; @@ -337,6 +338,7 @@ namespace slskd { endpoints.MapHub("/hub/application"); endpoints.MapHub("/hub/logs"); + endpoints.MapHub("/hub/search"); endpoints.MapControllers(); endpoints.MapHealthChecks("/health"); -- cgit v1.2.3