#include "store/SettingsStore.hpp"
#include <algorithm>
#include "core/AppPaths.hpp"
#include "core/Json.hpp"
#include "core/Log.hpp"
#include "spec/ModelLimits.hpp"
#include "spec/StoreKeys.hpp"
#include "store/AtomicFile.hpp"
namespace dp {
using spec::SettingsKeys;
SettingsStore& SettingsStore::instance() {
static SettingsStore store(AppPaths::prefDirectory());
return store;
}
SettingsStore::SettingsStore(std::string directory) { rebind(std::move(directory)); }
void SettingsStore::rebind(std::string directory) {
path_.clear();
if (!directory.empty()) {
if (directory.back() != '/') directory.push_back('/');
path_ = directory + SettingsKeys::kFileName; // empty directory: memory only, nothing is persisted
}
reload();
}
std::string SettingsStore::capped(const std::string& value, std::size_t maxBytes) {
return value.size() <= maxBytes ? value : value.substr(0, maxBytes);
}
void SettingsStore::reload() {
appearance_ = AppearanceMode::Dark;
showFeedAttachments_ = showFeedComments_ = showReportButtons_ = true;
recentEmojis_.clear();
draftPostTitle_.clear();
draftPostMessage_.clear();
draftReplyMessage_.clear();
if (path_.empty()) return;
bool tooLarge = false;
auto text = AtomicFile::read(path_, SettingsKeys::kMaxFileBytes, &tooLarge);
if (!text) {
if (tooLarge) {
++rejectedLoads_;
Log::error("settings file over size cap, using defaults: " + path_);
}
return;
}
Json j = Json::parse(*text, nullptr, false);
if (!j.is_object()) {
++rejectedLoads_;
Log::error("settings file is not a JSON object, using defaults: " + path_);
return;
}
if (auto raw = JsonRead::opt<std::string>(j, SettingsKeys::kAppearance)) {
if (auto mode = AppearanceMode::fromRaw(*raw)) appearance_ = *mode;
}
showFeedAttachments_ = JsonRead::get<bool>(j, SettingsKeys::kShowFeedAttachments, true);
showFeedComments_ = JsonRead::get<bool>(j, SettingsKeys::kShowFeedComments, true);
showReportButtons_ = JsonRead::get<bool>(j, SettingsKeys::kShowReportButtons, true);
if (const Json* emojis = JsonRead::child(j, SettingsKeys::kRecentEmojis); emojis && emojis->is_array()) {
for (const auto& e : *emojis) {
if (e.is_string() && e.get<std::string>().size() <= SettingsKeys::kMaxEmojiBytes &&
recentEmojis_.size() < SettingsKeys::kMaxRecentEmojis) {
recentEmojis_.push_back(e.get<std::string>());
}
}
}
draftPostTitle_ = capped(JsonRead::get<std::string>(j, SettingsKeys::kDraftPostTitle, ""), spec::ModelLimits::kMaxPostTitleLength);
draftPostMessage_ = capped(JsonRead::get<std::string>(j, SettingsKeys::kDraftPostMessage, ""), spec::ModelLimits::kMaxPostContentLength);
draftReplyMessage_ = capped(JsonRead::get<std::string>(j, SettingsKeys::kDraftReplyMessage, ""), spec::ModelLimits::kMaxCommentContentLength);
}
void SettingsStore::save() {
if (path_.empty()) return;
Json j = Json::object();
j[SettingsKeys::kAppearance] = appearance_.raw();
j[SettingsKeys::kShowFeedAttachments] = showFeedAttachments_;
j[SettingsKeys::kShowFeedComments] = showFeedComments_;
j[SettingsKeys::kShowReportButtons] = showReportButtons_;
j[SettingsKeys::kRecentEmojis] = recentEmojis_;
j[SettingsKeys::kDraftPostTitle] = draftPostTitle_;
j[SettingsKeys::kDraftPostMessage] = draftPostMessage_;
j[SettingsKeys::kDraftReplyMessage] = draftReplyMessage_;
const std::string text = j.dump(-1, ' ', false, Json::error_handler_t::replace);
if (!AtomicFile::write(path_, text, 0644, SettingsKeys::kMaxFileBytes)) ++failedSaves_;
}
void SettingsStore::changed() {
save();
notifyChanged();
}
void SettingsStore::setAppearance(AppearanceMode value) { appearance_ = value; changed(); }
void SettingsStore::setShowFeedAttachments(bool value) { showFeedAttachments_ = value; changed(); }
void SettingsStore::setShowFeedComments(bool value) { showFeedComments_ = value; changed(); }
void SettingsStore::setShowReportButtons(bool value) { showReportButtons_ = value; changed(); }
void SettingsStore::setDraftPostTitle(const std::string& value) {
draftPostTitle_ = capped(value, spec::ModelLimits::kMaxPostTitleLength);
changed();
}
void SettingsStore::setDraftPostMessage(const std::string& value) {
draftPostMessage_ = capped(value, spec::ModelLimits::kMaxPostContentLength);
changed();
}
void SettingsStore::setDraftReplyMessage(const std::string& value) {
draftReplyMessage_ = capped(value, spec::ModelLimits::kMaxCommentContentLength);
changed();
}
void SettingsStore::recordEmojiPick(const std::string& emoji) {
if (emoji.empty() || emoji.size() > SettingsKeys::kMaxEmojiBytes) return;
recentEmojis_.erase(std::remove(recentEmojis_.begin(), recentEmojis_.end(), emoji), recentEmojis_.end());
recentEmojis_.insert(recentEmojis_.begin(), emoji);
if (recentEmojis_.size() > SettingsKeys::kMaxRecentEmojis) recentEmojis_.resize(SettingsKeys::kMaxRecentEmojis);
changed();
}
} // namespace dp