many different fixes

This commit is contained in:
faumaray committed 2026-02-03 05:07:15 +03:00
1 parent a583c17916
commit 53801f2b53
36 files changed
+5149 -1291

No files matched your search

+144
View File
@@ -0,0 +1,144 @@
#include "echoion/core/UrlShortener.hpp"
#include "echoion/core/Logger.hpp"
#include <format>
#include <glaze/glaze.hpp>
namespace Echoion::Core {
UrlShortener::UrlShortener(std::shared_ptr<NetworkClient> networkClient)
: networkClient_(std::move(networkClient)) {
}
UrlShortener::~UrlShortener() = default;
Api::Result<std::string> UrlShortener::shortenYouTubeUrl(std::string_view videoId) {
std::string originalUrl = std::format("https://www.youtube.com/watch?v={}", videoId);
return shortenUrl(originalUrl);
}
Api::Result<std::string> UrlShortener::shortenUrl(std::string_view originalUrl) {
// Check cache first
{
std::lock_guard<std::mutex> lock(cacheMutex_);
auto it = cache_.find(std::string(originalUrl));
if (it != cache_.end()) {
auto now = std::chrono::system_clock::now();
if (now - it->second.timestamp < cacheTtl_) {
ECHOION_LOG_DEBUG(std::format("URL cache hit: {} -> {}",
originalUrl, it->second.shortenedUrl));
return it->second.shortenedUrl;
}
// Expired, remove from cache
cache_.erase(it);
}
}
if (!networkClient_) {
return std::unexpected(Api::Error(Api::ErrorCode::ServiceUnavailable,
"Network client not available"));
}
// faumaray.ru API endpoint for URL shortening
// POST /shorten with JSON body {"longUrl": "https://..."}
// Response: {"shortUrl": "https://faumaray.ru/xxxxx"}
std::string apiUrl = "https://faumaray.ru/shorten";
// Build JSON request body - API expects "longUrl" field
std::string jsonBody = std::format(R"({{"longUrl":"{}"}})", originalUrl);
std::vector<Api::HttpHeader> headers = {
{"Content-Type", "application/json"},
{"User-Agent", "EchoionBot/1.0"}
};
ECHOION_LOG_DEBUG(std::format("Shortening URL via faumaray.ru: {}", originalUrl));
auto response = networkClient_->post(apiUrl, jsonBody, headers);
if (!response) {
ECHOION_LOG_WARNING(std::format("URL shortening failed: {}",
response.error().message));
return std::unexpected(Api::Error(Api::ErrorCode::NetworkError,
"Failed to shorten URL"));
}
if (!response->isSuccess()) {
ECHOION_LOG_WARNING(std::format("URL shortening returned HTTP {}",
response->statusCode));
return std::unexpected(Api::Error(Api::ErrorCode::NetworkError,
std::format("Shortening service returned HTTP {}", response->statusCode)));
}
try {
// Parse response - expecting {"shortUrl": "https://faumaray.ru/xxxxx"}
glz::generic json;
if (glz::read_json(json, response->body)) {
ECHOION_LOG_WARNING("Failed to parse URL shortening response");
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
"Failed to parse shortening response"));
}
std::string shortenedUrl;
if (json.contains("shortUrl")) {
shortenedUrl = json["shortUrl"].get<std::string>();
} else {
ECHOION_LOG_WARNING("No shortUrl field in response");
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
"No shortUrl in response"));
}
// Cache the result
{
std::lock_guard<std::mutex> lock(cacheMutex_);
cache_[std::string(originalUrl)] = {
.shortenedUrl = shortenedUrl,
.timestamp = std::chrono::system_clock::now()
};
}
ECHOION_LOG_DEBUG(std::format("URL shortened: {} -> {}", originalUrl, shortenedUrl));
return shortenedUrl;
} catch (const std::exception& e) {
ECHOION_LOG_WARNING(std::format("Exception parsing shortening response: {}",
e.what()));
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
"Exception during URL shortening"));
}
}
std::optional<std::string> UrlShortener::getCachedUrl(std::string_view originalUrl) const {
std::lock_guard<std::mutex> lock(cacheMutex_);
auto it = cache_.find(std::string(originalUrl));
if (it != cache_.end()) {
auto now = std::chrono::system_clock::now();
if (now - it->second.timestamp < cacheTtl_) {
return it->second.shortenedUrl;
}
}
return std::nullopt;
}
void UrlShortener::clearCache() {
std::lock_guard<std::mutex> lock(cacheMutex_);
cache_.clear();
}
void UrlShortener::setCacheTtl(std::chrono::hours ttl) {
cacheTtl_ = ttl;
}
void UrlShortener::cleanExpiredCache() {
std::lock_guard<std::mutex> lock(cacheMutex_);
auto now = std::chrono::system_clock::now();
for (auto it = cache_.begin(); it != cache_.end();) {
if (now - it->second.timestamp >= cacheTtl_) {
it = cache_.erase(it);
} else {
++it;
}
}
}
} // namespace Echoion::Core