From ea78f7f0c5b0858f15a1af8a700100d7ae101ae1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 May 2021 12:33:27 -0500 Subject: add SearchDbContext --- src/slskd/API/Controllers/SearchesController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/API/Controllers') diff --git a/src/slskd/API/Controllers/SearchesController.cs b/src/slskd/API/Controllers/SearchesController.cs index 4a3dc7ab..6d322bda 100644 --- a/src/slskd/API/Controllers/SearchesController.cs +++ b/src/slskd/API/Controllers/SearchesController.cs @@ -102,7 +102,7 @@ namespace slskd.API.Controllers /// A matching search was not found. [HttpGet("{id}")] [Authorize] - [ProducesResponseType(typeof(Search), 200)] + [ProducesResponseType(typeof(Soulseek.Search), 200)] [ProducesResponseType(404)] public IActionResult GetById([FromRoute]Guid id) { -- cgit v1.2.3 From ccc8ad7b0ce16d69161c70ab16d530027c2b78ce Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 26 May 2021 23:48:59 -0500 Subject: rough in controller and DI --- src/slskd/API/Controllers/SearchesController.cs | 20 +++++++++++++------- src/slskd/Startup.cs | 4 +++- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src/slskd/API/Controllers') diff --git a/src/slskd/API/Controllers/SearchesController.cs b/src/slskd/API/Controllers/SearchesController.cs index 6d322bda..d38d509e 100644 --- a/src/slskd/API/Controllers/SearchesController.cs +++ b/src/slskd/API/Controllers/SearchesController.cs @@ -43,12 +43,14 @@ namespace slskd.API.Controllers /// /// /// - public SearchesController(ISoulseekClient client, ISearchTracker tracker) + public SearchesController(ISoulseekClient client, ISearchTracker tracker, ISearchService searchService) { Client = client; Tracker = tracker; + SearchService = searchService; } + private ISearchService SearchService { get; } private ISoulseekClient Client { get; } private ISearchTracker Tracker { get; } @@ -62,7 +64,7 @@ namespace slskd.API.Controllers /// The search terminated abnormally. [HttpPost("")] [Authorize] - [ProducesResponseType(typeof(IEnumerable), 200)] + [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(400)] [ProducesResponseType(typeof(string), 500)] public async Task Post([FromBody]SearchRequest request) @@ -73,13 +75,16 @@ namespace slskd.API.Controllers responseReceived: (e) => Tracker.AddOrUpdate(id, e), stateChanged: (e) => Tracker.AddOrUpdate(id, e)); - var results = new ConcurrentBag(); + var results = new ConcurrentBag(); var searchText = string.Join(' ', request.SearchText.Split(' ').Where(term => term.Length > 1)); try { - await Client.SearchAsync(SearchQuery.FromText(searchText), (r) => results.Add(r), SearchScope.Network, request.Token, options); + //await Client.SearchAsync(SearchQuery.FromText(searchText), (r) => results.Add(r), SearchScope.Network, request.Token, options); + var (guid, completed) = await SearchService.BeginAsync(SearchQuery.FromText(searchText), SearchScope.Network, id: id); + await completed; + return Ok(results); } catch (Exception ex) @@ -102,14 +107,15 @@ namespace slskd.API.Controllers /// A matching search was not found. [HttpGet("{id}")] [Authorize] - [ProducesResponseType(typeof(Soulseek.Search), 200)] + [ProducesResponseType(typeof(slskd.Search.Search), 200)] [ProducesResponseType(404)] - public IActionResult GetById([FromRoute]Guid id) + public async Task GetById([FromRoute]Guid id) { - Tracker.Searches.TryGetValue(id, out var search); + var search = await SearchService.FindAsync(search => search.Id == id); if (search == default) { + Console.WriteLine($"Search: {id} not found"); return NotFound(); } diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index bd2e8963..a4627114 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -150,7 +150,7 @@ namespace slskd Version = "v0", }); - if (File.Exists(Program.XmlDocumentationFile)) + if (System.IO.File.Exists(Program.XmlDocumentationFile)) { options.IncludeXmlComments(Program.XmlDocumentationFile); } @@ -181,6 +181,8 @@ namespace slskd services.AddSingleton(_ => new SharedFileCache(Options.Directories.Shared, 3600000)); + services.AddSingleton(); + services.AddHostedService(); services.AddSingleton(_ => Service.SoulseekClient); } -- cgit v1.2.3 From c3a3d3c3e21a0bbe04116b5c98f7be1df6ad3c92 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 May 2021 22:25:02 -0500 Subject: implement the rest of SearchService --- src/slskd/API/Controllers/SearchesController.cs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'src/slskd/API/Controllers') diff --git a/src/slskd/API/Controllers/SearchesController.cs b/src/slskd/API/Controllers/SearchesController.cs index d38d509e..05a0e002 100644 --- a/src/slskd/API/Controllers/SearchesController.cs +++ b/src/slskd/API/Controllers/SearchesController.cs @@ -70,22 +70,14 @@ namespace slskd.API.Controllers public async Task Post([FromBody]SearchRequest request) { var id = request.Id ?? Guid.NewGuid(); - - var options = request.ToSearchOptions( - responseReceived: (e) => Tracker.AddOrUpdate(id, e), - stateChanged: (e) => Tracker.AddOrUpdate(id, e)); - - var results = new ConcurrentBag(); - var searchText = string.Join(' ', request.SearchText.Split(' ').Where(term => term.Length > 1)); + slskd.Search.Search search = null; + try { - //await Client.SearchAsync(SearchQuery.FromText(searchText), (r) => results.Add(r), SearchScope.Network, request.Token, options); - var (guid, completed) = await SearchService.BeginAsync(SearchQuery.FromText(searchText), SearchScope.Network, id: id); - await completed; - - return Ok(results); + search = await SearchService.SearchAsync(id, SearchQuery.FromText(searchText), SearchScope.Network); + return Ok(search.Responses); } catch (Exception ex) { @@ -93,8 +85,7 @@ namespace slskd.API.Controllers } finally { - results = null; - Tracker.TryRemove(id); + search = null; } } -- cgit v1.2.3 From ae31d127746032803de0eabc9a8bfe3846f6fb78 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 27 May 2021 22:27:27 -0500 Subject: deprecate SearchTracker --- src/slskd/API/Controllers/SearchesController.cs | 4 +- src/slskd/Core/Search/ISearchTracker.cs | 52 --------------------- src/slskd/Core/Search/SearchService.cs | 1 - src/slskd/Core/Search/SearchTracker.cs | 62 ------------------------- src/slskd/Startup.cs | 1 - 5 files changed, 1 insertion(+), 119 deletions(-) delete mode 100644 src/slskd/Core/Search/ISearchTracker.cs delete mode 100644 src/slskd/Core/Search/SearchTracker.cs (limited to 'src/slskd/API/Controllers') diff --git a/src/slskd/API/Controllers/SearchesController.cs b/src/slskd/API/Controllers/SearchesController.cs index 05a0e002..d652f147 100644 --- a/src/slskd/API/Controllers/SearchesController.cs +++ b/src/slskd/API/Controllers/SearchesController.cs @@ -43,16 +43,14 @@ namespace slskd.API.Controllers /// /// /// - public SearchesController(ISoulseekClient client, ISearchTracker tracker, ISearchService searchService) + public SearchesController(ISoulseekClient client, ISearchService searchService) { Client = client; - Tracker = tracker; SearchService = searchService; } private ISearchService SearchService { get; } private ISoulseekClient Client { get; } - private ISearchTracker Tracker { get; } /// /// Performs a search for the specified . diff --git a/src/slskd/Core/Search/ISearchTracker.cs b/src/slskd/Core/Search/ISearchTracker.cs deleted file mode 100644 index c962138e..00000000 --- a/src/slskd/Core/Search/ISearchTracker.cs +++ /dev/null @@ -1,52 +0,0 @@ -// -// 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/. -// - -namespace slskd.Search -{ - using System; - using System.Collections.Concurrent; - using Soulseek; - - /// - /// Tracks active searches. - /// - public interface ISearchTracker - { - /// - /// Gets active searches. - /// - ConcurrentDictionary Searches { get; } - - /// - /// Adds or updates a tracked search. - /// - /// - /// - void AddOrUpdate(Guid id, SearchEventArgs args); - - /// - /// Removes all tracked searches. - /// - void Clear(); - - /// - /// Removes a tracked search. - /// - /// - void TryRemove(Guid id); - } -} \ No newline at end of file diff --git a/src/slskd/Core/Search/SearchService.cs b/src/slskd/Core/Search/SearchService.cs index f1818423..0fd4d6d5 100644 --- a/src/slskd/Core/Search/SearchService.cs +++ b/src/slskd/Core/Search/SearchService.cs @@ -107,7 +107,6 @@ namespace slskd.Search using var context = ContextFactory.CreateDbContext(); var search = await context .Searches - .Select(search => search.) .FirstOrDefaultAsync(expression); if (!includeResponses) diff --git a/src/slskd/Core/Search/SearchTracker.cs b/src/slskd/Core/Search/SearchTracker.cs deleted file mode 100644 index b1ab2b18..00000000 --- a/src/slskd/Core/Search/SearchTracker.cs +++ /dev/null @@ -1,62 +0,0 @@ -// -// 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/. -// - -namespace slskd.Search -{ - using System; - using System.Collections.Concurrent; - using Soulseek; - - /// - /// Tracks active searches. - /// - public class SearchTracker : ISearchTracker - { - /// - /// Gets active searches. - /// - public ConcurrentDictionary Searches { get; private set; } = - new ConcurrentDictionary(); - - /// - /// Adds or updates a tracked search. - /// - /// - /// - public void AddOrUpdate(Guid id, SearchEventArgs args) - { - Searches.AddOrUpdate(id, args.Search, (token, search) => args.Search); - } - - /// - /// Removes all tracked searches. - /// - public void Clear() - { - Searches.Clear(); - } - - /// - /// Removes a tracked search. - /// - /// - public void TryRemove(Guid id) - { - Searches.TryRemove(id, out _); - } - } -} \ No newline at end of file diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index a4627114..4d11ad1a 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -174,7 +174,6 @@ namespace slskd services.AddTransient(p => p.GetRequiredService>().CreateDbContext()); services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(_ => new RoomTracker(messageLimit: 250)); -- cgit v1.2.3 From 3a93d826852ca0d489efee6ce12e2f2400530949 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 28 May 2021 15:01:21 -0500 Subject: move SearchesController --- src/slskd/API/Controllers/SearchesController.cs | 114 ----------------- .../Core/Search/Controllers/SearchesController.cs | 135 +++++++++++++++++++++ 2 files changed, 135 insertions(+), 114 deletions(-) delete mode 100644 src/slskd/API/Controllers/SearchesController.cs create mode 100644 src/slskd/Core/Search/Controllers/SearchesController.cs (limited to 'src/slskd/API/Controllers') diff --git a/src/slskd/API/Controllers/SearchesController.cs b/src/slskd/API/Controllers/SearchesController.cs deleted file mode 100644 index d652f147..00000000 --- a/src/slskd/API/Controllers/SearchesController.cs +++ /dev/null @@ -1,114 +0,0 @@ -// -// 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/. -// - -namespace slskd.API.Controllers -{ - using System; - using System.Collections.Concurrent; - using System.Collections.Generic; - using System.Linq; - using System.Threading.Tasks; - using Microsoft.AspNetCore.Authorization; - using Microsoft.AspNetCore.Mvc; - using slskd.API.DTO; - using slskd.Search; - using Soulseek; - - /// - /// Search. - /// - [Route("api/v{version:apiVersion}/[controller]")] - [ApiVersion("0")] - [ApiController] - [Produces("application/json")] - [Consumes("application/json")] - public class SearchesController : ControllerBase - { - /// - /// Initializes a new instance of the class. - /// - /// - /// - public SearchesController(ISoulseekClient client, ISearchService searchService) - { - Client = client; - SearchService = searchService; - } - - private ISearchService SearchService { get; } - private ISoulseekClient Client { get; } - - /// - /// Performs a search for the specified . - /// - /// The search request. - /// - /// The search completed successfully. - /// The specified was malformed. - /// The search terminated abnormally. - [HttpPost("")] - [Authorize] - [ProducesResponseType(typeof(IEnumerable), 200)] - [ProducesResponseType(400)] - [ProducesResponseType(typeof(string), 500)] - public async Task Post([FromBody]SearchRequest request) - { - var id = request.Id ?? Guid.NewGuid(); - var searchText = string.Join(' ', request.SearchText.Split(' ').Where(term => term.Length > 1)); - - slskd.Search.Search search = null; - - try - { - search = await SearchService.SearchAsync(id, SearchQuery.FromText(searchText), SearchScope.Network); - return Ok(search.Responses); - } - catch (Exception ex) - { - return StatusCode(500, $"Search terminated abnormally: {ex.Message}"); - } - finally - { - search = null; - } - } - - /// - /// Gets the state of the search corresponding to the specified . - /// - /// The unique id of the search. - /// - /// The request completed successfully. - /// A matching search was not found. - [HttpGet("{id}")] - [Authorize] - [ProducesResponseType(typeof(slskd.Search.Search), 200)] - [ProducesResponseType(404)] - public async Task GetById([FromRoute]Guid id) - { - var search = await SearchService.FindAsync(search => search.Id == id); - - if (search == default) - { - Console.WriteLine($"Search: {id} not found"); - return NotFound(); - } - - return Ok(search); - } - } -} diff --git a/src/slskd/Core/Search/Controllers/SearchesController.cs b/src/slskd/Core/Search/Controllers/SearchesController.cs new file mode 100644 index 00000000..a027feed --- /dev/null +++ b/src/slskd/Core/Search/Controllers/SearchesController.cs @@ -0,0 +1,135 @@ +// +// 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/. +// + +namespace slskd.Search +{ + using System; + using System.Linq; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + using slskd.API.DTO; + using SearchQuery = Soulseek.SearchQuery; + using SearchScope = Soulseek.SearchScope; + + /// + /// Search. + /// + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class SearchesController : ControllerBase + { + /// + /// Initializes a new instance of the class. + /// + /// + public SearchesController(ISearchService searchService) + { + Searches = searchService; + } + + private ISearchService Searches { get; } + + /// + /// Performs a search for the specified . + /// + /// The search request. + /// + /// The search completed successfully. + /// The specified was malformed. + /// The search terminated abnormally. + [HttpPost("")] + [Authorize] + public async Task Post([FromBody]SearchRequest request) + { + var id = request.Id ?? Guid.NewGuid(); + var searchText = string.Join(' ', request.SearchText.Split(' ').Where(term => term.Length > 1)); + + Search search = null; + + try + { + search = await Searches.CreateAsync(id, SearchQuery.FromText(searchText), SearchScope.Network); + return Ok(search.Responses); + } + catch (Exception ex) + { + return StatusCode(500, $"Search terminated abnormally: {ex.Message}"); + } + finally + { + search = null; + } + } + + /// + /// Gets the state of the search corresponding to the specified . + /// + /// The unique id of the search. + /// + /// The request completed successfully. + /// A matching search was not found. + [HttpGet("{id}")] + [Authorize] + public async Task GetById([FromRoute]Guid id) + { + var search = await Searches.FindAsync(search => search.Id == id); + + if (search == default) + { + return NotFound(); + } + + return Ok(search); + } + + /// + /// Gets the state of the search corresponding to the specified . + /// + /// The unique id of the search. + /// + /// The request completed successfully. + /// A matching search was not found. + [HttpGet("{id}/responses")] + [Authorize] + public async Task GetByIdWithResponses([FromRoute] Guid id) + { + var search = await Searches.FindAsync(search => search.Id == id, includeResponses: true); + + if (search == default) + { + return NotFound(); + } + + return Ok(search); + } + + /// + /// Gets the list of active and completed searches. + /// + /// + [HttpGet("")] + [Authorize] + public async Task GetAll() + { + return Ok(await Searches.ListAsync()); + } + } +} -- cgit v1.2.3