2017-06-05 23:56:01 +00:00
|
|
|
/* Copyright 2017, Michele Santullo
|
|
|
|
* This file is part of "kamokan".
|
|
|
|
*
|
|
|
|
* "kamokan" is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* "kamokan" is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with "kamokan". If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fake_storage.hpp"
|
|
|
|
#include "num_to_token.hpp"
|
|
|
|
#include <utility>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2017-06-12 21:57:40 +00:00
|
|
|
namespace kamokan {
|
2017-06-05 23:56:01 +00:00
|
|
|
FakeStorage::FakeStorage (const Kakoune::SafePtr<SettingsBag>& parSettings, bool parItsConnected) :
|
|
|
|
Storage(parSettings),
|
|
|
|
m_submission_num(1),
|
|
|
|
m_its_connected(parItsConnected)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FakeStorage::~FakeStorage() = default;
|
|
|
|
|
|
|
|
void FakeStorage::connect_async() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FakeStorage::is_connected() const {
|
|
|
|
return m_its_connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FakeStorage::finalize_connection() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage::SubmissionResult FakeStorage::submit_pastie (
|
2017-06-06 23:18:52 +00:00
|
|
|
const boost::string_view& parText,
|
2017-06-05 23:56:01 +00:00
|
|
|
uint32_t parExpiry,
|
2017-06-06 23:18:52 +00:00
|
|
|
const boost::string_view& parLang,
|
2017-06-14 19:19:18 +00:00
|
|
|
bool parSelfDestruct,
|
2017-06-05 23:56:01 +00:00
|
|
|
const std::string& parRemoteIP
|
|
|
|
) const {
|
|
|
|
SubmittedPastie pastie;
|
|
|
|
std::string token = num_to_token(m_submission_num++);
|
2017-06-06 23:19:53 +00:00
|
|
|
pastie.text = std::string(parText);
|
2017-06-05 23:56:01 +00:00
|
|
|
pastie.expiry = parExpiry;
|
2017-06-06 23:19:53 +00:00
|
|
|
pastie.lang = std::string(parLang);
|
2017-06-05 23:56:01 +00:00
|
|
|
pastie.remote_ip = parRemoteIP;
|
|
|
|
pastie.token = token;
|
2017-06-14 19:19:18 +00:00
|
|
|
pastie.self_destruct = parSelfDestruct;
|
2017-06-05 23:56:01 +00:00
|
|
|
m_submitted_pasties.push_back(std::move(pastie));
|
|
|
|
|
|
|
|
Storage::SubmissionResult submission_res;
|
|
|
|
submission_res.token = token;
|
|
|
|
return submission_res;
|
|
|
|
}
|
|
|
|
|
2017-08-07 23:09:03 +00:00
|
|
|
Storage::RetrievedPastie FakeStorage::retrieve_pastie (
|
|
|
|
const boost::string_view& parToken,
|
|
|
|
uint32_t parMaxTokenLen,
|
|
|
|
const boost::string_view& parLang
|
|
|
|
) const {
|
2017-06-05 23:56:01 +00:00
|
|
|
auto it_found = std::find_if(
|
|
|
|
m_submitted_pasties.begin(),
|
|
|
|
m_submitted_pasties.end(),
|
|
|
|
[&](const SubmittedPastie& pastie) {
|
|
|
|
return pastie.token == parToken;
|
|
|
|
}
|
|
|
|
);
|
2017-06-16 07:11:26 +00:00
|
|
|
if (parToken.size() > static_cast<std::size_t>(parMaxTokenLen))
|
|
|
|
return RetrievedPastie (boost::optional<std::string>(), false, false);
|
|
|
|
else if (m_submitted_pasties.end() == it_found)
|
|
|
|
return RetrievedPastie (boost::optional<std::string>(), false, true);
|
2017-06-05 23:56:01 +00:00
|
|
|
else
|
2017-06-16 07:11:26 +00:00
|
|
|
return RetrievedPastie (boost::make_optional(it_found->text), it_found->self_destruct, true);
|
2017-06-05 23:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<FakeStorage::SubmittedPastie>& FakeStorage::submitted_pasties() const {
|
|
|
|
return m_submitted_pasties;
|
|
|
|
}
|
2017-06-12 21:57:40 +00:00
|
|
|
} //namespace kamokan
|