From 976fcca2e6dfa0b5f5c8872ac3c6ec8f24b54a29 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 26 Apr 2022 18:02:07 -0500 Subject: add endpoints to stop/cancel a search and delete a search record --- .../Search/API/Controllers/SearchesController.cs | 55 +++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'src/slskd/Search/API') diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index 5f740044..ae0f6570 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -129,7 +129,60 @@ namespace slskd.Search.API [Authorize] public async Task GetAll() { - return Ok(await Searches.ListAsync()); + var searches = await Searches.ListAsync(); + return Ok(searches); + } + + /// + /// Stops the search corresponding to the specified . + /// + /// The unique id of the search. + /// The search was stopped. + /// The search was not in progress. + /// + [HttpPut("{id}")] + [Authorize] + [ProducesResponseType(200)] + [ProducesResponseType(304)] + public async Task Cancel([FromRoute] Guid id) + { + var search = await Searches.FindAsync(search => search.Id == id); + + if (search == default) + { + return NotFound(); + } + + if (Searches.TryCancel(id)) + { + return Ok(); + } + + return StatusCode(304); + } + + /// + /// Deletes the search corresponding to the specified . + /// + /// The unique id of the search. + /// The search was deleted. + /// A search with the specified id could not be found. + /// + [HttpDelete("{id}")] + [Authorize] + [ProducesResponseType(204)] + [ProducesResponseType(404)] + public async Task Delete([FromRoute] Guid id) + { + var search = await Searches.FindAsync(search => search.Id == id, includeResponses: true); + + if (search == default) + { + return NotFound(); + } + + await Searches.DeleteAsync(search); + return NoContent(); } } } -- cgit v1.2.3 From 223543b5889a1e3ee139b597eaa2cdfc5988fe02 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 27 Apr 2022 22:25:47 -0500 Subject: broadcast creates and deletes --- src/slskd/Search/API/Hubs/SearchHub.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/slskd/Search/API') diff --git a/src/slskd/Search/API/Hubs/SearchHub.cs b/src/slskd/Search/API/Hubs/SearchHub.cs index 86434669..4417f8c9 100644 --- a/src/slskd/Search/API/Hubs/SearchHub.cs +++ b/src/slskd/Search/API/Hubs/SearchHub.cs @@ -26,8 +26,10 @@ namespace slskd.Search.API public static class SearchHubMethods { + public static readonly string Create = "CREATE"; public static readonly string Response = "RESPONSE"; public static readonly string Update = "UPDATE"; + public static readonly string Delete = "DELETE"; } /// @@ -57,6 +59,16 @@ namespace slskd.Search.API { return hub.Clients.All.SendAsync(SearchHubMethods.Response, new { searchId, response }); } + + public static Task BroadcastCreateAsync(this IHubContext hub, Search search) + { + return hub.Clients.All.SendAsync(SearchHubMethods.Create, search); + } + + public static Task BroadcastDeleteAsync(this IHubContext hub, Search search) + { + return hub.Clients.All.SendAsync(SearchHubMethods.Delete, search); + } } /// -- cgit v1.2.3 From 463c96b6f22d78543f25376edd2d7716af1b44a4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 27 Apr 2022 22:33:22 -0500 Subject: load searches from hub --- src/slskd/Search/API/Hubs/SearchHub.cs | 10 ++++++++++ src/web/src/components/Search/List/SearchList.js | 10 ++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'src/slskd/Search/API') diff --git a/src/slskd/Search/API/Hubs/SearchHub.cs b/src/slskd/Search/API/Hubs/SearchHub.cs index 4417f8c9..881cd7c2 100644 --- a/src/slskd/Search/API/Hubs/SearchHub.cs +++ b/src/slskd/Search/API/Hubs/SearchHub.cs @@ -26,6 +26,7 @@ namespace slskd.Search.API public static class SearchHubMethods { + public static readonly string Load = "LOAD"; public static readonly string Create = "CREATE"; public static readonly string Response = "RESPONSE"; public static readonly string Update = "UPDATE"; @@ -78,14 +79,23 @@ namespace slskd.Search.API public class SearchHub : Hub { public SearchHub( + ISearchService searchService, IStateMonitor stateMonitor, IOptionsMonitor optionsMonitor) { + Searches = searchService; StateMonitor = stateMonitor; OptionsMonitor = optionsMonitor; } + private ISearchService Searches { get; } private IStateMonitor StateMonitor { get; } private IOptionsMonitor OptionsMonitor { get; } + + public override async Task OnConnectedAsync() + { + var searches = await Searches.ListAsync(); + await Clients.Caller.SendAsync(SearchHubMethods.Load, searches); + } } } \ No newline at end of file diff --git a/src/web/src/components/Search/List/SearchList.js b/src/web/src/components/Search/List/SearchList.js index 6450672a..a774fb05 100644 --- a/src/web/src/components/Search/List/SearchList.js +++ b/src/web/src/components/Search/List/SearchList.js @@ -22,10 +22,16 @@ const SearchList = () => { const inputRef = useRef(); useEffect(() => { - get(); - const searchHub = createSearchLogHubConnection(); + searchHub.on('load', searches => { + setConnected(true); + setSearches(searches.reduce((acc, search) => { + acc[search.id] = search; + return acc; + }, {})); + }) + searchHub.on('update', search => { setConnected(true); setSearches(old => ({ ...old, [search.id]: search })) -- cgit v1.2.3 From 6c711fe518dba77bd7fdc3314cc6a23730db7e88 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 27 Apr 2022 22:37:46 -0500 Subject: prepare throttle --- src/slskd/Search/API/Hubs/SearchHub.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/slskd/Search/API') diff --git a/src/slskd/Search/API/Hubs/SearchHub.cs b/src/slskd/Search/API/Hubs/SearchHub.cs index 881cd7c2..dc27ca9d 100644 --- a/src/slskd/Search/API/Hubs/SearchHub.cs +++ b/src/slskd/Search/API/Hubs/SearchHub.cs @@ -46,6 +46,7 @@ namespace slskd.Search.API /// The operation context. public static Task BroadcastUpdateAsync(this IHubContext hub, Search search) { + // todo: throttle updates to at most 1 per 100ms or so return hub.Clients.All.SendAsync(SearchHubMethods.Update, search); } @@ -79,23 +80,24 @@ namespace slskd.Search.API public class SearchHub : Hub { public SearchHub( - ISearchService searchService, - IStateMonitor stateMonitor, - IOptionsMonitor optionsMonitor) + ISearchService searchService) { Searches = searchService; - StateMonitor = stateMonitor; - OptionsMonitor = optionsMonitor; } private ISearchService Searches { get; } - private IStateMonitor StateMonitor { get; } - private IOptionsMonitor OptionsMonitor { get; } public override async Task OnConnectedAsync() { var searches = await Searches.ListAsync(); await Clients.Caller.SendAsync(SearchHubMethods.Load, searches); + // todo: create buffer specific to this client + } + + public override Task OnDisconnectedAsync(Exception exception) + { + // todo: destroy buffer specific to this client + return Task.CompletedTask; } } } \ No newline at end of file -- cgit v1.2.3 From ddcefd51dcd7674e86fd54b4c0fbdf168c21170a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 28 Apr 2022 08:58:24 -0500 Subject: rename load to list --- src/slskd/Search/API/Hubs/SearchHub.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Search/API') diff --git a/src/slskd/Search/API/Hubs/SearchHub.cs b/src/slskd/Search/API/Hubs/SearchHub.cs index dc27ca9d..5c9fc114 100644 --- a/src/slskd/Search/API/Hubs/SearchHub.cs +++ b/src/slskd/Search/API/Hubs/SearchHub.cs @@ -26,7 +26,7 @@ namespace slskd.Search.API public static class SearchHubMethods { - public static readonly string Load = "LOAD"; + public static readonly string List = "LIST"; public static readonly string Create = "CREATE"; public static readonly string Response = "RESPONSE"; public static readonly string Update = "UPDATE"; @@ -90,7 +90,7 @@ namespace slskd.Search.API public override async Task OnConnectedAsync() { var searches = await Searches.ListAsync(); - await Clients.Caller.SendAsync(SearchHubMethods.Load, searches); + await Clients.Caller.SendAsync(SearchHubMethods.List, searches); // todo: create buffer specific to this client } -- cgit v1.2.3 From afb0af902010c2a5c64a054a7f0be0f6f655695b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 28 Apr 2022 20:56:32 -0500 Subject: remove disconnect handler --- src/slskd/Search/API/Hubs/SearchHub.cs | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'src/slskd/Search/API') diff --git a/src/slskd/Search/API/Hubs/SearchHub.cs b/src/slskd/Search/API/Hubs/SearchHub.cs index 5c9fc114..aaf9a287 100644 --- a/src/slskd/Search/API/Hubs/SearchHub.cs +++ b/src/slskd/Search/API/Hubs/SearchHub.cs @@ -15,8 +15,6 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // -using Microsoft.Extensions.Options; - namespace slskd.Search.API { using System; @@ -46,7 +44,6 @@ namespace slskd.Search.API /// The operation context. public static Task BroadcastUpdateAsync(this IHubContext hub, Search search) { - // todo: throttle updates to at most 1 per 100ms or so return hub.Clients.All.SendAsync(SearchHubMethods.Update, search); } @@ -91,13 +88,6 @@ namespace slskd.Search.API { var searches = await Searches.ListAsync(); await Clients.Caller.SendAsync(SearchHubMethods.List, searches); - // todo: create buffer specific to this client - } - - public override Task OnDisconnectedAsync(Exception exception) - { - // todo: destroy buffer specific to this client - return Task.CompletedTask; } } } \ No newline at end of file -- cgit v1.2.3