1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-11-27 00:43:47 +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) {
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"))) {
m_token_invalid = true;
return;
}
if (not pastie) {
if (not pastie_info.pastie) {
m_pastie_not_found = true;
return;
}
@ -160,13 +160,13 @@ namespace kamokan {
std::string processed_pastie;
if (m_syntax_highlight) {
//TODO: redirect to "pastie not found" if !pastie
processed_pastie = std::move(*pastie);
processed_pastie = std::move(*pastie_info.pastie);
}
else {
tawashi::Escapist houdini;
std::ostringstream oss;
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();
}
@ -181,6 +181,7 @@ namespace kamokan {
parContext["pastie_lines"] = pastie_to_numbered_lines(
boost::get<std::string>(parContext["pastie"])
);
parContext["self_destructed"] = pastie_info.self_destructed;
}
std::string PastieResponse::on_mustache_retrieve() {

View file

@ -137,16 +137,18 @@ namespace kamokan {
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_list = redis::IncRedis::opt_string_list;
RetrievedPastie retval;
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());
if (selfdes and string_conv<bool>(*selfdes)) {
const bool deleted = m_redis->del(parToken);
retval.self_destructed = deleted;
if (not deleted) {
auto statuslog = spdlog::get("statuslog");
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)
{
auto statuslog = spdlog::get("statuslog");
if (pastie)
statuslog->debug("Retrieving pastie with token \"{}\" gave a result of size {}", parToken, pastie->size());
if (retval.pastie)
statuslog->debug("Retrieving pastie with token \"{}\" gave a result of size {}", parToken, retval.pastie->size());
else
statuslog->debug("Retrieving pastie with token \"{}\" gave no results", parToken);
}
#endif
return pastie;
return retval;
}
#if defined(KAMOKAN_WITH_TESTING)

View file

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

View file

@ -62,7 +62,7 @@ namespace kamokan {
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(
m_submitted_pasties.begin(),
m_submitted_pasties.end(),
@ -71,9 +71,9 @@ namespace kamokan {
}
);
if (m_submitted_pasties.end() == it_found)
return boost::optional<std::string>();
return RetrievedPastie {boost::optional<std::string>(), false};
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 {

View file

@ -52,7 +52,7 @@ namespace kamokan {
const std::string& parRemoteIP
) 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;