Extracted searching

This commit is contained in:
emeric
2023-10-01 20:57:17 +02:00
parent b96a16095a
commit 955320e3a8
6 changed files with 182 additions and 118 deletions
+2 -1
View File
@@ -1,8 +1,9 @@
add_library(lmssubsonic SHARED add_library(lmssubsonic SHARED
impl/entrypoints/Bookmarks.cpp impl/entrypoints/Bookmarks.cpp
impl/entrypoints/MediaLibraryScanning.cpp
impl/entrypoints/MediaRetrieval.cpp impl/entrypoints/MediaRetrieval.cpp
impl/entrypoints/Scan.cpp impl/entrypoints/Searching.cpp
impl/entrypoints/UserManagement.cpp impl/entrypoints/UserManagement.cpp
impl/responses/Album.cpp impl/responses/Album.cpp
impl/responses/Artist.cpp impl/responses/Artist.cpp
+2 -94
View File
@@ -47,8 +47,9 @@
#include "utils/Utils.hpp" #include "utils/Utils.hpp"
#include "entrypoints/Bookmarks.hpp" #include "entrypoints/Bookmarks.hpp"
#include "entrypoints/MediaLibraryScanning.hpp"
#include "entrypoints/MediaRetrieval.hpp" #include "entrypoints/MediaRetrieval.hpp"
#include "entrypoints/Scan.hpp" #include "entrypoints/Searching.hpp"
#include "entrypoints/UserManagement.hpp" #include "entrypoints/UserManagement.hpp"
#include "responses/Artist.hpp" #include "responses/Artist.hpp"
#include "responses/Album.hpp" #include "responses/Album.hpp"
@@ -1028,84 +1029,6 @@ handleGetSongsByGenreRequest(RequestContext& context)
return response; return response;
} }
static
Response
handleSearchRequestCommon(RequestContext& context, bool id3)
{
// Mandatory params
std::string queryString {getMandatoryParameterAs<std::string>(context.parameters, "query")};
std::string_view query {queryString};
// Symfonium adds extra ""
if (context.clientInfo.name == "Symfonium")
query = StringUtils::stringTrim(query, "\"");
std::vector<std::string_view> keywords {StringUtils::splitString(query, " ")};
// Optional params
std::size_t artistCount {getParameterAs<std::size_t>(context.parameters, "artistCount").value_or(20)};
std::size_t artistOffset {getParameterAs<std::size_t>(context.parameters, "artistOffset").value_or(0)};
std::size_t albumCount {getParameterAs<std::size_t>(context.parameters, "albumCount").value_or(20)};
std::size_t albumOffset {getParameterAs<std::size_t>(context.parameters, "albumOffset").value_or(0)};
std::size_t songCount {getParameterAs<std::size_t>(context.parameters, "songCount").value_or(20)};
std::size_t songOffset {getParameterAs<std::size_t>(context.parameters, "songOffset").value_or(0)};
auto transaction {context.dbSession.createSharedTransaction()};
User::pointer user {User::find(context.dbSession, context.userId)};
if (!user)
throw UserNotAuthorizedError {};
Response response {Response::createOkResponse(context.serverProtocolVersion)};
Response::Node& searchResult2Node {response.createNode(id3 ? "searchResult3" : "searchResult2")};
if (artistCount > 0)
{
Artist::FindParameters params;
params.setKeywords(keywords);
params.setSortMethod(ArtistSortMethod::BySortName);
params.setRange({artistOffset, artistCount});
RangeResults<ArtistId> artistIds {Artist::find(context.dbSession, params)};
for (const ArtistId artistId : artistIds.results)
{
const auto artist {Artist::find(context.dbSession, artistId)};
searchResult2Node.addArrayChild("artist", createArtistNode(artist, context.dbSession, user, id3));
}
}
if (albumCount > 0)
{
Release::FindParameters params;
params.setKeywords(keywords);
params.setSortMethod(ReleaseSortMethod::Name);
params.setRange({albumOffset, albumCount});
RangeResults<ReleaseId> releaseIds {Release::find(context.dbSession, params)};
for (const ReleaseId releaseId : releaseIds.results)
{
const auto release {Release::find(context.dbSession, releaseId)};
searchResult2Node.addArrayChild("album", createAlbumNode(release, context.dbSession, user, id3));
}
}
if (songCount > 0)
{
Track::FindParameters params;
params.setKeywords(keywords);
params.setRange({songOffset, songCount});
RangeResults<TrackId> trackIds {Track::find(context.dbSession, params)};
for (const TrackId trackId : trackIds.results)
{
const auto track {Track::find(context.dbSession, trackId)};
searchResult2Node.addArrayChild("song", createSongNode(track, context.dbSession, user));
}
}
return response;
}
struct StarParameters struct StarParameters
{ {
std::vector<ArtistId> artistIds; std::vector<ArtistId> artistIds;
@@ -1145,20 +1068,6 @@ handleStarRequest(RequestContext& context)
return Response::createOkResponse(context.serverProtocolVersion); return Response::createOkResponse(context.serverProtocolVersion);
} }
static
Response
handleSearch2Request(RequestContext& context)
{
return handleSearchRequestCommon(context, false /* no id3 */);
}
static
Response
handleSearch3Request(RequestContext& context)
{
return handleSearchRequestCommon(context, true /* id3 */);
}
static static
Response Response
handleUnstarRequest(RequestContext& context) handleUnstarRequest(RequestContext& context)
@@ -1410,7 +1319,6 @@ static std::unordered_map<std::string, MediaRetrievalHandlerFunc> mediaRetrieval
{"/getCoverArt", handleGetCoverArt}, {"/getCoverArt", handleGetCoverArt},
}; };
void void
SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response)
{ {
@@ -17,7 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>. * along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "Scan.hpp" #include "MediaLibraryScanning.hpp"
#include "services/scanner/IScannerService.hpp" #include "services/scanner/IScannerService.hpp"
#include "utils/Service.hpp" #include "utils/Service.hpp"
@@ -26,44 +26,43 @@ namespace API::Subsonic::Scan
{ {
using namespace Scanner; using namespace Scanner;
static namespace
Response::Node
createStatusResponseNode()
{ {
Response::Node statusResponse; Response::Node
createStatusResponseNode()
const IScannerService::Status scanStatus {Service<IScannerService>::get()->getStatus()};
statusResponse.setAttribute("scanning", scanStatus.currentState == IScannerService::State::InProgress);
if (scanStatus.currentState == IScannerService::State::InProgress)
{ {
std::size_t count{}; Response::Node statusResponse;
if (scanStatus.currentScanStepStats && scanStatus.currentScanStepStats->currentStep == ScanStep::ScanningFiles) const IScannerService::Status scanStatus{ Service<IScannerService>::get()->getStatus() };
count = scanStatus.currentScanStepStats->processedElems;
statusResponse.setAttribute("count", count); statusResponse.setAttribute("scanning", scanStatus.currentState == IScannerService::State::InProgress);
if (scanStatus.currentState == IScannerService::State::InProgress)
{
std::size_t count{};
if (scanStatus.currentScanStepStats && scanStatus.currentScanStepStats->currentStep == ScanStep::ScanningFiles)
count = scanStatus.currentScanStepStats->processedElems;
statusResponse.setAttribute("count", count);
}
return statusResponse;
} }
return statusResponse;
} }
Response handleGetScanStatus(RequestContext& context)
Response
handleGetScanStatus(RequestContext& context)
{ {
Response response {Response::createOkResponse(context.serverProtocolVersion)}; Response response{ Response::createOkResponse(context.serverProtocolVersion) };
response.addNode("scanStatus", createStatusResponseNode()); response.addNode("scanStatus", createStatusResponseNode());
return response; return response;
} }
Response Response handleStartScan(RequestContext& context)
handleStartScan(RequestContext& context)
{ {
Service<IScannerService>::get()->requestImmediateScan(false); Service<IScannerService>::get()->requestImmediateScan(false);
Response response {Response::createOkResponse(context.serverProtocolVersion)}; Response response{ Response::createOkResponse(context.serverProtocolVersion) };
response.addNode("scanStatus", createStatusResponseNode()); response.addNode("scanStatus", createStatusResponseNode());
return response; return response;
@@ -0,0 +1,127 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Searching.hpp"
#include "services/database/Artist.hpp"
#include "services/database/Release.hpp"
#include "services/database/Session.hpp"
#include "services/database/Track.hpp"
#include "services/database/User.hpp"
#include "responses/Album.hpp"
#include "responses/Artist.hpp"
#include "responses/Song.hpp"
#include "ParameterParsing.hpp"
#include "ParameterParsing.hpp"
namespace API::Subsonic
{
using namespace Database;
namespace
{
Response handleSearchRequestCommon(RequestContext& context, bool id3)
{
// Mandatory params
std::string queryString{ getMandatoryParameterAs<std::string>(context.parameters, "query") };
std::string_view query{ queryString };
// Symfonium adds extra ""
if (context.clientInfo.name == "Symfonium")
query = StringUtils::stringTrim(query, "\"");
std::vector<std::string_view> keywords{ StringUtils::splitString(query, " ") };
// Optional params
std::size_t artistCount{ getParameterAs<std::size_t>(context.parameters, "artistCount").value_or(20) };
std::size_t artistOffset{ getParameterAs<std::size_t>(context.parameters, "artistOffset").value_or(0) };
std::size_t albumCount{ getParameterAs<std::size_t>(context.parameters, "albumCount").value_or(20) };
std::size_t albumOffset{ getParameterAs<std::size_t>(context.parameters, "albumOffset").value_or(0) };
std::size_t songCount{ getParameterAs<std::size_t>(context.parameters, "songCount").value_or(20) };
std::size_t songOffset{ getParameterAs<std::size_t>(context.parameters, "songOffset").value_or(0) };
auto transaction{ context.dbSession.createSharedTransaction() };
User::pointer user{ User::find(context.dbSession, context.userId) };
if (!user)
throw UserNotAuthorizedError{};
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
Response::Node& searchResult2Node{ response.createNode(id3 ? "searchResult3" : "searchResult2") };
if (artistCount > 0)
{
Artist::FindParameters params;
params.setKeywords(keywords);
params.setSortMethod(ArtistSortMethod::BySortName);
params.setRange({ artistOffset, artistCount });
RangeResults<ArtistId> artistIds{ Artist::find(context.dbSession, params) };
for (const ArtistId artistId : artistIds.results)
{
const auto artist{ Artist::find(context.dbSession, artistId) };
searchResult2Node.addArrayChild("artist", createArtistNode(artist, context.dbSession, user, id3));
}
}
if (albumCount > 0)
{
Release::FindParameters params;
params.setKeywords(keywords);
params.setSortMethod(ReleaseSortMethod::Name);
params.setRange({ albumOffset, albumCount });
RangeResults<ReleaseId> releaseIds{ Release::find(context.dbSession, params) };
for (const ReleaseId releaseId : releaseIds.results)
{
const auto release{ Release::find(context.dbSession, releaseId) };
searchResult2Node.addArrayChild("album", createAlbumNode(release, context.dbSession, user, id3));
}
}
if (songCount > 0)
{
Track::FindParameters params;
params.setKeywords(keywords);
params.setRange({ songOffset, songCount });
RangeResults<TrackId> trackIds{ Track::find(context.dbSession, params) };
for (const TrackId trackId : trackIds.results)
{
const auto track{ Track::find(context.dbSession, trackId) };
searchResult2Node.addArrayChild("song", createSongNode(track, context.dbSession, user));
}
}
return response;
}
}
Response handleSearch2Request(RequestContext& context)
{
return handleSearchRequestCommon(context, false /* no id3 */);
}
Response handleSearch3Request(RequestContext& context)
{
return handleSearchRequestCommon(context, true /* id3 */);
}
}
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2023 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "RequestContext.hpp"
#include "SubsonicResponse.hpp"
namespace API::Subsonic
{
Response handleSearch2Request(RequestContext& context);
Response handleSearch3Request(RequestContext& context);
}