1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-06-07 00:51:41 +00:00

Add a bool to mustache to tell if a pastie got self-destructed.

This commit is contained in:
King_DuckZ 2017-06-14 20:31:17 +01:00
parent b6edab7171
commit 218e9ab6cd
5 changed files with 21 additions and 14 deletions

View file

@ -132,13 +132,13 @@ namespace kamokan {
void PastieResponse::on_mustache_prepare (mstch::map& parContext) { void PastieResponse::on_mustache_prepare (mstch::map& parContext) {
boost::string_view token = cgi::drop_arguments(cgi_env().request_uri_relative()); boost::string_view token = cgi::drop_arguments(cgi_env().request_uri_relative());
boost::optional<std::string> pastie = this->storage().retrieve_pastie(token); Storage::RetrievedPastie pastie_info = this->storage().retrieve_pastie(token);
if (not is_valid_token(token, settings().as<uint32_t>("max_token_length"))) { if (not is_valid_token(token, settings().as<uint32_t>("max_token_length"))) {
m_token_invalid = true; m_token_invalid = true;
return; return;
} }
if (not pastie) { if (not pastie_info.pastie) {
m_pastie_not_found = true; m_pastie_not_found = true;
return; return;
} }
@ -160,13 +160,13 @@ namespace kamokan {
std::string processed_pastie; std::string processed_pastie;
if (m_syntax_highlight) { if (m_syntax_highlight) {
//TODO: redirect to "pastie not found" if !pastie //TODO: redirect to "pastie not found" if !pastie
processed_pastie = std::move(*pastie); processed_pastie = std::move(*pastie_info.pastie);
} }
else { else {
tawashi::Escapist houdini; tawashi::Escapist houdini;
std::ostringstream oss; std::ostringstream oss;
oss << R"(<pre><tt><font color="#EDEDED">)"; oss << R"(<pre><tt><font color="#EDEDED">)";
oss << houdini.escape_html(*pastie) << "</font></tt></pre>\n"; oss << houdini.escape_html(*pastie_info.pastie) << "</font></tt></pre>\n";
processed_pastie = oss.str(); processed_pastie = oss.str();
} }
@ -181,6 +181,7 @@ namespace kamokan {
parContext["pastie_lines"] = pastie_to_numbered_lines( parContext["pastie_lines"] = pastie_to_numbered_lines(
boost::get<std::string>(parContext["pastie"]) boost::get<std::string>(parContext["pastie"])
); );
parContext["self_destructed"] = pastie_info.self_destructed;
} }
std::string PastieResponse::on_mustache_retrieve() { std::string PastieResponse::on_mustache_retrieve() {

View file

@ -137,16 +137,18 @@ namespace kamokan {
return make_submission_result(ErrorReasons::PastieNotSaved); return make_submission_result(ErrorReasons::PastieNotSaved);
} }
boost::optional<std::string> Storage::retrieve_pastie (const boost::string_view& parToken) const { auto Storage::retrieve_pastie (const boost::string_view& parToken) const -> RetrievedPastie {
using opt_string = redis::IncRedis::opt_string; using opt_string = redis::IncRedis::opt_string;
using opt_string_list = redis::IncRedis::opt_string_list; using opt_string_list = redis::IncRedis::opt_string_list;
RetrievedPastie retval;
opt_string_list pastie_reply = m_redis->hmget(parToken, "pastie", "selfdes"); opt_string_list pastie_reply = m_redis->hmget(parToken, "pastie", "selfdes");
opt_string pastie = (pastie_reply and not pastie_reply->empty() ? (*pastie_reply)[0] : opt_string()); retval.pastie = (pastie_reply and not pastie_reply->empty() ? (*pastie_reply)[0] : opt_string());
opt_string selfdes = (pastie_reply and not pastie_reply->size() > 1 ? (*pastie_reply)[1] : opt_string()); opt_string selfdes = (pastie_reply and not pastie_reply->size() > 1 ? (*pastie_reply)[1] : opt_string());
if (selfdes and string_conv<bool>(*selfdes)) { if (selfdes and string_conv<bool>(*selfdes)) {
const bool deleted = m_redis->del(parToken); const bool deleted = m_redis->del(parToken);
retval.self_destructed = deleted;
if (not deleted) { if (not deleted) {
auto statuslog = spdlog::get("statuslog"); auto statuslog = spdlog::get("statuslog");
statuslog->error("Pastie \"{}\" was marked as self-destructing but DEL failed to delete it", parToken); statuslog->error("Pastie \"{}\" was marked as self-destructing but DEL failed to delete it", parToken);
@ -156,13 +158,13 @@ namespace kamokan {
#if defined(SPDLOG_DEBUG_ON) #if defined(SPDLOG_DEBUG_ON)
{ {
auto statuslog = spdlog::get("statuslog"); auto statuslog = spdlog::get("statuslog");
if (pastie) if (retval.pastie)
statuslog->debug("Retrieving pastie with token \"{}\" gave a result of size {}", parToken, pastie->size()); statuslog->debug("Retrieving pastie with token \"{}\" gave a result of size {}", parToken, retval.pastie->size());
else else
statuslog->debug("Retrieving pastie with token \"{}\" gave no results", parToken); statuslog->debug("Retrieving pastie with token \"{}\" gave no results", parToken);
} }
#endif #endif
return pastie; return retval;
} }
#if defined(KAMOKAN_WITH_TESTING) #if defined(KAMOKAN_WITH_TESTING)

View file

@ -38,6 +38,10 @@ namespace kamokan {
std::string token; std::string token;
boost::optional<tawashi::ErrorReasons> error; boost::optional<tawashi::ErrorReasons> error;
}; };
struct RetrievedPastie {
boost::optional<std::string> pastie;
bool self_destructed;
};
explicit Storage (const Kakoune::SafePtr<SettingsBag>& parSettings); explicit Storage (const Kakoune::SafePtr<SettingsBag>& parSettings);
kamokan_virtual_testing ~Storage(); kamokan_virtual_testing ~Storage();
@ -53,7 +57,7 @@ namespace kamokan {
const std::string& parRemoteIP const std::string& parRemoteIP
) const; ) const;
kamokan_virtual_testing boost::optional<std::string> retrieve_pastie (const boost::string_view& parToken) const; kamokan_virtual_testing RetrievedPastie retrieve_pastie (const boost::string_view& parToken) const;
#if defined(KAMOKAN_WITH_TESTING) #if defined(KAMOKAN_WITH_TESTING)
const SettingsBag& settings() const; const SettingsBag& settings() const;

View file

@ -62,7 +62,7 @@ namespace kamokan {
return submission_res; return submission_res;
} }
boost::optional<std::string> FakeStorage::retrieve_pastie (const boost::string_view& parToken) const { Storage::RetrievedPastie FakeStorage::retrieve_pastie (const boost::string_view& parToken) const {
auto it_found = std::find_if( auto it_found = std::find_if(
m_submitted_pasties.begin(), m_submitted_pasties.begin(),
m_submitted_pasties.end(), m_submitted_pasties.end(),
@ -71,9 +71,9 @@ namespace kamokan {
} }
); );
if (m_submitted_pasties.end() == it_found) if (m_submitted_pasties.end() == it_found)
return boost::optional<std::string>(); return RetrievedPastie {boost::optional<std::string>(), false};
else else
return boost::make_optional(it_found->text); return RetrievedPastie {boost::make_optional(it_found->text), it_found->self_destruct};
} }
const std::vector<FakeStorage::SubmittedPastie>& FakeStorage::submitted_pasties() const { const std::vector<FakeStorage::SubmittedPastie>& FakeStorage::submitted_pasties() const {

View file

@ -52,7 +52,7 @@ namespace kamokan {
const std::string& parRemoteIP const std::string& parRemoteIP
) const override; ) const override;
kamokan_virtual_testing boost::optional<std::string> retrieve_pastie (const boost::string_view& parToken) const override; kamokan_virtual_testing Storage::RetrievedPastie retrieve_pastie (const boost::string_view& parToken) const override;
const std::vector<SubmittedPastie>& submitted_pasties() const; const std::vector<SubmittedPastie>& submitted_pasties() const;