From 9ca6bd006fb9c0ee4ab41e46bd38ce3b6bebc40c Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Mon, 24 May 2021 12:30:32 +0400 Subject: [PATCH] /catalog/v2/categories goes through OPDSDumper too --- include/opds_dumper.h | 8 ++++++++ src/opds_dumper.cpp | 25 +++++++++++++++++++++++++ src/server/internalServer.cpp | 24 ++++-------------------- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/include/opds_dumper.h b/include/opds_dumper.h index 72d30ef4a..8d8da0f6e 100644 --- a/include/opds_dumper.h +++ b/include/opds_dumper.h @@ -64,6 +64,14 @@ class OPDSDumper */ std::string dumpOPDSFeedV2(const std::vector& bookIds, const std::string& query) const; + /** + * Dump the categories OPDS feed. + * + * @param categories list of category names + * @return The OPDS feed. + */ + std::string categoriesOPDSFeed(const std::vector& categories) const; + /** * Set the id of the library. * diff --git a/src/opds_dumper.cpp b/src/opds_dumper.cpp index c20d492e9..7e91e7989 100644 --- a/src/opds_dumper.cpp +++ b/src/opds_dumper.cpp @@ -179,4 +179,29 @@ string OPDSDumper::dumpOPDSFeedV2(const std::vector& bookIds, const return render_template(RESOURCE::catalog_v2_entries_xml, template_data); } +std::string OPDSDumper::categoriesOPDSFeed(const std::vector& categories) const +{ + const auto now = gen_date_str(); + kainjow::mustache::list categoryData; + for ( const auto& category : categories ) { + const auto urlencodedCategoryName = urlEncode(category); + categoryData.push_back(kainjow::mustache::object{ + {"name", category}, + {"urlencoded_name", urlencodedCategoryName}, + {"updated", now}, + {"id", gen_uuid(libraryId + "/categories/" + urlencodedCategoryName)} + }); + } + + return render_template( + RESOURCE::catalog_v2_categories_xml, + kainjow::mustache::object{ + {"date", now}, + {"endpoint_root", rootLocation + "/catalog/v2"}, + {"feed_id", gen_uuid(libraryId + "/categories")}, + {"categories", categoryData } + } + ); +} + } diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 77724e669..03e130dff 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -779,28 +779,12 @@ std::unique_ptr InternalServer::handle_catalog_v2_entries(const Reques std::unique_ptr InternalServer::handle_catalog_v2_categories(const RequestContext& request) { - const std::string root_url = normalizeRootUrl(m_root); - const auto now = gen_date_str(); - kainjow::mustache::list categoryData; - for ( const auto& category : mp_library->getBooksCategories() ) { - const auto urlencodedCategoryName = urlEncode(category); - categoryData.push_back(kainjow::mustache::object{ - {"name", category}, - {"urlencoded_name", urlencodedCategoryName}, - {"updated", now}, - {"id", gen_uuid(m_library_id + "/categories/" + urlencodedCategoryName)} - }); - } - + OPDSDumper opdsDumper(mp_library); + opdsDumper.setRootLocation(normalizeRootUrl(m_root)); + opdsDumper.setLibraryId(m_library_id); return ContentResponse::build( *this, - RESOURCE::catalog_v2_categories_xml, - kainjow::mustache::object{ - {"date", now}, - {"endpoint_root", root_url + "/catalog/v2"}, - {"feed_id", gen_uuid(m_library_id + "/categories")}, - {"categories", categoryData } - }, + opdsDumper.categoriesOPDSFeed(mp_library->getBooksCategories()), "application/atom+xml;profile=opds-catalog;kind=navigation" ); }