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 -94
View File
@@ -47,8 +47,9 @@
#include "utils/Utils.hpp"
#include "entrypoints/Bookmarks.hpp"
#include "entrypoints/MediaLibraryScanning.hpp"
#include "entrypoints/MediaRetrieval.hpp"
#include "entrypoints/Scan.hpp"
#include "entrypoints/Searching.hpp"
#include "entrypoints/UserManagement.hpp"
#include "responses/Artist.hpp"
#include "responses/Album.hpp"
@@ -1028,84 +1029,6 @@ handleGetSongsByGenreRequest(RequestContext& context)
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
{
std::vector<ArtistId> artistIds;
@@ -1145,20 +1068,6 @@ handleStarRequest(RequestContext& context)
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
Response
handleUnstarRequest(RequestContext& context)
@@ -1410,7 +1319,6 @@ static std::unordered_map<std::string, MediaRetrievalHandlerFunc> mediaRetrieval
{"/getCoverArt", handleGetCoverArt},
};
void
SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response)
{