Moved taskbarInfo into ContentResponseBlueprint

This commit is contained in:
Veloman Yunkan 2022-03-24 20:04:01 +04:00 committed by Matthieu Gautier
parent 2da8ea1650
commit ec2e10b40e
2 changed files with 21 additions and 27 deletions

View File

@ -115,7 +115,9 @@ std::unique_ptr<ContentResponse> ContentResponseBlueprint::generateResponseObjec
{ {
auto r = ContentResponse::build(m_server, m_template, m_data, m_mimeType); auto r = ContentResponse::build(m_server, m_template, m_data, m_mimeType);
r->set_code(m_httpStatusCode); r->set_code(m_httpStatusCode);
return r; return m_taskbarInfo
? withTaskbarInfo(m_taskbarInfo->bookName, m_taskbarInfo->archive, std::move(r))
: std::move(r);
} }
HTTP404HtmlResponse::HTTP404HtmlResponse(const InternalServer& server, HTTP404HtmlResponse::HTTP404HtmlResponse(const InternalServer& server,
@ -143,20 +145,12 @@ HTTP404HtmlResponse& HTTP404HtmlResponse::operator+(const std::string& msg)
return *this; return *this;
} }
HTTP404HtmlResponse& HTTP404HtmlResponse::operator+(const TaskbarInfo& taskbarInfo) ContentResponseBlueprint& ContentResponseBlueprint::operator+(const TaskbarInfo& taskbarInfo)
{ {
this->taskbarInfo.reset(new TaskbarInfo(taskbarInfo)); this->m_taskbarInfo.reset(new TaskbarInfo(taskbarInfo));
return *this; return *this;
} }
std::unique_ptr<ContentResponse> HTTP404HtmlResponse::generateResponseObject() const
{
auto r = ContentResponseBlueprint::generateResponseObject();
return taskbarInfo
? withTaskbarInfo(taskbarInfo->bookName, taskbarInfo->archive, std::move(r))
: std::move(r);
}
std::unique_ptr<Response> Response::build_416(const InternalServer& server, size_t resourceLength) std::unique_ptr<Response> Response::build_416(const InternalServer& server, size_t resourceLength)
{ {
auto response = Response::build(server); auto response = Response::build(server);

View File

@ -129,6 +129,17 @@ class ContentResponse : public Response {
std::string m_bookTitle; std::string m_bookTitle;
}; };
struct TaskbarInfo
{
const std::string bookName;
const zim::Archive* const archive;
TaskbarInfo(const std::string& bookName, const zim::Archive* a = nullptr)
: bookName(bookName)
, archive(a)
{}
};
std::unique_ptr<ContentResponse> withTaskbarInfo(const std::string& bookName, std::unique_ptr<ContentResponse> withTaskbarInfo(const std::string& bookName,
const zim::Archive* archive, const zim::Archive* archive,
std::unique_ptr<ContentResponse> r); std::unique_ptr<ContentResponse> r);
@ -166,6 +177,9 @@ public: // functions
return operator std::unique_ptr<ContentResponse>(); return operator std::unique_ptr<ContentResponse>();
} }
ContentResponseBlueprint& operator+(const TaskbarInfo& taskbarInfo);
protected: // functions protected: // functions
virtual std::unique_ptr<ContentResponse> generateResponseObject() const; virtual std::unique_ptr<ContentResponse> generateResponseObject() const;
@ -176,35 +190,21 @@ public: //data
const std::string m_mimeType; const std::string m_mimeType;
const std::string m_template; const std::string m_template;
kainjow::mustache::data m_data; kainjow::mustache::data m_data;
std::unique_ptr<TaskbarInfo> m_taskbarInfo;
}; };
class UrlNotFoundMsg {}; class UrlNotFoundMsg {};
extern const UrlNotFoundMsg urlNotFoundMsg; extern const UrlNotFoundMsg urlNotFoundMsg;
struct TaskbarInfo
{
const std::string bookName;
const zim::Archive* const archive;
TaskbarInfo(const std::string& bookName, const zim::Archive* a = nullptr)
: bookName(bookName)
, archive(a)
{}
};
struct HTTP404HtmlResponse : ContentResponseBlueprint struct HTTP404HtmlResponse : ContentResponseBlueprint
{ {
HTTP404HtmlResponse(const InternalServer& server, HTTP404HtmlResponse(const InternalServer& server,
const RequestContext& request); const RequestContext& request);
using ContentResponseBlueprint::operator+;
HTTP404HtmlResponse& operator+(UrlNotFoundMsg /*unused*/); HTTP404HtmlResponse& operator+(UrlNotFoundMsg /*unused*/);
HTTP404HtmlResponse& operator+(const std::string& errorDetails); HTTP404HtmlResponse& operator+(const std::string& errorDetails);
HTTP404HtmlResponse& operator+(const TaskbarInfo& taskbarInfo);
std::unique_ptr<ContentResponse> generateResponseObject() const override;
std::unique_ptr<TaskbarInfo> taskbarInfo;
}; };
class ItemResponse : public Response { class ItemResponse : public Response {